<?php 
ob_start();
session_start();
include "../config/config.php";

     $start_date = $_POST['start_date'];
     $end_date = $_POST['end_date'];
     
$conditions = " BETWEEN '$start_date' and '$end_date'";

/***************************** Section 1 Dashboard Details Starts Here *****************************/


// Purchase - Total Amount
$statement1 = $pdo->prepare("SELECT SUM(grand_total) as purchase_total_amt FROM `tbl_purchases` WHERE date $conditions and status=1");
$statement1->execute();
$purchase_amount = $statement1->fetchAll(PDO::FETCH_ASSOC);

// Sales - Total Amount
$statement2 = $pdo->prepare("SELECT CASE WHEN SUM(grand_total) IS NULL THEN 0 ELSE SUM(grand_total) END AS sales_total_amount FROM `tbl_sales` WHERE status=1 and date $conditions");
$statement2->execute();
$sales_amt = $statement2->fetchAll(PDO::FETCH_ASSOC);

// Quotation - Total Amount
$statement2 = $pdo->prepare("SELECT CASE WHEN SUM(grand_total) IS NULL THEN 0 ELSE SUM(grand_total) END AS quoatation_total_amount FROM `tbl_sales` WHERE status=2 and date $conditions");
$statement2->execute();
$quotation_amt = $statement2->fetchAll(PDO::FETCH_ASSOC);



// Sales Credit Collected Amount Here 
$sales_payment = $pdo->prepare("SELECT *,SUM(amount) as sales_credit_amount FROM `tbl_payments` WHERE accounts=1 and date $conditions");
$sales_payment->execute();
$sales_credit = $sales_payment->fetchAll(PDO::FETCH_ASSOC);


// Purchase Debit Starts Here
$purchase_payment = $pdo->prepare("SELECT *,SUM(amount) as purchase_debit_amt FROM `tbl_payments` WHERE accounts=2 and date $conditions");
$purchase_payment->execute();
$purchase_debit = $purchase_payment->fetchAll(PDO::FETCH_ASSOC);

// Scheme Credit Starts Here
$scheme_payment = $pdo->prepare("SELECT *,SUM(amount) as scheme_credit_amt FROM `tbl_payments` WHERE accounts=3 and date $conditions");
$scheme_payment->execute();
$scheme_credit = $scheme_payment->fetchAll(PDO::FETCH_ASSOC);

// Expenses - Total Amount
$statement_exp = $pdo->prepare("SELECT *,SUM(amount) as expenses_amt FROM `tbl_payments` WHERE accounts=5 and date $conditions");
$statement_exp->execute();
$expenses_amount = $statement_exp->fetchAll(PDO::FETCH_ASSOC);

// Advance Voucher - Total Amount
$statement_ad = $pdo->prepare("SELECT *,SUM(amount) as advance_voucher_amt FROM `tbl_payments` WHERE accounts=4 and date $conditions");
$statement_ad->execute();
$advannce_voucher_amount = $statement_ad->fetchAll(PDO::FETCH_ASSOC);




















// Section 1 Here
$response['purchase_total_amt']=number_format($purchase_amount[0]['purchase_total_amt'],2);
$response['sales_total_amount']=number_format($sales_amt[0]['sales_total_amount'],2);
$response['quoatation_total_amount']=number_format($quotation_amt[0]['quoatation_total_amount'],2);
$response['sales_credit_amount']=number_format($sales_credit[0]['sales_credit_amount'],2);
$response['purchase_debit_amt']=number_format($purchase_debit[0]['purchase_debit_amt'],2);
$response['scheme_credit_amt']=number_format($scheme_credit[0]['scheme_credit_amt'],2);
$response['total_expenses_amt']=number_format($expenses_amount[0]['expenses_amt'],2);
$response['total_advance_voucher_amt']=number_format($advannce_voucher_amount[0]['advance_voucher_amt'],2);
    


echo json_encode($response);
?>