setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $totalAmount = $_POST['totalAmount']; $tenure = $_POST['tenure']; $frequency = $_POST['frequency']; $bookingDate = $_POST['bookingDate']; $paymentMode = $_POST['paymentMode']; $salesAgent = $_POST['salesAgent']; $adminNote = $_POST['adminNote']; $invoiceId = $_POST['invoiceId']; $customerName = $_POST['customerName']; $customerAddress = $_POST['customerAddress']; $marketingAgent = $_POST['marketingAgent']; $item = $_POST['item']; $description = $_POST['description']; $quantity = $_POST['quantity']; $rate = $_POST['rate']; $tax = $_POST['tax']; $taxAmount = $_POST['taxAmount']; // EMI Calculation $emiAmount = round($totalAmount / $tenure, 2); $outstandingAmount = $totalAmount; $emiDate = new DateTime($bookingDate); // Set initial date $stmt = $db->prepare("INSERT INTO emi (customerId, emiNumber, emiAmount, emiDate, totalAmount, outstanding, tenure, frequency, bookingDate, invoiceId) VALUES (:customerId, :emiNumber, :emiAmount, :emiDate, :totalAmount, :outstanding, :tenure, :frequency, :bookingDate, :invoiceId)"); for ($i = 1; $i <= $tenure; $i++) { $outstandingAmount -= $emiAmount; $emiDateFormatted = $emiDate->format('Y-m-d'); $stmt->bindParam(':customerId', $_GET['customerId']); $stmt->bindParam(':emiNumber', $i); $stmt->bindParam(':emiAmount', $emiAmount); $stmt->bindParam(':emiDate', $emiDateFormatted); $stmt->bindParam(':totalAmount', $totalAmount); $stmt->bindParam(':outstanding', $outstandingAmount); $stmt->bindParam(':tenure', $tenure); $stmt->bindParam(':frequency', $frequency); $stmt->bindParam(':bookingDate', $bookingDate); $stmt->bindParam(':invoiceId', $invoiceId); $stmt->execute(); // Move to the next EMI date if (trim(strtolower($frequency)) === 'weekly') { $emiDate->modify('+1 week'); } elseif (trim(strtolower($frequency)) === 'monthly') { $emiDate->modify('+1 month'); } } // Insert into invoice table $stmt2 = $db->prepare("INSERT INTO invoice (customerId, invoiceId, customerName, address, frequency, invoiceDate, paymentMode, salesAgent, marketingAgent, tenure, item, description, qty, rate, tax, totalAmount, adminNote, taxAmount) VALUES (:customerId, :invoiceId, :customerName, :address, :frequency, :invoiceDate, :paymentMode, :salesAgent, :marketingAgent, :tenure, :item, :description, :qty, :rate, :tax, :totalAmount, :adminNote, :taxAmount)"); $stmt2->bindParam(':customerId', $_GET['customerId']); $stmt2->bindParam(':invoiceId', $invoiceId); $stmt2->bindParam(':customerName', $customerName); $stmt2->bindParam(':address', $customerAddress); $stmt2->bindParam(':frequency', $frequency); $stmt2->bindParam(':invoiceDate', $bookingDate); $stmt2->bindParam(':paymentMode', $paymentMode); $stmt2->bindParam(':salesAgent', $salesAgent); $stmt2->bindParam(':marketingAgent', $marketingAgent); $stmt2->bindParam(':tenure', $tenure); $stmt2->bindParam(':item', $item); $stmt2->bindParam(':description', $description); $stmt2->bindParam(':qty', $quantity); $stmt2->bindParam(':rate', $rate); $stmt2->bindParam(':tax', $tax); $stmt2->bindParam(':totalAmount', $totalAmount); $stmt2->bindParam(':adminNote', $adminNote); $stmt2->bindParam(':taxAmount', $taxAmount); $stmt2->execute(); echo '