import{api as c}from"./common-DEIQtiPI.js";import"./index-DOFLUERq.js";function _(){const t=new Date,n=t.getFullYear(),a=String(t.getMonth()+1).padStart(2,"0"),e=String(t.getDate()).padStart(2,"0");return`${n}-${a}-${e}`}function f(){const t=new Date,n=String(t.getHours()).padStart(2,"0"),a=String(t.getMinutes()).padStart(2,"0"),e=String(t.getSeconds()).padStart(2,"0");return`${n}:${a}:${e}`}const u={pagetype:"Stock Entry",handler:t=>{t.posting_date||(t.posting_date=_()),t.posting_time||(t.posting_time=f()),(!t.items||t.items.length===0)&&(t.items=[{}])}};async function p(t){const{value:n,record:a}=t;if(n)try{const o=(await c.call("jingrow.client.get_value",{pagetype:"Stock Entry Type",filters:{name:n},fieldname:["purpose","add_to_transit"]}))?.message;o&&(o.purpose&&(a.purpose=o.purpose),o.add_to_transit!==void 0&&(a.add_to_transit=o.add_to_transit))}catch(e){console.error("[Stock Entry stock_entry_type plugin] Failed to fetch purpose:",e)}}function m(t){const{value:n,record:a}=t,e=a.items;!e||!Array.isArray(e)||e.forEach(o=>{o.s_warehouse||(o.s_warehouse=n)})}function h(t){const{value:n,record:a}=t,e=a.items;!e||!Array.isArray(e)||e.forEach(o=>{o.t_warehouse||(o.t_warehouse=n)})}async function y(t){const{value:n,record:a,parentDoc:e}=t;if(!n)return;const o=e||{},s=a;try{const r={...o,pagetype:"Stock Entry",__islocal:o.__islocal??!o.name},i=await c.call("run_pg_method",{pgs:JSON.stringify(r),method:"get_item_details",args:JSON.stringify({item_code:n,warehouse:s.s_warehouse||s.t_warehouse,transfer_qty:s.transfer_qty,serial_no:s.serial_no,batch_no:s.batch_no,bom_no:s.bom_no,expense_account:s.expense_account,cost_center:s.cost_center,company:o.company,qty:s.qty||1,voucher_type:"Stock Entry",voucher_no:s.name,allow_zero_valuation:1})});i?.message&&g(a,i.message)}catch(r){console.error("[Stock Entry item_code plugin] Failed to fetch item details:",r)}}function g(t,n){n.item_name&&(t.item_name=n.item_name),n.description&&(t.description=n.description),n.stock_uom&&(t.stock_uom=n.stock_uom),n.uom&&(t.uom=n.uom),n.conversion_factor&&(t.conversion_factor=n.conversion_factor),n.qty!==void 0&&(t.qty=n.qty),n.transfer_qty!==void 0&&(t.transfer_qty=n.transfer_qty),n.basic_rate!==void 0&&(t.basic_rate=n.basic_rate);const a=t.transfer_qty||t.qty||1,e=t.basic_rate||0;t.basic_amount=a*e,n.expense_account&&(t.expense_account=n.expense_account),n.cost_center&&(t.cost_center=n.cost_center),n.has_serial_no!==void 0&&(t.has_serial_no=n.has_serial_no),n.has_batch_no!==void 0&&(t.has_batch_no=n.has_batch_no)}const l=[{fieldname:"stock_entry_type",handler:p},{fieldname:"from_warehouse",handler:m},{fieldname:"to_warehouse",handler:h},{fieldname:"item_code",handler:y}],v=[u,...l];export{v as default};
