From 27660c6bce02909201f0ee1c095c82990ce9ad50 Mon Sep 17 00:00:00 2001 From: yashkanakiya Date: Fri, 25 Aug 2023 09:33:30 +0530 Subject: [PATCH 1/8] fix initial tax per item issue --- .../estimate-invoice-common/CreateItemRow.vue | 2 +- .../CreateItemRowTax.vue | 31 ++++++++++++++++++- .../estimate-invoice-common/CreateTotal.vue | 6 ++-- resources/scripts/admin/stores/estimate.js | 10 ++++++ resources/scripts/admin/stores/invoice.js | 10 ++++++ .../views/estimates/create/EstimateCreate.vue | 15 ++++++++- .../views/invoices/create/InvoiceCreate.vue | 15 ++++++++- 7 files changed, 81 insertions(+), 8 deletions(-) diff --git a/resources/scripts/admin/components/estimate-invoice-common/CreateItemRow.vue b/resources/scripts/admin/components/estimate-invoice-common/CreateItemRow.vue index 8af4293f4..98becef75 100644 --- a/resources/scripts/admin/components/estimate-invoice-common/CreateItemRow.vue +++ b/resources/scripts/admin/components/estimate-invoice-common/CreateItemRow.vue @@ -274,7 +274,7 @@ const price = computed({ }, }) -const subtotal = computed(() => props.itemData.price * props.itemData.quantity) +const subtotal = computed(() => Math.round(props.itemData.price * props.itemData.quantity)) const discount = computed({ get: () => { diff --git a/resources/scripts/admin/components/estimate-invoice-common/CreateItemRowTax.vue b/resources/scripts/admin/components/estimate-invoice-common/CreateItemRowTax.vue index cf3b12946..caba83110 100644 --- a/resources/scripts/admin/components/estimate-invoice-common/CreateItemRowTax.vue +++ b/resources/scripts/admin/components/estimate-invoice-common/CreateItemRowTax.vue @@ -147,10 +147,20 @@ 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' + if (taxPerItemEnabled && !discountPerItemEnabled){ + return getTaxAmount() + } return ((props.discountedTotal + props.totalTax) * localTax.percent) / 100 } - if (props.discountedTotal && localTax.percent) { + 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' + if (taxPerItemEnabled && !discountPerItemEnabled){ + return getTaxAmount() + } return (props.discountedTotal * localTax.percent) / 100 } @@ -222,4 +232,23 @@ function removeTax(index) { state[props.storeProp].items[props.itemIndex].taxes.splice(index, 1) }) } + +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 type = props.store[props.storeProp].discount_type + if (modelDiscount > 0) { + props.store[props.storeProp].items.forEach((_i) => { + total += _i.total + }) + const proportion = (itemTotal / total).toFixed(2) + discount = type === 'fixed' ? modelDiscount * 100 : (total * modelDiscount) / 100 + const itemDiscount = Math.round(discount * proportion) + const discounted = itemTotal - itemDiscount + return Math.round((discounted * localTax.percent) / 100) + } + return Math.round((props.discountedTotal * localTax.percent) / 100) +} diff --git a/resources/scripts/admin/components/estimate-invoice-common/CreateTotal.vue b/resources/scripts/admin/components/estimate-invoice-common/CreateTotal.vue index a14c429ad..93abb12d8 100644 --- a/resources/scripts/admin/components/estimate-invoice-common/CreateTotal.vue +++ b/resources/scripts/admin/components/estimate-invoice-common/CreateTotal.vue @@ -233,9 +233,7 @@ const totalDiscount = computed({ }, set: (newValue) => { if (props.store[props.storeProp].discount_type === 'percentage') { - props.store[props.storeProp].discount_val = Math.round( - (props.store.getSubTotal * newValue) / 100 - ) + props.store[props.storeProp].discount_val = Math.round((props.store.getSubTotal * newValue.toFixed(2)) / 100) } else { props.store[props.storeProp].discount_val = Math.round(newValue * 100) } @@ -265,7 +263,7 @@ const itemWiseTaxes = computed(() => { } else if (tax.tax_type_id) { taxes.push({ tax_type_id: tax.tax_type_id, - amount: tax.amount, + amount: Math.round(tax.amount), percent: tax.percent, name: tax.name, }) diff --git a/resources/scripts/admin/stores/estimate.js b/resources/scripts/admin/stores/estimate.js index 5cedf789c..a49f182ba 100644 --- a/resources/scripts/admin/stores/estimate.js +++ b/resources/scripts/admin/stores/estimate.js @@ -144,6 +144,16 @@ export const useEstimateStore = (useWindow = false) => { .get(`/api/v1/estimates/${id}`) .then((response) => { Object.assign(this.newEstimate, response.data.data) + // if (this.newEstimate.discount_per_item === 'NO') { + // this.newEstimate.items.forEach((_i, index) => { + // if (_i.discount_type === 'fixed') + // this.newEstimate.items[index].discount = _i.discount / 100 + // }) + // } + // else { + // if (this.newEstimate.discount_type === 'fixed') + // this.newEstimate.discount = this.newEstimate.discount / 100 + // } resolve(response) }) .catch((err) => { diff --git a/resources/scripts/admin/stores/invoice.js b/resources/scripts/admin/stores/invoice.js index 97bcebff9..3ed07ef6d 100644 --- a/resources/scripts/admin/stores/invoice.js +++ b/resources/scripts/admin/stores/invoice.js @@ -135,6 +135,16 @@ export const useInvoiceStore = (useWindow = false) => { .then((response) => { Object.assign(this.newInvoice, response.data.data) this.newInvoice.customer = response.data.data.customer + if (this.newInvoice.discount_per_item === 'NO') { + this.newInvoice.items.forEach((_i, index) => { + if (_i.discount_type === 'fixed') + this.newInvoice.items[index].discount = _i.discount / 100 + }) + } + else { + if (this.newInvoice.discount_type === 'fixed') + this.newInvoice.discount = this.newInvoice.discount / 100 + } resolve(response) }) .catch((err) => { diff --git a/resources/scripts/admin/views/estimates/create/EstimateCreate.vue b/resources/scripts/admin/views/estimates/create/EstimateCreate.vue index 03863f7c2..67ef84aaf 100644 --- a/resources/scripts/admin/views/estimates/create/EstimateCreate.vue +++ b/resources/scripts/admin/views/estimates/create/EstimateCreate.vue @@ -138,6 +138,7 @@