« Back to History
yarn_out_boxwise_manual.php
|
20260721_154034.php
Initial Bulk Import
Copy Code
<?php $page_title = 'Yarn OUT – Filter Based'; /* ================= AUTH ================= */ require_once __DIR__.'/modules/auth/page_acl.php'; require_once __DIR__ . '/modules/activity/activity_logger.php'; $ctx = page_require_access('yarn_out_boxwise_manual'); $pdo = $ctx['pdo']; $cid = (int)$ctx['company_id']; $user = $ctx['user']; function h($s){ return htmlspecialchars((string)$s,ENT_QUOTES,'UTF-8'); } /* ================= AJAX : FILTER OPTIONS ================= */ if (isset($_GET['ajax']) && $_GET['ajax']==='options') { $level = $_GET['level'] ?? ''; $where = ['company_id=:cid','status="IN"']; $p = [':cid'=>$cid]; if (!empty($_GET['material'])) { $where[]='material=:m'; $p[':m']=$_GET['material']; } if (!empty($_GET['yarn_type'])) { $where[]='yarn_type=:yt'; $p[':yt']=$_GET['yarn_type']; } if (!empty($_GET['party_company'])) { $where[]='party_company=:pc'; $p[':pc']=$_GET['party_company']; } if (!empty($_GET['denier'])) { $where[]='denier=:d'; $p[':d']=$_GET['denier']; } if (!empty($_GET['color'])) { $where[]='color=:c'; $p[':c']=$_GET['color']; } $map = [ 'material' => 'material', 'yarn_type' => 'yarn_type', 'company' => 'party_company', 'denier' => 'denier', 'color' => 'color', 'box' => 'box_no' ]; if (!isset($map[$level])) exit; $sql = "SELECT DISTINCT {$map[$level]} v FROM yarn_box_stock WHERE ".implode(' AND ',$where)." ORDER BY v"; $st = $pdo->prepare($sql); $st->execute($p); echo json_encode(['ok'=>true,'data'=>$st->fetchAll(PDO::FETCH_COLUMN)]); exit; } /* ================= 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; } /* ================= AJAX : HISTORY ================= */ if (isset($_GET['ajax']) && $_GET['ajax']==='history') { $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 50 "); $st->execute([$cid]); echo json_encode(['ok'=>true,'rows'=>$st->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 { $last_out_id = 0; 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 ]); $last_out_id = (int)$pdo->lastInsertId(); $pdo->prepare(" UPDATE yarn_box_stock SET weight=0,status='OUT',updated_at=NOW() WHERE id=? ")->execute([$box['id']]); } $pdo->commit(); activity_log([ 'company_id' => $cid ?? 0, 'user_id' => $user['id'] ?? ($_SESSION['user_id'] ?? 0), 'module' => 'yarn', 'action_name' => 'create', 'entity_type' => 'yarn_out_boxwise', 'entity_id' => $last_out_id ?? 0, 'remarks' => 'Yarn out entry created' ]); $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>Material</label><select id="f_material"></select></div> <div class="form-group"><label>Yarn Type</label><select id="f_yarn_type"></select></div> <div class="form-group"><label>Company</label><select id="f_company"></select></div> <div class="form-group"><label>Denier</label><select id="f_denier"></select></div> <div class="form-group"><label>Color</label><select id="f_color"></select></div> <div class="form-group"><label>Box</label><select id="f_box"></select></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 class="form-group"><label>Show Entry</label><button type="button" class="btn ghost" id="btnHistory">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 id="historyArea"></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 load(level){ const p=new URLSearchParams({ ajax:'options',level, material:$('#f_material').value, yarn_type:$('#f_yarn_type').value, party_company:$('#f_company').value, denier:$('#f_denier').value, color:$('#f_color').value }); fetch('?'+p).then(r=>r.json()).then(j=>{ const m={ material:'#f_material',yarn_type:'#f_yarn_type', company:'#f_company',denier:'#f_denier', color:'#f_color',box:'#f_box' }; const el=$(m[level]); el.innerHTML='<option></option>'; j.data.forEach(v=>el.add(new Option(v,v))); }); } 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 || rows.some(x=>x.box_no==j.row.box_no)) return; rows.push(j.row); draw(); }); } $('#f_box').onchange=()=>addBox($('#f_box').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; }); }; $('#btnHistory').onclick=()=>{ fetch('?ajax=history').then(r=>r.json()).then(j=>{ if(!j.ok) return; let h=`<h4>OUT Entry History</h4> <table class="table erp-table"> <tr><th>Date</th><th>Assign</th><th>Boxes</th><th>Weight</th></tr>`; j.rows.forEach(r=>{ h+=`<tr><td>${r.out_date}</td><td>${r.assign_to}</td><td>${r.boxes}</td><td>${r.weight}</td></tr>`; }); h+='</table>'; $('#historyArea').innerHTML=h; }); }; load('material'); $('#f_material').onchange = ()=>load('yarn_type'); $('#f_yarn_type').onchange = ()=>load('company'); $('#f_company').onchange = ()=>load('denier'); $('#f_denier').onchange = ()=>load('color'); $('#f_color').onchange = ()=>load('box'); </script>