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

class Model_claim_petrol extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    public function fetchPetrol($user_id,$claim_id,$year,$month)
    {
        $results = $this->db->select('a.*')
            ->where('a.claim_id',$claim_id)

            // ->where('a.year')
            // ->where('a.month')
            // ->where('a.user_id')
            ->where('a.is_delete',0)
            // ->join('hr__claim_petrol_movement b','b.claim_petrol_id = a.id','left')
            ->get('hr__claim_petrol a')
            ->result();

        if(sizeof($results) > 0)
        {
            foreach ($results as $key => $result) 
            {
                $results[$key]->movements = $this->fetchMovement($result->id);
                $results[$key]->attachments = $this->fetchAttachment($result->id);
            }

        }

		return (sizeof($results))?$results:null;
    }

    public function fetchMovement($claim_petrol_id)
    {
        $results = $this->db->select('b.*,c.movementType as movement_type,d.name as customer_name')
                            ->where('a.claim_petrol_id',$claim_petrol_id)
                            ->where('a.is_delete',0)
                            ->join('hr__movement_group b','b.id = a.movement_id','left')
                            ->join('hr__movement_type c','c.id = b.movementType','left')
                            ->join('customer d','d.id = b.customerid','left')
                            ->get('hr__claim_petrol_movement a')
                            ->result();

		return (sizeof($results))?$results:null;
    }

    public function fetchAttachment($claim_petrol_id)
    {
        $results = $this->db->select('a.*')
                        ->where('a.claim_petrol_id',$claim_petrol_id)
                        // ->where()
                        // ->join()
                        ->get('hr__claim_petrol_attachment a')
                        ->result();
        return (sizeof($results))?$results:null;
    }

    public function create($inputs)
    {
        return ($this->db->insert('hr__claim_petrol',$inputs))?$this->db->insert_id():false; 
    }

    public function create_movements($inputs)
    {
        $this->db->insert_batch('hr__claim_petrol_movement',$inputs);
        return true;
    }

    public function create_attachments($inputs)
    {
        $this->db->insert_batch('hr__claim_petrol_attachment',$inputs);
        return true;
    }

    public function updatePetrol($sets = array(),$where = array())
    {
        $this->db->set($sets);
		foreach ($where as $key => $wh) {
			$this->db->where($key,$wh);
		}
        $this->db->update('hr__claim_petrol');
        return true;
    }

    public function fetchAttachmentbyUniqueid($unique_id)
    {
        $this->db->select('a.*');
        $this->db->where('a.unique_id',$unique_id);
        $query = $this->db->get('hr__claim_petrol_attachment a');
        return ($query->result())?$query->result():null;
    }
    
}
?>