UNPKG

nervatura-client

Version:
1 lines 61.6 kB
import{b as e,T as t,M as r,d as a,E as i,j as n,S as s,a as o}from"./main-DXT0hpEA.js";import"./module-FRmUNWHB.js";import"./module-CC7fmSsS.js";import"./module-C1fQGCtD.js";const d=(e,t,r)=>{const a={customer:t===r.custtype,place:t===r.placetype,product:t===r.protype,tool:t===r.toolgroup,trans:t===r.transtype};return null===t||(void 0===a[e]||a[e])},l=(e,t)=>Math.round(e*10**t)/10**t;class u{constructor(e){this.host=e,this.app=e.app,this.store=e.app.store,this.module={},this.addPrintQueue=this.addPrintQueue.bind(this),this.calcFormula=this.calcFormula.bind(this),this.calcPrice=this.calcPrice.bind(this),this.checkEditor=this.checkEditor.bind(this),this.checkTranstype=this.checkTranstype.bind(this),this.createReport=this.createReport.bind(this),this.createShipping=this.createShipping.bind(this),this.createTrans=this.createTrans.bind(this),this.createTransOptions=this.createTransOptions.bind(this),this.deleteEditor=this.deleteEditor.bind(this),this.deleteEditorItem=this.deleteEditorItem.bind(this),this.editItem=this.editItem.bind(this),this.exportEvent=this.exportEvent.bind(this),this.exportQueueAll=this.exportQueueAll.bind(this),this.exportQueue=this.exportQueue.bind(this),this.getTransFilter=this.getTransFilter.bind(this),this.loadEditor=this.loadEditor.bind(this),this.newFieldvalue=this.newFieldvalue.bind(this),this.nextTransNumber=this.nextTransNumber.bind(this),this.onEditEvent=this.onEditEvent.bind(this),this.onSelector=this.onSelector.bind(this),this.onSideEvent=this.onSideEvent.bind(this),this.prevTransNumber=this.prevTransNumber.bind(this),this.reportOutput=this.reportOutput.bind(this),this.reportPath=this.reportPath.bind(this),this.reportSettings=this.reportSettings.bind(this),this.saveEditor=this.saveEditor.bind(this),this.saveEditorForm=this.saveEditorForm.bind(this),this.searchQueue=this.searchQueue.bind(this),this.setEditor=this.setEditor.bind(this),this.setEditorItem=this.setEditorItem.bind(this),this.setFieldvalue=this.setFieldvalue.bind(this),this.setFormActions=this.setFormActions.bind(this),this.setModule=this.setModule.bind(this),this.setLink=this.setLink.bind(this),this.setPassword=this.setPassword.bind(this),this.setPattern=this.setPattern.bind(this),this.shippingAddAll=this.shippingAddAll.bind(this),this.showStock=this.showStock.bind(this),this.tableValues=this.tableValues.bind(this),this.transCopy=this.transCopy.bind(this),e.addController(this)}setModule(e){this.module=e}async addPrintQueue(r,a){const{requestData:i,resultError:n,showToast:s,msg:o}=this.app,{current:d,dataset:l}=this.store.data[e.EDIT],u=this.store.data[e.LOGIN].data,c=l.report.filter((e=>e.reportkey===r))[0],p={method:"POST",data:[{nervatype:u.groups.filter((e=>"nervatype"===e.groupname&&e.groupvalue===d.type))[0].id,ref_id:d.item.id,qty:parseInt(a,10),employee_id:u.employee.id,report_id:c.id}]},m=await i("/ui_printqueue",p);return m.error?n(m):(s(t.SUCCESS,o("",{id:"report_add_groups"})),!0)}calcFormula(t){const{inputBox:i}=this.host,{setData:n,data:s}=this.store,{sql:o,initItem:d}=this.app.modules,{getSql:u,requestData:c,resultError:p,msg:m}=this.app,h=i({title:m("",{id:"msg_warning"}),message:m("",{id:"ms_load_formula"}),infoText:`${m("",{id:"msg_delete_info"})} ${m("",{id:"ms_continue_warning"})}`,defaultOK:!0,onEvent:{onModalEvent:async a=>{if(n("current",{modalForm:null}),a.key===r.OK){const r={method:"POST",data:[{key:"formula",text:u(s[e.LOGIN].data.engine,o.trans.formula_items()).sql,values:[t]}]},a=await c("/view",r);if(a.error)return p(a);const i=s[e.EDIT].dataset.movement_head[0].qty,n=s[e.EDIT].dataset.movement_head[0].place_id,m=s[e.EDIT].dataset.formula_head.filter((e=>e.id===t))[0].qty;let h=[];a.formula.forEach((t=>{const r={...d({tablename:"movement",dataset:s[e.EDIT].dataset,current:s[e.EDIT].current}),product_id:t.product_id,place_id:null===t.place_id?n:t.place_id,qty:1===t.shared?-Math.ceil(i/m):-l(i/m*t.qty,2)};h=[...h,r]}));for(let t=0;t<s[e.EDIT].dataset.movement.length;t+=1){const e=await c("/movement",{method:"DELETE",query:{id:s.edit.dataset.movement[t].id}});if(e&&e.error)return p(e)}const f=await c("/movement",{method:"POST",data:h});if(f.error)return p(f);this.loadEditor({ntype:s[e.EDIT].current.type,ttype:s[e.EDIT].current.transtype,id:s[e.EDIT].current.item.id,form:"movement"})}return!0}}});return n("current",{modalForm:h,side:a.HIDE})}calcPrice(t,r){const{data:a}=this.store;let i=a[e.EDIT].dataset.tax.filter((e=>e.id===parseInt(r.tax_id,10)))[0];i=void 0!==i?i.rate:0;let n=a[e.EDIT].dataset.currency.filter((t=>t.curr===a[e.EDIT].current.item.curr))[0];n=void 0!==n?n.digit:2;let s=0,o=0,d=0,u=0;switch(t){case"netamount":s=parseFloat(r.netamount),0!==parseFloat(r.qty)&&(u=l(s/(1-parseFloat(r.discount)/100)/parseFloat(r.qty),parseInt(n,10)),o=l(s*parseFloat(i),parseInt(n,10))),d=l(s+o,parseInt(n,10));break;case"amount":d=parseFloat(r.amount),0!==parseFloat(r.qty)&&(s=l(d/(1+parseFloat(i)),parseInt(n,10)),o=l(d-s,parseInt(n,10)),u=l(s/(1-parseFloat(r.discount)/100)/parseFloat(r.qty),parseInt(n,10)));break;default:u=parseFloat(r.fxprice),s=l(u*(1-parseFloat(r.discount)/100)*parseFloat(r.qty),parseInt(n,10)),o=l(u*(1-parseFloat(r.discount)/100)*parseFloat(r.qty)*parseFloat(i),parseInt(n,10)),d=l(s+o,parseInt(n,10))}return{...r,fxprice:u,netamount:s,vatamount:o,amount:d}}checkEditor(t,n){const{modalFormula:s}=this.module,{inputBox:o}=this.host,{msg:d}=this.app,{setData:l,data:u}=this.store,c=n=>{switch(n){case i.LOAD_EDITOR:this.loadEditor(t);break;case i.SET_EDITOR_ITEM:this.setEditorItem(t);break;case i.LOAD_FORMULA:const n=s({formula:t.formula,partnumber:u[e.EDIT].dataset.movement_head[0].partnumber,description:u[e.EDIT].dataset.movement_head[0].description,formulaValues:u[e.EDIT].dataset.formula_head.map((e=>({value:String(e.id),text:e.transnumber}))),onEvent:{onModalEvent:async e=>{l("current",{modalForm:null}),e.key===r.OK&&this.calcFormula(e.data.value)}}});l("current",{modalForm:n,side:a.HIDE});break;case i.NEW_FIELDVALUE:this.newFieldvalue(t.fieldname);break;case i.CREATE_TRANS:this.createTrans(t);break;case i.CREATE_TRANS_OPTIONS:this.createTransOptions();break;case i.FORM_ACTION:this.setFormActions(t)}};if(!0===u[e.EDIT].dirty&&u[e.EDIT].current.item||!0===u[e.EDIT].form_dirty&&u[e.EDIT].current.form){const t=o({title:d("",{id:"msg_warning"}),message:d("",{id:"msg_dirty_text"}),infoText:d("",{id:"msg_delete_info"}),defaultOK:!0,onEvent:{onModalEvent:async t=>{if(l("current",{modalForm:null}),t.key===r.OK){const t=u[e.EDIT].form_dirty?await this.saveEditorForm():await this.saveEditor();t&&(l(e.EDIT,t),c(n))}else c(n)}}});return l("current",{modalForm:t,side:a.HIDE})}return c(n)}async checkTranstype(t,r){const{requestData:a,resultError:i,getSql:n}=this.app,s=this.store.data[e.LOGIN].data;if(("trans"===t.ntype||"transitem"===t.ntype||"transmovement"===t.ntype||"transpayment"===t.ntype)&&null===t.ttype){const e={method:"POST",data:[{key:"transtype",text:n(s.engine,{select:["groupvalue"],from:"groups g",inner_join:["trans t","on",[["g.id","=","t.transtype"],["and","t.id","=","?"]]]}).sql,values:[t.id]}]},o=await a("/view",e);return o.error?i(o):this.checkEditor({...t,ntype:"trans",ttype:o.transtype[0].groupvalue,id:t.id},r)}return this.checkEditor(t,r)}async createReport(t){let r=t;const{requestData:a,resultError:i,saveToDisk:n}=this.app,{current:s}=this.store.data[e.EDIT];let o=[];s.fieldvalue.forEach((e=>{e.selected&&(o=[...o,`filters[${e.name}]=${e.value}`])}));const d=s.item,l={type:"xml"===r?"xml":"auto",ctype:"xml"===r?"application/xml; charset=UTF-8":"csv"===r?"text/csv; charset=UTF-8":"application/pdf",template:d.reportkey,title:d.reportkey,orient:d.orientation,size:d.size,filters:o.join("&")},u=await a(this.reportPath(l),{});if(u&&u.error)return i(u);let c;if("csv"===r){const e=new Blob([u],{type:"text/csv;charset=utf-8;"});c=URL.createObjectURL(e),r="csv"}else c=URL.createObjectURL(u,{type:l.ctype});if("print"===r)return window.open(c,"_blank");let p=`${l.title}_${(new Date).toISOString().split("T")[0]}.${r}`;return p=p.split("/").join("_"),n(c,p)}async createShipping(){const{initItem:r}=this.app.modules,{requestData:a,resultError:i,showToast:n,msg:s,createHistory:o}=this.app,{dataset:d,current:l}=this.store.data[e.EDIT];if(null===l.shipping_place_id)return n(t.ERROR,`${s("",{id:"msg_required"})} ${s("",{id:"inventory_warehouse"})}`);if(d.shiptemp.length>0){let e={...r({tablename:"trans",dataset:d,current:l}),transtype:d.groups.filter((e=>"transtype"===e.groupname&&"delivery"===e.groupvalue))[0].id,direction:d[l.type][0].direction,transdate:new Date(l.item.shippingdate).toISOString().split("T")[0],duedate:null,curr:null,paidtype:null};const t=d.delivery_pattern.filter((e=>1===e.defpattern))[0];void 0!==t&&(e={...e,fnote:t.notes});const n={method:"POST",data:{key:"nextNumber",values:{numberkey:`delivery_${l.direction}`,step:!0}}};let s=await a("/function",n);if(s.error)return i(s),null;if(e={...e,transnumber:s},s=await a("/trans",{method:"POST",data:[e]}),s.error)return i(s),null;e.id=s[0],await o("save");let u=[];if(d.shiptemp.forEach((t=>{const a={...r({tablename:"movement",dataset:d,current:l}),trans_id:e.id,shippingdate:`${new Date(l.shippingdate).toISOString().slice(0,10)}T${new Date(l.shippingdate).toLocaleTimeString("en",{hour12:!1}).replace("24","00")}`,product_id:t.product_id,place_id:l.shipping_place_id,notes:t.batch_no,qty:"out"===l.direction?-t.qty:t.qty};u=[...u,a]})),s=await a("/movement",{method:"POST",data:u}),s.error)return i(s),null;let c=[];const p=d.groups.filter((e=>"nervatype"===e.groupname&&"movement"===e.groupvalue))[0].id,m=d.groups.filter((e=>"nervatype"===e.groupname&&"item"===e.groupvalue))[0].id;return s.forEach(((e,t)=>{const a={...r({tablename:"link",dataset:d,current:l}),nervatype_1:p,ref_id_1:e,nervatype_2:m,ref_id_2:d.shiptemp[t].item_id};c=[...c,a]})),s=await a("/link",{method:"POST",data:c}),s.error?(i(s),null):this.loadEditor({ntype:l.type,ttype:l.transtype,id:l.item.id,shipping:!0})}return!0}async createTrans(r){const{data:a}=this.store,{initItem:i}=this.app.modules,{resultError:n,showToast:s,requestData:o,createHistory:u,msg:c}=this.app,p={...a[e.EDIT].dataset.trans[0]};let m=a[e.EDIT].dataset.groups.filter((e=>e.id===p.transtype))[0].groupvalue,h=p.transtype,f=a[e.EDIT].dataset.groups.filter((e=>e.id===p.direction))[0].groupvalue,_=p.direction;if(void 0!==r.new_transtype&&void 0!==r.new_direction){m=r.new_transtype;const i=a[e.LOGIN].data.audit.filter((e=>"trans"===e.nervatypeName&&e.subtypeName===m))[0];if(void 0!==i&&"disabled"===i.inputfilterName)return s(t.INFO,`${c("",{id:"msg_editor_invalid"})} ${m}`),!1;h=a[e.EDIT].dataset.groups.filter((e=>"transtype"===e.groupname&&e.groupvalue===m))[0].id,f=r.new_direction,_=a[e.EDIT].dataset.groups.filter((e=>"direction"===e.groupname&&e.groupvalue===f))[0].id}if(("receipt"===m||"worksheet"===m)&&"in"===f)return s(t.INFO,`${c("",{id:"msg_input_invalid"})} in`),!1;if("cancellation"===p.transcast)return s(t.INFO,c("",{id:"msg_create_cancellation_err1"})),!1;if("cancellation"===r.transcast&&["invoice","receipt"].includes(m)&&0===p.deleted)return s(t.INFO,c("",{id:"msg_create_cancellation_err2"})),!1;if("cancellation"===r.transcast&&a[e.EDIT].dataset.cancel_link.length>0)return s(t.INFO,`${c("",{id:"msg_create_cancellation_err3"})} ${a[e.EDIT].dataset.cancel_link[0].transnumber}`),!1;if("amendment"===r.transcast&&1===p.deleted)return s(t.INFO,c("",{id:"msg_create_amendment_err"})),!1;const y={id:null,transtype:h,transnumber:null,crdate:(new Date).toISOString().split("T")[0],transdate:(new Date).toISOString().split("T")[0],duedate:null,customer_id:p.customer_id,employee_id:p.employee_id,department:p.department,project_id:p.project_id,place_id:p.place_id,paidtype:p.paidtype,curr:p.curr,notax:p.notax,paid:0,acrate:p.acrate,notes:p.notes,intnotes:p.intnotes,fnote:p.fnote,transtate:a[e.EDIT].dataset.transtate.filter((e=>"transtate"===e.groupname&&"ok"===e.groupvalue))[0].id,closed:0,deleted:0,direction:_,cruser_id:a[e.LOGIN].data.employee.id,trans_transcast:r.transcast||"normal"};if(null!==p.duedate&&(y.duedate=`${(new Date).toISOString().split("T")[0]}T00:00:00`),"invoice"===m&&"out"===f){const t=a[e.EDIT].dataset.settings.filter((e=>"default_deadline"===e.fieldname))[0];if(void 0!==t){const e=new Date;y.duedate=`${new Date(e.setDate(e.getDate()+parseInt(t.value,10))).toISOString().split("T")[0]}T00:00:00`}}"receipt"===m&&(y.customer_id=null);const v=(e=>"waybill"===e.transtype?"link":"delivery"!==e.transtype||"normal"!==e.transcast||"in"!==e.direction&&"out"!==e.direction?"copy"===e.cmdtype&&"normal"===e.transcast?"":"normal"!==e.transcast||e.refno?"reflink":"refnumber":"")({transtype:m,transcast:r.transcast,direction:f,cmdtype:r.cmdtype,refno:r.refno});"refnumber"!==v&&"reflink"!==v||(y.ref_transnumber=p.transnumber);let E=`${m}_${f}`;"waybill"!==m&&"cash"!==m||(E=m);const g={method:"POST",data:{key:"nextNumber",values:{numberkey:E,step:!0}}};let T=await o("/function",g);if(T.error)return n(T),null;if("cancellation"===r.transcast?(y.transnumber=`${T}/C`,"delivery"!==m&&"inventory"!==m&&(y.deleted=1),y.transdate=p.transdate,y.duedate=p.duedate):"amendment"===r.transcast?y.transnumber=`${T}/A`:y.transnumber=T,T=await o("/trans",{method:"POST",data:[y]}),T.error)return n(T),null;y.id=T[0];let I=[];if(a[e.EDIT].current.fieldvalue.forEach((t=>{if("trans_transcast"!==t.fieldname&&"invoice"!==m){const i=a[e.EDIT].dataset.deffield.filter((e=>e.fieldname===t.fieldname))[0],n=d("trans",i.subtype,y);if(0===t.deleted&&1===i.visible&&n||0===t.deleted&&n&&"copy"===r.cmdtype){const e=this.tableValues("fieldvalue",t);e.id=null,e.ref_id=y.id,I=[...I,e]}}})),I.length>0&&(T=await o("/fieldvalue",{method:"POST",data:I}),T.error))return n(T),null;if("link"===v||"reflink"===v){const t={...i({tablename:"link"}),nervatype_1:a[e.EDIT].dataset.groups.filter((e=>"nervatype"===e.groupname&&"trans"===e.groupvalue))[0].id,ref_id_1:y.id,nervatype_2:a[e.EDIT].dataset.groups.filter((e=>"nervatype"===e.groupname&&"trans"===e.groupvalue))[0].id,ref_id_2:p.id};if(T=await o("/link",{method:"POST",data:[t]}),T.error)return n(T),null}let k=[];if("invoice"===m||"receipt"===m){const t=(e,t,r)=>{let a=0;return e.forEach((e=>{e.product_id===t&&e.deposit===r&&(a+=e.qty)})),a},i=(e,t,r)=>{const a=e;return a.netamount=l(a.fxprice*(1-a.discount/100)*a.qty,r),a.vatamount=l(a.fxprice*(1-a.discount/100)*a.qty*t,r),a.amount=l(a.netamount+a.vatamount,r),a},n={};r.from_inventory&&a[e.EDIT].dataset.transitem_invoice?a[e.EDIT].dataset.transitem_shipping.forEach((r=>{const s=a[e.EDIT].dataset.item.filter((e=>`${e.id}-${e.product_id}`===r.id))[0];if(void 0!==s){let o="out"===a[e.EDIT].dataset.groups.filter((e=>e.id===p.direction))[0].groupvalue?-parseFloat(r.sqty):parseFloat(r.sqty);if(0===s.deleted&&o>0&&(Object.keys(n).includes(String(s.product_id))||(o-=t(a[e.EDIT].dataset.transitem_invoice,s.product_id,0),n[s.product_id]=!0),0!==o)){let e=this.tableValues("item",s);e.qty=o,e=i(e,s.rate,p.digit),k=[...k,e]}}})):a[e.EDIT].dataset.item.forEach((s=>{if(0===s.deleted)if(r.netto_qty&&a[e.EDIT].dataset.transitem_invoice){let r=s.qty;if(Object.keys(n).includes(String(s.product_id))||(r-=t(a[e.EDIT].dataset.transitem_invoice,s.product_id,0),n[s.product_id]=!0),0!==r){let e=this.tableValues("item",s);e.qty=r,e=i(e,s.rate,p.digit),k=[...k,e]}}else k=[...k,this.tableValues("item",s)]}));[...k].forEach((r=>{if(1===r.deposit){const i=t(a[e.EDIT].dataset.transitem_invoice,r.product_id,1);if(0!==i){const e=this.tableValues("item",r);e.qty=-i,k=[e,...k]}}}))}else a[e.EDIT].dataset.item.forEach((e=>{0===e.deleted&&(k=[...k,this.tableValues("item",e)])}));if([...k].forEach((e=>{const t=e;if(t.id=null,t.trans_id=y.id,t.ownstock=0,"invoice"!==m&&"receipt"!==m&&(t.deposit=0),"cancellation"===r.transcast&&(t.qty=-t.qty,t.netamount=-t.netamount,t.vatamount=-t.vatamount,t.amount=-t.amount),"amendment"===r.transcast){const e=this.tableValues("item",t);e.qty=-e.qty,e.netamount=-e.netamount,e.vatamount=-e.vatamount,e.amount=-e.amount,k=[...k,e]}})),k.length>0&&(T=await o("/item",{method:"POST",data:k}),T.error))return n(T),null;let b=[];if(a[e.EDIT].dataset.payment.forEach((e=>{if(0===e.deleted){const t=this.tableValues("payment",e);t.id=null,t.trans_id=y.id,"cancellation"===r.transcast&&(t.amount=-t.amount),b=[...b,t]}})),b.length>0&&(T=await o("/payment",{method:"POST",data:b}),T.error))return n(T),null;let D=[];const w=[];if("formula"===m||"production"===m){const t=this.tableValues("movement",a[e.EDIT].dataset.movement_head[0]);t.id=null,t.trans_id=y.id,D=[...D,t]}if((a[e.EDIT].dataset.movement||[]).forEach((e=>{if(0===e.deleted){(e.item_id||e.ref_id)&&w.push({id:e.id,item_id:e.item_id,ref_id:e.ref_id});const t=this.tableValues("movement",e);t.id=null,t.trans_id=y.id,"cancellation"===r.transcast&&(t.qty=-t.qty),D=[...D,t]}})),D.length>0){if(T=await o("/movement",{method:"POST",data:D}),T.error)return n(T),null;let t=[];const r=a[e.EDIT].dataset.groups.filter((e=>"nervatype"===e.groupname&&"movement"===e.groupvalue))[0].id,s=a[e.EDIT].dataset.groups.filter((e=>"nervatype"===e.groupname&&"item"===e.groupvalue))[0].id;for(let n=0;n<w.length;n+=1){const o={...i({tablename:"link"}),nervatype_1:r};null!==w[n].item_id?(o.ref_id_1=T[n].id,o.nervatype_2=s,o.ref_id_2=w[n].item_id,t=[...t,o]):(o.ref_id_1=T[a[e.EDIT].dataset.movement.findIndex((e=>e.id===w[n].ref_id))].id,o.nervatype_2=r,o.ref_id_2=T[a[e.EDIT].dataset.movement.findIndex((e=>e.id===w[n].id))].id,t=[...t,o])}if(t.length>0&&(T=await o("/link",{method:"POST",data:t}),T.error))return n(T),null}return await u("save"),this.loadEditor({ntype:"trans",ttype:m,id:y.id})}createTransOptions(){const{modalTrans:t}=this.module,{setData:i}=this.store,n={...this.store.data[e.EDIT]},s={directions:["in","out"],baseTranstype:n.current.transtype,transtype:n.current.transtype,direction:n.dataset.groups.filter((e=>e.id===n.current.item.direction))[0].groupvalue,elementCount:parseInt(n.dataset.element_count[0].pec,10),doctypes:["order","worksheet","rent","invoice","receipt"],refno:!0,netto:!0,from:!1,nettoDiv:!1,fromDiv:!1};switch(s.transtype){case"offer":s.doctypes=["offer","order","worksheet","rent"],s.transtype="order",s.nettoDiv=!1,s.fromDiv=!1;break;case"order":case"worksheet":case"rent":s.doctypes=["offer","order","worksheet","rent","invoice","receipt"],s.transtype="invoice",s.nettoDiv=!0,0===s.elementCount?s.fromDiv=!0:s.fromDiv=!1;break;case"invoice":s.doctypes=["order","worksheet","rent","invoice","receipt"],s.transtype="order",s.nettoDiv=!1,s.fromDiv=!1;break;default:s.transtype="order"}const o=t({...s,onEvent:{onModalEvent:async({key:e,data:t})=>{i("current",{modalForm:null}),e===r.OK&&this.createTrans({cmdtype:"create",transcast:"normal",new_transtype:t.newTranstype,new_direction:t.newDirection,refno:t.refno,from_inventory:t.fromInventory,netto_qty:t.nettoQty})}}});i("current",{modalForm:o,side:a.HIDE})}deleteEditor(){const{inputBox:i}=this.host,{data:n,setData:s}=this.store,{sql:o}=this.app.modules,{resultError:d,requestData:l,createHistory:u,getSql:c,showToast:p,msg:m,currentModule:h}=this.app,f=()=>{s(e.EDIT,{dataset:{},current:{},dirty:!1,form_dirty:!1}),h({data:{module:e.SEARCH}})},_=async()=>{const t=await l(`/${n[e.EDIT].current.type}`,{method:"DELETE",query:{id:n[e.EDIT].current.item.id}});return t&&t.error?d(t):(await u("delete"),f())},y=i({title:m("",{id:"msg_warning"}),message:m("",{id:"msg_delete_text"}),infoText:m("",{id:"msg_delete_info"}),onEvent:{onModalEvent:async a=>{if(s("current",{modalForm:null}),a.key===r.OK){if(null===n[e.EDIT].current.item.id)return f();if(void 0!==o[n[e.EDIT].current.type].delete_state){const r=c(n[e.LOGIN].data.engine,o[n[e.EDIT].current.type].delete_state()),a={method:"POST",data:[{key:"state",text:r.sql,values:Array(r.prmCount).fill(n[e.EDIT].current.item.id)}]},i=await l("/view",a);if(i.error)return d(i);i.state[0].sco>0?p(t.ERROR,m("",{id:"msg_integrity_err"})):_()}else _()}return!0}}});return s("current",{modalForm:y,side:a.HIDE})}deleteEditorItem(t){const{inputBox:i}=this.host,{data:n,setData:s}=this.store,{resultError:o,requestData:d,createHistory:l,msg:u}=this.app,c=()=>{this.loadEditor({ntype:n[e.EDIT].current.type,ttype:n[e.EDIT].current.transtype,id:n[e.EDIT].current.item.id,form:t.fkey})},p=async()=>{if(null===t.id)return c();const e=t.table?t.table:t.fkey,r=await d(`/${e}`,{method:"DELETE",query:{id:t.id}});return r&&r.error?o(r):t.callback?(t.callback(),!0):(await l("delete"),c())};if(t.prompt)return p();const m=i({title:u("",{id:"msg_warning"}),message:u("",{id:"msg_delete_text"}),infoText:u("",{id:"msg_delete_info"}),onEvent:{onModalEvent:async e=>{s("current",{modalForm:null}),e.key===r.OK&&p()}}});return s("current",{modalForm:m,side:a.HIDE})}async editItem(t){const{inputBox:a}=this.host,{data:i,setData:n}=this.store,{resultError:s,requestData:o,msg:d}=this.app,l=async(e,t)=>{const r={method:"POST",data:{key:"getPriceValue",values:{vendorprice:t.vendorprice,product_id:t.product_id,posdate:e.transdate,curr:e.curr,qty:t.qty,customer_id:e.customer_id}}};return o("/function",r)};let u={...i[e.EDIT]};if(["fieldvalue_value","fieldvalue_notes","fieldvalue_deleted"].includes(t.name)){const e=u.current.fieldvalue.findIndex((e=>e.id===t.id));e>-1&&["all","update"].includes(u.audit)&&(u={...u,dirty:!0,current:{...u.current,fieldvalue:[...u.current.fieldvalue]}},u.current.fieldvalue[e]={...u.current.fieldvalue[e],[t.name.split("_")[1]]:"fieldvalue_deleted"===t.name?1:t.value.toString()})}else if(u.current.form)switch(u={...u,form_dirty:!0},void 0!==u.current.form[t.name]&&(u={...u,current:{...u.current,form:{...u.current.form,[t.name]:t.value}}}),u.current.form_type){case"item":if("product_id"===t.name&&void 0!==t.item){u={...u,current:{...u.current,form:{...u.current.form,description:t.item.description,unit:t.item.unit,tax_id:parseInt(t.item.tax_id,10)}}},0===u.current.form.qty&&(u={...u,current:{...u.current,form:{...u.current.form,qty:1}}});const e=await l(u.current.item,u.current.form);if(e.error)return s(e);u={...u,current:{...u.current,form:{...u.current.form,fxprice:Number.isNaN(parseFloat(e.price))?0:parseFloat(e.price),discount:Number.isNaN(parseFloat(e.discount))?0:parseFloat(e.discount)}}},u={...u,current:{...u.current,form:this.calcPrice("fxprice",u.current.form)}}}else switch(t.name){case"qty":if(0===parseFloat(u.current.form.fxprice)){const e=await l(u.current.item,u.current.form);if(e.error)return s(e);u={...u,current:{...u.current,form:{...u.current.form,fxprice:Number.isNaN(parseFloat(e.price))?0:parseFloat(e.price),discount:Number.isNaN(parseFloat(e.discount))?0:parseFloat(e.discount)}}}}"blur"===t.event_type&&(u={...u,current:{...u.current,form:this.calcPrice("fxprice",u.current.form)}});break;case"fxprice":case"tax_id":case"discount":"blur"!==t.event_type&&"tax_id"!==t.name||(u={...u,current:{...u.current,form:this.calcPrice("fxprice",u.current.form)}});break;case"amount":"blur"===t.event_type&&(u={...u,current:{...u.current,form:this.calcPrice("amount",u.current.form)}});break;case"netamount":"blur"===t.event_type&&(u={...u,current:{...u.current,form:this.calcPrice("netamount",u.current.form)}})}break;case"price":case"discount":"customer_id"===t.name&&(u={...u,current:{...u.current,price_customer_id:t.value}});break;case"invoice_link":"ref_id_1"===t.name&&void 0!==t.item?(u={...u,current:{...u.current,price_customer_id:t.value,invoice_link:[...u.current.invoice_link]}},u.current.invoice_link[0]={...u.current.invoice_link[0],curr:t.item.curr}):"link_qty"!==t.name&&"link_rate"!==t.name||(u={...u,current:{...u.current,invoice_link_fieldvalue:this.setFieldvalue(u.current.invoice_link_fieldvalue,t.name,u.current.form.id,null,t.value)}});break;case"payment_link":"ref_id_2"===t.name&&void 0!==t.item?(u={...u,current:{...u.current,payment_link:[...u.current.payment_link]}},u.current.payment_link[0]={...u.current.payment_link[0],curr:t.item.curr}):"link_qty"!==t.name&&"link_rate"!==t.name||(u={...u,current:{...u.current,payment_link_fieldvalue:this.setFieldvalue(u.current.payment_link_fieldvalue,t.name,u.current.form.id,null,t.value)}})}else switch(void 0!==u.current.item[t.name]&&!1===t.extend?(u={...u,current:{...u.current,item:{...u.current.item,[t.name]:t.value}}},t.label_field&&(u={...u,current:{...u.current,item:{...u.current.item,[t.label_field]:t.refnumber||null}}})):void 0!==u.template.options.extend&&!0===t.extend&&(u={...u,current:{...u.current,extend:{...u.current.extend,[t.name]:t.value}}}),"all"!==u.audit&&"update"!==u.audit||(u={...u,dirty:!0}),u.current.type){case"report":if(u={...u,dirty:!1,current:{...u.current,fieldvalue:[...u.current.fieldvalue]}},"selected"===t.name){const e=u.current.fieldvalue.findIndex((e=>e.id===t.id));e>-1&&(u.current.fieldvalue[e]={...u.current.fieldvalue[e],selected:t.value})}else{const e=u.current.fieldvalue.findIndex((e=>e.name===t.name));e>-1&&(u.current.fieldvalue[e]={...u.current.fieldvalue[e],value:t.value})}break;case"printqueue":u={...u,dirty:!1},u={...u,printqueue:{...u.printqueue,[t.name]:t.value}};break;case"trans":switch(t.name){case"closed":if(1===t.value){const i=a({title:d("",{id:"msg_warning"}),message:d("",{id:"msg_close_text"}),infoText:d("",{id:"msg_delete_info"}),onEvent:{onModalEvent:async a=>{n("current",{modalForm:null}),a.key===r.OK&&(u={...u,current:{...u.current,closed:1}}),a.key===r.CANCEL&&(u={...u,current:{...u.current,item:{...u.current.item,[t.name]:0}}}),n(e.EDIT,u)}}});return n("current",{modalForm:i})}break;case"paiddate":u={...u,current:{...u.current,item:{...u.current.item,transdate:t.value}}};break;case"direction":if("cash"===u.current.transtype){const e=u.dataset.groups.filter((e=>e.id===t.value))[0].groupvalue;u={...u,template:{...u.template,options:{...u.template.options,opposite:"out"===e}}}}break;case"seltype":u={...u,current:{...u.current,extend:{...u.current.extend,seltype:t.value,ref_id:null,refnumber:""},item:{...u.current.item,customer_id:null,employee_id:null,ref_transnumber:null}}};break;case"ref_id":switch(u={...u,current:{...u.current,extend:{...u.current.extend,refnumber:t.refnumber,ntype:"transitem"===u.current.extend.seltype?"trans":u.current.extend.seltype,transtype:t.item&&t.item.transtype?t.item.transtype.split("-")[0]:""}}},u.current.extend.seltype){case"customer":u={...u,current:{...u.current,item:{...u.current.item,customer_id:t.value}}};break;case"employee":u={...u,current:{...u.current,item:{...u.current.item,employee_id:t.value}}};break;case"transitem":u={...u,current:{...u.current,item:{...u.current.item,ref_transnumber:t.refnumber}}}}break;case"trans_wsdistance":case"trans_wsrepair":case"trans_wstotal":case"trans_reholiday":case"trans_rebadtool":case"trans_reother":case"trans_wsnote":case"trans_rentnote":u={...u,current:{...u.current,fieldvalue:this.setFieldvalue(u.current.fieldvalue,t.name,u.current.item.id,null,t.value)}};break;case"shippingdate":case"shipping_place_id":u={...u,dirty:!1},u={...u,current:{...u.current,[t.name]:t.value}};break;case"fnote":u={...u,current:{...u.current,item:{...u.current.item,fnote:t.value}}}}}return n(e.EDIT,u)}exportEvent(){const{saveToDisk:t}=this.app,{dataset:r,current:a}=this.store.data[e.EDIT],i={...a.item},n=e=>`${new Date(e).toISOString().slice(0,10)}T${new Date(e).toLocaleTimeString("en",{hour12:!1}).replace("24","00")}`.replaceAll("-","").replaceAll(":","");let s=`BEGIN:VCALENDAR\nPRODID:-//nervatura.com/NONSGML Nervatura Calendar//EN\nVERSION:2.0\nBEGIN:VEVENT\nUID:${null!==i.uid?i.uid:`${Math.random().toString(16).slice(2)}-${Math.random().toString(16).slice(2)}`}`;if(null!==i.fromdate&&(s+=`\nDTSTART:${n(i.fromdate)}`),null!==i.todate&&(s+=`\nDTEND:${n(i.todate)}`),null!==i.subject&&(s+=`\nSUMMARY:${i.subject}`),null!==i.place&&(s+=`\nLOCATION:${i.place}`),null!==i.description&&(s+=`\nDESCRIPTION:${i.description}`),null!==i.eventgroup){const e=r.eventgroup.filter((e=>e.id===i.eventgroup))[0];void 0!==e&&(s+=`\nCATEGORY:${e.groupvalue}`)}s+="\nEND:VEVENT\nEND:VCALENDAR";const o=`${i.calnumber.replace(/\//g,"_")}.ics`;t(URL.createObjectURL(new Blob([s],{type:"text/ics;charset=utf-8;"})),o)}exportQueueAll(){const{inputBox:a}=this.host,{showToast:i,msg:n}=this.app,{setData:s}=this.store,{dataset:o,current:d}=this.store.data[e.EDIT],l={...d.item};if(o.items.length>0){if("print"===l.mode)return i(t.ERROR,`${n("",{id:"ms_export_invalid"})} ${n("",{id:"printqueue_mode_print"})}`);const e=a({title:n("",{id:"msg_warning"}),message:n("",{id:"label_export_all_selected"}),infoText:`${n("",{id:"msg_delete_info"})} ${n("",{id:"ms_continue_warning"})}`,defaultOK:!0,onEvent:{onModalEvent:async e=>{if(s("current",{modalForm:null}),e.key===r.OK){let e=!0;for(let t=0;t<o.items.length&&e;t+=1)e=await this.exportQueue(o.items[t],(()=>{}));this.searchQueue()}}}});return s("current",{modalForm:e})}return!0}async exportQueue(t,r){const{current:a}=this.store.data[e.EDIT],i={...a.item};let n=await this.reportOutput({type:i.mode,template:t.reportkey,title:t.refnumber,orient:i.orientation,size:i.size,copy:t.copies,nervatype:t.typename,id:t.ref_id});return n&&(n=this.deleteEditorItem({fkey:"items",table:"ui_printqueue",id:t.id,prompt:!0,callback:r||this.searchQueue})),n}getTransFilter(t,r){const{data:a}=this.store;switch(a[e.LOGIN].data.transfilterName){case"usergroup":t.where.push(["and","cruser_id","in",[{select:["id"],from:"employee",where:["usergroup","=","?"]}]]),r.push(a[e.LOGIN].data.employee.usergroup);break;case"own":t.where.push(["and","cruser_id","=","?"]),r.push(a[e.LOGIN].data.employee.id)}return[t,r]}async loadEditor(t){const{forms:r,dataSet:a,sql:n,initItem:s}=this.app.modules,{getSql:o,requestData:d,resultError:l,getSetting:u}=this.app,{setData:c,data:p}=this.store,{ntype:m,ttype:h,id:f}=t;let _,y={dataset:{},current:{type:m,transtype:h},dirty:!1,form_dirty:!1};null===f&&(_=s({tablename:m,transtype:h,dataset:y.dataset,current:y.current}));let v=[];a[m](h).forEach((t=>{let r={};"table"===t.infoType?(r={select:["*"],from:t.classAlias},t.where&&(r.where=t.where),t.order&&(r.order_by=t.order)):r=void 0!==n[m][t.sqlKey]?n[m][t.sqlKey](m):void 0!==n[m][t.infoName]?n[m][t.infoName](m):n.all[t.infoName](m);const a=o(p[e.LOGIN].data.engine,r);null!==f||0===a.prmCount?v=[...v,{key:t.infoName,text:a.sql,values:a.prmCount>0&&null!==f?Array(a.prmCount).fill(f):[]}]:y={...y,dataset:{...y.dataset,[t.infoName]:[]}}})),"report"!==m&&a.report().forEach((t=>{const r={key:t.infoName,sql:n.report[t.infoName](m),values:[]};if("printqueue"!==m&&(r.values=[...r.values,p[e.LOGIN].data.employee.usergroup,y.current.type]),"trans"===y.current.type){const e=["and","r.transtype","=",[[],{select:["id"],from:"groups",where:[["groupname","=","'transtype'"],["and","groupvalue","=","?"]]}]];r.sql.where=[...r.sql.where,e],r.values=[...r.values,y.current.transtype]}v=[...v,{key:r.key,text:o(p[e.LOGIN].data.engine,r.sql).sql,values:r.values}]}));const E={method:"POST",data:v},g=await d("/view",E);return g.error?l(g):(y={...y,dataset:{...y.dataset,...g}},null===f&&(null===_&&(_=s({tablename:m,transtype:h,dataset:y.dataset,current:y.current})),"delivery"===h&&(_={..._,direction:y.dataset.groups.filter((e=>"direction"===e.groupname&&"transfer"===e.groupvalue))[0].id}),y={...y,dataset:{...y.dataset,[m]:[_]}}),c(e.EDIT,y),!(!t.cb_key||t.cb_key===i.SET_EDITOR)||("trans"===m?t.shipping?this.setEditor(t,r.shipping(y.dataset[m][0],y,u("ui")),y):this.setEditor(t,r[h](y.dataset[m][0],y,u("ui")),y):this.setEditor(t,r[m](y.dataset[m][0],y,u("ui")),y)))}newFieldvalue(r){const{initItem:a}=this.app.modules,{requestData:i,resultError:n,showToast:s,msg:o}=this.app,{data:d}=this.store,l=async t=>{const r={method:"POST",data:[t]},a=await i("/fieldvalue",r);return a.error?n(a):this.loadEditor({ntype:d[e.EDIT].current.type,ttype:d[e.EDIT].current.transtype,id:d[e.EDIT].current.item.id,form:"fieldvalue",form_id:a[0]})};if(""!==r){const t=d[e.EDIT].dataset.deffield.filter((e=>e.fieldname===r))[0],i=d[e.LOGIN].data.groups.filter((e=>e.id===t.fieldtype))[0].groupvalue;let n={...a({tablename:"fieldvalue"}),id:null,fieldname:t.fieldname},s=!1;switch(i){case"bool":n={...n,value:"false"};break;case"date":n={...n,value:(new Date).toISOString().split("T")[0]};break;case"time":n={...n,value:"00:00"};break;case"float":case"integer":n={...n,value:"0"};break;case"valuelist":n={...n,value:t.valuelist.split("|")[0]}}return["customer","tool","trans","transitem","transmovement","transpayment","product","project","employee","place"].includes(i)&&(s=!0),s?this.onSelector(i,"",(e=>{const t=e.id.split("/");n={...n,value:String(parseInt(t[2],10))},l(n)})):l(n),!0}return s(t.ERROR,o("",{id:"fields_deffield_missing"}))}async nextTransNumber(){const{getSql:t,requestData:r,resultError:a}=this.app,{data:n}=this.store,{current:s,dataset:o}=n[e.EDIT],d=s.transtype,l=o.groups.filter((e=>e.id===s.item.direction))[0].groupvalue,u={select:["min(id) as id"],from:"trans",where:[["transtype","=","?"],["and","id",">","?"]]},c=[s.item.transtype,s.item.id];["cash","waybill"].includes(d)||(u.where.push(["and","direction","=","?"]),c.push(s.item.direction)),["invoice_out","receipt_out","cash_out","cash_in"].includes(`${d}_${l}`)||u.where.push(["and","deleted","=","0"]);const p=this.getTransFilter(u,c),m={method:"POST",data:[{key:"next",text:t(n.login.data.engine,p[0]).sql,values:p[1]}]},h=await r("/view",m);return h.error?a(h):(null===h.next[0].id?["delivery_out","delivery_in"].includes(`${d}_${l}`)||this.checkEditor({ntype:"trans",ttype:d,id:null},i.LOAD_EDITOR):this.checkEditor({ntype:"trans",ttype:d,id:h.next[0].id},i.LOAD_EDITOR),!0)}async prevTransNumber(){const{getSql:t,requestData:r,resultError:a}=this.app,{data:n}=this.store;if("trans"!==n[e.EDIT].current.type||null===n[e.EDIT].current.item.id)return!0;const s=n[e.EDIT].current.transtype,o=n[e.EDIT].dataset.groups.filter((t=>t.id===n[e.EDIT].current.item.direction))[0].groupvalue,d={select:["max(id) as id"],from:"trans",where:[["transtype","=","?"],["and","id","<","?"]]},l=[n[e.EDIT].current.item.transtype,n[e.EDIT].current.item.id];["cash","waybill"].includes(s)||(d.where.push(["and","direction","=","?"]),l.push(n[e.EDIT].current.item.direction)),["invoice_out","receipt_out","cash_out","cash_in"].includes(`${s}_${o}`)||d.where.push(["and","deleted","=","0"]);const u=this.getTransFilter(d,l),c={method:"POST",data:[{key:"prev",text:t(n[e.LOGIN].data.engine,u[0]).sql,values:u[1]}]},p=await r("/view",c);return p.error?a(p):null===p.prev[0].id||this.checkEditor({ntype:"trans",ttype:s,id:p.prev[0].id},i.LOAD_EDITOR)}async reportOutput(e){const{requestData:t,resultError:r,saveToDisk:a}=this.app;if("printqueue"===e.type)return this.addPrintQueue(e.template,e.copy);const i=await t(this.reportPath(e),{});if(i&&i.error)return r(i),!1;const n=URL.createObjectURL(i,{type:"xml"===e.type?"application/xml; charset=UTF-8":"application/pdf"});if("print"===e.type)window.open(n,"_blank");else{let t=`${e.title}_${(new Date).toISOString().split("T")[0]}.${e.type}`;t=t.split("/").join("_"),a(n,t)}return!0}reportPath(t){const{current:r}=this.store.data[e.EDIT],a=new URLSearchParams;return a.append("reportkey",t.template),a.append("orientation",t.orient),a.append("size",t.size),a.append("output",t.type),t.filters?`/report?${a.toString()}&${t.filters}`:(a.append("nervatype",t.nervatype||r.type),`/report?${a.toString()}&filters[@id]=${t.id||r.item.id}`)}reportSettings(){const{modalReport:t}=this.module,{getSetting:i}=this.app,{setData:n}=this.store,{current:s,dataset:o,template:d}=this.store.data[e.EDIT],l=this.store.data[e.LOGIN].data,u="trans"===s.type?o.groups.filter((e=>e.id===s.item.direction))[0].groupvalue:"out";let c="trans"===s.type?o.settings.filter((e=>e.fieldname===`default_trans_${s.transtype}_${u}_report`))[0]:o.settings.filter((e=>e.fieldname===`default_${s.type}_report`))[0],p=[];o.report.forEach((e=>{let t=l.audit.filter((t=>"report"===t.nervatypeName&&t.subtype===e.id))[0];t=t?t.inputfilterName:"all","disabled"!==t&&("trans"===s.type?s.item.direction===e.direction&&(p=[...p,{value:e.reportkey,text:e.repname}]):p=[...p,{value:e.reportkey,text:e.repname}])})),c=void 0!==c?c.value:p.length>0?p[0].value:"";const m=t({title:s.item[d.options.title_field],template:c,templates:p,orient:i("page_orient"),size:i("page_size"),report_size:i("report_size"),report_orientation:i("report_orientation"),copy:1,onEvent:{onModalEvent:e=>{n("current",{modalForm:null}),e.key===r.OK&&this.reportOutput(e.data)}}});n("current",{modalForm:m,side:a.HIDE})}onEditEvent({key:t,data:r}){const{setData:a}=this.store,i=this.store.data[e.EDIT];switch(t){case n.CHANGE:a(e.EDIT,{current:{...i.current,[r.fieldname]:r.value}});break;case n.CHECK_EDITOR:this.checkEditor(...r);break;case n.CHECK_TRANSTYPE:this.checkTranstype(...r);break;case n.EDIT_ITEM:this.editItem(r);break;case n.SET_PATTERN:this.setPattern(r);break;case n.SELECTOR:this.onSelector(...r);break;case n.FORM_ACTION:this.setFormActions(r)}}async onSelector(e,t,a){const{modalSelector:i}=this.module,{quick:n}=this.app.modules,{quickSearch:s,resultError:o}=this.app,{setData:d}=this.store;let l={view:e,columns:n[e]().columns,result:[],filter:t,onEvent:{onModalEvent:async({key:t,data:n})=>{switch(t){case r.CANCEL:d("current",{modalForm:null});break;case r.SELECTED:d("current",{modalForm:null}),a(n.value,n.filter);break;case r.SEARCH:default:const t=await s(e,n.value);if(t.error)return o(t);l={...l,result:t.result},d("current",{modalForm:i(l)})}return!0}}};d("current",{modalForm:i(l)})}async onSideEvent({key:t,data:r}){const{showHelp:a,saveBookmark:n}=this.app,{setData:o}=this.store,d=this.store.data[e.EDIT],l=this.store.data[e.LOGIN].data;switch(t){case s.CHANGE:o(e.EDIT,{[r.fieldname]:r.value});break;case s.BACK:if(d.current.form)return this.checkEditor({ntype:d.current.type,ttype:d.current.transtype,id:d.current.item.id,form:d.current.form_type},i.LOAD_EDITOR);if("transitem_shipping"===d.current.form_type)return this.checkEditor({ntype:d.current.type,ttype:d.current.transtype,id:d.current.item.id,form:d.current.form_type},i.LOAD_EDITOR);const t=l.groups.filter((e=>e.id===d.current.item.nervatype))[0].groupvalue;this.checkEditor({ntype:t,ttype:null,id:d.current.item.ref_id,form:d.current.type},i.LOAD_EDITOR);break;case s.CHECK:this.checkEditor(...r);break;case s.PREV_NUMBER:this.prevTransNumber();break;case s.NEXT_NUMBER:this.nextTransNumber();break;case s.SAVE:let u=null;u=d.current.form?await this.saveEditorForm():await this.saveEditor(),u&&this.loadEditor({ntype:u.current.type,ttype:u.current.transtype,id:u.current.item.id,form:u.current.view});break;case s.DELETE:if(d.current.form)return this.deleteEditorItem({fkey:d.current.form_type,table:d.current.form_datatype,id:d.current.form.id});this.deleteEditor();break;case s.NEW:const{ntype:c,ttype:p}=r[0];"shipping"===p?this.onSelector("transitem_delivery","",(e=>{const t=e.id.split("/");this.checkEditor({ntype:t[0],ttype:t[1],id:parseInt(t[2],10),shipping:!0},i.LOAD_EDITOR)})):this.checkEditor({ntype:c||d.current.type,ttype:p||d.current.transtype,id:null},i.LOAD_EDITOR);break;case s.COPY:this.transCopy(r.value);break;case s.LINK:this.setLink(r.type,r.field);break;case s.PASSWORD:this.setPassword();break;case s.SHIPPING_ADD_ALL:this.shippingAddAll();break;case s.SHIPPING_CREATE:this.createShipping();break;case s.REPORT_SETTINGS:this.reportSettings();break;case s.SEARCH_QUEUE:this.searchQueue();break;case s.EXPORT_QUEUE_ALL:this.exportQueueAll();break;case s.CREATE_REPORT:this.createReport(r.value);break;case s.EXPORT_EVENT:this.exportEvent();break;case s.SAVE_BOOKMARK:n([...r.value]);break;case s.HELP:a(r.value)}return!0}async saveEditor(){const{sql:t,initItem:r,validator:a}=this.app.modules,{resultError:i,createHistory:n,requestData:s,getSql:o}=this.app,{data:d}=this.store;let l={...d[e.EDIT]};const u=null===l.current.item.id;let c=this.tableValues(l.current.type,l.current.item);if(c=await a(l.current.type,c),c.error)return i(c),null;let p=await s(`/${l.current.type}`,{method:"POST",data:[c]});if(p.error)return i(p),null;null===l.current.item.id&&(l={...l,current:{...l.current,item:{...l.current.item,id:p[0]}}}),l={...l,dirty:!1},await n("save"),void 0!==l.current.extend&&(null===l.current.extend.ref_id&&(l={...l,current:{...l.current,extend:{...l.current.extend,ref_id:l.current.item.id}}}),null===l.current.extend.trans_id&&(l={...l,current:{...l.current,extend:{...l.current.extend,trans_id:l.current.item.id}}})),l={...l,current:{...l.current,fieldvalue:[...l.current.fieldvalue]}};for(let e=0;e<l.current.fieldvalue.length;e+=1)null===l.current.fieldvalue[e].ref_id&&(l.current.fieldvalue[e]={...l.current.fieldvalue[e],ref_id:l.current.item.id});if("trans"===l.current.type)switch(l.current.transtype){case"invoice":if(!u){const r={method:"POST",data:[{key:"fields",text:o(d[e.LOGIN].data.engine,t.trans.invoice_customer()).sql,values:[l.current.item.customer_id]}]},a=await s("/view",r);if(a.error)return i(a),null;a.fields.length>0&&Object.keys(a.fields[0]).forEach((e=>{l={...l,current:{...l.current,fieldvalue:this.setFieldvalue(l.current.fieldvalue,e,l.current.item.id,null,a.fields[0][e])}}}))}break;case"worksheet":const a={trans_wsdistance:0,trans_wsrepair:0,trans_wstotal:0,trans_wsnote:""};Object.keys(a).forEach((e=>{l={...l,current:{...l.current,fieldvalue:this.setFieldvalue(l.current.fieldvalue,e,l.current.item.id,null,a[e])}}}));break;case"rent":const n={trans_reholiday:0,trans_rebadtool:0,trans_reother:0,trans_rentnote:""};Object.keys(n).forEach((e=>{l={...l,current:{...l.current,fieldvalue:this.setFieldvalue(l.current.fieldvalue,e,l.current.item.id,null,n[e])}}}));break;case"delivery":let c=[];if(l.dataset.movement.forEach((e=>{let t={...r({tablename:"movement",dataset:l.dataset,current:l.current}),...e};new Date(t.shippingdate).toLocaleDateString()!==new Date(l.current.item.transdate).toLocaleDateString()&&(t={...t,shippingdate:`${new Date(l.current.item.transdate).toISOString().split("T")[0]}T00:00:00`},c=[...c,t])})),c.length>0&&(p=await s("/movement",{method:"POST",data:c}),p.error))return i(p),null}if(l.current.fieldvalue.length>0){if(p=await s("/fieldvalue",{method:"POST",data:l.current.fieldvalue}),p.error)return i(p),null;l={...l,current:{...l.current,fieldvalue:[...l.current.fieldvalue]}};for(let e=0;e<p.length;e+=1)l.current.fieldvalue[e].id||(l.current.fieldvalue[e]={...l.current.fieldvalue[e],id:p[e]})}if(void 0!==l.current.extend){let t=String(l.template.options.extend).split("_")[0],a={...l.current.extend};switch(l.current.transtype){case"waybill":if(t=a.seltype,"transitem"===a.seltype)t="link",a=r({tablename:"link",dataset:l.dataset,current:l.current}),a=l.dataset.translink.length>0?{...a,...l.dataset.translink[0]}:{...a,nervatype_1:d[e.LOGIN].data.groups.filter((e=>"nervatype"===e.groupname&&"trans"===e.groupvalue))[0].id,nervatype_2:d[e.LOGIN].data.groups.filter((e=>"nervatype"===e.groupname&&"trans"===e.groupvalue))[0].id,ref_id_1:l.current.item.id},a={...a,ref_id_2:l.current.extend.ref_id};else if(a=null,l.dataset.translink.length>0&&(p=await s("/link",{method:"DELETE",query:{id:l.dataset.translink[0].id}}),p&&p.error))return i(p),null;break;case"formula":case"production":const{product:n,...o}=a;a="formula"===l.current.transtype?{...o,shippingdate:`${new Date(l.current.item.transdate).toISOString().split("T")[0]}T00:00:00`}:{...o,shippingdate:l.current.item.duedate,place_id:l.current.item.place_id}}if(null!==a){if(p=await s(`/${t}`,{method:"POST",data:[a]}),p.error)return i(p),null;null===a.id&&(a={...a,id:p[0]}),l={...l,current:{...l.current,extend:a}}}}return l}async saveEditorForm(){const{initItem:t,validator:r}=this.app.modules,{resultError:a,createHistory:i,requestData:n}=this.app,{data:s}=this.store;let o={...s[e.EDIT]},d=this.tableValues(o.current.form_datatype,o.current.form);if(d=await r(o.current.form_datatype,d),d.error)return a(d),null;const l=await n(`/${o.current.form_datatype}`,{method:"POST",data:[d]});if(l.error)return a(l),null;switch(null===o.current.form.id&&(o={...o,current:{...o.current,form:{...o.current.form,id:l[0]}}}),o={...o,form_dirty:!1},await i("save"),o.current.form_type){case"movement":if("delivery"===o.current.transtype){let r=[],i=null,d=o.dataset.movement_transfer.filter((e=>e.id===o.current.form.id))[0];void 0!==d?(d=o.dataset.movement.filter((e=>e.id===d.ref_id))[0],d={...t({tablename:"movement",dataset:o.dataset,current:o.current}),...d}):(d={...t({tablename:"movement",dataset:o.dataset,current:o.current}),place_id:o.current.item.place_id},i={...t({tablename:"link",dataset:o.dataset,current:o.current}),nervatype_1:s[e.LOGIN].data.groups.filter((e=>"nervatype"===e.groupname&&"movement"===e.groupvalue))[0].id,nervatype_2:s[e.LOGIN].data.groups.filter((e=>"nervatype"===e.groupname&&"movement"===e.groupvalue))[0].id,ref_id_2:o.current.form.id}),d={...d,product_id:o.current.form.product_id,qty:-o.current.form.qty,notes:o.current.form.notes},r=[...r,this.tableValues("movement",d)],o.dataset.movement_transfer.forEach((e=>{d={...t({tablename:"movement",dataset:o.dataset,current:o.current}),...this.tableValues("movement",e)},d.id!==o.current.form.id&&d.place_id!==o.current.form.place_id&&(d={...d,place_id:o.current.form.place_id},r=[...r,d])}));const l=await n("/movement",{method:"POST",data:r});if(l.error)return a(l),null;if(null!==i){i={...i,ref_id_1:l[0]};const e=await n("/link",{method:"POST",data:[i]});if(e.error)return a(e),null}}break;case"price":case"discount":if(null!==o.current.price_link_customer||null!==o.current.price_customer_id)if(null===o.current.price_customer_id){const e=await n("/link",{method:"DELETE",query:{id:o.current.price_link_customer}});if(e&&e.error)return a(e),null}else{const r={...t({tablename:"link",dataset:o.dataset,current:o.current}),id:o.current.price_link_customer,nervatype_1:s[e.LOGIN].data.groups.filter((e=>"nervatype"===e.groupname&&"price"===e.groupvalue))[0].id,ref_id_1:o.current.form.id,nervatype_2:s[e.LOGIN].data.groups.filter((e=>"nervatype"===e.groupname&&"customer"===e.groupvalue))[0].id,ref_id_2:o.current.price_customer_id},i=await n("/link",{method:"POST",data:[r]});if(i.error)return a(i),null}break;case"invoice_link":case"payment_link":const r=`${o.current.form_type}_fieldvalue`;for(let e=0;e<o.current[r].length;e+=1)null===o.current[r][e].ref_id&&(o={...o,current:{...o.current,[r]:[...o.current[r]]}},o.current[r][e]={...o.current[r][e],ref_id:o.current.form.id});const i={link_qty:"0",link_rate:"1"};let d=o.current[r];Object.keys(i).forEach((e=>{d=this.setFieldvalue(d,e,o.current.form.id,i[e])})),o={...o,current:{...o.current,[r]:d}};const l=await n("/fieldvalue",{method:"POST",data:d});if(l.error)return a(l),null}return o}async searchQueue(){const{sql:t}=this.app.modules,{resultError:r,requestData:a,getSql:i}=this.app,{setData:n,data:s}=this.store;let o={...s[e.EDIT]};const d={method:"POST",data:[{key:"items",text:i(s[e.LOGIN].data.engine,t.printqueue.items(o.printqueue)).sql,values:[]}]},l=await a("/view",d);return l.error?r(l):(o={...o,dataset:{...o.dataset,items:l.items}},n(e.EDIT,o))}setEditor(r,a,i){const{initItem:n}=this.app.modules,{resultError:s,showToast:o,getAuditFilter:d,currentModule:l,msg:u}=this.app,{setData:c,data:p}=this.store;let m={...i||p[e.EDIT]};if(void 0===m.dataset[m.current.type]||0===m.dataset[m.current.type].length)return o(t.ERROR,u("",{id:"msg_editor_invalid"}),0),!1;if(m={...m,template:a,panel:a.options.panel,caption:a.options.title,audit:d(m.current.type,m.current.transtype)[0],side_view:"edit"},"disabled"===m.audit)return!1;if(null===m.dataset[m.current.type][0].id?(m={...m,current:{...m.current,item:m.dataset[m.current.type][0]}},a.options.search_form?m={...m,title_field:a.options.title_field}:("all"!==m.audit&&"update"!==m.audit||(m={...m,dirty:!0}),m={...m,title_field:`${u("",{id:"label_new"})} ${a.options.title}`}),void 0!==a.options.extend&&(m={...m,current:{...m.current,extend:n({tablename:a.options.extend,dataset:m.dataset,current:m.current})}})):(m={...m,current:{...m.current,item:{...n({tablename:m.current.type,dataset:m.dataset,current:m.current}),...m.dataset[m.current.type][0]}}},void 0!==a.options.extend&&(m={...m,current:{...m.current,extend:n({tablename:a.options.extend,dataset:m.dataset,current:m.current})}},void 0!==m.dataset[a.options.extend]&&m.dataset[a.options.extend].length>0&&(m={...m,current:{...m.current,extend:{...m.current.extend,...m.dataset[a.options.extend][0]}}}))),m={...m,current:{...m.current,state:"normal",page:0}},"trans"===m.current.type&&(void 0!==m.dataset.trans[0].transcast&&"cancellation"===m.dataset.trans[0].transcast&&(m={...m,current:{...m.current,state:"cancellation"}}),m.dataset.pattern)){const e=m.dataset.pattern.filter((e=>1===e.defpattern))[0];m={...m,current:{...m.current,template:e?e.id:""}}}if("normal"===m.current.state&&1===m.current.item.deleted?m={...m,current:{...m.current,state:"deleted"}}:1===m.current.item.closed&&(m={...m,current:{...m.current,state:"closed"}}),m={...m,current:{...m.current,fieldvalue:m.dataset.fieldvalue||[]}},Object.keys(m.template.view).forEach((e=>{m={...m,template:{...m.template,view:{...m.template.view,[e]:{...m.template.view[e],view_audit:"all"}}}},m="setting"===e?{...m,template:{...m.template,view:{...m.template.view,[e]:{...m.template.view[e],view_audit:"disabled"}}}}:{...m,template:{...m.template,view:{...m.template.view,[e]:{...m.template.view[e],view_audit:d(a.view[e].audit_type||e,a.view[e].audit_transtype||null)[0]}}}}})),"report"===m.current.type){let t={fields:{}};try{t=JSON.parse(m.dataset.report[0].report)}catch(t){return l({data:{module:e.SEARCH}}),s({error:{message:t.message}})}Object.keys(t.fields).forEach(((e,r)=>{const a=t.fields[e],i=a.selected?a.selected:"in"===a.wheretype;let n={id:r+1,rowtype:"reportfield",datatype:a.fieldtype,name:e,label:a.description,selected:i,empty:"in"===a.wheretype?"false":"true",value:a.value};switch(a.fieldtype){case"bool":n={...n,value:0};break;case"valuelist":n={...n,description:"in"!==a.wheretype?a.valuelist.split("|").unshift(""):a.valuelist.split("|")};break;case"date":if(void 0===n.value)if(a.defvalue){const e=new Date;n={...n,value:new Date(e.setDate(e.getDate()+parseInt(