<?php

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

class Partners_model extends CI_Model {

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

    function getcustomers($customers)
    {
         $table_name = "CUSTOMERS";
      $this->db->select("NAME,ADDRESS,CARD,MAXDEBT,CURDEBT,NOTES")->from($table_name);

  if($customers)
        {
            $this->db->like('CUSTOMERS.NAME',$customers);
        }
        return $this->db->get()-> result();
     }
     function getcustomerdefaulting()
    {
         $table_name = "CUSTOMERS";
      $this->db->select("CUSTOMERS.NAME,CUSTOMERS.ADDRESS,CUSTOMERS.CARD,CUSTOMERS.MAXDEBT,CUSTOMERS.CURDEBT,CUSTOMERS.NOTES")->from($table_name);
      $this->db->where('CURDEBT > 0');
        return $this->db->get()-> result();
     }
     function getcustomerdiary($customers,$datefrom,$dateto)
     {
         $table_name = "CUSTOMERS";
         $this->db->select("CUSTOMERS.NAME AS CUSTOMER,TICKETS.TICKETID AS TICKET,CUSTOMERS.TAXID AS TAX,DATE_FORMAT(RECEIPTS.DATENEW,('%Y-%m-%d')) AS DATE,PAYMENTS.PAYMENT,PAYMENTS.TOTAL")->from($table_name);
          if($customers)
        {
            $this->db->like('CUSTOMERS.NAME',$customers);
        }
         if ($datefrom && $dateto) {
            $this->db->where("DATENEW >=", $datefrom);
            $this->db->where("DATENEW <=", $dateto);
        } else if ($datefrom) {
            $this->db->where('DATENEW LIKE "%' . $datefrom .'%"');
        }
        else if ($dateto) {
            $this->db->where("DATENEW <=", $dateto );
        }
         $this->db->join('TICKETS','TICKETS.CUSTOMER = CUSTOMERS.ID','LEFT');
          $this->db->join('RECEIPTS','RECEIPTS.ID = TICKETS.ID','LEFT');
           $this->db->join('PAYMENTS','RECEIPTS.ID = PAYMENTS.RECEIPT','LEFT');
        return $this->db->get()-> result();
     }
   //SUPPLIERS
     function getsuppliers($suppliers)
    {
         $table_name = "SUPPLIERS";
      $this->db->select("SUPPLIERS.NAME,SUPPLIERS.ADDRESS,SUPPLIERS.CARD,SUPPLIERS.MAXDEBT,SUPPLIERS.CURDEBT,SUPPLIERS.NOTES")->from($table_name);

  if($suppliers)
        {
            $this->db->like('SUPPLIERS.NAME',$suppliers);
        }
        return $this->db->get()-> result();
     }
          function gesupplierdefaulting()
    {
         $table_name = "SUPPLIERS";
       $this->db->select("SUPPLIERS.NAME,SUPPLIERS.ADDRESS,SUPPLIERS.CARD,SUPPLIERS.MAXDEBT,SUPPLIERS.CURDEBT,SUPPLIERS.NOTES")->from($table_name);
      $this->db->where('CURDEBT > 0');
        return $this->db->get()-> result();
     }
    function getsupplierdiary($suppliers,$datefrom,$dateto)
     {
        $table_name = "SUPPLIERS";
         $this->db->select("SUPPLIERS.NAME AS SUPPLIER,TICKETS.TICKETID AS TICKET,SUPPLIERS.TAXID AS TAX,DATE_FORMAT(RECEIPTS.DATENEW,('%Y-%m-%d')) AS DATE,PAYMENTS.PAYMENT,PAYMENTS.TOTAL")->from($table_name);
          if($suppliers)
        {
            $this->db->like('SUPPLIERS.NAME',$suppliers);
        }
         if ($datefrom && $dateto) {
            $this->db->where("DATENEW >=", $datefrom);
            $this->db->where("DATENEW <=", $dateto."+1 day");
        } else if ($datefrom) {
            $this->db->where('DATENEW LIKE "%' . $datefrom .'%"');
        }
        else if ($dateto) {
            $this->db->where("DATENEW <=", $dateto."+1 day");
        }
         $this->db->join('TICKETS','TICKETS.SUPPLIER = SUPPLIERS.ID','LEFT');
          $this->db->join('RECEIPTS','RECEIPTS.ID = TICKETS.ID','LEFT');
           $this->db->join('PAYMENTS','RECEIPTS.ID = PAYMENTS.RECEIPT','LEFT');
        return $this->db->get()-> result();
     }
         function getsupplierdiarydet($suppliers,$datefrom,$dateto)
     {
         $table_name = "SUPPLIERS";
             $this->db->select("SUPPLIERS.NAME AS SUPPLIER,PRODUCTS.NAME AS PRODUCT,DATE_FORMAT(STOCKDIARY.DATENEW, ('%Y-%m-%d')) DATENEW,STOCKDIARY.UNITS,UNITS.NAME AS UOM,ROUND(STOCKDIARY.PRICE/STOCKDIARY.UNITS) AS PRICE,STOCKDIARY.PRICE AS TOTAL")->from($table_name);
           if ($datefrom && $dateto) {
            $this->db->where("DATENEW >=", $datefrom);
            $this->db->where("DATENEW <=", $dateto."+1 day");
        } else if ($datefrom) {
            $this->db->where("DATENEW", $datefrom);
        }
        else if ($dateto) {
            $this->db->where("DATENEW <=", $dateto."+1 day");
        }

             if($suppliers)
        {
            $this->db->like('SUPPLIERS.NAME',$suppliers);
        }
  
             $this->db->join('STOCKDIARY','STOCKDIARY.SUPPLIER = SUPPLIERS.ID','LEFT');
               $this->db->join('PRODUCTS','PRODUCTS.ID = STOCKDIARY.PRODUCT','LEFT');
                               $this->db->join('MATERIALS_UNITS','MATERIALS_UNITS.MATERIAL = PRODUCTS.ID','LEFT');
                    $this->db->join('UNITS','UNITS.ID = MATERIALS_UNITS.UNIT','LEFT');
                         $this->db->order_by('STOCKDIARY.DATENEW');
                             return $this->db->get()-> result();
     }
   }