<?php

defined('BASEPATH') OR exit('No direct script access allowed');
class Multi_orders extends MY_Controller {
    var $cats;
    var $thispage;
    public function __construct() {
        parent::__construct();
        $this->load->model('booking');
        $this->thispage = "multi_orders";
    }

    public function index($cats = 1) {
        $this->cats = $cats;
        $this->all($cats);
    }

    public function all($page = 1, $id_producer = '', $active = '') {
        $searchfor = $this->security->xss_clean($this->input->get('search'));
        $whr = '';
        $data['id_producer'] = $id_producer;
        $data['active'] = $active;
        if (($id_producer))
            $whr = " id_producer=$id_producer";
        if (($active))
            $whr = "$whr and active=" . str_replace("2", "0", $active);
        $this->db->from('multi_orders');
        if ($whr)
            $this->db->where($whr);
        $max = 40;
        $data['all_count'] = ceil($this->db->count_all_results() / $max);
        $this->db->from('multi_orders');
        $this->db->order_by('id', 'desc');
        if ($whr)
            $this->db->where($whr);
        $this->db->limit($max, ($max * $page) - $max    );
        $data['all_groups'] = $this->db->get()->result_array();
        if ($searchfor) {
            $this->booking->add_rep_user("  البحث عن    " . $searchfor);
            $split_stemmed = explode(" ", $searchfor);
            $whr = " (";
            while (list($key, $val) = @each($split_stemmed)) {
                if ($val <> " " and strlen($val) > 0) {

                    $whr .= " ( BINARY name LIKE '%" . $val . "%'   ) and";
                }
            }
            $whr = substr($whr, 0, (strLen($whr) - 4));
            $whr .= " )or     cid LIKE '" . $searchfor . "'  OR  mobile = '" . $searchfor . "'";

            if (!$this->session->userdata('group'))
                $whr = " cid LIKE '" . $searchfor . "'  OR  mobile = '" . $searchfor . "'";

            $query = $this->db->query('SELECT * FROM multi_orders where ' . $whr);
            $data['all_count'] = 0;
            $whr = '';
            $data['all_groups'] = $query->result_array();
            $data['all_count'] = count($data['all_groups']);
        }
        foreach ($data['all_groups'] as $row) {
            //$data['all_groups']['allcat']=$this->db->from('cat')->where("parentcatid=".$row['catid'])->count_all_results();
        }
        $data['title'] = 'العملاء';
        $data['view'] = '../controllers/views/multiorder/all';
        $data['thispage'] = $this->thispage;
        $this->load->view('../controllers/views/layout', $data);
    }

