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

class New_payment_cont extends CI_Controller {

        public function __construct() {
        parent::__construct();
        $this->load->model('Product_model');
       // $this->load->model('General');
       // $this->load->model('Common_model');
       
        if(!isset($this->session->userdata['login_data']['username'])) { 
                        redirect('login'); 
                        exit();
            }
        
         
        }

    
        function index(){
            $this->db->select('*');
            $this->db->from('mes_bank');
            $this->db->where('bank_stetus', 1);
            $hasil2=$this->db->get();  
            $bank = $hasil2->result();
            
            for($i=0; $i<count($bank); $i++){
             $bnkdt[] = array($bank[$i]->bank_id,$bank[$i]->bank_name); 
        }
         
            $data['bank'] = $bnkdt;
            $this->db->select('*');
            $this->db->from('cities');
            $hasil3=$this->db->get();  
            $city = $hasil3->result();
            
                for($i=0; $i<count($city); $i++){
             $citydt[] = array($city[$i]->id,$city[$i]->name_en); 
                } 
        
            $data['city'] = $citydt;
            $data['ref'] = $this->Product_model->salserep_list();
            
            $data['content'] = "pages/new_payment";        
            $this->load->view('template',$data);
            
        }
        
        function get_customer(){
            if(!isset($this->session->userdata['login_data']['username'])) { 
                        redirect('login'); 
                        exit();
            }
            
            
                $rest_api_base_url = 'https://neha.webip.lk/lumen/public';
                $post_endpoint = '/get_cust_all';
                $request_data = array('refid' => $this->input->post('id'));
                $response = perform_http_request('POST', $rest_api_base_url . $post_endpoint, $request_data); 
                $cust = json_decode($response);
                
                echo json_encode($cust);
        }
        
        function get_invlist(){
            
             if(!isset($this->session->userdata['login_data']['username'])) { 
                        redirect('login'); 
                        exit();
            }
            
            $this->db->select('*');
            $this->db->from('mes_qto');
            $this->db->where('po_status', 1);
            $this->db->where('po_auth', 1);
            $this->db->where('po_payment', 0);
            $this->db->where('po_sup_id',$this->input->post('id'));
            $hasil2=$this->db->get();  
            $bank = $hasil2->result();
            
             $bnkdt[]=array();
            for($i=0; $i<count($bank); $i++){
                
                    $this->db->select('pay_invid');
                    $this->db->from('mes_payment');
                    $this->db->where('pay_invid',$bank[$i]->po_id);
                    $this->db->where('pay_stetus', 1);
                    $this->db->where('pay_authrize', 0);
                    $hasil3=$this->db->get();  
                    $bank1 = $hasil3->result();
            
                        if(count($bank1)<1){
                      
                                $this->db->select('ret_invnum');
                                $this->db->from('inv_return');
                                $this->db->where('ret_invnum',$bank[$i]->po_id);
                                $this->db->where('ret_proceed', 0);
                                $hasil4=$this->db->get();  
                                $bank2 = $hasil4->result();
                                
                                if(count($bank2)<1){
                                    $bnkdt[] = array($bank[$i]->po_id,$bank[$i]->qto_code.$bank[$i]->po_id,$bank[$i]->uniq_id); 
                                }
                        
                        }
            
            }
            
            if(count($bank)<1){
                $bnkdt[]=array();
            }
            
            $this->db->select("*");    
            $this->db->from('mes_customer');
            $this->db->where('cust_id',$this->input->post('id'));
            $query = $this->db->get();
            $sdata = $query->result();
            
                        
            $data[0]=$bnkdt;
            $data[1] = $sdata[0]->cust_pay_excess;
            $data[2] = $sdata[0]->cust_return_excess;
            echo json_encode($data);
        }
        
        function get_allinvlist(){
            
             if(!isset($this->session->userdata['login_data']['username'])) { 
                        redirect('login'); 
                        exit();
            }
            
            $this->db->select('*');
            $this->db->from('mes_qto');
            $this->db->where('po_status', 1);
            $this->db->where('po_auth', 1);
            $this->db->where('po_sup_id',$this->input->post('id'));
            $hasil2=$this->db->get();  
            $bank = $hasil2->result();
            
            for($i=0; $i<count($bank); $i++){
             $bnkdt[] = array($bank[$i]->po_id,$bank[$i]->qto_code.$bank[$i]->po_id); 
            }
            
            if(count($bank)<1){
                $bnkdt[]=array();
            }
            
            $this->db->select("*");    
            $this->db->from('mes_customer');
            $this->db->where('cust_id',$this->input->post('id'));
            $query = $this->db->get();
                        $sdata = $query->result();
            
                        
            $data[0]=$bnkdt;
            $data[1] = $sdata[0]->cust_pay_excess;
            echo json_encode($data);
        }
        
