import{t as e}from"./http-BrugN6o-.js";import{n as t,t as n}from"./notification-BM55Quw3.js";import{t as r}from"./error-handling-CRDNZ9kr.js";import{t as i}from"./moment-DiFv0SGL.js";import{rt as a}from"./main-CgSmSYZA.js";var o={expense_category_id:null,expense_date:i().format(`YYYY-MM-DD`),expense_number:``,amount:100,notes:``,attachment_receipt:null,customer_id:``,currency_id:``,payment_method_id:``,receiptFiles:[],customFields:[],fields:[],in_use:!1,selectedCurrency:null},s=(i=!1)=>{let s=i?window.pinia.defineStore:t,{global:c}=window.i18n;return s(`expense`,{state:()=>({expenses:[],totalExpenses:0,selectAllField:!1,selectedExpenses:[],paymentModes:[],showExchangeRate:!1,currentExpense:{...o}}),getters:{getCurrentExpense:e=>e.currentExpense,getSelectedExpenses:e=>e.selectedExpenses},actions:{resetCurrentExpenseData(){this.currentExpense={...o}},fetchExpenses(t){return new Promise((n,i)=>{e.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=>{r(e),i(e)})})},fetchExpense(t){return new Promise((n,i)=>{e.get(`/api/v1/expenses/${t}`).then(e=>{e.data&&(Object.assign(this.currentExpense,e.data.data),this.currentExpense.selectedCurrency=e.data.data.currency,this.currentExpense.attachment_receipt=null,e.data.data.attachment_receipt_url?a.isImageFile(e.data.data.attachment_receipt_meta.mime_type)?this.currentExpense.receiptFiles=[{image:`/reports/expenses/${t}/receipt?${e.data.data.attachment_receipt_meta.uuid}`}]:this.currentExpense.receiptFiles=[{type:`document`,name:e.data.data.attachment_receipt_meta.file_name}]:this.currentExpense.receiptFiles=[]),n(e)}).catch(e=>{r(e),i(e)})})},duplicateExpense({id:t,expense_date:i}){return new Promise((a,o)=>{e.post(`/api/v1/expenses/${t}/duplicate`,{expense_date:i}).then(e=>{n().showNotification({type:`success`,message:c.t(`expenses.duplicated_message`)}),a(e)}).catch(e=>{r(e),o(e)})})},addExpense(t){let i=a.toFormData(t);return new Promise((t,a)=>{e.post(`/api/v1/expenses`,i).then(e=>{this.expenses.push(e.data),n().showNotification({type:`success`,message:c.t(`expenses.created_message`)}),t(e)}).catch(e=>{r(e),a(e)})})},updateExpense({id:t,data:i,isAttachmentReceiptRemoved:o}){let s=n(),l=a.toFormData(i);return l.append(`_method`,`PUT`),l.append(`is_attachment_receipt_removed`,o),new Promise(n=>{e.post(`/api/v1/expenses/${t}`,l).then(e=>{let t=this.expenses.findIndex(t=>t.id===e.data.id);this.expenses[t]=i.expense,s.showNotification({type:`success`,message:c.t(`expenses.updated_message`)}),n(e)})}).catch(e=>{r(e),reject(e)})},setSelectAllState(e){this.selectAllField=e},selectExpense(e){this.selectedExpenses=e,this.selectedExpenses.length===this.expenses.length?this.selectAllField=!0:this.selectAllField=!1},selectAllExpenses(e){this.selectedExpenses.length===this.expenses.length?(this.selectedExpenses=[],this.selectAllField=!1):(this.selectedExpenses=this.expenses.map(e=>e.id),this.selectAllField=!0)},deleteExpense(t){let i=n();return new Promise((n,a)=>{e.post(`/api/v1/expenses/delete`,t).then(e=>{let r=this.expenses.findIndex(e=>e.id===t);this.expenses.splice(r,1),i.showNotification({type:`success`,message:c.t(`expenses.deleted_message`,1)}),n(e)}).catch(e=>{r(e),a(e)})})},deleteMultipleExpenses(){let t=n();return new Promise((n,i)=>{e.post(`/api/v1/expenses/delete`,{ids:this.selectedExpenses}).then(e=>{this.selectedExpenses.forEach(e=>{let t=this.expenses.findIndex(t=>t.id===e.id);this.expenses.splice(t,1)}),t.showNotification({type:`success`,message:c.t(`expenses.deleted_message`,2)}),n(e)}).catch(e=>{r(e),i(e)})})},fetchPaymentModes(t){return new Promise((n,i)=>{e.get(`/api/v1/payment-methods`,{params:t}).then(e=>{this.paymentModes=e.data.data,n(e)}).catch(e=>{r(e),i(e)})})}}})()};export{s as t};