<?php
class Payment_schedule_model extends CI_Model
{
  function __construct()
  {
    parent::__construct();
  }

  public function select_schedule($columns="*",$data="",$join="")
  {
    $this->db->select($columns)->from('payment_schedules ps');
    $this->db->join('users u','ps.created_by=u.user_id','left');
    if(!empty($join) && $join=='work'){
      $this->db->join('work_contracts wc','wc.work_id=ps.reference_id','left');
      $this->db->join('clients c','wc.client_id=c.client_id','left');
      if ($this->session->userdata('branch_id') != 0) {
        $this->db->where('wc.branch_id', $this->session->userdata('branch_id'));
      }
    }
    if(!empty($join) && $join=='subcontract'){
      $this->db->join('sub_contracts s','s.contract_id=ps.reference_id','left');
      $this->db->join('work_contracts wc','wc.work_id=s.work_id','left');
      $this->db->join('sub_contractors c','c.contractor_id=s.contractor','left');
      if ($this->session->userdata('branch_id') != 0) {
        $this->db->where('c.branch_id', $this->session->userdata('branch_id'));
      }
    }
    if(!empty($data)){
      $this->db->where($data);
    }
    $this->db->where("ps.delete_status",0);
    $query = $this->db->get();
    return $query;
  }
  public function update_schedule($data)
  {
    $this->db->where('schedule_id', $data['schedule_id']);
    $query=$this->db->update('payment_schedules', $data);
    return $query;
  }
  public function create_schedule($data){
    $query = $this->db->insert('payment_schedules', $data);
    $result=array();
    $result['insert_id']=$this->db->insert_id();
    $result['status']=$query;
    return $result;
  }
}
?>
