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

class Receive_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('Invoice_model');
         $this->load->model('Setings_model');
    }

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

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

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

    public function invoice_list(){
        $user_id = $this->input->post('user_id');
        $data['invoice_list'] = $this->Invoice_model->get_invoice_for_user($user_id);
        
        $paid_invoices = $this->Invoice_model->paid_invoices($user_id);
        $data['paid_invoices'] = array();
        if (count($paid_invoices) > 0) {
            foreach ($paid_invoices as $key => $value) {
                $data['paid_invoices'][] = $value->invoice_id;
            }
        }
        
        $this->load->view('user_invoice_list',$data);
    }

    function user_balance(){
        $user_id = $this->input->post('user_id');
        $total_payment_amount = $this->Invoice_model->get_from_table('payment_received','SUM(amount) payment_received_amount',array('customer_id'=>$user_id));
        $total_paid_invoice_amount = $this->Invoice_model->get_from_table('invoice_payment','SUM(amount) invoice_payment_received_amount',array('customer_id'=>$user_id));
        $total_credit_amount = $this->Invoice_model->get_from_table('credit_note','SUM(total) credit_amount',array('customer_id'=>$user_id));
        // echo $this->db->last_query();
        $balance_amount = (($total_payment_amount[0]->payment_received_amount + $total_credit_amount[0]->credit_amount) - $total_paid_invoice_amount[0]->invoice_payment_received_amount);
        echo (($balance_amount > 0) ? $balance_amount : 0);
    }

    public function pay(){
        $customer_id    = $this->input->post('customer_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');
        $deposit_to     = $this->input->post('deposit_to');
        $note           = $this->input->post('note');
        $paid_invoice   = $this->input->post('paid_invoice');
        
        $payment_data = array(
                'customer_id'   => $customer_id,
                'amount'        => $amount,
                'bank_charges'  => $bank_charges,
                'payment_date'  => date('Y-m-d',strtotime(str_replace('/', '-', $payment_date))),
                'payment_type'  => $payment_type,
                'reference'     => $reference,
                'deposit_to'    => $deposit_to,
                'note'          => $note,
                'date_added'    => date('Y-m-d')
            );
        $payment_id = $this->Invoice_model->insert_data('payment_received',$payment_data);

        if (count($paid_invoice) > 0) {
            foreach ($paid_invoice as $key => $value) {
                if ($value[0] > 0) {
                    $payment_invoice_data[] = array(
                        'customer_id'           => $customer_id,
                        'payment_received_id'   => $payment_id,
                        'sale_invoice_id'       => $value[0],
                        'amount'                => $value[1],
                        'date_added'            => date('Y-m-d')
                    );
                }
            }
            $this->Invoice_model->insert_data_batch('invoice_payment',$payment_invoice_data);
            $this->Invoice_model->update_data('credit_note',array('is_adjusted'=>1),array('is_adjusted'=>0,'customer_id'=>$customer_id));
        }

        echo 1;
    }

    public function recipt_print($recive_id){
        $data['setings'] = $this->Setings_model->get_setings();
        
        $data['payment_receive_details'] = $this->Invoice_model->get_from_table('payment_received','*',array('payment_received_id'=>$recive_id));
       
        $data['receive_through_details'] = $this->Invoice_model->get_from_table('bank_accounts','*',array('account_id'=>$data['payment_receive_details'][0]->deposit_to));
        $data['customer_details'] = $this->Invoice_model->get_from_table('contact_user','*',array('id'=>$data['payment_receive_details'][0]->customer_id));
        $data['received_invoce_details'] = $this->Invoice_model->received_invoce_details($recive_id); 
        
        
        // echo "<pre>";print_r($data);die();
        
        
        $this->load->view('print_payment_recive',$data);
    }
}
?>