    public function add($cats = 1) {
        $add = FALSE;
        $this->cats = $cats;
        if ($this->input->server('REQUEST_METHOD') =="POST") {
            $this->form_validation->set_rules('id_producer[]', 'product id', 'trim|required');
            if ($this->form_validation->run() === FALSE) {
               die();$add = FALSE;
            } else {
                $id_product = $this->input->post('id_producer');
                $quantity = $this->input->post('quantity');
                $price = $this->input->post('product_price');
                $datas = $this->input->post();
                foreach ($id_product as $key => $value) {            
                    $data['name'] =$value;    
                    $data['quantity'] =  $quantity[$key]; 
                    $data['price'] =  $price[$key]; 
                    $data['create_on'] = date('Y-m-d h:m:sa'); 
                    $this->db->insert('products',$data);
                    $insert_id[] = $this->db->insert_id();
                }  
                 $start_expiry = $this->input->post('start_expiry');
                $datearsh11 = explode("-", $start_expiry);
                $datearsh1 = explode("/", trim($datearsh11[0]));
                $start_date = "$datearsh1[0]/$datearsh1[1]/$datearsh1[2]";
                //   echo "$datearsh1[2]-$datearsh1[0]-$datearsh1[1]";
                $start_date_t = strtotime("$datearsh1[2]-$datearsh1[0]-$datearsh1[1]");
                $datearsh1 = explode("/", trim($datearsh11[1]));
                //$datearsh1 = explode("-", $this->input->post('start_date')) ;
                $expiry_date = "$datearsh1[0]/$datearsh1[1]/$datearsh1[2]";
                $expiry_date_t = strtotime("$datearsh1[2]-$datearsh1[0]-$datearsh1[1]");
                $product_id_array = $this->input->post('id_producer'); 
                $id_producer = $this->input->post('id_producer');
                $id_user = $this->input->post('id_user');
                $discount = $this->input->post('discount');
                $discount_why = $this->input->post('discount_why');
                $paymentnew = trim($this->input->post('paymentnew'));
                $paid = $this->input->post('paid');
                foreach ($id_product as $row) {
                    $query = $this->db->get_where('bills_producer', array('id' => $row));
                    $data['edit'] = $query->row_array();
                    $discount_remainder[] = $data['edit']['valuess']; 
                    $payment[] = $data['edit']['valuess'];
                    $name_order[] =$data['edit']['name'];
                }
                // $name_order = implode($name_order,',');
                $payment = array_sum($payment);
                $discount_remainder = $payment - $discount;
                if (!$discount)
                    $discount = 0;
                if (($paymentnew) and $this->input->post('paymentnew') == 1) {
                    $payment = $paymentnew;
                    $discount_remainder = $payment - $discount;
                }
                if ($paid == 4 OR $paid == 3) {
                    $payment = 0;
                    $discount = 0;
                    $discount_remainder = 0;
                }
                $result = FALSE;
                    $query = $this->db->get_where('bills_user', array('id' => $id_user));
                    $dataaa = $query->row_array();
                    $name = $dataaa['username'];
                    $sex = $dataaa['sex'];
                    $add = TRUE;
                    $product_id_array = implode($product_id_array,',');
                       foreach ($insert_id as $key => $value) {
                        $dataadd = array(
                            'id_user' => $id_user,
                            'id_producer' => $value,
                            'start_date' => $start_date,
                            'expiry_date' => $expiry_date,
                            'start_date_t' => $start_date_t,
                            'expiry_date_t' => $expiry_date_t,
                            'date_now' => date("d/m/y", $this->booking->tissme_now()),
                            'date_now_t' => $this->booking->tissme_now(),
                            'payment' => $price[$key],
                            'discount' => $discount,
                            'discount_remainder' => $price[$key] - $discount,
                            'discount_why' => $discount_why,
                            'name_order' => $name_order[$key],
                            'since_time_register ' => $this->booking->tissme_now(),
                            'name' => $name,
                            'sex' => $sex,
                            'paid' => $paid,
                        );
                        $dataadd = $this->security->xss_clean($dataadd);
                        $result = $this->db->insert('multi_orders', $dataadd);
                    }
                if ($result) {
                    $this->session->set_flashdata('msg', '');
                    redirect(base_url('Multi_orders/index/'));
                }
            }
        }
        if (!$add) {
            $this->db->from('bills_user');
            $this->db->order_by('id', 'desc');
            //$this->db->where(" stop!='1' ");
            $data['bills_user'] = $this->db->get()->result_array();

            $this->db->from('bills_producer');
            $this->db->order_by('id', 'desc');
            $this->db->where(" active='1' ");
            $data['bills_producer'] = $this->db->get()->result_array();
            $data['title'] = 'اضافة عميل';
            $data['cats'] = $this->cats;
            $data['thispage'] = $this->thispage;
            $data['view'] = '../controllers/views/multiorder/add';
            $this->load->view('../controllers/views/layout', $data);
        }
    }

    public function printorder($id) {

        $query = $this->db->get_where('multi_orders', array('id' => $id));
        $data['show'] = $query->row_array();
        $query = $this->db->get_where('bills_user', array('id' => $data['show']['id_user']));
        $data['show2'] = $query->row_array();
        $query = $this->db->get_where('bills_producer', array('id' => $data['show']['id_producer']));
        $data['producer'] = $query->row_array();
        $add = FALSE;
        $data['title'] = '  عرض عميل';
        $data['thispage'] = 'multiorder';
        $data['view'] = '../controllers/views/multiorder/show';
        $this->load->view('../controllers/views/multiorder/show', $data);
    }

    public function del($id) {
        $query = $this->db->get_where('multi_orders', array('id' => $id));
        $data['edit'] = $query->row_array();
        $this->db->where('id', $id);
        $result = $this->db->delete('multi_orders');
        if (count($result) > 0) {
            $this->session->set_flashdata('msg', 'تم حذف  القسم بفضل الله!');
            redirect(base_url('Multi_orders/index/'));
        }
    }    
    public function get_price() {
        $id = $this->input->post('id');
        if(!empty($id)){   
            $this->db->where('id',$id);
        $array = $this->db->get('bills_producer')->row_array();
        $data['price'] = @$array['valuess'];
        if($data){ 
        $data['status']=1;
        }else{
            $data['status']=0;
        }
        }else{
            $data['status']=0;
            $data['message']='invalid id';
        }
        echo json_encode($data);
    }
}

?>