« Back to History
khata_entry.php
|
20260723_000646.php
Initial Domain Snapshot
Copy Code
<?php declare(strict_types=1); require_once __DIR__ . '/auth.php'; require_login(); error_reporting(E_ALL); ini_set('display_errors', '1'); $SHOP_ID = (int)($_SESSION['owner']['shop_id'] ?? 0); if ($SHOP_ID <= 0) { die('Invalid shop session.'); } require_once __DIR__ . '/partials/header.php'; /* ====================== CONFIG CHECK ====================== */ $cfg = $pdo->prepare(" SELECT khata_enabled FROM system_config WHERE shop_id = ? "); $cfg->execute([$SHOP_ID]); if (!(int)$cfg->fetchColumn()) { die('Khata module disabled.'); } $error = ''; $success = ''; /* ====================== FETCH PARTIES ====================== */ $pStmt = $pdo->prepare(" SELECT party_id, party_name FROM khata_party WHERE shop_id = ? ORDER BY party_name "); $pStmt->execute([$SHOP_ID]); $parties = $pStmt->fetchAll(PDO::FETCH_ASSOC); /* ====================== ADD ENTRY (DIYA / LIYA) ====================== */ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_entry'])) { $party_id = (int)($_POST['party_id'] ?? 0); $type = $_POST['entry_type'] ?? ''; $amount = (float)($_POST['amount'] ?? 0); $entry_date = $_POST['entry_date'] ?? ''; $note = trim($_POST['note'] ?? ''); if ( $party_id <= 0 || !in_array($type, ['DIYA', 'LIYA'], true) || $amount <= 0 || !$entry_date ) { $error = 'Invalid entry details.'; } else { // Diya = + , Liya = - $signed_amount = ($type === 'DIYA') ? $amount : -$amount; $pdo->prepare(" INSERT INTO khata_entry (shop_id, party_id, entry_date, entry_type, amount, note) VALUES (?, ?, ?, ?, ?, ?) ")->execute([ $SHOP_ID, $party_id, $entry_date, $type, $signed_amount, $note ]); $success = 'Khata entry added successfully.'; } } /* ====================== FETCH ENTRIES ====================== */ $eStmt = $pdo->prepare(" SELECT e.entry_id, p.party_name, e.entry_type, e.amount, e.note, e.entry_date FROM khata_entry e JOIN khata_party p ON p.party_id = e.party_id WHERE e.shop_id = ? ORDER BY e.entry_date DESC, e.entry_id DESC "); $eStmt->execute([$SHOP_ID]); $entries = $eStmt->fetchAll(PDO::FETCH_ASSOC); ?> <h2>Khata Entry</h2> <div class="container"> <!-- ====================== ADD ENTRY FORM ====================== --> <div class="card"> <?php if ($error): ?> <div class="alert alert-danger"><?=h($error)?></div> <?php endif; ?> <?php if ($success): ?> <div class="alert alert-success"><?=h($success)?></div> <?php endif; ?> <form method="post"> <input type="hidden" name="add_entry" value="1"> <div class="form-group"> <label>Party</label> <select name="party_id" required> <option value="">Select Party</option> <?php foreach ($parties as $p): ?> <option value="<?=$p['party_id']?>"><?=h($p['party_name'])?></option> <?php endforeach; ?> </select> </div> <div class="form-group"> <label>Date</label> <input type="date" name="entry_date" required> </div> <div class="form-group"> <label>Type</label> <select name="entry_type" required> <option value="">Select</option> <option value="DIYA">Diya (You Gave)</option> <option value="LIYA">Liya (You Received)</option> </select> </div> <div class="form-group"> <label>Amount</label> <input type="number" name="amount" step="0.01" min="0.01" required> </div> <div class="form-group"> <label>Note</label> <input type="text" name="note" placeholder="Optional note"> </div> <button class="btn btn-primary">Save Entry</button> </form> </div> <!-- ====================== ENTRY LIST ====================== --> <div class="card"> <h3>Khata Entries</h3> <?php if (!$entries): ?> <p class="text-muted">No entries found.</p> <?php else: ?> <table class="table"> <thead> <tr> <th>Party</th> <th>Date</th> <th>Type</th> <th class="text-right">Amount</th> <th>Note</th> <th>Action</th> </tr> </thead> <tbody> <?php foreach ($entries as $e): ?> <tr> <td><?=h($e['party_name'])?></td> <td><?=h($e['entry_date'])?></td> <td><?=h($e['entry_type'])?></td> <td class="text-right"><?=number_format((float)$e['amount'], 2)?></td> <td><?=h($e['note'])?></td> <td> <form method="post" action="khata_delete.php" onsubmit="return confirm('Kya aap is khata entry ko delete karna chahte ho?');"> <input type="hidden" name="entry_id" value="<?=$e['entry_id']?>"> <button class="btn btn-danger btn-sm">Delete</button> </form> </td> </tr> <?php endforeach; ?> </tbody> </table> <?php endif; ?> </div> </div> <?php require_once __DIR__ . '/partials/footer.php';