<?php

class Deliverychallan {

	/*************************************************
	/ This function add new Invoice
	*************************************************/
	
	public static function AddDeliveryChallan($data) {
		global $db;
				
		$id = $db->query_insert(TABLE_DELIVERY_CHALLAN, $data);
		if($db->affected_rows > 0){
			//$db->query_insert("vrs_logs", $data);
			return $id;
		}else{
			return false;
		}
	
	} // AddInvoice

	/*************************************************
	/ This function add new Invoice Product
	 *************************************************/

	public static function AddDeliveryChallanProduct($data) {
		global $db;
		
		$id = $db->query_insert(TABLE_DELIVERY_CHALLAN_PRODUCT, $data);
      echo $id;
		if($db->affected_rows > 0){
			//$db->query_insert("vrs_logs", $data);
			return $id;
		}else{
			return false;
		}

	} // AddInvoiceProduct


	/*************************************************
	/ This function 
    Invoice
	 *************************************************/

	public static function UpdateDeliveryChallanSetup($pid,$data) {
		global $db;

		$id = $db->query_update(TABLE_DELIVERY_CHALLAN,$data,"id='$pid'");
	
		if($db->affected_rows > 0){
			//$db->query_insert("vrs_logs", $data);

			return $id;
		}else{
			return $id;
		}

	} 
  // UpdateInvoice


	public static function GetDeliveryChallanSetupById($id) {
	
		global $db;
		$query = "SELECT * from ".TABLE_DELIVERY_CHALLAN." WHERE id ='$id'";
		//print($query);exit;
		$rows = $db->query_first($query);
	
                if($db->affected_rows > 0){
			
                        return $rows;
                }else{
			
                        return false;
                }
	}//Invoice

	public static function GetChallanSetupByNumber($number) {

		global $db;
		$query = "SELECT * from ".TABLE_DELIVERY_CHALLAN." WHERE code ='$number'";
		//print($query);exit;
		$rows = $db->query_first($query);

		if($db->affected_rows > 0){

			return $rows;
		}else{

			return false;
		}
	}//Invoice

	/**********************************************************
	/ This function will return Invoice
	 **********************************************************/

	public static function GetDeliveryChallanList($filter="") {

		global $db;
		$query = "SELECT * FROM " . TABLE_DELIVERY_CHALLAN . " $filter";
        
		$rows = $db->fetch_all_array($query);
      
		
        
		if($db->affected_rows > 0){

			return $rows;
		}else{

			return false;
		}
	}//Invoice

	public static function GetIChallanListJoinCustomer($filter="",$pageno="null",$pagesize="null") {

		global $db;
		$query = "SELECT *,".TABLE_DELIVERY_CHALLAN.".id AS invoiceid  from ".TABLE_DELIVERY_CHALLAN." ". $filter;
		$rows = $db->fetch_all_array($query,$pageno,$pagesize);

		if($db->affected_rows > 0){

			return $rows;
		}else{

			return false;
		}
	}//Invoice

	/**********************************************************
	/ This function will return Invoice
	 **********************************************************/

	public static function GetInvoiceReportList($filter="",$pageno="null",$pagesize="null") {

		global $db;
		$query = "SELECT *, cs.category_id AS cscategory_id, inv.code AS code, cs.id AS custid, inv.status AS status from ".TABLE_DELIVERY_CHALLAN." AS inv ". $filter;
		//print($query);exit;
		$rows = $db->fetch_all_array($query,$pageno,$pagesize);

		if($db->affected_rows > 0){

			return $rows;
		}else{

			return false;
		}
	}//Invoice

	/**********************************************************
	/ This function will return Invoice
	 **********************************************************/

	public static function GetChallanCustomerReportList($filter="",$pageno="null",$pagesize="null") {

		global $db;
		$query = "SELECT 'invoice' AS nameTable, inv.id, inv.description, inv.code AS code , inv.customer_id, inv.discount, inv.discount_price, inv.total_amount, inv.payment_due,
		inv.sales_person_id, inv.status AS status, inv.create_date, inv.validate_date, inv.datetime, invPr.invoice_id,
		invPr.product_id, invPr.product_qty, invPr.unit_cost_price, invPr.unit_distributor_price, invPr.unit_sale_price,
		'' AS payment_type, '' AS cheque_bank_name, '' AS cheque_number,
		'' AS cheque_payee_name, '' AS cheque_date, '' AS bank_name, '' AS bank_account_number,
		'' AS party_name, '' AS from_bank_name from ".TABLE_DELIVERY_CHALLAN." AS inv ". $filter;
		//print($query);exit;
		$rows = $db->fetch_all_array($query,$pageno,$pagesize);
		//print($rows);exit;
		if($db->affected_rows > 0){

			return $rows;
		}else{

			return false;
		}
	}//Invoice

	/**********************************************************
	/ This function will return Invoice
	 **********************************************************/

