« Back to History
va_fetch_challan.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php /* ====================================================================== FILE: /erp/va_fetch_challan.php ====================================================================== */ require_once __DIR__ . '/modules/auth/page_acl.php'; // Context access check $ctx = page_require_access('va_receive_entry'); $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; $challan = $_GET['challan'] ?? ''; if (!$challan) { echo json_encode(['status' => 'error', 'message' => 'No challan provided']); exit; } /** * SMART LOOKUP: * 1. Hum pehle 'va_send_entry' se wo latest Financial Year nikalenge jisme ye challan hai. * 2. Phir usi FY ke base par total sent aur total received calculate karenge. */ // Step 1: Find the Latest Sent Entry for this challan $s = $pdo->prepare(" SELECT MAX(party_name) as party, financial_year, SUM(pcs) as total_sent FROM va_send_entry WHERE challan_no = :ch AND company_id = :cid GROUP BY financial_year ORDER BY financial_year DESC LIMIT 1 "); $s->execute([':ch' => $challan, ':cid' => $company_id]); $sd = $s->fetch(PDO::FETCH_ASSOC); if (!$sd || !$sd['total_sent']) { echo json_encode(['status' => 'not_found']); exit; } $latest_fy = $sd['financial_year']; $total_sent = (int)$sd['total_sent']; // Step 2: Calculate already received pieces ONLY for this FY $r = $pdo->prepare(" SELECT SUM(total_pcs) FROM va_receive_entry WHERE challan_no = :ch AND company_id = :cid AND financial_year = :fy "); $r->execute([ ':ch' => $challan, ':cid' => $company_id, ':fy' => $latest_fy ]); $already_received = (int)$r->fetchColumn(); // Step 3: Response echo json_encode([ 'status' => 'ok', 'party' => $sd['party'], 'fy' => $latest_fy, 'total_sent' => $total_sent, 'pending' => $total_sent - $already_received ]); exit;