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

class Supplier_management {

  function __construct()
  {
    ini_set('max_execution_time', 5000);
    ini_set("memory_limit", "-1");
    

    $CI =& get_instance();
    $this->CI =& get_instance();
    $this->CI->load->library('session');
    $CI->load->model('User_model', 'User');
    $CI->load->model('Account_model', 'Account');
    $CI->load->model('Supplier_model', 'Supplier');
  }


  public function calculate_advance_amount($id){
    $CI =& get_instance();
    $json_data['advance_paid'] = $CI->Account->select_account_transaction("COALESCE(SUM(ats.tr_amount),0) as total_amount",array("s.supplier_id" => $id,"ats.tr_type" => 'supplier_advance_transaction',"ats.reference_type" => 'supplier'),'supplier')->row()->total_amount;
    $advance_returned_as_order = $CI->Account->select_advance_return_transaction("COALESCE(SUM(art.tr_amount),0) as total_amount",array("art.supplier_id" => $id,"art.tr_type" => 'purchase_order_transaction',"art.reference_type" => 'purchase_order'))->row()->total_amount;
    $advance_returned_as_bill = $CI->Account->select_advance_return_transaction("COALESCE(SUM(art.tr_amount),0) as total_amount",array("art.supplier_id" => $id,"art.tr_type" => 'purchase_bill_transaction',"art.reference_type" => 'purchase_bill'))->row()->total_amount;
    $json_data['advance_returned'] = $advance_returned_as_order + $advance_returned_as_bill;
    $json_data['balance'] = $json_data['advance_paid']-$json_data['advance_returned'];
    return $json_data;
  }

}
?>
