var _=Object.defineProperty;var u=Object.getOwnPropertySymbols;var w=Object.prototype.hasOwnProperty,y=Object.prototype.propertyIsEnumerable;var f=(l,c,a)=>c in l?_(l,c,{enumerable:!0,configurable:!0,writable:!0,value:a}):l[c]=a,x=(l,c)=>{for(var a in c||(c={}))w.call(c,a)&&f(l,a,c[a]);if(u)for(var a of u(c))y.call(c,a)&&f(l,a,c[a]);return l};import{I as F,a as d,d as S}from"./vendor.01d0adc5.js";import{h as o,s as m,u as r}from"./main.c43fbacd.js";var E={expense_category_id:null,expense_date:F().format("YYYY-MM-DD"),amount:100,notes:"",attachment_receipt:null,customer_id:"",currency_id:"",payment_method_id:"",receiptFiles:[],customFields:[],fields:[],in_use:!1,selectedCurrency:null};const D=(l=!1)=>{const c=l?window.pinia.defineStore:S,{global:a}=window.i18n;return c({id:"expense",state:()=>({expenses:[],totalExpenses:0,selectAllField:!1,selectedExpenses:[],paymentModes:[],showExchangeRate:!1,currentExpense:x({},E)}),getters:{getCurrentExpense:t=>t.currentExpense,getSelectedExpenses:t=>t.selectedExpenses},actions:{resetCurrentExpenseData(){this.currentExpense=x({},E)},fetchExpenses(t){return new Promise((n,i)=>{d.get("/api/v1/expenses",{params:t}).then(e=>{this.expenses=e.data.data,this.totalExpenses=e.data.meta.expense_total_count,n(e)}).catch(e=>{o(e),i(e)})})},fetchExpense(t){return new Promise((n,i)=>{d.get(`/api/v1/expenses/${t}`).then(e=>{e.data&&(Object.assign(this.currentExpense,e.data.data),this.currentExpense.selectedCurrency=e.data.data.currency,e.data.data.attachment_receipt?m.isImageFile(e.data.data.attachment_receipt_meta.mime_type)?this.currentExpense.receiptFiles=[{image:`/expenses/${t}/receipt`}]:this.currentExpense.receiptFiles=[{type:"document",name:e.data.data.attachment_receipt_meta.file_name}]:this.currentExpense.receiptFiles=[]),n(e)}).catch(e=>{o(e),i(e)})})},addExpense(t){const n=m.toFormData(t);return new Promise((i,e)=>{d.post("/api/v1/expenses",n).then(s=>{this.expenses.push(s.data),r().showNotification({type:"success",message:a.t("expenses.created_message")}),i(s)}).catch(s=>{o(s),e(s)})})},updateExpense({id:t,data:n}){const i=r(),e=m.toFormData(n);return e.append("_method","PUT"),new Promise(s=>{d.post(`/api/v1/expenses/${t}`,e).then(p=>{let h=this.expenses.findIndex(g=>g.id===p.data.id);this.expenses[h]=n.expense,i.showNotification({type:"success",message:a.t("expenses.updated_message")}),s(p)})}).catch(s=>{o(s),reject(s)})},setSelectAllState(t){this.selectAllField=t},selectExpense(t){this.selectedExpenses=t,this.selectedExpenses.length===this.expenses.length?this.selectAllField=!0:this.selectAllField=!1},selectAllExpenses(t){if(this.selectedExpenses.length===this.expenses.length)this.selectedExpenses=[],this.selectAllField=!1;else{let n=this.expenses.map(i=>i.id);this.selectedExpenses=n,this.selectAllField=!0}},deleteExpense(t){const n=r();return new Promise((i,e)=>{d.post("/api/v1/expenses/delete",t).then(s=>{let p=this.expenses.findIndex(h=>h.id===t);this.expenses.splice(p,1),n.showNotification({type:"success",message:a.tc("expenses.deleted_message",1)}),i(s)}).catch(s=>{o(s),e(s)})})},deleteMultipleExpenses(){const t=r();return new Promise((n,i)=>{d.post("/api/v1/expenses/delete",{ids:this.selectedExpenses}).then(e=>{this.selectedExpenses.forEach(s=>{let p=this.expenses.findIndex(h=>h.id===s.id);this.expenses.splice(p,1)}),t.showNotification({type:"success",message:a.tc("expenses.deleted_message",2)}),n(e)}).catch(e=>{o(e),i(e)})})},fetchPaymentModes(t){return new Promise((n,i)=>{d.get("/api/v1/payment-methods",{params:t}).then(e=>{this.paymentModes=e.data.data,n(e)}).catch(e=>{o(e),i(e)})})}}})()};export{D as u};
