<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Sale_Multiple_Payment extends CI_Controller
{
    var $main_menu_name = "sale_multiple_payment";
    var $sub_menu_name = "sale_multiple_payment";
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Sales_Model');
        $this->load->model('Supplier_Model');
        $this->load->model('Warehouse_Model');
        $this->load->model('Common_Model');
        $this->load->model('Tax_Rates_Model');
        $this->load->model('Customer_Model');
        $this->load->model('Sequerty_Model');
        $this->load->model('Product_Models');
        $this->load->model('Expenses_Model');
        $this->load->model('category_models');
        $this->load->model('Unit_Model');
        $this->load->model('User_Model');
        $this->load->model('Sale_Multiple_Payment_Model');
    }
    public function index()
    {
        $data['expenses']       = $this->Expenses_Model->get_all_expenses();
        $data['main_menu_name'] = $this->main_menu_name;
        $data['sub_menu_name']  = 'sale_multiple_payment_list';
        $this->load->view('sale_multiple_payment_list', $data);
    }
    public function itemlist()
    {
        $data['expenses']       = $this->Expenses_Model->get_all_expenses();
        $data['main_menu_name'] = $this->main_menu_name;
        $data['sub_menu_name']  = $this->sub_menu_name;
        $this->load->view('expenses', $data);
    }
    public function manage()
    {
        $data['main_menu_name'] = $this->main_menu_name;
        $data['sub_menu_name']  = 'sale_multiple_payment_add';
        $customer_id            = $this->uri->segment('3');
        $data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
        $data['customer_list']  = $this->Customer_Model->get_all_customers();
        $data['sales_list']     = $this->Sale_Multiple_Payment_Model->get_sale_complete_info_by_customer_id($customer_id);
        $data['customer_id']    = $customer_id;
        $this->load->view('sale_multiple_payment', $data);
    }
    public function save_sale_multiple_payment()
    {	
		$total_val					= $this->input->post('sale_pymnt_amount');
        $warehouse_id               = $this->input->post('warehouse_id');
        $cash_collector_id          = $this->input->post('cash_collector_id');
        $sale_pymnt_paying_by       = $this->input->post('sale_pymnt_paying_by');
        $customer_id                = intval($this->input->post('customer_id'));
        $sale_pymnt_date_time       = date('Y-m-d', strtotime($this->input->post('sale_pymnt_date_time')));
        $row                        = $this->input->post('row_c');
        $rowCount                   = $this->input->post('rowCount');
		$sale_note                = $this->input->post('sale_note');
        $user_id                    = $this->session->userdata('ss_user_id');
        $sale_pymnt_added_date_time = date("Y-m-d H:i:s");
        $data_item                  = array();
        $this->db->trans_start();
        $smp_last_id        = '0';
        $smp_id             = 0;
        $old_in_smp_last_id = $this->Sale_Multiple_Payment_Model->get_next_ref_no();
        $old_in_smp_last_id = $old_in_smp_last_id->smp_last_id;
        $smp_last_id        = $old_in_smp_last_id + 1;
        $smp_no             = 'MP/' . sprintf('%03d', $smp_last_id);
        $sp_data_item       = array(
            'smp_no' => $smp_no,
            'smp_last_id' => $smp_last_id,
            'smp_amount' => floatval($this->input->post('smp_amount')),
            'smp_no' => $smp_no,
            'user_id' => $user_id,
            'customer_id' => $customer_id,
            'smp_added_date' => $sale_pymnt_date_time,
            'warehouse_id' => $this->session->userdata('ss_warehouse_id')
        );
        $this->Sale_Multiple_Payment_Model->save_sale_multiple_payment_header($sp_data_item);
        $smp_id                           = $this->db->insert_id();
        $sale_pymnt_cheque_no             = $this->input->post('sale_pymnt_cheque_no');
        $sale_pymnt_crdt_card_no          = $this->input->post('sale_pymnt_crdt_card_no');
        $sale_pymnt_crdt_card_holder_name = $this->input->post('sale_pymnt_crdt_card_holder_name');
        $sale_pymnt_crdt_card_month       = $this->input->post('sale_pymnt_crdt_card_month');
        $sale_pymnt_crdt_card_year        = $this->input->post('sale_pymnt_crdt_card_year');
        $sale_pymnt_crdt_card_type        = $this->input->post('sale_pymnt_crdt_card_type');
        for ($i = 1; $i <= $rowCount; $i++) {
            if (isset($row[$i]['sale_id'][0])) {
                $collection = '';
                if (isset($row[$i]['collection'][0])) {
                    $collection = $row[$i]['collection'][0];
                }
				//if($this->input->post('sale_pymnt_cheque_no'))
				//$sale_note.= '<br />Cheque Value:'.$total_val;
                $data_item = array(
                    'sale_id' => $row[$i]['sale_id'][0],
                    'sale_pymnt_amount' => floatval($row[$i]['amount'][0]),
                    'sale_pymnt_paying_by' => $sale_pymnt_paying_by,
                    'user_id' => $user_id,
                    'sale_pymnt_added_date_time' => $sale_pymnt_added_date_time,
                    'sale_payment_type' => 'sale',
                    'sale_pymnt_cheque_no' => $sale_pymnt_cheque_no,
                    'sale_pymnt_crdt_card_no' => $sale_pymnt_crdt_card_no,
                    'sale_pymnt_crdt_card_holder_name' => $sale_pymnt_crdt_card_holder_name,
                    'sale_pymnt_crdt_card_type' => $sale_pymnt_crdt_card_type,
                    'sale_pymnt_crdt_card_month' => $sale_pymnt_crdt_card_month,
                    'sale_pymnt_crdt_card_year' => $sale_pymnt_crdt_card_year,
                    'sale_pymnt_date_time' => $sale_pymnt_date_time,
                    'smp_id' => $smp_id,
					'sale_pymnt_note' => $sale_note
                );
                if ($collection) {
                    $this->Sale_Multiple_Payment_Model->save_sale_multiple_payment($data_item);
                }
                $disMsg = "";
            }
        }
        $disMsg = '';
        $this->db->trans_complete();
        $trans_status = $this->db->trans_status();
        if ($trans_status == FALSE) {
            $this->db->trans_rollback();
        } else {
            $this->db->trans_commit();
        }
        echo json_encode(array(
            'sale_id' => 1,
            'error' => 0,
            'disMsg' => $disMsg
        ));
    }
    public function expenses_add_row()
    {
        $product_id        = $this->input->post('id');
        $rowCount          = $this->input->post('rowCount');
        $pro_dlts          = $this->Product_Models->get_product_by_id($product_id);
        $supp_id           = $this->input->post('supp_id');
        $cat_id            = $this->input->post('cat_id');
        $exp_voucher_no    = $this->input->post('exp_voucher_no');
        $exp_datetime      = $this->input->post('exp_datetime');
        $row_details       = '';
        $msg               = '';
        $error             = false;
        $selbox            = '';
        $tmp_th_e          = $rowCount;
        $pymnt_cheque_date = '';
        $required_date     = date('m/d/Y');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('supp_id', 'Supplier', 'required');
        if (!$exp_datetime) {
            $error = true;
            $msg .= "Voucher Date, ";
        }
        if (!$error) {
            $prodcut_name_fld_type = 'hidden';
            $prodcut_name_txt      = $pro_dlts->product_name;
            if ($pro_dlts->product_name == 'Other') {
                $prodcut_name_fld_type = 'text';
                $prodcut_name_txt      = '';
            }
            $unit_list    = $this->Unit_Model->get_all_unit_list();
            $unit_sel_box = '';
            $unit_sel_box .= "<select id=\"product_unit_$tmp_th_e\" class=\"\" name=\"row_e[$tmp_th_e][product_unit][]\">";
            $unit_sel_box .= "<option value=\"\">
                            -Select-
                            </option>";
            foreach ($unit_list as $row) {
                $sel = '';
                if ($pro_dlts->product_unit == $row->unit_id) {
                    $sel = " selected=\"selected\"";
                } else if ($prodcut_name_txt == '') {
                    if ($row->unit_id == 6) {
                        $sel = " selected=\"selected\"";
                    }
                }
                $unit_sel_box .= "<option value=\"$row->unit_id\" $sel>
                                $row->unit_name
                            </option>";
            }
            $unit_sel_box .= "
                        </select>";
            $row_details = "
            <tr id=\"row_e_$tmp_th_e\">
            <td class=\"text-left\">
             $prodcut_name_txt
            <input type=\"$prodcut_name_fld_type\" style=\"width:100%; text-align:left\" name=\"row_e[$tmp_th_e][product_name][]\" id=\"product_name_$tmp_th_e\" value=\"$pro_dlts->product_name\" class=\"pymnt_amount\">
            <input type=\"hidden\" style=\"width:100%; text-align:left\" name=\"row_e[$tmp_th_e][product_id][]\" id=\"product_id_$tmp_th_e\" value=\"$pro_dlts->product_id\" class=\"product_id\">
            </td>
            <td>$unit_sel_box</td>
            <td><input type=\"text\" style=\"width:100%; text-align:right\" name=\"row_e[$tmp_th_e][expitm_qty][]\" id=\"expitm_qty_$tmp_th_e\" value=\"1.00\" class=\"expitm_qty\" onchange=\"changeQtyByProductID(this.value,$tmp_th_e);\" onclick=\"this.select(); setTmpVal(this.value);\"></td>
            <td><input type=\"text\" style=\"width:100%; text-align:right\" name=\"row_e[$tmp_th_e][product_cost][]\" id=\"product_cost_$tmp_th_e\" value=\"$pro_dlts->product_cost\" class=\"product_cost\" onchange=\"calculateTotal();\" ></td>
            <td><input type=\"text\" style=\"width:100%; text-align:right\" name=\"row_e[$tmp_th_e][expitm_dis][]\" id=\"expitm_dis_$tmp_th_e\" value=\"\" class=\"expitm_dis\" onchange=\"changeDiscountByProductID(this.value,1);\" onclick=\"this.select(); setTmpVal(this.value);\">
            <input type=\"hidden\" style=\"width:100%; text-align:right\" name=\"row_e[$tmp_th_e][expitm_dis_val][]\" id=\"expitm_val_$tmp_th_e\" value=\"\" class=\"expitm_dis_val\">
            </td>
            <td><input type=\"text\" style=\"width:100%; text-align:right\" name=\"row_e[$tmp_th_e][sub_total_item][]\" id=\"sub_total_item_$tmp_th_e\" value=\"\" class=\"\" readonly></td>
            <td><a onclick=\"deleteExpensesItem($tmp_th_e)\"><i style=\"cursor:pointer;\" title=\"Remove\" id=\"1446800197032\" class=\"fa fa-times tip podel\"></i></a></td>
            </tr>
            ";
            $e           = array(
                'status' => 1,
                'row_details' => $row_details,
                'msg' => $row_details
            );
            echo json_encode($e);
        } else {
            $e = array(
                'status' => 0,
                'row_details' => '',
                'msg' => $msg
            );
            echo json_encode($e);
        }
    }
    public function view()
    {
        $data['main_menu_name']    = $this->main_menu_name;
        $data['sub_menu_name']     = '';
        $exp_id                    = $this->uri->segment('3');
        $data['exp_item_list']     = $this->Expenses_Model->get_exp_item_list_by_exp_id($exp_id);
        $data['exp_details']       = $this->Purchases_Order_Model->get_exp_info($exp_id);
        $data['customer_details']  = $this->Customer_Model->get_customer_info($data['exp_details']['customer_id']);
        $data['warehouse_details'] = $this->Warehouse_Model->get_warehouse_info($data['exp_details']['warehouse_id']);
        $data['exp_id']            = $exp_id;
        $this->load->view('expenses_view', $data);
    }
    public function get_next_ref_no()
    {
        $query            = $this->Expenses_Model->get_next_ref_no();
        $result           = $query->row();
        $exp_reference_no = sprintf("%05d", $result->exp_id + 1);
        $exp_reference_no = $exp_reference_no;
        echo json_encode(array(
            'exp_reference_no' => $exp_reference_no
        ));
    }
    public function expenses_details()
    {
        $data['main_menu_name'] = $this->main_menu_name;
        $data['sub_menu_name']  = '';
        $exp_id                 = $this->uri->segment('3');
        if (isset($exp_id)) {
            $data['exp_details'] = $this->Expenses_Model->get_expenses_details_by_id($exp_id);
            $data['expitm_list'] = $this->Expenses_Model->get_expenses_items_by_id($exp_id);
        }
        $data['exp_id'] = $exp_id;
        $this->load->view('expenses_details', $data);
    }
    public function sale_multiple_payment_print()
    {
        $smp_id                    = $this->uri->segment('3');
        $data['mp_details']        = $this->Sale_Multiple_Payment_Model->get_sale_multiple_payment_details_by_id($smp_id);
        $mp_details                = $data['mp_details'];
        $data['mp_itm_list']       = $this->Sale_Multiple_Payment_Model->get_sale_multiple_payment_items_by_id($smp_id);
        $data['warehouse_details'] = $this->Warehouse_Model->get_warehouse_info($data['mp_details']->warehouse_id);
        $this->load->view('models/sale_multiple_payment_print', $data);
    }
    public function exp_details()
    {
        $exp_id                    = $this->input->get('exp_id');
        $data['exp_details']       = $this->Expenses_Model->get_exp_info($exp_id);
        $data['exp_item_list']     = $this->Expenses_Model->get_exp_item_list_by_exp_id($exp_id);
        $data['customer_details']  = $this->Customer_Model->get_customer_info($data['exp_details']['customer_id']);
        $data['warehouse_details'] = $this->Warehouse_Model->get_warehouse_info($data['exp_details']['warehouse_id']);
        $data['cr_limit_list']     = $this->Common_Model->get_all_cr_limit();
        $this->load->view('models/expenses_print', $data);
    }
    public function list_sale_multiple_payment()
    {
        $requestData   = $_REQUEST;
        $columns       = array(
            0 => 'exp_id',
            1 => 'exp_id',
            2 => 'exp_id',
            3 => 'exp_id',
            4 => 'exp_id',
            5 => 'exp_id'
        );
        $data          = array();
        $expenses      = $this->Sale_Multiple_Payment_Model->get_all_list_sale_multiple_payment();
        $totalData     = count($expenses);
        $totalFiltered = $totalData;
        foreach ($expenses as $row) {
            $nestedData            = array();
            $smp_id                = $row['smp_id'];
            $total_paid_amount     = '';
            $nestedData[]          = $row['smp_no'];
            $nestedData[]          = $row['cus_name'];
            $nestedData[]          = $row['smp_amount'];
            $nestedData[]          = date('d/M/Y H:i:s', strtotime($row['smp_added_date']));
            $prepared_status_dis   = '';
            $checked_status_dis    = '';
            $approved_status_dis   = '';
            $authorized_status_dis = '';
            $actionTxtDisble       = '';
            $actionTxtEnable       = '';
            $actionTxtUpdate       = '';
            $actionTxtDelete       = '';
            $url                   = '';
            $nestedData[]          = '<div class="btn-group text-left">
                            <button data-toggle="dropdown" class="btn btn-default btn-xs btn-primary dropdown-toggle" type="button">Actions <span class="caret"></span></button>
                            <ul role="menu" class="dropdown-menu pull-right">
                          
                            
                            <li><a onClick="fbs_click(' . $row['smp_id'] . ')" data-toggle="modal" href="#" data-placement="top" data-original-title="Edit suppliers"><i class="fa fa-print"></i> Print Payment</a></li>
                            
                            </ul></div>';
            $data[]                = $nestedData;
        }
        $json_data = array(
            "recordsTotal" => intval($totalData),
            "recordsFiltered" => intval($totalFiltered),
            "data" => $data
        );
        echo json_encode($json_data);
    }
}