<?php
defined('BASEPATH') OR exit('No direct script access allowed');


/**
*| --------------------------------------------------------------------------
*| Agritun Vente Controller
*| --------------------------------------------------------------------------
*| Agritun Vente site
*|
*/
class Agritun_vente extends Admin	
{
	
	public function __construct()
	{
		parent::__construct();

		$this->load->model('model_agritun_vente');
		$this->load->model('group/model_group');
		$this->lang->load('web_lang', $this->current_lang);
	}

	/**
	* show all Agritun Ventes
	*
	* @var $offset String
	*/
	public function index($offset = 0)
	{
		$this->is_allowed('agritun_vente_list');

		$filter = $this->input->get('q');
		$field 	= $this->input->get('f');

		$this->data['agritun_ventes'] = $this->model_agritun_vente->get($filter, $field, $this->limit_page, $offset);
		$this->data['agritun_vente_counts'] = $this->model_agritun_vente->count_all($filter, $field);

		$config = [
			'base_url'     => 'administrator/agritun_vente/index/',
			'total_rows'   => $this->data['agritun_vente_counts'],
			'per_page'     => $this->limit_page,
			'uri_segment'  => 4,
		];

		$this->data['pagination'] = $this->pagination($config);

		$this->template->title('عمليات البيع List');
		$this->render('backend/standart/administrator/agritun_vente/agritun_vente_list', $this->data);
	}
	
	/**
	* Add new agritun_ventes
	*
	*/
	public function add()
	{
		$this->is_allowed('agritun_vente_add');

		$this->template->title('عمليات البيع New');
		$this->render('backend/standart/administrator/agritun_vente/agritun_vente_add', $this->data);
	}

	/**
	* Add New Agritun Ventes
	*
	* @return JSON
	*/
	public function add_save()
	{
		if (!$this->is_allowed('agritun_vente_add', false)) {
			echo json_encode([
				'success' => false,
				'message' => cclang('sorry_you_do_not_have_permission_to_access')
				]);
			exit;
		}
		
		

		$this->form_validation->set_rules('type_vente', 'نوع المنتوج', 'trim|required');
		

		$this->form_validation->set_rules('qualite', 'النوع', 'trim|required');
		

		$this->form_validation->set_rules('quantite', 'الكمية', 'trim|required|max_length[300]');
		

		$this->form_validation->set_rules('prix_u', 'سعر الكغ', 'trim|required|max_length[300]');
		

		$this->form_validation->set_rules('montant', 'المجموع', 'trim|required|max_length[300]');
		

		$this->form_validation->set_rules('acheteur', 'المشتري', 'trim|required|max_length[300]');
		

		$this->form_validation->set_rules('tel', 'رقم الهاتف', 'trim|required|max_length[11]');
		

		$this->form_validation->set_rules('i_src', 'I Src', 'trim|required|max_length[300]');
		

		

		if ($this->form_validation->run()) {
		
			$save_data = [
				'type_vente' => $this->input->post('type_vente'),
				'qualite' => $this->input->post('qualite'),
				'quantite' => $this->input->post('quantite'),
				'prix_u' => $this->input->post('prix_u'),
				'montant' => $this->input->post('montant'),
				'acheteur' => $this->input->post('acheteur'),
				'date' => date('Y-m-d H:i:s'),
				'tel' => $this->input->post('tel'),
				'i_src' => $this->input->post('i_src'),
			];

			
			
			$save_agritun_vente = $this->model_agritun_vente->store($save_data);
            

			if ($save_agritun_vente) {
				if ($this->input->post('save_type') == 'stay') {
					$this->data['success'] = true;
					$this->data['id'] 	   = $save_agritun_vente;
					$this->data['message'] = cclang('success_save_data_stay', [
						anchor('administrator/agritun_vente/edit/' . $save_agritun_vente, 'Edit Agritun Vente'),
						anchor('administrator/agritun_vente', ' Go back to list')
					]);
				} else {
					set_message(
						cclang('success_save_data_redirect', [
						anchor('administrator/agritun_vente/edit/' . $save_agritun_vente, 'Edit Agritun Vente')
					]), 'success');

            		$this->data['success'] = true;
					$this->data['redirect'] = base_url('administrator/agritun_vente');
				}
			} else {
				if ($this->input->post('save_type') == 'stay') {
					$this->data['success'] = false;
					$this->data['message'] = cclang('data_not_change');
				} else {
            		$this->data['success'] = false;
            		$this->data['message'] = cclang('data_not_change');
					$this->data['redirect'] = base_url('administrator/agritun_vente');
				}
			}

		} else {
			$this->data['success'] = false;
			$this->data['message'] = 'Opss validation failed';
			$this->data['errors'] = $this->form_validation->error_array();
		}

		$this->response($this->data);
	}
	
		/**
	* Update view Agritun Ventes
	*
	* @var $id String
	*/
	public function edit($id)
	{
		$this->is_allowed('agritun_vente_update');

		$this->data['agritun_vente'] = $this->model_agritun_vente->find($id);

		$this->template->title('عمليات البيع Update');
		$this->render('backend/standart/administrator/agritun_vente/agritun_vente_update', $this->data);
	}

