<?php

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

class Sales_model extends CI_Model {

    public function get_sales_list($dateDebut, $dateFin) {
        $this->db->select("ID, date_format(receivePaymentDate,'%d-%m-%Y') as saleDate, CONCAT(formNo, '[', lpad(serialNo,8,'0'), ']') as formNo, totalReceived as grandTotal", FALSE);
        $this->db->where('receivePaymentDate >=', $dateDebut);
        $this->db->where('receivePaymentDate <=', $dateFin);
        $this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
        $query = $this->db->get('comp_receive_payment_others');
        if ($query->num_rows() >= 1) {
            return $query->result();
        } else {
            return FALSE;
        }
    }

    public function get_sale_master($id) {
        $this->db->select("ID, date_format(receivePaymentDate,'%d-%m-%Y') as saleDate, CONCAT(formNo, '[', lpad(serialNo,8,'0'), ']') as formNo, bankID, totalReceived as grandTotal", FALSE);
        $this->db->where('ID', $id);
        $this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
        $query = $this->db->get('comp_receive_payment_others');
        if ($query->num_rows() >= 1) {
            return $query->result();
        } else {
            return FALSE;
        }
    }

    public function get_sale_master_detail($masterId) {
        $this->db->select("ID, description, amountExcludedTax as amount", FALSE);
        $this->db->where('receivePaymentOthersID', $masterId);
        $query = $this->db->get('comp_receive_payment_others_detail');
        if ($query->num_rows() >= 1) {
            return $query->result();
        } else {
            return FALSE;
        }
    }

    public function get_sale_journal_master($id) {
//akmal 20160804 use ci query to be captured into log. 
//        $sql = "Select j.ID as journalID, j.journalID AS journalNo from comp_journal j, comp_receive_payment_others rp "
//                . " Where j.journalID=rp.journalNo AND rp.ID = ?";
//        $query = $this->db->query($sql, $id);
		$this->db->select('j.ID as journalID, j.journalID AS journalNo')
				->join('comp_receive_payment_others rp','j.companyID = rp.companyID and j.journalID=rp.journalNo ' ,'left', false)
				->where('rp.ID ',$id);
        $query = $this->db->get('comp_journal j');

        if ($query->num_rows() >= 1) {
            return $query->result();
        } else {
            return FALSE;
        }
    }

	public function get_currentSale($mode=1) {
		//mode 1=today, 2=week, 3=month, 4=year
		switch ($mode){
			case 1 :
				// $this->db->where('receivePaymentDate','CURRENT_DATE');
				$this->db->where('receivePaymentDate', date('Y-m-d'));
				break;
			case 2 :
				// $this->db->where('year(receivePaymentDate)','year(CURRENT_DATE)');
				// $this->db->where('week(receivePaymentDate)','week(CURRENT_DATE)');
				$this->db->where('receivePaymentDate >=', date('Y-m-d', strtotime('previous monday')));
				$this->db->where('receivePaymentDate <=', date('Y-m-d', strtotime('next sunday')));
				break;
			case 3 :
				// $this->db->where('year(receivePaymentDate)','year(CURRENT_DATE)');
				// $this->db->where('month(receivePaymentDate)','month(CURRENT_DATE)');
				$this->db->where('year(receivePaymentDate)', date('Y'));
				$this->db->where('month(receivePaymentDate)', date('m'));
				break;
			case 4 :
				// $this->db->where('year(receivePaymentDate)','year(CURRENT_DATE)');
				$this->db->where('year(receivePaymentDate)', date('Y'));
				break;
		}
		$rs = $this->db->select('sum(totalReceived) as sale',false)
		->where('companyID',element('compID', $this->session->userdata('logged_in')))
		->where('is_delete',0)
		->get('comp_receive_payment_others');
		return ($rs->num_rows() > 0) ? $rs->result()[0]->sale : 0;
	}

    //General function for copy paste

    public function trash($tname, $id) {
        $query = $this->db->query("select * from $tname where ID = ? ", $id);
        if ($query->num_rows > 0) {
            $data = $query->result();
            $dataSet = get_object_vars($data[0]);
            $this->db->insert($tname . '_bak', $dataSet);
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function trash_detail($tname, $masterID, $keyName) {
        $query = $this->db->query("select * from $tname where $keyName = ? ", $masterID);
        if ($query->num_rows > 0) {
            $data = $query->result();
            foreach ($data AS $row) {
                $dataSet = get_object_vars($row);
                $this->db->insert($tname . '_bak', $dataSet);
            }
        } else {
            return FALSE;
        }
    }

    public function insert($tname, $newData) {
        $sql = $this->db->insert_string($tname, $newData);
        $query = $this->db->query($sql);
        if ($query === TRUE) {
            return $this->db->insert_id();
        } else {
            return 0;
        }
    }

    public function delete_row($tname, $id, $fielName = NULL) {
        $fielName = (is_null($fielName)) ? "ID":$fielName;
        $this->db->where($fielName, $id);
        $query = $this->db->delete($tname);
        if ($query === TRUE) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function update_byID($tname, $id, $data) {
        $this->db->where('ID', $id);
        $query = $this->db->update($tname, $data);
        if ($query === TRUE) {
            return TRUE;
        } else {
            return FALSE;
        }
    }


}
