« Back to History
yarn_out_boxwise.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php $page_title = 'Yarn OUT – Box Wise'; /* ================= AUTH ================= */ require_once __DIR__.'/modules/auth/page_acl.php'; $ctx = page_require_access('yarn_out_boxwise'); $pdo = $ctx['pdo']; $cid = (int)$ctx['company_id']; $user = $ctx['user']; function h($s){ return htmlspecialchars((string)$s,ENT_QUOTES,'UTF-8'); } /* ================= AJAX : BOX DETAIL ================= */ if (isset($_GET['ajax']) && $_GET['ajax']==='box_detail') { $box = (int)($_GET['box_no'] ?? 0); $st = $pdo->prepare(" SELECT * FROM yarn_box_stock WHERE company_id=? AND box_no=? AND status='IN' "); $st->execute([$cid,$box]); $r = $st->fetch(PDO::FETCH_ASSOC); echo json_encode(['ok'=>(bool)$r,'row'=>$r]); exit; } /* ================= AJAX : STOCK ================= */ if (isset($_GET['ajax']) && $_GET['ajax']==='stock') { $st = $pdo->prepare(" SELECT COUNT(*) boxes, COALESCE(SUM(weight),0) weight FROM yarn_box_stock WHERE company_id=? AND status='IN' "); $st->execute([$cid]); $cur = $st->fetch(PDO::FETCH_ASSOC); $rows = json_decode($_GET['rows'] ?? '[]', true); $outWt = 0; foreach($rows as $r){ $outWt += (float)$r['weight']; } echo json_encode([ 'ok'=>true, 'current'=>$cur, 'future'=>[ 'boxes'=>max(0,$cur['boxes']-count($rows)), 'weight'=>max(0,$cur['weight']-$outWt) ] ]); exit; } /* ================= AJAX : LAST ENTRY ================= */ if (isset($_GET['ajax']) && $_GET['ajax']==='last') { $st = $pdo->prepare(" SELECT out_date, assign_to, COUNT(*) boxes, SUM(weight) weight FROM yarn_out_boxwise WHERE company_id=? GROUP BY out_date,assign_to ORDER BY out_date DESC LIMIT 1 "); $st->execute([$cid]); $last = $st->fetch(PDO::FETCH_ASSOC); if(!$last){ echo json_encode(['ok'=>false]); exit; } $it = $pdo->prepare(" SELECT box_no, weight FROM yarn_out_boxwise WHERE company_id=? AND out_date=? "); $it->execute([$cid,$last['out_date']]); echo json_encode(['ok'=>true,'summary'=>$last,'items'=>$it->fetchAll(PDO::FETCH_ASSOC)]); exit; } /* ================= SAVE OUT ================= */ $flash = null; if ($_SERVER['REQUEST_METHOD']==='POST') { $rows = json_decode($_POST['box_json'] ?? '', true); $assign = trim($_POST['assign_to'] ?? ''); $date = $_POST['out_date'] ?? ''; if (!$rows || !$assign || !$date) { $flash=['type'=>'error','msg'=>'Invalid data']; } else { $pdo->beginTransaction(); try { foreach ($rows as $r) { $boxNo = (int)$r['box_no']; $st = $pdo->prepare(" SELECT * FROM yarn_box_stock WHERE company_id=? AND box_no=? AND status='IN' FOR UPDATE "); $st->execute([$cid,$boxNo]); $box = $st->fetch(PDO::FETCH_ASSOC); if(!$box) throw new Exception("Box {$boxNo} available nahi hai"); $pdo->prepare(" INSERT INTO yarn_out_boxwise (company_id,out_date,material,box_no,weight, yarn_type,party_company,denier,color,assign_to) VALUES (?,?,?,?,?,?,?,?,?,?) ")->execute([ $cid,$date,$box['material'],$box['box_no'],$box['weight'], $box['yarn_type'],$box['party_company'], $box['denier'],$box['color'],$assign ]); $pdo->prepare(" UPDATE yarn_box_stock SET weight=0,status='OUT',updated_at=NOW() WHERE id=? ")->execute([$box['id']]); } $pdo->commit(); $flash=['type'=>'success','msg'=>'Yarn OUT saved']; } catch(Throwable $e){ $pdo->rollBack(); $flash=['type'=>'error','msg'=>$e->getMessage()]; } } } ?> <?php require_once __DIR__.'/partials/header.php'; ?> <div class="form-card"> <?php if($flash): ?> <div class="alert <?=$flash['type']?>"><?=$flash['msg']?></div> <?php endif; ?> <form method="post" id="outForm" autocomplete="off"> <input type="hidden" name="box_json" id="box_json"> <div class="form-cols-4"> <div class="form-group"> <label>Date</label> <input type="date" name="out_date" value="<?=date('Y-m-d')?>"> </div> <div class="form-group"> <label>Box No</label> <input type="number" id="box_input" placeholder="Type box & Enter"> </div> <div class="form-group"> <label>Assign</label> <select name="assign_to" required> <option value="">— Select —</option> <?php $a=$pdo->prepare("SELECT name FROM yarn_assigne WHERE company_id=? ORDER BY name"); $a->execute([$cid]); foreach($a as $r) echo '<option>'.h($r['name']).'</option>'; ?> </select> </div> </div> <div class="form-cols-4"> <div class="form-group"><label>Current Stock</label><div id="curStock">—</div></div> <div class="form-group"><label>Future Stock</label><div id="futStock">—</div></div> <div class="form-group"><label>Last Entry</label> <button type="button" class="btn ghost" id="btnLast">View</button> </div> </div> <table class="table erp-table" id="boxTable"> <thead> <tr> <th>Box</th><th>Material</th><th>Yarn</th> <th>Company</th><th>Denier</th><th>Color</th><th>Weight</th><th></th> </tr> </thead> <tbody></tbody> </table> <button class="btn primary">Save OUT</button> </form> <div id="popup"></div> </div> <?php require_once __DIR__.'/partials/footer.php'; ?> <script> const $=s=>document.querySelector(s); let rows=[]; $('#outForm').onkeydown=e=>{ if(e.key==='Enter') e.preventDefault(); }; function loadStock(){ fetch('?ajax=stock&rows='+encodeURIComponent(JSON.stringify(rows))) .then(r=>r.json()).then(j=>{ if(!j.ok) return; $('#curStock').innerHTML=`Boxes: ${j.current.boxes}<br>Weight: ${j.current.weight}`; $('#futStock').innerHTML=`Boxes: ${j.future.boxes}<br>Weight: ${j.future.weight}`; }); } function addBox(box){ fetch('?ajax=box_detail&box_no='+box) .then(r=>r.json()) .then(j=>{ if(!j.ok){ alert('Box available nahi hai'); return; } if(rows.some(x=>x.box_no==j.row.box_no)){ alert('Box already added'); return; } rows.push(j.row); draw(); }); } $('#box_input').onkeydown=e=>{ if(e.key==='Enter'){ e.preventDefault(); addBox(e.target.value); e.target.value=''; } }; function draw(){ const tb=$('#boxTable tbody'); tb.innerHTML=''; rows.forEach((r,i)=>{ tb.insertAdjacentHTML('beforeend',` <tr> <td>${r.box_no}</td> <td>${r.material}</td> <td>${r.yarn_type}</td> <td>${r.party_company}</td> <td>${r.denier}</td> <td>${r.color}</td> <td>${r.weight}</td> <td><button type="button" onclick="rows.splice(${i},1);draw()">×</button></td> </tr> `); }); $('#box_json').value=JSON.stringify(rows.map(r=>({box_no:r.box_no,weight:r.weight}))); loadStock(); } $('#btnLast').onclick=()=>{ fetch('?ajax=last').then(r=>r.json()).then(j=>{ if(!j.ok) return; let h=`<h4>Last Entry</h4> Boxes:${j.summary.boxes} Weight:${j.summary.weight} <table class="table"><tr><th>Box</th><th>Weight</th></tr>`; j.items.forEach(b=>h+=`<tr><td>${b.box_no}</td><td>${b.weight}</td></tr>`); h+='</table>'; $('#popup').innerHTML=h; }); }; </script>