        function new_payment(){
            
             if(!isset($this->session->userdata['login_data']['username'])) { 
                        redirect('login'); 
                        exit();
            }
            
            
           // print_r($_POST);
           
           $inv = implode(",",$this->input->post('invoices'));
           $val = implode(",",$this->input->post('values'));
           if($this->input->post('chk_date')!=""){$ckdt = $this->input->post('chk_date');}else{$ckdt="";}
            
            $data = array(
                'cust_id' => $this->input->post('custselect'),
                'paytype' => $this->input->post('paytype'),
                'chk_id' => $this->input->post('chk_id'),
                'chk_bank' => $this->input->post('chk_bank'),
                'pay_branch' => $this->input->post('chk_bank_bnch'),
                'chk_date' => $ckdt,
                'amount' => $this->input->post('inv_total'),
                'remark' => $this->input->post('chk_remark'),
                'invoices' => $inv,
                'values' => $val,
                'recipt' => $this->input->post('inv_receipt'),
                'balance' => ($this->input->post('balance') - $this->input->post('inv_total')),
                'user_name' => $this->session->userdata['login_data']['user_id']
               ); 
            
            
            $rest_api_base_url = 'https://neha.webip.lk/lumen/public';
                $post_endpoint = '/new_payment';
                $request_data = $data;
                $response = perform_http_request('POST', $rest_api_base_url . $post_endpoint, $request_data); 
                $cust = json_decode($response); 
                
            echo json_encode($response);    
        }
    
        function view_all_cheque(){
            
             if(!isset($this->session->userdata['login_data']['username'])) { 
                        redirect('login'); 
                        exit();
            }
            
            
             $this->db->select('*');
            $this->db->from('mes_bank');
            $this->db->where('bank_stetus', 1);
            $hasil2=$this->db->get();  
            $bank = $hasil2->result();
            
            for($i=0; $i<count($bank); $i++){
             $bnkdt[] = array($bank[$i]->bank_id,$bank[$i]->bank_name); 
            }
         
            $data['bank'] = $bnkdt;
            $this->db->select('*');
            $this->db->from('cities');
            $hasil3=$this->db->get();  
            $city = $hasil3->result();
            
                for($i=0; $i<count($city); $i++){
             $citydt[] = array($city[$i]->id,$city[$i]->name_en); 
                } 
        
            $data['city'] = $citydt;
            
            $data['content'] = "pages/all_cheque";        
            $this->load->view('template',$data);
            
        
        }
        
