beginTransaction(); // Insert into credit_sales $sql = "INSERT INTO credit_sales (bunk_id, customer_id, date, fuel_type_id, quantity, rate, amount) VALUES (:bunk_id, :customer_id, :date, :fuel_type_id, :quantity, :rate, :amount)"; $stmt = $pdoconn->prepare($sql); $stmt->execute([ ':bunk_id' => $bunk_id, ':customer_id' => $customer_id, ':date' => $date, ':fuel_type_id' => $fuel_type_id, ':quantity' => $quantity, ':rate' => $rate, ':amount' => $amount ]); // Update customer's outstanding balance $update_sql = "UPDATE credit_customers SET outstanding_balance = outstanding_balance + :amount WHERE id = :customer_id"; $update_stmt = $pdoconn->prepare($update_sql); $update_stmt->execute([':amount' => $amount, ':customer_id' => $customer_id]); $pdoconn->commit(); $success_message = "Credit sale recorded successfully!"; } catch (PDOException $e) { $pdoconn->rollBack(); $error_message = "Error recording sale: " . $e->getMessage(); } } else { $error_message = "All fields are required."; } } // Fetch master data for dropdowns try { $bunks_stmt = $pdoconn->query("SELECT id, name FROM bunks ORDER BY name"); $bunks = $bunks_stmt->fetchAll(PDO::FETCH_ASSOC); $customers_stmt = $pdoconn->query("SELECT id, name, bunk_id FROM credit_customers ORDER BY name"); $customers = $customers_stmt->fetchAll(PDO::FETCH_ASSOC); $fuel_types_stmt = $pdoconn->query("SELECT id, name FROM fuel_types ORDER BY name"); $fuel_types = $fuel_types_stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $bunks = []; $customers = []; $fuel_types = []; $page_error = "Error fetching master data: " . $e->getMessage(); } // Fetch recent credit sales try { $sales_stmt = $pdoconn->query(" SELECT cs.*, c.name as customer_name, b.name as bunk_name, ft.name as fuel_type_name FROM credit_sales cs JOIN credit_customers c ON cs.customer_id = c.id JOIN bunks b ON cs.bunk_id = b.id JOIN fuel_types ft ON cs.fuel_type_id = ft.id ORDER BY cs.date DESC, cs.id DESC LIMIT 20 "); $sales = $sales_stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $sales = []; $page_error = "Error fetching sales: " . $e->getMessage(); } ?>
| Date | Customer | Bunk | Fuel | Qty | Rate | Amount |
|---|---|---|---|---|---|---|
| No credit sales recorded yet. | ||||||
| .php echo htmlspecialchars(number_format($sale['rate'], 2)); ?> | ||||||