<?php

class Sales extends CI_Model
{
    public $recordID;
    public $transID;
    public $brCode;
    public $invoiceNum;
    public $catCode;
    public $modelno;
    public $custName;
    public $qty;
    public $price;
    public $cash;
    public $dollar;
    public $creditCard;
    public $bankshare;
    public $wTaxOption;
    public $withholdingtax;
    public $layawayDP;
    public $layawayBal;
    public $layawaySP;
    public $layawayFP;
    public $checque;
    public $misc;
    public $transType;
    public $remarks;
    public $transDate;
    public $validated;

    public function __construct()
	{
		parent::__construct();

		$this->load->database();
    }


    public function getSalesByTransID($transID)
    {
        return $this->db->get_where("sales", array("transID"=>$transID));
    }

    public function getSalesByRecordID($recordID)
    {
        $result = $this->db->get_where("sales", array("recordID"=>$recordID));
        return $result->row();
    }

    public function getNextID($transID)
    {
        $nextID = 0;

        $this->db->select("IFNULL(MAX(CAST(replace(recordID, transID, '') AS UNSIGNED)), 0) + 1 as NextID");
        $result = $this->db->get_where("sales", array("transid"=>$transID));

        foreach($result->result() as $row)
        $nextID = $row->NextID;

        return $nextID;
    }

    public function addSales()
    {
        $dateFormatedtransDate = DateTime::createFromFormat('m/d/Y', $this->transDate);
        $this->transDate = $dateFormatedtransDate->format('Y-m-d');
        
        return $this->db->insert('sales', $this);
    }

    public function editSales($recordID, $updateColumn)
    {
        $dateFormatedtransDate = DateTime::createFromFormat('m/d/Y', $updateColumn["transDate"]);
        $updateColumn["transDate"] = $dateFormatedtransDate->format('Y-m-d');
        
        $this->db->where('recordID', $recordID);
        return $this->db->update('sales', $updateColumn);
    }

    public function editSalesSetQtyToZero($recordID)
    {
        return $this->db->update('sales', array("qty"=>0), array("recordID"=>$recordID));
    }

    public function deleteSales($recordID)
    {
        return $this->db->delete("sales", array("recordID"=>$recordID));
    }
}