        function get_allcheq(){
            
             if(!isset($this->session->userdata['login_data']['username'])) { 
                        redirect('login'); 
                        exit();
            }
            
            $this->db->from('mes_payment_main');
            $this->db->select("*");
            $this->db->join( "mes_bank", "mes_bank.bank_id = mes_payment_main.pay_chkbnk", 'left');
            $this->db->join( "mes_user", "mes_user.user_id = mes_payment_main.pay_ref_id", 'left');
            $this->db->join( "cities", "mes_payment_main.pay_bbranch = cities.id", 'left');
            $this->db->join( "mes_customer", "mes_customer.cust_id = mes_payment_main.pay_cust_id", 'left');
            $this->db->where('pay_type',1);
            $this->db->order_by('pay_id', 'desc');
            $this->db->limit(100);
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
            $det[0]="";
            $det[1] ="<tr>
														<th >ID</th>
														<th>Slip ID</th>
														<th>Inv ID</th>
														<th>Detail</th>
														<th style='text-align: right;'>Value (LKR)</th>
														<th style='text-align: right;'>Date</th>
														<th style='text-align: center;'>Status</th>
														<th style='text-align: center;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
          $x=0;
                for($i=0; $i<count($data); $i++){
                    
                     $checkdel = "ID : ". $data[$i]->pay_chkid."<br>Bank : ".$data[$i]->bank_name."<br>Date : ".$data[$i]->pay_chkdate;
                      if($data[$i]->pay_chkreturn==1){
                         $return_canbtn = "<button type='button' class='btn btn-outline-secondary btn-sm'>Returned</button>";
                     }else if($data[$i]->pay_chkreturn==0 && $data[$i]->pay_authrize==1 ){
                         $return_canbtn = " <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."' onclick='returnchk(this.id)'  data-backdrop='static' data-keyboard='false'>Return</button>"; 
                     }else{$return_canbtn = "";}
                     
                    
                    $this->db->from('mes_payment');
                    $this->db->select("*");
                    $this->db->where('pay_main_id',$data[$i]->pay_id);
                    $this->db->order_by('pay_id', 'desc');
                    $detailtax = $this->db->get();
                    $inv_data = $detailtax->result();
                    $inv_="";
                    for($j=0; $j<count($inv_data); $j++){
                        $inv_ =  'INV'.$inv_data[$j]->pay_invid." , ".$inv_;
                    }
                    
                    if($data[$i]->pay_type==1){$ptype = "Cheque<br>".$checkdel; $auth_wrd="Authorize"; $returnbtn = $return_canbtn; $tdstyle = 'style="background-color: #ddf2fd;"'; }elseif($data[$i]->pay_type==3){$ptype = "Payment Excess"; $auth_wrd="Authorize";
                    }elseif($data[$i]->pay_type==4){
                        $ptype = "Return Excess"; $auth_wrd="Authorize"; $returnbtn = ""; $tdstyle="";
                    }
                    else{$ptype = "Cash"; $auth_wrd="Authorize"; $returnbtn = ""; $tdstyle="";}
                    
                    $viewbtn = "<a href ='".base_url()."payslip/".$data[$i]->pay_uniqid."' target='_blank'>  <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."'   data-backdrop='static' data-keyboard='false'>View</button></a>";
                    $authbtn = "<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."' onclick='authpayment(this.id)'  data-backdrop='static' data-keyboard='false'>".$auth_wrd."</button>
                   <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."' onclick='edit_view(this.id)' data-target='#Modal_edit' data-backdrop='static' data-keyboard='false'>Edit</button>";
                    $canbtn = " <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."' onclick='cancelpayment(this.id)'  data-backdrop='static' data-keyboard='false'>Cancel</button>";
                   $auth_canbtn = " <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."' onclick='cancelpayment(this.id)'  data-backdrop='static' data-keyboard='false'>Unauthorize and Cancel</button>";
                   
                    if($data[$i]->pay_authuser=="0"){$authuser = "";}else{$authuser = "<p><b>".$data[$i]->pay_authuser."</b></p>";}
                    
                    
                   if($data[$i]->pay_stetus==1){
                       if($data[$i]->pay_authrize==0 && $data[$i]->pay_chkreturn==0){
                           $sts = "<td style='color:yellow; font-size:25px; text-align: center;'>&#9679;</td>";  
                            if($this->session->userdata['login_data']['userlevel']<6 || $this->session->userdata['login_data']['userlevel'] == 10){$adminbtn = $authbtn;}else{$adminbtn="";}
                         $display_btn = $adminbtn.$canbtn.$viewbtn.$returnbtn;
                        }else if($data[$i]->pay_chkreturn==1){
                            $display_btn = $viewbtn.$returnbtn;
                            $sts = "<td style='color:blue; font-size:25px; text-align: center;'>&#9679;</td>"; 
                        }else{
                            $sts = "<td style='color:green; font-size:25px; text-align: center;'>&#9679;</td>";  
                            $display_btn = $viewbtn.$canbtn.$returnbtn.$authuser; 
                        }
                       
                   }else{
                       $sts = "<td style='color:Red; font-size:25px; text-align: center;'>&#9679;</td>";
                       $display_btn =$viewbtn;
                   }
                    
               $x = $x + 1;
                    
                    $dat =  "
                              <tr>
                              <td >$x</td>
                               <td>".$data[$i]->pay_code.$data[$i]->pay_id."</td>
                              <td>".$inv_."<br>".$data[$i]->cust_company."<br><b>".$data[$i]->user_fname." ".$data[$i]->user_lname."</b></td>
                              <td>".$data[$i]->pay_chkid."<br>".$data[$i]->bank_name."-".$data[$i]->name_en."<br><b>".$data[$i]->pay_chkdate."</b></td>
                                <td style='text-align: right;'>".number_format($data[$i]->pay_amount,2,".",",") ."</td>
                                 <td style='text-align: right;'>".$data[$i]->pay_date."</td>
                                $sts
                                
                                <td style='text-align: right;'>
                                
                                 $display_btn
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
        
        function get_filter_allcheq(){
            
             if(!isset($this->session->userdata['login_data']['username'])) { 
                        redirect('login'); 
                        exit();
            }
            
            $search =$this->input->post('search');
            $bnk = $this->input->post('chk_bank');
            
            if(($search=="")&&($bnk=="0")){
                $this->get_allcheq();
            }else{
                
            
            
            $this->db->from('mes_payment_main');
            $this->db->select("*");
            $this->db->join( "mes_bank", "mes_bank.bank_id = mes_payment_main.pay_chkbnk", 'left');
            $this->db->join( "mes_user", "mes_user.user_id = mes_payment_main.pay_ref_id", 'left');
            $this->db->join( "cities", "mes_payment_main.pay_bbranch = cities.id", 'left');
            $this->db->join( "mes_customer", "mes_customer.cust_id = mes_payment_main.pay_cust_id", 'left');
            $this->db->where('pay_type',1);
              if($bnk!=0){
                   $this->db->where('pay_chkbnk',$bnk);
               }
               if($search!=""){
                   $this->db->like('pay_chkid',$search);
                   $this->db->or_like('pay_chkdate', $search);
                   $this->db->or_like('pay_amount', $search);
                   $this->db->or_like('pay_date', $search);
               }
               
            $this->db->order_by('pay_id', 'desc');
            //$this->db->limit(200);
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
            $det[0]="";
            $det[1] ="<tr>
														<th >ID</th>
														<th>Slip ID</th>
														<th>Inv ID</th>
														<th>Detail</th>
														<th style='text-align: right;'>Value (LKR)</th>
														<th style='text-align: right;'>Date</th>
														<th style='text-align: center;'>Status</th>
														<th style='text-align: center;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
          $x=0;
                for($i=0; $i<count($data); $i++){
                    
                     $checkdel = "ID : ". $data[$i]->pay_chkid."<br>Bank : ".$data[$i]->bank_name."<br>Date : ".$data[$i]->pay_chkdate;
                      if($data[$i]->pay_chkreturn==1){
                         $return_canbtn = "<button type='button' class='btn btn-outline-secondary btn-sm'>Returned</button>";
                     }else if($data[$i]->pay_chkreturn==0 && $data[$i]->pay_authrize==1 ){
                         $return_canbtn = " <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."' onclick='returnchk(this.id)'  data-backdrop='static' data-keyboard='false'>Return</button>"; 
                     }else{$return_canbtn = "";}
                     
                    
                    $this->db->from('mes_payment');
                    $this->db->select("*");
                    $this->db->where('pay_main_id',$data[$i]->pay_id);
                    $this->db->order_by('pay_id', 'desc');
                    $detailtax = $this->db->get();
                    $inv_data = $detailtax->result();
                    $inv_="";
                    for($j=0; $j<count($inv_data); $j++){
                        $inv_ =  'INV'.$inv_data[$j]->pay_invid." , ".$inv_;
                    }
                    
                    if($data[$i]->pay_type==1){$ptype = "Cheque<br>".$checkdel; $auth_wrd="Authorize"; $returnbtn = $return_canbtn; $tdstyle = 'style="background-color: #ddf2fd;"'; }elseif($data[$i]->pay_type==3){$ptype = "Payment Excess"; $auth_wrd="Authorize";
                    }elseif($data[$i]->pay_type==4){
                        $ptype = "Return Excess"; $auth_wrd="Authorize"; $returnbtn = ""; $tdstyle="";
                    }
                    else{$ptype = "Cash"; $auth_wrd="Authorize"; $returnbtn = ""; $tdstyle="";}
                    
                    $viewbtn = "<a href ='".base_url()."payslip/".$data[$i]->pay_uniqid."' target='_blank'>  <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."'   data-backdrop='static' data-keyboard='false'>View</button></a>";
                    $authbtn = "<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."' onclick='authpayment(this.id)'  data-backdrop='static' data-keyboard='false'>".$auth_wrd."</button>
                   <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."' onclick='edit_view(this.id)' data-target='#Modal_edit' data-backdrop='static' data-keyboard='false'>Edit</button>";
                    $canbtn = " <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."' onclick='cancelpayment(this.id)'  data-backdrop='static' data-keyboard='false'>Cancel</button>";
                   $auth_canbtn = " <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."' onclick='cancelpayment(this.id)'  data-backdrop='static' data-keyboard='false'>Unauthorize and Cancel</button>";
                   
                    if($data[$i]->pay_authuser=="0"){$authuser = "";}else{$authuser = "<p><b>".$data[$i]->pay_authuser."</b></p>";}
                    
                    
                   if($data[$i]->pay_stetus==1){
                       if($data[$i]->pay_authrize==0 && $data[$i]->pay_chkreturn==0){
                           $sts = "<td style='color:yellow; font-size:25px; text-align: center;'>&#9679;</td>";  
                            if($this->session->userdata['login_data']['userlevel']<2){$adminbtn = $authbtn;}else{$adminbtn="";}
                         $display_btn = $adminbtn.$canbtn.$viewbtn.$returnbtn;
                        }else if($data[$i]->pay_chkreturn==1){
                            $display_btn = $viewbtn.$returnbtn;
                            $sts = "<td style='color:blue; font-size:25px; text-align: center;'>&#9679;</td>"; 
                        }else{
                            $sts = "<td style='color:green; font-size:25px; text-align: center;'>&#9679;</td>";  
                            $display_btn = $viewbtn.$canbtn.$returnbtn.$authuser; 
                        }
                       
                   }else{
                       $sts = "<td style='color:Red; font-size:25px; text-align: center;'>&#9679;</td>";
                       $display_btn =$viewbtn;
                   }
                    
               $x = $x + 1;
                    
                    $dat =  "
                              <tr>
                              <td >$x</td>
                               <td>".$data[$i]->pay_code.$data[$i]->pay_id."</td>
                              <td>".$inv_."<br>".$data[$i]->cust_company."<br><b>".$data[$i]->user_fname." ".$data[$i]->user_lname."</b></td>
                              <td>".$data[$i]->pay_chkid."<br>".$data[$i]->bank_name."-".$data[$i]->name_en."<br><b>".$data[$i]->pay_chkdate."</b></td>
                                <td style='text-align: right;'>".number_format($data[$i]->pay_amount,2,".",",") ."</td>
                                 <td style='text-align: right;'>".$data[$i]->pay_date."</td>
                                $sts
                                
                                <td style='text-align: right;'>
                                
                                 $display_btn
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
            }
        }
        
        function get_retcheq(){
            
             if(!isset($this->session->userdata['login_data']['username'])) { 
                        redirect('login'); 
                        exit();
            }
            
            
            $this->db->from('mes_payment_main');
            $this->db->select("*");
            $this->db->join( "mes_bank", "mes_bank.bank_id = mes_payment_main.pay_chkbnk", 'left');
            $this->db->join( "cities", "mes_payment_main.pay_bbranch = cities.id", 'left');
            $this->db->join( "mes_user", "mes_user.user_id = mes_payment_main.pay_ref_id", 'left');
            $this->db->join( "mes_customer", "mes_customer.cust_id = mes_payment_main.pay_cust_id", 'left');
            $this->db->where('pay_type',1);
            $this->db->where('pay_chkreturn',1);
            $this->db->order_by('pay_id', 'desc');
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
            $det[0]="";
            $det[1] ="<tr>
														<th >ID</th>
														<th>Slip ID</th>
														<th>Inv ID</th>
														<th>Details</th>
														
														<th style='text-align: right;'>Value (LKR)</th>
														<th style='text-align: right;'>Balance (LKR)</th>
														<th style='text-align: right;'>Date</th>
														<th style='text-align: center;'>Status</th>
														<th style='text-align: center;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
          $x=0;
                for($i=0; $i<count($data); $i++){
                    
                     $checkdel = "ID : ". $data[$i]->pay_chkid."<br>Bank : ".$data[$i]->bank_name."<br>Date : ".$data[$i]->pay_chkdate;
                
                    $this->db->from('mes_payment');
                    $this->db->select("*");
                    $this->db->where('pay_main_id',$data[$i]->pay_id);
                    $this->db->order_by('pay_id', 'desc');
                    $detailtax = $this->db->get();
                    $inv_data = $detailtax->result();
                    $inv_="";
                    for($j=0; $j<count($inv_data); $j++){
                        $inv_ =  'INV'.$inv_data[$j]->pay_invid." , ".$inv_;
                    }
                    
                    $this->db->from('chk_collect');
                    $this->db->select("sum(amount) as paid");
                    $this->db->where('stetus',1);
                    $this->db->where('chq_return',0);
                    $this->db->where('pay_id',$data[$i]->pay_id);
                    $this->db->order_by('pay_id', 'desc');
                    $detail = $this->db->get();
                    $coldata = $detail->result();
                    
                    $viewbtn = "<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."' onclick='view_col(this.id)'  data-backdrop='static' data-keyboard='false'>View</button>";
                   
                     if($this->session->userdata['login_data']['userlevel']<6 || $this->session->userdata['login_data']['userlevel']==10){
                         $canbtn = " <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."' onclick='cancelpayment(this.id)'  data-backdrop='static' data-keyboard='false'>Cancel</button>";
                     }else{$canbtn = "";}
                    
                    
                   
               
                    if($coldata[0]->paid >= $data[$i]->pay_amount){
                        $bal = "Complete";
                        $coll = "";
                    }else{
                        $bal = "Pending";
                        
                         if($this->session->userdata['login_data']['userlevel']<6 || $this->session->userdata['login_data']['userlevel']==10){
                             $coll = "<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."' onclick='collect(this.id)'  data-backdrop='static' data-keyboard='false'>Collection</button>";
                     
                         }else{ $coll = "";}
                       
                    }
                    
                    if($coldata[0]->paid < $data[$i]->pay_amount){
                        
                        $display_btn =$viewbtn.$canbtn.$coll;
                    
               $x = $x + 1;
                    
                    $dat =  "
                              <tr>
                              <td>$x</td>
                               <td>".$data[$i]->pay_code.$data[$i]->pay_id."</td>
                              <td>".$inv_."<br>".$data[$i]->cust_company."<br><b>".$data[$i]->user_fname." ".$data[$i]->user_lname."</b></td>
                              <td>".$data[$i]->pay_chkid."<br>".$data[$i]->bank_name."-".$data[$i]->name_en."<br><b>".$data[$i]->pay_chkdate."</b></td>
                                <td style='text-align: right;'>".number_format($data[$i]->pay_amount,2,".",",") ."</td>
                                <td style='text-align: right;'>".number_format((($data[$i]->pay_amount) - ($coldata[0]->paid)),2,".",",") ."</td>
                                 <td style='text-align: right;'>".$data[$i]->pay_date."</td>
                                
                                <td style='text-align: right;'>
                                 $bal
                                </td>
                                <td style='text-align: right;'>
                                
                            
                                 $display_btn
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                    }
                    
                    
                    
                
            
            	
           echo json_encode($det);
        }
        
        function chk_collection(){
            
            
             if(!isset($this->session->userdata['login_data']['username'])) { 
                        redirect('login'); 
                        exit();
            }
            
             $this->db->select('*');
            $this->db->from('mes_payment_main');
            $this->db->where('pay_id', $this->input->post('pay_id'));
            $hasil2=$this->db->get();  
            $payment = $hasil2->result();
            
            if($this->input->post('paytype')==1){
                
                       $this->form_validation->set_rules('chk_id', 'Cheque Number', 'required|is_natural|min_length[6]|max_length[6]|trim'); 
			}else{
			    $this->form_validation->set_rules('inv_amnt', 'Amount', 'required');
			}
            
                 if ($this->form_validation->run() == FALSE) { 
             
                $ret = "Error";
                } else{
                    $data = array(
                
                'rtn_chk_id' => $payment[0]->pay_chkid, 
                'ref_id' => $payment[0]->pay_ref_id,
                'cus_id' => $payment[0]->pay_cust_id,
                'pay_type' => $this->input->post('paytype'),
                'chk_id' => $this->input->post('chk_id'),
                'chk_bak' => $this->input->post('chk_bank'),
                'chk_brnch' => $this->input->post('chk_bank_bnch'),
                'chk_date' => $this->input->post('chk_date'),
                'pay_id' => $this->input->post('pay_id'), 
                'amount'  => $this->input->post('inv_amnt'),
                'date'  => date("Y-m-d"),
                'created_by'  => $this->session->userdata['login_data']['username']
            );
                $result = $this->db->insert('chk_collect',$data);
                
                if($result){
                    $ret = "New Payment Saved";
                }else{
                    $ret = "Please Re enter all details";
                } 
        
              
                }
            
           echo json_encode($ret);
            
        }
        
        function get_collection1(){
            
             if(!isset($this->session->userdata['login_data']['username'])) { 
                        redirect('login'); 
                        exit();
            }
            
            $this->db->from('chk_collect');
            $this->db->select("*");
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
                  
            
            $det[0]="";
            $det[1] ="<tr>
														<th >ID</th>
														<th>Inv ID</th>
														<th style='text-align: right;'>Value (LKR)</th>
														<th style='text-align: right;'>Date</th>
														<th style='text-align: right;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
          $x=0;
                for($i=0; $i<count($data); $i++){
                    
                    
               $x = $x + 1;
                    $del = "<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->col_id."' onclick='delet(this.id)'  data-backdrop='static' data-keyboard='false'>Delete</button>";
                    $dat =  "
                              <tr>
                              <td>$x</td>
                               <td>PAY".$data[$i]->pay_id."</td>
                                <td style='text-align: right;'>".number_format($data[$i]->amount,2,".",",") ."</td>
                                 <td style='text-align: right;'>".$data[$i]->date."</td>
                                 <td style='text-align: right;'>".$del."</td>
                              
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        } 
        
        function get_collection(){
            
            
             if(!isset($this->session->userdata['login_data']['username'])) { 
                        redirect('login'); 
                        exit();
            }
            
            $this->db->from('mes_payment_main');
            $this->db->select("*");
            $this->db->join( "mes_bank", "mes_bank.bank_id = mes_payment_main.pay_chkbnk", 'left');
            $this->db->join( "cities", "mes_payment_main.pay_bbranch = cities.id", 'left');
            $this->db->join( "mes_user", "mes_user.user_id = mes_payment_main.pay_ref_id", 'left');
            $this->db->join( "mes_customer", "mes_customer.cust_id = mes_payment_main.pay_cust_id", 'left');
            $this->db->where('pay_type',1);
            $this->db->where('pay_chkreturn',1);
            $this->db->order_by('pay_id', 'desc');
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
            $det[0]="";
            $det[1] ="<tr>
														<th >ID</th>
														<th>Slip ID</th>
														<th>Inv ID</th>
														<th>Details</th>
														
														<th style='text-align: right;'>Value (LKR)</th>
														<th style='text-align: right;'>Balance (LKR)</th>
														<th style='text-align: right;'>Date</th>
														<th style='text-align: center;'>Status</th>
														<th style='text-align: center;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
          $x=0;
                for($i=0; $i<count($data); $i++){
                    
                     $checkdel = "ID : ". $data[$i]->pay_chkid."<br>Bank : ".$data[$i]->bank_name."<br>Date : ".$data[$i]->pay_chkdate;
                
                    $this->db->from('mes_payment');
                    $this->db->select("*");
                    $this->db->where('pay_main_id',$data[$i]->pay_id);
                    $this->db->order_by('pay_id', 'desc');
                    $detailtax = $this->db->get();
                    $inv_data = $detailtax->result();
                    $inv_="";
                    for($j=0; $j<count($inv_data); $j++){
                        $inv_ =  'INV'.$inv_data[$j]->pay_invid." , ".$inv_;
                    }
                    
                    $this->db->from('chk_collect');
                    $this->db->select("sum(amount) as paid");
                    $this->db->where('stetus',1);
                    $this->db->where('chq_return',0);
                    $this->db->where('pay_id',$data[$i]->pay_id);
                    $this->db->order_by('pay_id', 'desc');
                    $detail = $this->db->get();
                    $coldata = $detail->result();
                    
                    $viewbtn = "<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."' onclick='view_col(this.id)'  data-backdrop='static' data-keyboard='false'>View</button>";
                   
                    $canbtn = " <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->pay_id."' onclick='cancelpayment(this.id)'  data-backdrop='static' data-keyboard='false'>Cancel</button>";
                   
               
                    if($coldata[0]->paid >= $data[$i]->pay_amount){
                        $bal = "Complete";
                        $coll = "";
                        
                        $display_btn =$viewbtn.$canbtn.$coll;
                    
               $x = $x + 1;
                    
                    $dat =  "
                              <tr>
                              <td>$x</td>
                               <td>".$data[$i]->pay_code.$data[$i]->pay_id."</td>
                              <td>".$inv_."<br>".$data[$i]->cust_company."<br><b>".$data[$i]->user_fname." ".$data[$i]->user_lname."</b></td>
                              <td>".$data[$i]->pay_chkid."<br>".$data[$i]->bank_name."-".$data[$i]->name_en."<br><b>".$data[$i]->pay_chkdate."</b></td>
                                <td style='text-align: right;'>".number_format($data[$i]->pay_amount,2,".",",") ."</td>
                                <td style='text-align: right;'>".number_format((($data[$i]->pay_amount) - ($coldata[0]->paid)),2,".",",") ."</td>
                                 <td style='text-align: right;'>".$data[$i]->pay_date."</td>
                                
                                <td style='text-align: right;'>
                                 $bal
                                </td>
                                <td style='text-align: right;'>
                                
                                 $display_btn
                                </td>
                               
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                    }
                    
                    
                    
                
            
            	
           echo json_encode($det);
        }
        
        function inv_collection(){
            
             if(!isset($this->session->userdata['login_data']['username'])) { 
                        redirect('login'); 
                        exit();
            }
            
            $this->db->from('chk_collect');
            $this->db->select("*");
            $this->db->join( "mes_user", "mes_user.user_id = chk_collect.ref_id", 'left');
            $this->db->join( "mes_customer", "mes_customer.cust_id = chk_collect.cus_id", 'left');
            $this->db->join( "mes_bank", "mes_bank.bank_id = chk_collect.chk_bak", 'left');
            $this->db->join( "cities", "cities.id = chk_collect.chk_brnch", 'left');
            $this->db->where('pay_id', $this->input->post('id'));
            $this->db->order_by("col_id", "desc");
            
            $detailtax = $this->db->get();
            $data = $detailtax->result();
            
            $det[0]="";
            $det[1] ="
            
            <tr>
														<th >ID</th>
														
														<th>Details</th>
														<th style='text-align: right;'>Value (LKR)</th>
														<th style='text-align: right;'>Date</th>
														<th style='text-align: right;'>Action</th>
                                                                                                             
																											
													</tr>";
            
          $det[2]="";
          
          $x=0;
                for($i=0; $i<count($data); $i++){
                    
                    
               $x = $x + 1;
               
               if($data[$i]->pay_type==1){
                   
                    if(($data[$i]->stetus==1) && ($data[$i]->chq_return==0)){
                   
                                $del = "<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->col_id."' onclick='delet(this.id)'  data-backdrop='static' data-keyboard='false'>Cancel</button>
                                 <button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->col_id."' onclick='returnck(this.id)'  data-backdrop='static' data-keyboard='false'>Return</button>";
                        }else{
                   
                      $del = "<button type='button' class='btn btn-danger btn-sm' type='button' id='".$data[$i]->col_id."'   data-backdrop='static' data-keyboard='false'>Canceled</button>";
                   
                        }
               }else if($data[$i]->stetus==0){
                   $del = "<button type='button' class='btn btn-danger btn-sm' type='button' id='".$data[$i]->col_id."' onclick='delet(this.id)'  data-backdrop='static' data-keyboard='false'>Canceled</button>";
                   
               }else{
                    $del = "<button type='button' class='btn btn-outline-secondary btn-sm' type='button' id='".$data[$i]->col_id."' onclick='delet(this.id)'  data-backdrop='static' data-keyboard='false'>Cancel</button>";
               }
               
            if($data[$i]->pay_type==0){$tpy = "Cash";}else if($data[$i]->pay_type==1){$tpy = "Cheque"."<br>".$data[$i]->chk_id."<br>".$data[$i]->bank_name."<br>".$data[$i]->name_en."<br>".$data[$i]->chk_date;}else{$tpy = "Bank Deposit";}
                 
                    $dat =  "
                              <tr>
                              <td>$x</td>
                               <td>".$tpy."</td>
                                <td style='text-align: right;'>".number_format($data[$i]->amount,2,".",",") ."</td>
                                 <td style='text-align: right;'>".$data[$i]->date."</td>
                                 <td style='text-align: right;'>".$del."</td>
                              
                              
                              </tr>";
                              $det[2] =$det[2].$dat;
                }
                
            
            	
           echo json_encode($det);
        }
    
        function del_collection(){
            
             if(!isset($this->session->userdata['login_data']['username'])) { 
                        redirect('login'); 
                        exit();
            }
            
                        $this->db->set('stetus', 0);
                        $this->db->where('col_id', $this->input->post('id'));
                        $del=$this->db->update('chk_collect');
                      
            
                    //$this->db->where('col_id', $this->input->post('id'));
                    //$del =  $this->db->delete('chk_collect');
                  
            if($del){
                $ret = "Data Removed";
            }else{
                $ret = "Please Retry";
            }
            echo json_encode($ret);
        }
        
        function return_collection(){
            
                        $this->db->set('chq_return', 1);
                        $this->db->where('col_id', $this->input->post('id'));
                        $del=$this->db->update('chk_collect');
                      
            
                    //$this->db->where('col_id', $this->input->post('id'));
                    //$del =  $this->db->delete('chk_collect');
                  
            if($del){
                $ret = "Data Removed";
            }else{
                $ret = "Please Retry";
            }
            echo json_encode($ret);
        }
    
}        

