<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Supplier_Multiple_Payment extends CI_Controller
{
    var $main_menu_name = "supplier_multiple_payment";
    var $sub_menu_name = "supplier_multiple_payment";
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Purchases_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('Supplier_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('Supplier_Multiple_Payment_Model');
        $this->load->model('Cheque_Process_Model');
        $this->load->model('Sales_Model');
         $this->load->model('Supplier_Payment_Adjustment_Model');
        //Supplier_Payment_Adjustment_Model
    }
    public function index()
    {
        $data['main_menu_name'] = $this->main_menu_name;
        $data['sub_menu_name']  = 'supplier_multiple_payment_add';
        $data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
        $data['supplier_list']  = $this->Supplier_Model->get_all_supplier();
        $this->load->view('supplier_multiple_payment_filter', $data);
    }
	function supplier_multiple_payment_list()
    {
        //$data['expenses']       = $this->Expenses_Model->get_all_expenses();
        $data['main_menu_name'] = $this->main_menu_name;
        $data['sub_menu_name']  = 'supplier_multiple_payment';
        $this->load->view('supplier_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']  = 'supplier_multiple_payment_add';
        $supplier_id            = $this->uri->segment('3');
        $data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
        $data['supplier_list']  = $this->Supplier_Model->get_all_supplier();
        $data['purchases_list']     = $this->Supplier_Multiple_Payment_Model->get_purchase_complete_info_by_customer_id($supplier_id);
        $data['supplier_id']    = $supplier_id;
        $this->load->view('supplier_multiple_payment', $data);
    }*/
	function supplier_multiple_payment_add(){
		/*var srh_from_date=$('#srh_from_date').val();
	var srh_to_date=$('#srh_to_date').val();
	*/
	
	
        $data['main_menu_name'] = $this->main_menu_name;
        $data['sub_menu_name']  = 'supplier_multiple_payment_add';
        $data['warehouse_details'] = $this->Warehouse_Model->get_warehouse_info($this->input->get('srh_warehouse_id'));
        $data['supplier_details']  = $this->Supplier_Model->get_supplier_info($this->input->get('srh_supplier_id'));
        $data['purchases_list']     = $this->Supplier_Multiple_Payment_Model->get_purchase_complete_info_by_supplier_id($this->input->get('srh_supplier_id'),$this->input->get('srh_warehouse_id'));
        
        $data['purchases_rtn_list']     = $this->Supplier_Multiple_Payment_Model->get_purchase_rtn_complete_info_by_supplier_id($this->input->get('srh_supplier_id'),$this->input->get('srh_warehouse_id'));
        
        
        $data['sup_ajes_list']     = $this->Supplier_Payment_Adjustment_Model->get_supplier_payment_adjustment_list($this->input->get('srh_supplier_id'),$this->input->get('srh_warehouse_id'));
        
        
      //  print_r($data['sup_ajes_list']);
        
        $data['bank_details'] = $this->Supplier_Multiple_Payment_Model->get_all_bank_accounts();
	//	print_r($data['bank_details']);
		
		$srh_to_date = $this->input->get('srh_to_date');
		$srh_from_date = $this->input->get('srh_from_date');
		$srh_supplier_id = $this->input->get('srh_supplier_id');
		$srh_payment_term = '' ;
		$ss_user_id = '' ;
		$return_name = '' ;
		$srh_warehouse_id = $this->input->get('srh_warehouse_id');
		//echo 
		
		
		
		$data['supplier_balance']	= $this->Supplier_Multiple_Payment_Model->get_supplier_balance_by_supplier_id($srh_warehouse_id , $srh_to_date , $srh_from_date ,  $srh_supplier_id , $srh_payment_term , $ss_user_id , $return_name );
		$data['supplier_id']    = $this->input->get('srh_supplier_id');
		
		//get pending sales list
		$customer_id=0;
		$customer_id= $data['supplier_details']['customer_id'];
		$data['customer_id']=$customer_id;
		$cus_sales_list=array();
		if($customer_id){
		    $cus_sales_list=$this->Supplier_Multiple_Payment_Model->get_sale_complete_info_by_customer_id($customer_id);
		}
		$data['cus_sales_list']=$cus_sales_list;
		
		
	//	echo "customer_id:$customer_id";
        
		
		
		$this->load->view('supplier_multiple_payment_add', $data);
    }
    public function save_sasupplier_multiple_payment()
    {	
       // print_r($this->input->post());
		$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('supplier_id'));
        $sale_pymnt_date_time       = date('Y-m-d', strtotime($this->input->post('sale_pymnt_date_time')));
        $row                        = $this->input->post('row_c');
       // print_r($row);
        $rowCount                   = $this->input->post('rowCount');
		$sale_note               	= $this->input->post('sale_note');
		$sale_note_two             	= $this->input->post('sale_note_two');
        $user_id                    = $this->session->userdata('ss_user_id');
        $b_id             	= $this->input->post('b_id');
        $sale_pymnt_added_date_time = date("Y-m-d");
        $data_item                  = array();
        $this->db->trans_start();
        $smp_last_id        = '0';
        $smp_id             = 0;
        $old_in_smp_last_id = $this->Supplier_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,
            'supplier_id' => $customer_id,
            'smp_added_date' => $sale_pymnt_date_time,
            'warehouse_id' => $this->session->userdata('ss_warehouse_id'),
            'b_id' => $b_id,
            'smp_date'=>date("Y-m-d H:i:s"),
        );
        $this->Supplier_Multiple_Payment_Model->save_supplier_multiple_payment_header($sp_data_item);
       // echo $this->db->last_query();
        $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');
        
      //  echo "rowCount:$rowCount";
        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' => $row[$i]['sale_payment_type'][0],
                    '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,
					'sale_pymnt_note_two' => $sale_note_two,
                );
                
               // echo "Test <br/>";
                //echo "collection:$collection";
               // print_r($data_item);
                
                if ($collection) {
                    $this->Supplier_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' => $smp_id,
            '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->Supplier_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 supplier_multiple_payment_print()
    {
        
       
        $smp_id                    = $this->uri->segment('3');
       //  echo "smp_id:$smp_id";
        $data['mp_details']        = $this->Supplier_Multiple_Payment_Model->get_supplier_multiple_payment_details_by_id($smp_id);
        
        $mp_details                = $data['mp_details'];
       // print_r($mp_details);
        
        $data['mp_itm_list']       = $this->Supplier_Multiple_Payment_Model->get_supplier_multiple_payment_items_by_id($smp_id);
        
        $data['mp_itm_list']       = $this->Supplier_Multiple_Payment_Model->get_supplier_multiple_payment_items_by_id($smp_id);
        
        $data['mp_itm_grn_return_list']       = $this->Supplier_Multiple_Payment_Model->get_supplier_multiple_payment_grn_return_items_by_id($smp_id);
        
        $data['mp_itm_sup_ajes_list']       = $this->Supplier_Multiple_Payment_Model->get_supplier_multiple_payment_sup_adj_items_by_id($smp_id);
        
         //$data['mp_itm_list']       = $this->Supplier_Multiple_Payment_Model->test_fn($smp_id);
        
        //$data['mp_itm_list']       = $this->Supplier_Multiple_Payment_Model->get_total_paid_by_grn_return_id_and_smp_id($smp_id);
        
      //  $data['mp_itm_grn_return_list']       = $this->Supplier_Multiple_Payment_Model->get_total_paid_by_grn_return_id_and_smp_id($smp_id);
        
        $data['warehouse_details'] = $this->Warehouse_Model->get_warehouse_info($data['mp_details']->warehouse_id);
        $this->load->view('models/supplier_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->Supplier_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_supplier_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->Supplier_Multiple_Payment_Model->get_all_list_supplier_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['supp_company_name'];
            $nestedData[]          = $row['smp_amount'];
            $nestedData[]          = date('Y-m-d', strtotime($row['smp_added_date']));
            $prepared_status_dis   = '';
            $checked_status_dis    = '';
            $approved_status_dis   = '';
            $authorized_status_dis = '';
            $actionTxtDisble       = '';
            $actionTxtEnable       = '';
            $actionTxtUpdate       = '';
            $actionTxtDelete       = '';
            $url                   = '';
            
            //url,amount,chque_date,payee,payee_only,checkedValue
            $amount=$row['smp_amount'];
            $chque_date=$row['smp_date'];
            
            $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 style="cursor:pointer"><a onClick="fbs_click(' . $row['smp_id'] . ')" ><i class="fa fa-print"></i> Print Payment</a></li>
                             <li style="cursor:pointer"><a onClick="cheque_print(' . $row['smp_id'] . ','.$amount.','.$chque_date.')" ><i class="fa fa-print"></i> Print Cheque</a></li>
                            
                            </ul></div>';
                            //url,amount,chque_date,payee,payee_only,checkedValue
            $data[]                = $nestedData;
        }
        
        /*
        
                            <li style="cursor:pointer"><a onClick="delete_smp(' . $row['smp_id'] . ')" ><i class="fa fa-trash-o"></i> Delete Payment</a></li>
                            */
                            
                            $json_data = array(
            "recordsTotal" => intval($totalData),
            "recordsFiltered" => intval($totalFiltered),
            "data" => $data
        );
        echo json_encode($json_data);
    }
	function delete_smp()
    {
		$smp_id        	= $this->input->get('smp_id');
		$result			= $this->Supplier_Multiple_Payment_Model->delete_supplier_multiple_payment_by_id($smp_id);
		//echo $result;
		//$result = 0;
		$data = array('result'=>$result);
		//print_r($data);
		return json_encode($data);
	}
	
}