	public static function GetChallanCustomerReportListMonthlySale($filter="",$pageno="null",$pagesize="null") {

		global $db;
		$query = "SELECT inv.code AS code, inv.customer_id, inv.discount_price, inv.total_amount, 
		inv.status AS status, inv.create_date, invPr.invoice_id,
		invPr.product_id, sum(invPr.product_qty) AS product_qty from ".TABLE_DELIVERY_CHALLAN." AS inv ". $filter;
		//print($query);exit;
		$rows = $db->fetch_all_array($query,$pageno,$pagesize);
		//print($rows);exit;
		if($db->affected_rows > 0){
			return $rows;
		}else{
			return false;
		}
	}//Invoice

	public static function GetChallanCustomerReportListMonthlyPayment($filter="",$pageno="null",$pagesize="null") {

		global $db;
		$query = "SELECT pay.code AS code, pay.customer_id, pay.amount, pay.payment_type, 
		pay.status AS status, pay.create_date from ".TABLE_PAYMENT." AS pay ". $filter;
		//print($query);exit;
		$rows = $db->fetch_all_array($query,$pageno,$pagesize);
		//print($rows);exit;
		if($db->affected_rows > 0){
			return $rows;
		}else{
			return false;
		}
	}//Invoice

	/**********************************************************
	/ This function will return Invoice
	 **********************************************************/

	public static function GetChallanCustomerReportListMonthlySaleCustomers($filter="",$pageno="null",$pagesize="null") {

		global $db;
		$query = "SELECT inv.code AS code, inv.customer_id, sum(inv.discount_price) AS discount_price, 
		sum(inv.total_amount) AS total_amount, count(inv.customer_id) AS total_invoices,
		inv.status AS status  from ".TABLE_DELIVERY_CHALLAN." AS inv ". $filter;
		//print($query);exit;
		$rows = $db->fetch_all_array($query,$pageno,$pagesize);
		//print($rows);exit;
		if($db->affected_rows > 0){
			return $rows;
		}else{
			return false;
		}
	}//Invoice

	public static function GetChallanCustomerReportListMonthlyPaymentCustomers($filter="",$pageno="null",$pagesize="null") {

		global $db;
		$query = "SELECT pay.code AS code, pay.customer_id,
		sum(pay.amount) AS total_amount, count(pay.customer_id) AS total_invoices,
		pay.status AS status  from ".TABLE_PAYMENT." AS pay ". $filter;
		//print($query);exit;
		$rows = $db->fetch_all_array($query,$pageno,$pagesize);
		//print($rows);exit;
		if($db->affected_rows > 0){
			return $rows;
		}else{
			return false;
		}
	}//Invoice

	/**********************************************************
	/ This function will return Invoice
	 **********************************************************/

	public static function GetChallanPaymentReportList($filter="",$pageno="null",$pagesize="null") {

		global $db;
		$query = "SELECT *, inv.create_date AS invoice_create_date, inv.code AS invoicecode from ".TABLE_DELIVERY_CHALLAN." AS inv ". $filter;
		//print($query);exit;
		$rows = $db->fetch_all_array($query,$pageno,$pagesize);

		if($db->affected_rows > 0){

			return $rows;
		}else{

			return false;
		}
	}//Invoice

	public static function GetChallanPaymentReport($filter="",$pageno="null",$pagesize="null") {

		global $db;
		$query = "SELECT *, inv.create_date AS invoice_create_date, inv.code AS invoicecode,
					pay.create_date AS payment_create_date, pay.code AS paymentcode from ".TABLE_CUSTOMERSETUP." AS cus JOIN ".TABLE_DELIVERY_CHALLAN." AS inv ON cus.id = inv.customer_id ". $filter;
		//print($query);exit;
		$rows = $db->fetch_all_array($query,$pageno,$pagesize);

		if($db->affected_rows > 0){

			return $rows;
		}else{

			return false;
		}
	}//Invoice

	/**********************************************************
	/ This function will return Invoice
	 **********************************************************/

	public static function GetChallanPaymentReportLists($filter="",$pageno="null",$pagesize="null") {

		global $db;
		$query = "SELECT 'invoice' AS nameTable, inv.create_date AS create_date, inv.code AS code,
 		inv.customer_id AS customer_id, '' AS invoice_reference_code, '' AS payment_type, '' AS amount,
 		'' AS cheque_bank_name, '' AS cheque_number, '' AS cheque_payee_name, '' AS cheque_date,
 		'' AS bank_name, '' AS bank_account_number, '' AS party_name, '' AS from_bank_name,
 		inv.discount, inv.discount_price, inv.total_amount, inv.payment_due, inv.sales_person_id, inv.status
 		from ".TABLE_DELIVERY_CHALLAN."  AS inv ". $filter;
			//print($query);exit;
		$rows = $db->fetch_all_array($query,$pageno,$pagesize);

		if($db->affected_rows > 0){

			return $rows;
		}else{

			return false;
		}
	}//Invoice