	/**
	* Update Agritun Ventes
	*
	* @var $id String
	*/
	public function edit_save($id)
	{
		if (!$this->is_allowed('agritun_vente_update', false)) {
			echo json_encode([
				'success' => false,
				'message' => cclang('sorry_you_do_not_have_permission_to_access')
				]);
			exit;
		}
				$this->form_validation->set_rules('type_vente', 'نوع المنتوج', 'trim|required');
		

		$this->form_validation->set_rules('qualite', 'النوع', 'trim|required');
		

		$this->form_validation->set_rules('quantite', 'الكمية', 'trim|required|max_length[300]');
		

		$this->form_validation->set_rules('prix_u', 'سعر الكغ', 'trim|required|max_length[300]');
		

		$this->form_validation->set_rules('montant', 'المجموع', 'trim|required|max_length[300]');
		

		$this->form_validation->set_rules('acheteur', 'المشتري', 'trim|required|max_length[300]');
		

		$this->form_validation->set_rules('tel', 'رقم الهاتف', 'trim|required|max_length[11]');
		

		$this->form_validation->set_rules('i_src', 'I Src', 'trim|required|max_length[300]');
		

		
		if ($this->form_validation->run()) {
		
			$save_data = [
				'type_vente' => $this->input->post('type_vente'),
				'qualite' => $this->input->post('qualite'),
				'quantite' => $this->input->post('quantite'),
				'prix_u' => $this->input->post('prix_u'),
				'montant' => $this->input->post('montant'),
				'acheteur' => $this->input->post('acheteur'),
				'date' => date('Y-m-d H:i:s'),
				'tel' => $this->input->post('tel'),
				'i_src' => $this->input->post('i_src'),
			];


			
			
			$save_agritun_vente = $this->model_agritun_vente->change($id, $save_data);

			if ($save_agritun_vente) {
				if ($this->input->post('save_type') == 'stay') {
					$this->data['success'] = true;
					$this->data['id'] 	   = $id;
					$this->data['message'] = cclang('success_update_data_stay', [
						anchor('administrator/agritun_vente', ' Go back to list')
					]);
				} else {
					set_message(
						cclang('success_update_data_redirect', [
					]), 'success');

            		$this->data['success'] = true;
					$this->data['redirect'] = base_url('administrator/agritun_vente');
				}
			} else {
				if ($this->input->post('save_type') == 'stay') {
					$this->data['success'] = false;
					$this->data['message'] = cclang('data_not_change');
				} else {
            		$this->data['success'] = false;
            		$this->data['message'] = cclang('data_not_change');
					$this->data['redirect'] = base_url('administrator/agritun_vente');
				}
			}
		} else {
			$this->data['success'] = false;
			$this->data['message'] = 'Opss validation failed';
			$this->data['errors'] = $this->form_validation->error_array();
		}

		$this->response($this->data);
	}
	
	/**
	* delete Agritun Ventes
	*
	* @var $id String
	*/
	public function delete($id = null)
	{
		$this->is_allowed('agritun_vente_delete');

		$this->load->helper('file');

		$arr_id = $this->input->get('id');
		$remove = false;

		if (!empty($id)) {
			$remove = $this->_remove($id);
		} elseif (count($arr_id) >0) {
			foreach ($arr_id as $id) {
				$remove = $this->_remove($id);
			}
		}

		if ($remove) {
            set_message(cclang('has_been_deleted', 'agritun_vente'), 'success');
        } else {
            set_message(cclang('error_delete', 'agritun_vente'), 'error');
        }

		redirect_back();
	}

		/**
	* View view Agritun Ventes
	*
	* @var $id String
	*/
	public function view($id)
	{
		$this->is_allowed('agritun_vente_view');

		$this->data['agritun_vente'] = $this->model_agritun_vente->join_avaiable()->filter_avaiable()->find($id);

		$this->template->title('عمليات البيع Detail');
		$this->render('backend/standart/administrator/agritun_vente/agritun_vente_view', $this->data);
	}
	
	/**
	* delete Agritun Ventes
	*
	* @var $id String
	*/
	private function _remove($id)
	{
		$agritun_vente = $this->model_agritun_vente->find($id);

		
		
		return $this->model_agritun_vente->remove($id);
	}
	
	
	/**
	* Export to excel
	*
	* @return Files Excel .xls
	*/
	public function export()
	{
		$this->is_allowed('agritun_vente_export');

		$this->model_agritun_vente->export(
			'agritun_vente', 
			'agritun_vente',
			$this->model_agritun_vente->field_search
		);
	}

	/**
	* Export to PDF
	*
	* @return Files PDF .pdf
	*/
	public function export_pdf()
	{
		$this->is_allowed('agritun_vente_export');

		$this->model_agritun_vente->pdf('agritun_vente', 'agritun_vente');
	}


	public function single_pdf($id = null)
	{
		$this->is_allowed('agritun_vente_export');

		$table = $title = 'agritun_vente';
		$this->load->library('HtmlPdf');
      
        $config = array(
            'orientation' => 'p',
            'format' => 'a4',
            'marges' => array(5, 5, 5, 5)
        );

        $this->pdf = new HtmlPdf($config);
        $this->pdf->setDefaultFont('stsongstdlight'); 

        $result = $this->db->get($table);
       
        $data = $this->model_agritun_vente->find($id);
        $fields = $result->list_fields();

        $content = $this->pdf->loadHtmlPdf('core_template/pdf/pdf_single', [
            'data' => $data,
            'fields' => $fields,
            'title' => $title
        ], TRUE);

        $this->pdf->initialize($config);
        $this->pdf->pdf->SetDisplayMode('fullpage');
        $this->pdf->writeHTML($content);
        $this->pdf->Output($table.'.pdf', 'H');
	}

	
}


/* End of file agritun_vente.php */
/* Location: ./application/controllers/administrator/Agritun Vente.php */