« Back to History
process_jobwork.php
|
20260721_154032.php
Initial Bulk Import
Copy Code
<?php // 1. Load ERP Context require_once __DIR__ . '/../modules/auth/page_acl.php'; $ctx = page_require_access('jobwork_send'); $pdo = $ctx['pdo']; $company_id = $ctx['company_id']; if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_challan'])) { $challan_no = $_POST['challan_no']; $challan_date = $_POST['challan_date']; $party_name = $_POST['party_name']; $items = $_POST['items'] ?? []; try { $pdo->beginTransaction(); // 2. Delete existing records for this challan (Delete-then-Insert pattern) $del_send = $pdo->prepare("DELETE FROM jobwork_send WHERE challan_no = ? AND company_id = ?"); $del_send->execute([$challan_no, $company_id]); $del_header = $pdo->prepare("DELETE FROM chalan_data WHERE challan_no = ? AND company_id = ?"); $del_header->execute([$challan_no, $company_id]); // 3. Insert into chalan_data (Header) $ins_header = $pdo->prepare("INSERT INTO chalan_data (company_id, challan_no, challan_date, party_name, items_json) VALUES (?, ?, ?, ?, ?)"); $ins_header->execute([$company_id, $challan_no, $challan_date, $party_name, json_encode($items)]); // 4. Insert into jobwork_send (Rows) $ins_send = $pdo->prepare(" INSERT INTO jobwork_send (company_id, challan_no, party_name, entry_date, stock_type, yarn_type, company, denier, color, boxes, weight) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "); foreach ($items as $item) { if (empty($item['yarn_type'])) continue; $ins_send->execute([ $company_id, $challan_no, $party_name, $challan_date, $item['stock_type'], $item['yarn_type'], $item['company'], $item['denier'], $item['color'], $item['boxes'], $item['weight'] ]); } // 5. Activity Tracker (per item, like beam_sent_entry) $user_id = $ctx['user']['id'] ?? null; $ip_address = $_SERVER['REMOTE_ADDR'] ?? ''; $user_agent = $_SERVER['HTTP_USER_AGENT'] ?? ''; $activity_stmt = $pdo->prepare("INSERT INTO user_activity_logs ( company_id, user_id, functional_role_id, activity_type, module_name, page_slug, action_name, reference_id, activity_note, ip_address, user_agent, created_at ) VALUES ( :company_id, :user_id, NULL, :activity_type, :module_name, :page_slug, :action_name, :reference_id, :activity_note, :ip_address, :user_agent, NOW() )"); foreach ($items as $item) { if (empty($item['yarn_type'])) continue; $activity_stmt->execute([ ':company_id' => $company_id, ':user_id' => $user_id, ':activity_type' => 'jobwork_send', ':module_name' => 'jobwork_send_entry', ':page_slug' => 'jobwork_send_entry', ':action_name' => 'save_challan', ':reference_id' => $item['yarn_type'], ':activity_note' => "Challan saved | Party: $party_name | Challan No: $challan_no | Yarn: {$item['yarn_type']} | Company: {$item['company']} | Boxes: {$item['boxes']} | Weight: {$item['weight']}", ':ip_address' => $ip_address, ':user_agent' => $user_agent, ]); } $pdo->commit(); echo "<script>alert('Challan saved successfully!'); window.location='jobwork_send_entry.php';</script>"; } catch (Exception $e) { $pdo->rollBack(); die("Fatal Error: " . $e->getMessage()); } } else { header("Location: jobwork_send_entry.php"); }