	public static function GetChallanPaymentReportListsForOwner($filter="",$pageno="null",$pagesize="null") {

		global $db;
		$query = "SELECT 'invoice' AS nameTable, inv.create_date AS create_date,
 		inv.customer_id AS customer_id, '' AS amount,
 		inv.discount, inv.discount_price, inv.total_amount, inv.payment_due, inv.status
 		from ".TABLE_DELIVERY_CHALLAN."  AS inv ". $filter;
		//print($query);exit;
		$rows = $db->fetch_all_array($query,$pageno,$pagesize);

		if($db->affected_rows > 0){

			return $rows;
		}else{

			return false;
		}
	}//Invoice

	/**********************************************************
	/ This function will return Invoice Products
	 **********************************************************/

	public static function GetDeliverychallanProducts($filter="",$pageno="null",$pagesize="null") {

		global $db;
		$query = "SELECT * from ".TABLE_DELIVERY_CHALLAN_PRODUCT." ". $filter;
       
		//print($query);exit;
		$rows = $db->fetch_all_array($query,$pageno,$pagesize);
		//print($rows);
		if($db->affected_rows > 0){
			return $rows;
		}else{
			return false;
		}
	}//InvoiceProducts

	/**********************************************************
	/ This function will delete the Invoice Products
	 **********************************************************/

	public static function DeleteDeliveryChallanProducts($challan_id) {
		global $db;
		$query = "Delete from " . TABLE_DELIVERY_CHALLAN_PRODUCT . "  WHERE challan_id='$challan_id'";
    //  error_log('Query: ' . print_r($query, true));
		$rows = $db->query_first($query);
		if($db->affected_rows > 0){
			return true;
		}else{
			return false;
		}
	}//DeleteInvoiceProducts
  
  public static function GetInvoiceProductByInvoiceIdAndProductId($invoice_id, $product_id) {
        global $db;

        // Using the constant TABLE_DELIVERY_CHALLAN_PRODUCT to reference the invoice product table
        $sql = "SELECT * FROM " . TABLE_DELIVERY_CHALLAN_PRODUCT . " 
                WHERE invoice_id = :invoice_id AND product_id = :product_id";
        
        $stmt = $db->prepare($sql);
        $stmt->bindParam(':invoice_id', $invoice_id, PDO::PARAM_INT);
        $stmt->bindParam(':product_id', $product_id, PDO::PARAM_INT);
        $stmt->execute();

        // Fetch and return the invoice product record
        return $stmt->fetch(PDO::FETCH_OBJ);
    }
  
  public static function GetInvoicePaymentReportListsForOwner123($filter = "", $pageno = "null", $pagesize = "null") {

    global $db;
    
    // Adjusted the query to include `adv_tax`, `adv_tax_price`, and `po_number` for the new web app
    $query = "SELECT 'invoice' AS nameTable, inv.create_date AS create_date,
        inv.customer_id AS customer_id, '' AS amount,
        inv.discount, inv.discount_price, inv.total_amount, inv.payment_due, inv.status,
        '' AS withholding_tax, '' AS sales_tax, '' AS goods_return, '' AS shelf_rent, 
        '' AS builty_charges, '' AS other_expense, inv.adv_tax, inv.adv_tax_price, inv.po_number
        from ".TABLE_DELIVERY_CHALLAN." AS inv ". $filter;
    
    // Execute the query and fetch the results
    $rows = $db->fetch_all_array($query, $pageno, $pagesize);

    if ($db->affected_rows > 0) {
        return $rows;
    } else {
        return false;
    }
} // Invoice
  
  
public static function GetInvoicePaymentReportLists123($filter="",$pageno="null",$pagesize="null") {

		global $db;
		$query = "SELECT 'invoice' AS nameTable, inv.create_date AS create_date, inv.code AS code,
 		inv.customer_id AS customer_id, '' AS invoice_reference_code, '' AS payment_type, '' AS amount,
 		'' AS cheque_bank_name, '' AS cheque_number, '' AS cheque_payee_name, '' AS cheque_date,
 		'' AS bank_name, '' AS bank_account_number, '' AS party_name, '' AS from_bank_name,
		'' AS withholding_tax, '' AS sales_tax, '' AS goods_return, '' AS shelf_rent, '' AS builty_charges, '' AS other_expense,
 		inv.discount, inv.discount_price, inv.total_amount, inv.payment_due, inv.sales_person_id, inv.status, '' AS description
 		from ".TABLE_DELIVERY_CHALLAN."  AS inv ". $filter;
			//print($query);exit;
		$rows = $db->fetch_all_array($query,$pageno,$pagesize);

		if($db->affected_rows > 0){

			return $rows;
		}else{

			return false;
		}
	}//Invoice
 // Invoice




	public static function GetNumRows() { global $db; return $db->GetNumRows(); }
	public static function GetNumberOfPages() { global $db; return $db->GetNumberOfPages(); }

  
  

  
  
}

?>
