From dbd75bbe684c8dff35fa834d30904800b563ad4c Mon Sep 17 00:00:00 2001 From: yashkanakiya Date: Fri, 25 Aug 2023 17:45:43 +0530 Subject: [PATCH] add changes in tax per item calculation --- .../estimate-invoice-common/CreateItemRow.vue | 23 +++++--- .../CreateItemRowTax.vue | 15 ++++-- .../estimate-invoice-common/CreateTotal.vue | 38 ++++++++++---- resources/scripts/admin/stores/estimate.js | 52 ++++++++++++++----- resources/scripts/admin/stores/invoice.js | 51 +++++++++++++----- .../views/estimates/create/EstimateCreate.vue | 9 +++- .../views/invoices/create/InvoiceCreate.vue | 8 ++- 7 files changed, 148 insertions(+), 48 deletions(-) diff --git a/resources/scripts/admin/components/estimate-invoice-common/CreateItemRow.vue b/resources/scripts/admin/components/estimate-invoice-common/CreateItemRow.vue index 98becef7..3a25fda8 100644 --- a/resources/scripts/admin/components/estimate-invoice-common/CreateItemRow.vue +++ b/resources/scripts/admin/components/estimate-invoice-common/CreateItemRow.vue @@ -271,6 +271,7 @@ const price = computed({ } else { updateItemAttribute('price', newValue) } + setDiscount() }, }) @@ -281,13 +282,8 @@ const discount = computed({ return props.itemData.discount }, set: (newValue) => { - if (props.itemData.discount_type === 'percentage') { - updateItemAttribute('discount_val', (subtotal.value * newValue) / 100) - } else { - updateItemAttribute('discount_val', Math.round(newValue * 100)) - } - updateItemAttribute('discount', newValue) + setDiscount() }, }) @@ -399,7 +395,7 @@ const v$ = useVuelidate( function updateTax(data) { props.store.$patch((state) => { - state[props.storeProp].items[props.index]['taxes'][data.index] = data.item + state[props.storeProp].items[props.index].taxes[data.index] = data.item }) let lastTax = props.itemData.taxes[props.itemData.taxes.length - 1] @@ -416,6 +412,16 @@ function updateTax(data) { syncItemToStore() } +function setDiscount() { + const newValue = props.store[props.storeProp].items[props.index].discount + + if (props.itemData.discount_type === 'percentage'){ + updateItemAttribute('discount_val', Math.round((subtotal.value * newValue) / 100)) + }else{ + updateItemAttribute('discount_val', Math.round(newValue * 100)) + } +} + function searchVal(val) { updateItemAttribute('name', val) } @@ -489,6 +495,9 @@ function syncItemToStore() { totalTax: totalTax.value, tax: totalTax.value, taxes: [...itemTaxes], + tax_type_ids: itemTaxes.flatMap(_t => + _t.tax_type_id ? _t.tax_type_id : [], + ), } props.store.updateItem(data) diff --git a/resources/scripts/admin/components/estimate-invoice-common/CreateItemRowTax.vue b/resources/scripts/admin/components/estimate-invoice-common/CreateItemRowTax.vue index caba8311..a61a1055 100644 --- a/resources/scripts/admin/components/estimate-invoice-common/CreateItemRowTax.vue +++ b/resources/scripts/admin/components/estimate-invoice-common/CreateItemRowTax.vue @@ -148,7 +148,7 @@ const filteredTypes = computed(() => { const taxAmount = computed(() => { if (localTax.compound_tax && props.discountedTotal) { const taxPerItemEnabled = props.store[props.storeProp].tax_per_item === 'YES' - const discountPerItemEnabled = props.store[props.storeProp].discount_per_item === 'NO' + const discountPerItemEnabled = props.store[props.storeProp].discount_per_item === 'YES' if (taxPerItemEnabled && !discountPerItemEnabled){ return getTaxAmount() } @@ -157,7 +157,7 @@ const taxAmount = computed(() => { if (props.discountedTotal && localTax.percent) { const taxPerItemEnabled = props.store[props.storeProp].tax_per_item === 'YES' - const discountPerItemEnabled = props.store[props.storeProp].discount_per_item === 'NO' + const discountPerItemEnabled = props.store[props.storeProp].discount_per_item === 'YES' if (taxPerItemEnabled && !discountPerItemEnabled){ return getTaxAmount() } @@ -181,6 +181,13 @@ watch( } ) +watch( + () => taxAmount.value, + () => { + updateRowTax() + }, +) + // Set SelectedTax if (props.taxData.tax_type_id > 0) { selectedTax.value = taxTypeStore.taxTypes.find( @@ -230,6 +237,8 @@ function openTaxModal() { function removeTax(index) { props.store.$patch((state) => { state[props.storeProp].items[props.itemIndex].taxes.splice(index, 1) + state[props.storeProp].items[props.itemIndex].tax = 0 + state[props.storeProp].items[props.itemIndex].totalTax = 0 }) } @@ -237,7 +246,7 @@ function getTaxAmount() { let total = 0 let discount = 0 const itemTotal = props.discountedTotal - const modelDiscount = props.store[props.storeProp].discount ? props.store[props.storeProp].discount.toFixed(2) : 0 + const modelDiscount = props.store[props.storeProp].discount ? props.store[props.storeProp].discount : 0 const type = props.store[props.storeProp].discount_type if (modelDiscount > 0) { props.store[props.storeProp].items.forEach((_i) => { diff --git a/resources/scripts/admin/components/estimate-invoice-common/CreateTotal.vue b/resources/scripts/admin/components/estimate-invoice-common/CreateTotal.vue index 93abb12d..2d756592 100644 --- a/resources/scripts/admin/components/estimate-invoice-common/CreateTotal.vue +++ b/resources/scripts/admin/components/estimate-invoice-common/CreateTotal.vue @@ -191,7 +191,7 @@