<?php

include_once("connect.php");
include_once("session.php");

$data = json_decode($_POST['data']);
if($data->Type != "reminder"){
    die("This info lacks the transaction type");
}
$reminderData = $data->Reminder;
$invoices = $data->Invoice;
$credits = $data->Credits;
$summary = $data->Summary;
$payment = $data->PaymentInformation;
$payment->CPD = 0;
$payment->CPDDate = "0000-00-00 00:00:00";
$query = "INSERT INTO PAYMENT_HEAD (PaymentDate, AccountNumber, SalesmanID, CPD, CPDDate, CashAmount, CheckAmount, `Notes`) VALUES (DATE_ADD(NOW(), INTERVAL -4 HOUR), '" . $summary->AccountNumber . "', '" . $_SESSION['SalesmanID'] . "', " . $payment->CPD . " , '" . $payment->CPDDate . "', " . $payment->Cash . ", " . $payment->Check . ", '" . $data->Notes . "')";
$result = mysqli_query($con, $query);
$paymentID = mysqli_insert_id($con);
foreach($invoices as $invoice){
    $partialPayment = ($invoice->Total != $invoice->PartialPayment)?1:0;
    $query = "INSERT INTO PAYMENT_DETAIL (PaymentID, InvoiceID, InvoiceDate, Reference, PartialPayment, SubtotalAmount, DiscountAmount, TaxAmount, PaymentAmount) VALUES ($paymentID, '" . mysqli_real_escape_string($con,$invoice->InvoiceID) . "', '" . $invoice->InvoiceDate . "', '" . mysqli_real_escape_string($con, $invoice->Remarks) . "', 1, " . $invoice->Subtotal . ", 0, " . $invoice->Tax . ", 0);";
    if(mysqli_query($con, $query)){
        
    }else{
        die(mysqli_error($con) . "<br />" . $query);
    }
}
$query = "INSERT INTO PAYMENT_REMINDER (`PaymentID`, `Reason`, `Notes`, `NextVisitDate`) VALUES ($paymentID, '" . $reminderData->Reason . "','" . $reminderData->Notes . "','" . $reminderData->NextVisitDate . "');";
$result = mysqli_query($con, $query);
include_once("./modules/payment/payment.classes.php");
$obj = new Payment($paymentID);
//$obj->SendPaymentEmail();
echo $paymentID;



?>