<?php

class Invoice {

	/*************************************************
	/ This function add new Invoice
	*************************************************/
	
	public static function AddInvoice($data) {
		global $db;
				
		$id = $db->query_insert(TABLE_INVOICE, $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 AddInvoiceProduct($data) {
		global $db;

		$id = $db->query_insert(TABLE_INVOICE_PRODUCT, $data);
		if($db->affected_rows > 0){
			//$db->query_insert("vrs_logs", $data);
			return $id;
		}else{
			return false;
		}

	} // AddInvoiceProduct


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

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

		$id = $db->query_update(TABLE_INVOICE,$data,"id='$pid'");

		if($db->affected_rows > 0){
			//$db->query_insert("vrs_logs", $data);

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

	} // UpdateInvoice


	public static function GetInvoiceSetupById($id) {
	
		global $db;
		$query = "SELECT * from ".TABLE_INVOICE." 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 GetInvoiceSetupByNumber($number) {

		global $db;
		$query = "SELECT * from ".TABLE_INVOICE." 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 GetInvoiceList($filter="",$pageno="null",$pagesize="null") {

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

		if($db->affected_rows > 0){

			return $rows;
		}else{

			return false;
		}
	}//Invoice

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

		global $db;
		$query = "SELECT *,".TABLE_INVOICE.".id AS invoiceid  from ".TABLE_INVOICE." ". $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_INVOICE." 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 GetInvoiceCustomerReportList($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_INVOICE." 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 GetInvoiceCustomerReportList123($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.add_discount, inv.add_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_INVOICE." 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 GetInvoiceCustomerReportListMonthlySale($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,
            invPrAgg.invoice_id,
            IFNULL(invPrAgg.product_qty, 0) AS product_qty,
            IFNULL(invPrAgg.carton_qty, 0) AS carton_qty
        FROM " . TABLE_INVOICE . " AS inv
        LEFT JOIN (
            SELECT 
                invPr.invoice_id,
                SUM(invPr.product_qty) AS product_qty,
                SUM(
                    CASE 
                        WHEN IFNULL(prod.carton_pieces, 0) > 0 
                        THEN invPr.product_qty / prod.carton_pieces 
                        ELSE 0 
                    END
                ) AS carton_qty
            FROM " . TABLE_INVOICE_PRODUCT . " AS invPr
            LEFT JOIN " . TABLE_PRODUCTSETUP . " AS prod 
                ON invPr.product_id = prod.id
            GROUP BY invPr.invoice_id
        ) AS invPrAgg 
            ON inv.id = invPrAgg.invoice_id
        " . $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 GetInvoiceCustomerReportListMonthlyPayment($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 GetInvoiceCustomerReportListMonthlySaleCustomers($filter = "", $pageno = "null", $pagesize = "null")
{
    global $db;

    $query = "
        SELECT 
            inv.customer_id,
            SUM(inv.discount_price) AS discount_price,
            SUM(inv.total_amount) AS total_amount,
            COUNT(inv.id) AS total_invoices,
            SUM(IFNULL(invPrAgg.product_qty, 0)) AS product_qty,
            SUM(IFNULL(invPrAgg.carton_qty, 0)) AS carton_qty,
            inv.status AS status
        FROM " . TABLE_INVOICE . " AS inv
        LEFT JOIN (
            SELECT 
                invPr.invoice_id,
                SUM(invPr.product_qty) AS product_qty,
                SUM(
                    CASE 
                        WHEN IFNULL(prod.carton_pieces, 0) > 0 
                        THEN invPr.product_qty / prod.carton_pieces 
                        ELSE 0 
                    END
                ) AS carton_qty
            FROM " . TABLE_INVOICE_PRODUCT . " AS invPr
            LEFT JOIN " . TABLE_PRODUCTSETUP . " AS prod 
                ON invPr.product_id = prod.id
            GROUP BY invPr.invoice_id
        ) AS invPrAgg 
            ON inv.id = invPrAgg.invoice_id
        " . $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 GetInvoiceCustomerReportListMonthlyPaymentCustomers($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 GetInvoicePaymentReportList($filter="",$pageno="null",$pagesize="null") {

		global $db;
		$query = "SELECT *, inv.create_date AS invoice_create_date, inv.code AS invoicecode from ".TABLE_INVOICE." 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 GetInvoicePaymentReport($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_INVOICE." 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 GetInvoicePaymentReportLists($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_INVOICE."  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 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_INVOICE."  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 GetInvoicePaymentReportLists123categorywise($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_INVOICE . " AS inv " . $filter . " 
            
            UNION ALL

            SELECT 
                'payment' AS nameTable, 
                pay.create_date AS create_date, 
                pay.code AS code,
                pay.customer_id AS customer_id, 
                pay.invoice_reference_code, 
                pay.payment_type, 
                pay.amount,
                pay.cheque_bank_name, 
                pay.cheque_number, 
                pay.cheque_payee_name, 
                pay.cheque_date,
                pay.bank_name, 
                pay.bank_account_number, 
                pay.party_name, 
                pay.from_bank_name,
                pay.withholding_tax, 
                pay.sales_tax, 
                pay.goods_return, 
                pay.shelf_rent, 
                pay.builty_charges, 
                pay.other_expense,
                '', '', '', '', '', '', pay.description
            FROM " . TABLE_PAYMENT . " AS pay " . $filter . "
            
            ORDER BY customer_id, create_date";

    $rows = $db->fetch_all_array($query, $pageno, $pagesize);

    if ($db->affected_rows > 0) {
        return $rows;
    } else {
        return false;
    }
}



	public static function GetInvoicePaymentReportListsForOwner($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_INVOICE."  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 GetInvoicePaymentReportListsForOwner123($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,
		'' AS withholding_tax, '' AS sales_tax, '' AS goods_return, '' AS shelf_rent, '' AS builty_charges, '' AS other_expense
 		from ".TABLE_INVOICE."  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 GetInvoiceProducts($filter="",$pageno="null",$pagesize="null") {

		global $db;
		$query = "SELECT * from ".TABLE_INVOICE_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 DeleteInvoiceProducts($invoice_id) {
		global $db;
		$query = "Delete from " . TABLE_INVOICE_PRODUCT . "  WHERE invoice_id='$invoice_id'";
		$rows = $db->query_first($query);
		if($db->affected_rows > 0){
			return true;
		}else{
			return false;
		}
	}//DeleteInvoiceProducts
  
 
  public static function getLedgerReport($dateFrom, $dateTo, $catType)
{
    global $db;

    // Category Filter Logic
    $catCondition = $catType;

    // ================= Opening Balance Query ==================
    $openingBalanceQuery = "
        SELECT 
            cs.id AS customer_id,
            cs.name AS customer_name,
            IFNULL(CEIL(SUM(p.total_payment)), 0) AS total_payment,
            CEIL((IFNULL(SUM(i.total_invoice), 0) + IFNULL(cs.balance, 0))) AS total_invoice,
            CEIL((IFNULL(SUM(p.total_payment), 0) 
             - (IFNULL(SUM(i.total_invoice), 0) + IFNULL(cs.balance, 0)))) AS opening_balance
        FROM 
            customer_setup cs
        LEFT JOIN (
            SELECT 
                customer_id,
                CEIL(SUM(amount)) AS total_payment
            FROM 
                payment
            WHERE 
                DATE(create_date) < '$dateFrom'
                AND status <> 'Cancel'
            GROUP BY 
                customer_id
        ) p ON cs.id = p.customer_id
        LEFT JOIN (
            SELECT 
                customer_id,
                CEIL(SUM(total_amount)) AS total_invoice
            FROM 
                invoice
            WHERE 
                DATE(create_date) < '$dateFrom'
                AND status NOT IN ('Draft', 'Cancel')
            GROUP BY 
                customer_id
        ) i ON cs.id = i.customer_id
        WHERE 
            cs.status = 'active'
            $catCondition
        GROUP BY 
            cs.id
        ORDER BY 
            cs.name ASC
    ";

    // ================= Transaction Query ==================
    $transactionQuery = "
        SELECT 
            t.customer_id,
            cs.name AS customer_name,
            t.create_date,
            t.reference_code,
            t.transaction_type,
            CEIL(t.debit) AS debit,
            CEIL(t.credit) AS credit,
            t.status,
            t.payment_type,
            t.from_bank_name,
            t.cheque_number,
            t.cheque_date
        FROM (
            SELECT 
                customer_id,
                create_date,
                code AS reference_code,
                'Invoice' AS transaction_type,
                CEIL(total_amount) AS debit,
                0 AS credit,
                status,
                '' AS payment_type,
                '' AS from_bank_name,
                '' AS cheque_number,
                '' AS cheque_date
            FROM 
                invoice
            WHERE 
                DATE(create_date) BETWEEN '$dateFrom' AND '$dateTo'
                AND status NOT IN ('Draft', 'Cancel')

            UNION ALL

            SELECT 
                customer_id,
                create_date,
                code AS reference_code,
                'Payment' AS transaction_type,
                0 AS debit,
                CEIL(amount) AS credit,
                status,
                payment_type,
                from_bank_name,
                cheque_number,
                cheque_date
            FROM 
                payment
            WHERE 
                DATE(create_date) BETWEEN '$dateFrom' AND '$dateTo'
                AND status <> 'Cancel'
        ) t
        JOIN 
            customer_setup cs ON cs.id = t.customer_id
        WHERE 
            cs.status = 'active'
            $catCondition
        ORDER BY 
            cs.name ASC, t.create_date
    ";
//echo $openingBalanceQuery;
    // ================= Execute Queries ==================
    $openingBalanceResult = $db->fetch_all_array($openingBalanceQuery);
    $transactionResult = $db->fetch_all_array($transactionQuery);

    // ================= Return Combined Data ==================
    return [
        'opening_balance' => $openingBalanceResult,
        'transactions' => $transactionResult
    ];
}

  
 public static function GetInvoiceAgingReport($filter)
{
    global $db;

    $query = "
        SELECT 
            cs.id AS Customer_ID,
            cs.name AS Customer_Name,
            inv.id AS Invoice_ID,
            inv.create_date AS Invoice_Date,
            inv.total_amount AS Invoice_Amount,
            IFNULL(SUM(p.amount), 0) AS Payment_Received,
            (inv.total_amount - IFNULL(SUM(p.amount), 0)) AS Balance,
            CASE 
                WHEN DATEDIFF(CURDATE(), inv.create_date) BETWEEN 0 AND 15 THEN '0-15 Days'
                WHEN DATEDIFF(CURDATE(), inv.create_date) BETWEEN 16 AND 30 THEN '16-30 Days'
                WHEN DATEDIFF(CURDATE(), inv.create_date) BETWEEN 31 AND 60 THEN '31-60 Days'
                WHEN DATEDIFF(CURDATE(), inv.create_date) BETWEEN 61 AND 90 THEN '61-90 Days'
                ELSE '90+ Days'
            END AS Aging_Slab
        FROM 
            ".TABLE_INVOICE." inv
        LEFT JOIN 
            ".TABLE_PAYMENT." p ON CAST(inv.id AS CHAR) = p.invoice_reference_code
        JOIN 
            customer_setup cs ON inv.customer_id = cs.id
        WHERE 
            inv.status NOT IN ('Paid', 'Cancel')
            $filter
        GROUP BY 
            inv.id, Aging_Slab
        ORDER BY 
            cs.name ASC, Aging_Slab
    ";
echo $query;
    $rows = $db->fetch_all_array($query);

    if ($db->affected_rows > 0) {
        return $rows;
    } else {
        return false;
    }
}
  
  /**********************************************************
 * Multi Category Monthly Sale Report
 * Summary Mode: Customer-wise monthly sales
 * Uses:
 * invoice
 * invoice_product
 * customer_setup
 * customer_category
 * product_setup
 **********************************************************/
public static function GetInvoiceCustomerReportListMonthlySaleMultiCategory($filter = "", $pageno = "null", $pagesize = "null")
{
    global $db;

    $query = "
        SELECT 
            inv.customer_id,
            cust.name AS customer_name,
            cust.category_id,
            cat.name AS category_name,

            COUNT(DISTINCT inv.id) AS total_invoices,

            SUM(IFNULL(invPrAgg.product_qty, 0)) AS product_qty,
            SUM(IFNULL(invPrAgg.carton_qty, 0)) AS carton_qty,

            SUM(IFNULL(inv.total_amount, 0)) AS total_amount,

            SUM(IFNULL(inv.discount_price, 0)) AS discount_price,
            SUM(IFNULL(inv.add_discount_price, 0)) AS add_discount_price,
            SUM(IFNULL(inv.discount_price, 0) + IFNULL(inv.add_discount_price, 0)) AS total_discount,

            SUM(IFNULL(inv.total_amount, 0) + IFNULL(inv.discount_price, 0) + IFNULL(inv.add_discount_price, 0)) AS sale_price

        FROM " . TABLE_INVOICE . " AS inv

        JOIN " . TABLE_CUSTOMERSETUP . " AS cust 
            ON inv.customer_id = cust.id

        LEFT JOIN " . TABLE_CUSTOMERCATEGORYSETUP . " AS cat 
            ON cust.category_id = cat.id

        LEFT JOIN (
            SELECT 
                invPr.invoice_id,
                SUM(invPr.product_qty) AS product_qty,
                SUM(
                    CASE 
                        WHEN IFNULL(prod.carton_pieces, 0) > 0 
                        THEN invPr.product_qty / prod.carton_pieces 
                        ELSE 0 
                    END
                ) AS carton_qty
            FROM " . TABLE_INVOICE_PRODUCT . " AS invPr
            LEFT JOIN " . TABLE_PRODUCTSETUP . " AS prod 
                ON invPr.product_id = prod.id
            GROUP BY invPr.invoice_id
        ) AS invPrAgg 
            ON inv.id = invPrAgg.invoice_id

        " . $filter;

    // print($query);exit;

    $rows = $db->fetch_all_array($query, $pageno, $pagesize);

    if ($db->affected_rows > 0) {
        return $rows;
    } else {
        return false;
    }
}



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

?>
