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

class Boq_management {

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


    $CI =& get_instance();
    $CI->CI =& get_instance();
    $CI->CI->load->library('session');
    $CI->load->model('User_model', 'User');
    $CI->load->model('Work_model', 'Work');
    $CI->load->model('Account_model', 'Account');
    $CI->load->model('Settings_model', 'Settings');
    $CI->load->model('Stock_model', 'Stock');
    $CI->load->model('Boq_model', 'Boq');
    $CI->load->model('Material_model','Material');

  }


  public function auto_correct_material_consumption_balance($project_id,$start_date=''){
    $CI =& get_instance();

    $boq_data = $CI->Boq->select_boq("*,b.created_by as boq_created_by",array('project_id' => $project_id))->result();
    foreach ($boq_data as $key11 => $value11) {
      $dpr_where['bdp.boq_id'] = $value11->boq_id;
      if($start_date!=''){
        $dpr_where['work_date >='] = $start_date;
      }
      $boq_dpr_data = $CI->Boq->select_boq_dpr('bdp.dpr_id,bdp.work_date',$dpr_where,"",array("bdp.work_date",'asc'))->result();
      $stock_materials = $CI->Stock->select_stock_register("m.material_name,m.material_id,mu.unit_name",array("sl.work_id"=>$value11->project_id),'sr.material_id')->result();
      foreach ($boq_dpr_data as $key22 => $value22) {
        foreach ($stock_materials as $key => $value) {
          $opening_stock_where["sr.stock_date <"] = $purchase_where["p.purchase_date <"] = $transfer_in_where["st.transfer_date <"] = $transfer_out_where["st.transfer_date <"] = $consumed_where["sc.consumption_date <"] = $purchase_return_where["pr.return_date <"] = $sales_invoice_where["si.invoice_date <"] = $material_issue_where["smb.bill_date <"] = $material_issue_return_where["smr.returned_date <"] = $value22->work_date;
          $opening_stock_where["sr.material_id"] = $purchase_where["pi.material_id"] = $transfer_in_where["sr2.material_id"] = $purchase_where_today["pi.material_id"] = $transfer_in_where_today["sr2.material_id"] = $transfer_out_where["sr1.material_id"] = $consumed_where["sr.material_id"] = $purchase_return_where["m.material_id"] = $sales_invoice_where["m.material_id"] = $material_issue_where["sr.material_id"] = $material_issue_return_where["sr.material_id"] = $value->material_id;
          $opening_stock_where["sl.work_id"] = $purchase_where["sl.work_id"] = $transfer_in_where["sl2.work_id"] = $purchase_where_today["sl.work_id"] = $transfer_in_where_today["sl2.work_id"] = $transfer_out_where["sl1.work_id"] = $consumed_where["sl.work_id"] = $purchase_return_where["sl.work_id"] = $sales_invoice_where["sl.work_id"] = $material_issue_where["sc.work_id"] = $material_issue_return_where["sc.work_id"] = $value11->project_id;
          $purchase_where_today["p.purchase_date"] = $transfer_in_where_today["st.transfer_date"] = $value22->work_date;

          $total_opening_qty = $CI->Stock->select_stock_register("COALESCE(SUM(sr.total_opening_qty),0) as total", $opening_stock_where)->row()->total;
          $total_purchased_qty = $CI->Purchases->select_purchase_items("COALESCE(SUM(pi.quantity),0) as total",$purchase_where)->row()->total;
          $total_transferred_in_qty = $CI->Stock->select_stock_transfers("COALESCE(SUM(st.transferred_qty),0) as total", $transfer_in_where)->row()->total;
          $total_transferred_out_qty = $CI->Stock->select_stock_transfers("COALESCE(SUM(st.transferred_qty),0) as total", $transfer_out_where)->row()->total;
          $total_consumption_qty = $CI->Stock->select_stock_consumption("COALESCE(SUM(sc.consumption_qty),0) as total", $consumed_where)->row()->total;
          $total_consumption_qty += $CI->Subcontracts->select_material_issue_items("COALESCE(SUM(smi.quantity),0) as total", $material_issue_where)->row()->total;
          $total_consumption_qty -= $CI->Subcontracts->select_material_return("COALESCE(SUM(smr.returned_qty),0) as total", $material_issue_return_where)->row()->total;
          $total_purchase_return_qty = $CI->Purchases->select_purchase_return("COALESCE(SUM(pr.returned_qty),0) as total", $purchase_return_where)->row()->total;
          $total_sold_qty = $CI->Sales_invoice->select_invoice_sale_item("COALESCE(SUM(sii.quantity),0) as total", $sales_invoice_where)->row()->total;
          $value->opening_stock_today = $total_opening_qty+$total_purchased_qty+$total_transferred_in_qty-$total_transferred_out_qty-$total_consumption_qty-$total_sold_qty-$total_purchase_return_qty;

          $total_purchased_qty_today = $CI->Purchases->select_purchase_items("COALESCE(SUM(pi.quantity),0) as total",$purchase_where_today)->row()->total;
          $total_transferred_in_qty_today = $CI->Stock->select_stock_transfers("COALESCE(SUM(st.transferred_qty),0) as total", $transfer_in_where_today)->row()->total;
          $value->received_qty_today = $total_purchased_qty_today+$total_transferred_in_qty_today;

          $material_consumption['dpr_id'] = $material_consumption_where['bdmc.dpr_id'] = $value22->dpr_id;
          $material_consumption['material_id'] = $material_consumption_where['bdmc.material_id'] = $value->material_id;
          $material_consumption_details = $CI->Boq->select_boq_dpr_material_consumption('',$material_consumption_where);
          if($material_consumption_details->num_rows()==1){
            $material_consumption['opening_stock'] = $value->opening_stock_today;
            $material_consumption['received_qty'] = $value->received_qty_today;
            $consumed_qty = $material_consumption_details->row()->consumed_qty;
            $material_consumption['balance_qty'] = $material_consumption['opening_stock']+$material_consumption['received_qty']-$consumed_qty;
            $material_consumption_result = $CI->Boq->update_boq_dpr_material_consumption($material_consumption,$material_consumption_where);
          }
        }
      }
    }
  }

}
?>
