<?php

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

class Payment_model extends CI_Model {

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

// GET MATERIALREQUEST LISTS FOR MANAGE
    function getpayments() {
    
////Fetch data
        $this->db->select("PAYMENTS.ID,  RECEIPTS.DATENEW, CLOSEDCASH.HOST, CLOSEDCASH.HOSTSEQUENCE, PAYMENTS.PAYMENT, PAYMENTS.NOTES, PAYMENTS.TOTAL");
  
        $this->db->join('RECEIPTS', 'PAYMENTS.RECEIPT = RECEIPTS.ID', 'LEFT');
        $this->db->join('CLOSEDCASH', 'RECEIPTS.MONEY = CLOSEDCASH.MONEY', 'LEFT');
        
        $rows = $this->db->get("PAYMENTS")->result();
//        echo $this->db->last_query();exit;
        return $rows;
    }

     function getpaymenttypes() {
    
////Fetch data
        $this->db->select("PAYMENTS.ID,  RECEIPTS.DATENEW, CLOSEDCASH.HOST, CLOSEDCASH.HOSTSEQUENCE, PAYMENTS.PAYMENT, PAYMENTS.NOTES, PAYMENTS.TOTAL");
  
        $this->db->join('RECEIPTS', 'PAYMENTS.RECEIPT = RECEIPTS.ID', 'LEFT');
        $this->db->join('CLOSEDCASH', 'RECEIPTS.MONEY = CLOSEDCASH.MONEY', 'LEFT');
        $this->db->order_by(`PAYMENT`);
        $rows = $this->db->get("PAYMENTS")->result();
        echo $this->db->last_query();
        return $rows;
    }


    function getcustpaymentdetails($cname){
$table_name = "CUSTOMERS C";
$this->db->select('C.TAXID, C.NAME, C.CARD, C.MAXDEBT, C.CURDEBT, C.CURDATE')->from($table_name);
$this->db->where('C.ID', $cname);

return $this->db->get()->result();
}
    function getsupplierpaymentdetails($sname){
$table_name = "SUPPLIERS S";
$this->db->select('S.TAXID, S.NAME, S.CARD, S.MAXDEBT, S.CURDEBT, S.CURDATE')->from($table_name);
$this->db->where('S.ID', $sname);

return $this->db->get()->result();
}
       function gethostsequence() {      

        
        $this->db->select('HOSTSEQUENCE');
     
        return $this->db->get('CLOSEDCASH')->result();
    }
       function gethostseqid() {
        $this->db->select('MONEY');
       $this->db->where('DATEEND', null);
        return $this->db->get('CLOSEDCASH')->result();
    }
     function getticketsnum() {
        $this->db->select('ID');

        return $this->db->get('TICKETSNUM_PAYMENT')->result();
    }
      function getticketsnumsupplier() {
        $this->db->select('ID');

        return $this->db->get('TICKETSNUM_SUPPLIER')->result();
    }
      function updateticketsnum($ticketsnum) {

         $update = array(
            'ID' => $ticketsnum
        );
       return ($this->db->update('TICKETSNUM_PAYMENT', $update)) ? true : false;

    }
     function updateticketsnumsupplier($ticketsnumsupplier) {

         $update = array(
            'ID' => $ticketsnumsupplier
        );
       return ($this->db->update('TICKETSNUM_SUPPLIER', $update)) ? true : false;

    }
      function getcustomers($customer) {
      $this->db->select('CURDEBT');
        $this->db->where('ID', $customer);
  return $this->db->get('CUSTOMERS')->result();

    }
          function getsuppliers($supplier) {
      $this->db->select('CURDEBT');
        $this->db->where('ID', $supplier);
  return $this->db->get('SUPPLIERS')->result();

    }
    function updatecustomers($newdebt,$customer) {

         $update = array(
            'CURDEBT' => $newdebt
        );
         $this->db->where('ID', $customer);
       return ($this->db->update('CUSTOMERS', $update)) ? true : false;

    }
     function updatecustomers2($date,$customer) {

         $update = array(
            'CURDATE' => $date
        );
         $this->db->where('ID', $customer);
       return ($this->db->update('CUSTOMERS', $update)) ? true : false;

    }
       function updatesuppliers($newcredit,$supplier) {

         $update = array(
            'CURDEBT' => $newcredit
        );
         $this->db->where('ID', $supplier);
       return ($this->db->update('SUPPLIERS', $update)) ? true : false;

    }
      function updatesuppliers2($date,$supplier) {

         $update = array(
            'CURDATE' => $date
        );
         $this->db->where('ID', $supplier);
       return ($this->db->update('SUPPLIERS', $update)) ? true : false;

    }
       function addcashintoreceipts($money, $date) {


        $add = array(
            'MONEY' => $money,
            'DATENEW' => $date
        );

        $this->db->insert('RECEIPTS', $add);
    }
     function cashin($id, $payment, $total, $note) {


        $add = array(
            'RECEIPT' => $id,
            'PAYMENT' => $payment,
            'TOTAL' => $total,
            'NOTES' => $note
        );

        $this->db->insert('PAYMENTS', $add);
    }
    function cashincash($id2, $id, $payment, $total, $note) {


        $add = array(
            'ID' => $id2,
            'RECEIPT' => $id,
            'PAYMENT' => $payment,
            'TOTAL' => $total,
            'NOTES' => $note
        );

        $this->db->insert('PAYMENTS', $add);
    }
    function cashindebtpaid($id2, $id, $payment, $total, $note) {


        $add = array(
            'ID' => $id2,
            'RECEIPT' => $id,
            'PAYMENT' => $payment,
            'TOTAL' => $total,
            'NOTES' => $note
        );

        $this->db->insert('PAYMENTS', $add);
    }
       function cashincreditpaid($id3, $id, $payment, $total, $note) {


        $add = array(
            'ID' => $id3,
            'RECEIPT' => $id,
            'PAYMENT' => $payment,
            'TOTAL' => $total,
            'NOTES' => $note
        );

        $this->db->insert('PAYMENTS', $add);
    }
      function cashintotickets($id, $tickettype, $ticketid, $person, $customer, $status) {


        $add = array(
            'ID' => $id,
            'TICKETTYPE' => $tickettype,
            'TICKETID' => $ticketid,
            'PERSON' => $person,
            'CUSTOMER' => $customer,
            'STATUS' => $status
        );

        $this->db->insert('TICKETS', $add);
    }
    function cashintoticketssupplier($id, $tickettype, $ticketid, $person, $supplier, $status) {


        $add = array(
            'ID' => $id,
            'TICKETTYPE' => $tickettype,
            'TICKETID' => $ticketid,
            'PERSON' => $person,
            'SUPPLIER' => $supplier,
            'STATUS' => $status
        );

        $this->db->insert('TICKETS', $add);
    }
    // GET SUPPLIERID BY NAME
    function getcustomeridbn($name) {
        $this->db->where('NAME', $name);
        $q = $this->db->get('CUSTOMERS')->result();
        return $q[0]->ID;
    }
    function _getSUPPLIERIDbN($name) {
        $this->db->where('NAME', $name);
        $q = $this->db->get('SUPPLIERS')->result();
        return $q[0]->ID;
    }
}

