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

class Bill_payment extends CI_Controller {
    
    public function __construct() {
        parent::__construct();
         if (! $this->session->userdata ( 'user_logged_in' ) || !company_url($this->session->userdata ('company_slag'))) {
            redirect ( 'login', 'refresh' );
        }
        $this->load->vars ( array (
                'global' => 'Available to all views',
                'header' => 'common/header',
                'left' => 'common/left',
                'footer' => 'common/footer' 
        ) );

        $this->load->model('User_model');
        $this->load->model('Purchase_model');
        $this->load->model('Setings_model');
    }

    public function index(){
        $data['payment_list'] = $this->Purchase_model->bill_payment_list();
        // echo $this->db->last_query();die();
        $this->load->view('payment_list',$data);
    }

    public function newpayment(){
        $data['payment_received_id'] = !empty($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $where = array('contact_type' => 2,'status' => 1);
        $data['customer_list'] = $this->User_model->user_list_where($where); 

        $where_bank = array('status' => 1);
        $data['bank_list'] = $this->Purchase_model->get_from_table('bank_accounts','*',$where_bank);
        
        $data['recived_payment_data'] = array();
        $this->load->view('bill_payment',$data);
    }

    public function bill_list(){
        $user_id = $this->input->post('user_id');
        $data['bill_list'] = $this->Purchase_model->get_bills_for_user($user_id);
        
        $paid_bills = $this->Purchase_model->paid_bills($user_id);
        
        $data['paid_bill'] = array();
        if (count($paid_bills) > 0) {
            foreach ($paid_bills as $key => $value) {
                $data['paid_bill'][] = $value->bill_id;
            }
        }
        $this->load->view('vendor_bill_list',$data);
    }

    function user_balance(){
        $user_id = $this->input->post('user_id');
        $total_payment_amount = $this->Purchase_model->get_from_table('payment_made','SUM(amount) payment_made_amount',array('vendor_id'=>$user_id));
        $total_paid_bill_amount = $this->Purchase_model->get_from_table('bill_payment','SUM(payment) bill_payment_amount',array('vendor_id'=>$user_id));
        $total_debit_note_amount = $this->Purchase_model->get_from_table('devid_note','SUM(total) debit_amount',array('vendor_id'=>$user_id));
        
        $balance_amount = (($total_payment_amount[0]->payment_made_amount + $total_debit_note_amount[0]->debit_amount)- $total_paid_bill_amount[0]->bill_payment_amount);
        echo (($balance_amount > 0) ? $balance_amount : 0);
    }

    public function pay(){
        $vendor_id    = $this->input->post('vendor_id');
        $amount         = $this->input->post('amount');
        //$bank_charges   = !empty($this->input->post('bank_charges')) ? $this->input->post('bank_charges') : 0;
        $payment_date   = $this->input->post('payment_date');
        $payment_type   = $this->input->post('payment_type');
        $reference      = $this->input->post('reference');
        $paid_through     = $this->input->post('paid_through');
        $note           = $this->input->post('note');
        $paid_bills   = $this->input->post('paid_bills');
        
        $payment_data = array(
                'vendor_id'   => $vendor_id,
                'amount'        => $amount,
                //'bank_charges'  => $bank_charges,
                'payment_date'  => date('Y-m-d',strtotime(str_replace('/', '-', $payment_date))),
                'payment_type'  => $payment_type,
                'reference'     => $reference,
                'paid_through'  => $paid_through,
                'note'          => $note,
                'date_added'    => date('Y-m-d')
            );
        $payment_id = $this->Purchase_model->insert_data('payment_made',$payment_data);

        if (count($paid_bills) > 0) {
            foreach ($paid_bills as $key => $value) {
                if ($value[0] > 0) {
                    $payment_bills_data[] = array(
                        'vendor_id'             => $vendor_id,
                        'payment_made_id'       => $payment_id,
                        'purchase_bill_id'      => $value[0],
                        'payment'               => $value[1],
                        'date_added'            => date('Y-m-d')
                    );
                }
            }
            $this->Purchase_model->insert_data_batch('bill_payment',$payment_bills_data);
            $this->Invoice_model->update_data('devid_note',array('is_adjusted'=>1),array('is_adjusted'=>0,'customer_id'=>$vendor_id));
        }
        echo $payment_id;
    }

    public function recipt_print($payment_id){
        $data['setings'] = $this->Setings_model->get_setings();
        $data['payment_made_details'] = $this->Purchase_model->get_from_table('payment_made','*',array('payment_made_id'=>$payment_id));
        $data['paid_through_details'] = $this->Purchase_model->get_from_table('bank_accounts','*',array('account_id'=>$data['payment_made_details'][0]->paid_through));
        $data['vendor_details'] = $this->Purchase_model->get_from_table('contact_user','*',array('id'=>$data['payment_made_details'][0]->vendor_id));
        $data['paid_bills_details'] = $this->Purchase_model->payment_bill_details($payment_id);
        // echo "<pre>";print_r($data);die();
        $this->load->view('print_payment_make',$data);
    }
}
?>