2 lines
11 KiB
JavaScript
2 lines
11 KiB
JavaScript
import{a as R,u as ae,f as pe}from"./redux-vendor-CfkcHgWN.js";import{L as W,j as T,a0 as v,h as S,A as ye,u as me,c as fe,W as G,et as we,G as w,S as xe,V as U,b as Se}from"./index-D80MdVQn.js";import{j as e,aj as O,ak as P,al as b,am as E,T as N,ao as je,an as ee,ac as Ae,B as ue,I as be,b3 as Ee,aq as ke,ar as te,b7 as re,as as ne}from"./mui-vendor-D_SVL-Dx.js";import{r as s}from"./react-vendor-CmFgShSM.js";import{M as Fe}from"./MonthlyDataCalendar-_xfuL-Zd.js";import{L as z}from"./LabelField-BYhJrYEE.js";import{c as x}from"./checkPathStartsWith-DedybDvj.js";import{a as Te,s as _e}from"./slaughter-get-guilds-for-allocate-CQRKvxsV.js";import{d as ve}from"./Tune-BBzxV8DK.js";import{d as Ge}from"./EditOutlined-CdYoJAhn.js";import{d as Re}from"./DeleteOutlineOutlined-BmausW-i.js";const Qe=R("STEWRD_GET_OUT_SELL_SERVICE",async(l,{dispatch:o})=>{o(W());const{data:r,status:c}=await T.get("roles-products",{params:{role:v(),...l}});return o(S()),{data:r,status:c}}),We=R("STEWARD_DELETE_SEGMENT",async(l,{dispatch:o})=>{o(W());try{const{data:r,status:c}=await T.delete(`app-segmentation/0/?key=${l}`);return o(S()),{data:r,status:c}}catch(r){return o(S()),{error:r}}}),Le=R("STEWARD_SUBMIT_SEGMANT",async(l,{dispatch:o})=>{try{o(W());const{data:r,status:c}=await T.post("app-segmentation/",l);return o(S()),{data:r,status:c}}catch(r){return o(S()),{error:r.response.data.result}}}),$e=R("STEWARD_EDIT_SEGMENT_SERVICE",async(l,{dispatch:o})=>{var r,c;o(W());try{const{data:C,status:y}=await T.put("app-segmentation/0/",l);return o(S()),{data:C,status:y}}catch(C){return o(S()),{error:(c=(r=C.response)==null?void 0:r.data)==null?void 0:c.result}}}),Ie=l=>fe().shape({weight:Se().required("وزن لاشه الزامی است").min(.01,"وزن باید بیشتر از 0 باشد").test("max-production-date-amount",`وزن نمیتواند بیشتر از موجودی تاریخ تولید (${(l==null?void 0:l.toLocaleString())||0} کیلوگرم) باشد!`,function(o){return!l||l===null?!0:o<=l}),product_key:U().when("segmentType",{is:"own",then:U().required("انتخاب کلید الزامی است")})}),Be=({updateTable:l,productKey:o,editData:r,item:c})=>{const C=ae(),[y]=s.useContext(ye),[j,A]=s.useState("own"),[k]=s.useState("free"),[m,L]=s.useState("governmental"),[h,oe]=s.useState(""),[D,M]=s.useState([]),[q,V]=s.useState([]),[i,$]=s.useState({key:"",item:"",buyerType:"",allocationType:""}),p=pe(t=>t.userSlice.selectedSubUser),[se,Y]=s.useState(null),[H,le]=s.useState({}),[I,J]=s.useState(null),[d,B]=s.useState(null),[F,ie]=s.useState({governmental:[],free:[]}),g=me({initialValues:{product_key:o||(r==null?void 0:r.productkey)||"",weight:(r==null?void 0:r.weight)||"",segmentType:r&&r!=null&&r.guildkey?"free":"own"},validationSchema:Ie(d),onSubmit:t=>{if(r){const n={weight:t.weight,key:c==null?void 0:c.key};C($e(n)).then(u=>{var a;(a=u.payload)!=null&&a.error?y({vertical:"top",horizontal:"center",msg:u.payload.error,severity:"error"}):(C(G({right:!1,bottom:!1,content:null})),l(),y({vertical:"top",horizontal:"center",msg:"عملیات با موفقیت انجام شد.",severity:"success"}))})}else{let n;if(j==="own")n={product_key:t==null?void 0:t.product_key,weight:t.weight,sale_type:k,quota:m,production_date:I,distribution_type:"web"};else{if(!(i!=null&&i.key)){y({vertical:"top",horizontal:"center",msg:"لطفا مباشر یا صنف را انتخاب کنید",severity:"error"});return}n={guild_key:(i==null?void 0:i.buyerType)==="Guild"?i==null?void 0:i.key:null,steward_key:(i==null?void 0:i.buyerType)==="Steward"?i==null?void 0:i.key:null,weight:t.weight,product_key:o||"",sale_type:k,quota:m,production_date:I,distribution_type:"web"},n=Object.fromEntries(Object.entries(n).filter(([,u])=>u!==null))}C(Le(n)).then(u=>{u.payload.error?y({vertical:"top",horizontal:"center",msg:u.payload.error,severity:"error"}):(C(G({right:!1,bottom:!1,content:null})),l(),y({vertical:"top",horizontal:"center",msg:"عملیات با موفقیت انجام شد.",severity:"success"}))})}}}),ce=t=>{const n=t.target.value;A(n),g.setFieldValue("segmentType",n)},de=t=>{const n=t.target.value;L(n)},Ce=t=>{const n=t.target.value;oe(n),$({key:"",item:"",buyerType:"",allocationType:""}),M([]),V([])},he=t=>{if(t&&t.formattedDate){Y(t.formattedDate);const n=H[t.formattedDate];n&&n.originalDay&&J(n.originalDay),n&&n.value1!==void 0?B(n.value1):B(null)}},Q=s.useCallback(t=>{if(!Array.isArray(t))return{};const n={};return t.forEach(u=>{if(u.day&&u.amount!==void 0){const f=new we(new Date(u.day)).format("YYYY/MM/DD");n[f]={value1:u.amount,originalDay:u.day,active:u.active===!0}}}),n},[]),_=s.useCallback(t=>{const n=Q(t);le(n)},[Q]),ge=s.useCallback(async()=>{try{const t=v();let n="kill-house-remain-weight";t==="Steward"?n="steward-remain-weight":t==="Guilds"&&(n="guild-remain-weight");const u=await T.get(`${n}/`,{params:{role_key:(x("slaughter")||x("steward")||x("senf"))&&(p==null?void 0:p.key)||""}});if(u.data){ie({governmental:u.data.governmental||[],free:u.data.free||[]});const a=m==="governmental"?u.data.governmental:u.data.free;_(a)}}catch(t){console.error("Error fetching calendar data:",t)}},[m,_,p]);return s.useEffect(()=>{ge()},[]),s.useEffect(()=>{!r&&h&&(h==="guilds"?C(Te({free:!0,role_key:(x("slaughter")||x("steward"))&&(p==null?void 0:p.key)||""})).then(t=>{M(t.payload.data||[])}):h==="stewards"&&C(_e({free:!0,role_key:(x("slaughter")||x("steward"))&&(p==null?void 0:p.key)||""})).then(t=>{V(t.payload.data||[])}))},[h,p==null?void 0:p.key,r]),s.useEffect(()=>{if(F.governmental.length>0||F.free.length>0){const t=m==="governmental"?F.governmental:F.free;_(t),Y(null),J(null),B(null)}},[m,F,_]),s.useEffect(()=>{g.validateForm()},[d]),s.useEffect(()=>{v()==="Guilds"&&!r&&(A("own"),g.setFieldValue("segmentType","own"))},[r]),e.jsx(w,{container:!0,direction:"column",justifyContent:"center",gap:2,children:e.jsx(w,{container:!0,direction:"column",justifyContent:"center",gap:2,pt:2,children:e.jsxs("form",{onSubmit:g.handleSubmit,style:{display:"flex",flexDirection:"column",gap:xe.LARGE+4},children:[!r&&v()!=="Guilds"&&e.jsx(z,{label:"قطعه بندی (کاربر)",children:e.jsx(O,{children:e.jsxs(P,{row:!0,"aria-labelledby":"segment-type-radio-group",name:"segmentType",value:j,onChange:ce,sx:{justifyContent:"space-between"},children:[e.jsx(b,{value:"own",control:e.jsx(E,{}),label:"قطعه بندی (کاربر)"}),e.jsx(b,{value:"free",control:e.jsx(E,{}),label:"تخصیص به قطعه بند"})]})})}),!r&&j==="free"&&e.jsxs(e.Fragment,{children:[e.jsx(z,{label:"خریداران",children:e.jsx(O,{fullWidth:!0,children:e.jsxs(P,{row:!0,"aria-labelledby":"buyer-category-radio-group",name:"buyerCategory",value:h,onChange:Ce,sx:{justifyContent:"space-between"},children:[e.jsx(b,{value:"stewards",control:e.jsx(E,{}),label:"مباشرین"}),e.jsx(b,{value:"guilds",control:e.jsx(E,{}),label:"اصناف"})]})})}),h&&e.jsx(w,{xs:12,container:!0,children:(()=>{const t=h==="guilds"?D:q;return!t||t.length===0?e.jsx(N,{variant:"body2",color:"text.secondary",sx:{width:"100%",textAlign:"center",padding:2,fontStyle:"italic"},children:h==="guilds"?"هیچ صنفی یافت نشد":"هیچ مباشری یافت نشد"}):e.jsx(je,{fullWidth:!0,disablePortal:!0,id:"buyer-selection",options:h==="guilds"?D.map(u=>{var a,f;return{data:u,label:`${u==null?void 0:u.guildsName} ${(a=u==null?void 0:u.user)==null?void 0:a.fullname} (${(f=u==null?void 0:u.user)==null?void 0:f.mobile})`}}):q.map(u=>{var a,f;return{data:u,label:`${(u==null?void 0:u.name)||""} - ${((a=u==null?void 0:u.user)==null?void 0:a.fullname)||""} (${((f=u==null?void 0:u.user)==null?void 0:f.mobile)||""})`}}),onChange:(u,a)=>{var f,X,Z,K;h==="guilds"?$({item:a==null?void 0:a.data,key:(f=a==null?void 0:a.data)==null?void 0:f.key,allocationType:(X=a==null?void 0:a.data)!=null&&X.steward?"steward_steward":"steward_guild",buyerType:(Z=a==null?void 0:a.data)!=null&&Z.steward?"Steward":"Guild"}):h==="stewards"&&$({item:a==null?void 0:a.data,key:(K=a==null?void 0:a.data)==null?void 0:K.key,allocationType:"steward_steward",buyerType:"Steward"})},renderInput:u=>e.jsx(ee,{fullWidth:!0,...u,label:h==="guilds"?"انتخاب صنف":"انتخاب مباشر"})})})()})]}),!r&&e.jsx(z,{label:"نوع انبار",children:e.jsx(O,{children:e.jsxs(P,{row:!0,"aria-labelledby":"segment-type-radio-group",name:"segmentType",value:m,onChange:de,sx:{justifyContent:"space-between"},children:[e.jsx(b,{value:"governmental",control:e.jsx(E,{}),label:"دولتی"}),e.jsx(b,{value:"free",control:e.jsx(E,{}),label:"آزاد"})]})})}),!r&&e.jsx(w,{style:{width:"100%"},container:!0,xs:12,lg:3,justifyContent:"center",alignItems:"center",gap:1,children:e.jsx(Fe,{onDateSelect:he,dayData:H,selectedDate:se,label:`تاریخ تولید گوشت ${d!==null?`(موجودی: ${d==null?void 0:d.toLocaleString()} کیلوگرم)`:""}`})}),e.jsx(ee,{id:"weight",name:"weight",label:"وزن لاشه",type:"number",InputProps:{endAdornment:e.jsx(Ae,{position:"end",children:"کیلوگرم"})},value:g.values.weight,onChange:g.handleChange,onBlur:g.handleBlur,error:g.touched.weight&&!!g.errors.weight||d&&g.values.weight>d,helperText:d&&g.values.weight>d?`وزن نمیتواند بیشتر از موجودی تاریخ تولید (${d==null?void 0:d.toLocaleString()} کیلوگرم) باشد!`:g.touched.weight&&g.errors.weight,fullWidth:!0}),e.jsxs(w,{container:!0,spacing:2,children:[e.jsx(w,{xs:6,children:e.jsx(ue,{type:"submit",fullWidth:!0,variant:"contained",color:"primary",disabled:!r&&(!I||d&&g.values.weight>d||j==="free"&&!(i!=null&&i.key)),children:r?"ویرایش":"ثبت"})}),e.jsx(w,{xs:6,children:e.jsx(ue,{fullWidth:!0,variant:"outlined",color:"primary",onClick:()=>{C(G({right:!1,bottom:!1,content:null}))},children:"انصراف"})})]})]})})})},Xe=({item:l,updateTable:o,productKey:r})=>{const c=ae(),[C,y]=s.useState(null),j=h=>{y(h.currentTarget)},A=()=>{y(null)},k=!!C,m=k?"popover":void 0,L=()=>{A(),c(We(l.key)).then(()=>{o()})};return e.jsxs("div",{children:[e.jsx(be,{"aria-describedby":m,variant:"contained",color:"primary",onClick:j,children:e.jsx(ve,{})}),e.jsx(Ee,{anchorOrigin:{vertical:"bottom",horizontal:"right"},transformOrigin:{vertical:"top",horizontal:"left"},id:m,open:k,anchorEl:C,onClose:A,children:e.jsxs(ke,{sx:{p:1},children:[e.jsx(te,{disablePadding:!0,children:e.jsxs(re,{sx:{color:"primary.main"},onClick:()=>{A(),c(G({right:!(window.innerWidth<=600),bottom:window.innerWidth<=600,title:"ویرایش قطعه بندی",content:e.jsx(Be,{item:l,updateTable:o,editData:l,productKey:r})}))},children:[e.jsx(ne,{sx:{minWidth:36,color:"inherit"},children:e.jsx(Ge,{fontSize:"small"})}),e.jsx(N,{variant:"body2",children:"ویرایش"})]})}),e.jsx(te,{disablePadding:!0,children:e.jsxs(re,{sx:{color:"error.main"},onClick:L,children:[e.jsx(ne,{sx:{minWidth:36,color:"inherit"},children:e.jsx(Re,{fontSize:"small"})}),e.jsx(N,{variant:"body2",color:"inherit",children:"حذف"})]})})]})})]})};export{Xe as S,Be as a,Qe as s};
|