<?php

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

class Billing extends MY_Controller {

    var $cats;
    var $thispage;
    public function __construct() {
        parent::__construct();
        $this->load->model('booking');
        $this->thispage = "orders";
        $this->db->from('bills_orders');
        $this->db->where("start_date_t  IS NULL");
        $data['all_groups'] = $this->db->get()->result_array();
        $this->operator_id = $this->session->userdata('operator_id');
        if(get_ap('accountant',$this->operator_id)==1){ }else{ redirect("","refresh");}
        foreach ($data['all_groups'] as $row) {
            $datearsh1 = explode("/", $row['start_date']);
            $day1 = $datearsh1[0];
            $month1 = $datearsh1[1];
            $year1 = $datearsh1[2];
            $start_date = strtotime("$year1-$month1-$day1");

            $datearsh1 = explode("/", $row['expiry_date']);
            $day1 = $datearsh1[0];
            $month1 = $datearsh1[1];
            $year1 = $datearsh1[2];
            $expiry_date = strtotime("$year1-$month1-$day1");


            $datearsh1 = explode("/", $row['date_now']);
            $day1 = $datearsh1[0];
            $month1 = $datearsh1[1];
            $year1 = $datearsh1[2];
            $date_now = strtotime("$year1-$month1-$day1");
            $edit_data = array(
                'start_date_t' => $start_date,
                'expiry_date_t' => $expiry_date,
                'date_now_t' => $date_now,
            );
            $edit_data = $this->security->xss_clean($edit_data);
            $result = $this->db->where('id', $row['id'])->update('bills_orders', $edit_data);
        }
        $edit_data = array(
            'active' => '0',
        );
        $edit_data = $this->security->xss_clean($edit_data);
        $result = $this->db->update('bills_orders', $edit_data);

        $edit_data = array(
            'active' => '1',);
        $edit_data = $this->security->xss_clean($edit_data);
        $result = $this->db->where("expiry_date_t<" . $this->booking->tissme_now())->update('bills_orders', $edit_data);

        
        
           $edit_data = array(
            'stop' => '1',
        );
        $edit_data = $this->security->xss_clean($edit_data);
        $result = $this->db->update('bills_user', $edit_data);
        $this->db->from('bills_orders');
        $this->db->where("active='0'");
        $data['all_groups'] = $this->db->get()->result_array();
        foreach ($data['all_groups'] as $row) {
   
                    $edit_data = array(
            'stop' => '2',
        );
        $edit_data = $this->security->xss_clean($edit_data);
        $result = $this->db->where('id', $row['id_user'])->update('bills_user', $edit_data);
            
        }
    }

    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('bills_orders');
        if ($whr)
            $this->db->where($whr);
        $max = 40;
        $data['all_count'] = ceil($this->db->count_all_results() / $max);
        $this->db->from('bills_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 bills_orders where ' . $whr);
            $data['all_count'] = 0;
            $whr = '';
            $data['all_groups'] = $query->result_array();
            $data['all_count'] = count($data['all_groups']);
            // echo print_r($this->db->last_query());
        }
        /// $whr=" cid LIKE '" . $searchfor . "'  OR  mobile = '" . $searchfor . "'";
        foreach ($data['all_groups'] as $row) {
            //$data['all_groups']['allcat']=$this->db->from('cat')->where("parentcatid=".$row['catid'])->count_all_results();
        }
        $data['title'] = 'Billing';
        $data['view'] = '../controllers/views/' . $this->thispage . '/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->post('submit')) {
            $this->form_validation->set_rules('id_producer', '', 'trim|required');
            // $this->form_validation->set_rules('id_user', '', 'trim|required');
            //	$this->form_validation->set_rules('comment', '', 'trim|required'); 
            //	$this->form_validation->set_rules('comm', '', 'trim|required'); 
            if ($this->form_validation->run() === FALSE) {

                $add = FALSE;
            } else {
                $start_expiry = $this->input->post('start_expiry');

                $datearsh11 = explode("-", $start_expiry);

                $datearsh1 = explode("/", trim($datearsh11[0]));

            //$datearsh1 = explode("-", $this->input->post('start_date')) ;
                $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]");


                $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');
                $query = $this->db->get_where('bills_producer', array('id' => $id_producer));
                $data['edit'] = $query->row_array();
                $name_order = $data['edit']['name'];
                $discount_remainder = $payment = $data['edit']['valuess'];
                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;
                foreach ($id_user as $selectedOption) {
                    $query = $this->db->get_where('bills_user', array('id' => $selectedOption));
                    $dataaa = $query->row_array();
                    $name = $dataaa['username'];
                    $sex = $dataaa['sex'];
                    $add = TRUE;
                    $dataadd = array(
                        'id_user' => $selectedOption,
                        'id_producer' => $id_producer,
                        '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' => $payment,
                        'discount' => $discount,
                        'discount_remainder' => $discount_remainder,
                        'discount_why' => $discount_why,
                        'name_order' => $name_order,
                        'since_time_register ' => $this->booking->tissme_now(),
                        'name' => $name,
                        'sex' => $sex,
                        'paid' => $paid,
                    );
                    $dataadd = $this->security->xss_clean($dataadd);
                    $result = $this->db->insert('bills_orders', $dataadd);
                }
                if ($result) {
                    $this->session->set_flashdata('msg', 'تم اضافة الفاتورة بفضل الله!');
                    redirect(base_url('' . $this->thispage . '/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['cats_select']  = $this->db->get()->result_array(); 


            $data['title'] = 'اضافة عميل';
            $data['cats'] = $this->cats;
            $data['thispage'] = $this->thispage;
            $data['view'] = '../controllers/views/' . $this->thispage . '/add';
            $this->load->view('../controllers/views/layout', $data);
        }
    }

    public function printorder($id) {

        $query = $this->db->get_where('bills_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'] = $this->thispage;
        $data['view'] = '../controllers/views/' . $this->thispage . '/show';
        $this->load->view('../controllers/views/' . $this->thispage . '/show', $data);
    }

    public function show($id) {

        $query = $this->db->get_where('bills_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();




        $add = FALSE;













        $data['title'] = '  عرض عميل';
        $data['thispage'] = $this->thispage;
        $data['view'] = '../controllers/views/' . $this->thispage . '/show';
        $this->load->view('../controllers/views/layout', $data);
    }

    public function del($id) {
        $query = $this->db->get_where('bills_orders', array('id' => $id));
        $data['edit'] = $query->row_array();


        $this->db->where('id', $id);
        $result = $this->db->delete('bills_orders');
        if (count($result) > 0) {
            $this->session->set_flashdata('msg', 'تم حذف  القسم بفضل الله!');
            redirect(base_url('orders/index/'));
        }
    }

    public function edit($id) {

        $query = $this->db->get_where('bills_orders', array('id' => $id));
        $data['edit'] = $query->row_array();

        $data['edit']['checked'] = array('', '', '', '', '', '', '', '', '');


        $data['edit']['checked'][$data['edit']['active']] = 'checked';
        $add = FALSE;
        if ($this->input->post('submit')) {
            $this->form_validation->set_rules('valuess', '', 'trim|required');
            $this->form_validation->set_rules('name', '', 'trim|required');
            //	$this->form_validation->set_rules('comment', '', 'trim|required'); 
            //	$this->form_validation->set_rules('comm', '', 'trim|required'); 


            if ($this->form_validation->run() === FALSE) {

                $add = FALSE;
            } else {






                $add = TRUE;
                $edit_data = array(
                    'valuess' => $this->input->post('valuess'),
                    'name' => $this->input->post('name'),
                    'active' => $this->input->post('active'),
                    'note' => $this->input->post('note'),
                );
                $edit_data = $this->security->xss_clean($edit_data);
                $result = $this->db->where('id', $id)->update('bills_orders', $edit_data);

                if ($result) {
                    $this->session->set_flashdata('msg', 'تم التعديل المنتج بفضل الله!');
                    redirect(base_url('' . $this->thispage . '/index/'));
                }
            }
        }


        if (!$add) {










            $data['title'] = 'تعديل  شقة';
            $data['thispage'] = $this->thispage;
            $data['view'] = '../controllers/views/' . $this->thispage . '/edit';
            $this->load->view('../controllers/views/layout', $data);
        }
    }

    public function clint_blacklist($id) {

        $this->db->where('id', $id)->update('bills_orders', array('oky' => 'ok', 'comment' => $this->input->post('comment')));

        $this->session->set_flashdata('msg', 'تم عمل بلاك لست بفضل الله');
        redirect(base_url() . 'dashboard');
    }
    

}

?>