diff --git a/src/pages/Item/components/ItemProposal.vue b/src/pages/Item/components/ItemProposal.vue index 30f050097..5c735d170 100644 --- a/src/pages/Item/components/ItemProposal.vue +++ b/src/pages/Item/components/ItemProposal.vue @@ -56,26 +56,6 @@ const defaultColumnAttrs = { sortable: false, }; const emit = defineEmits(['onDialogClosed', 'itemReplaced']); - -const priceStatusClass = (proposalPrice) => { - const originalPrice = sale.value?.price; - - if ( - !originalPrice || - !ticketConfig.value || - typeof ticketConfig.value.lackAlertPrice !== 'number' - ) { - return 'price-ok'; - } - - const priceIncreasePercentage = - ((proposalPrice - originalPrice) / originalPrice) * 100; - - return priceIncreasePercentage > ticketConfig.value.lackAlertPrice - ? 'price-alert' - : 'price-ok'; -}; - const columns = computed(() => [ { ...defaultColumnAttrs, @@ -196,7 +176,6 @@ const columns = computed(() => [ { title: t('Replace'), icon: 'change_circle', - show: (row) => isSelectionAvailable(row), action: change, isPrimary: true, }, @@ -204,11 +183,18 @@ const columns = computed(() => [ }, ]); -function extractMatchValues(obj) { - return Object.keys(obj) - .filter((key) => key.startsWith(MATCH)) - .map((key) => parseInt(key.replace(MATCH, ''), 10)); -} +const priceStatusClass = (proposalPrice) => { + const originalPrice = sale.value?.price; + const { lackAlertPrice: lackAlert } = ticketConfig.value; + if (!originalPrice || !ticketConfig.value || typeof lackAlert !== 'number') { + return 'price-ok'; + } + + const percentage = ((proposalPrice - originalPrice) / originalPrice) * 100; + + return percentage > lackAlert ? 'price-alert' : 'price-ok'; +}; + const gradientStyleClass = (row) => { let color = 'white'; const value = parseFloat(row); @@ -226,28 +212,49 @@ const gradientStyleClass = (row) => { } return color; }; + +const extractMatchValues = (obj) => { + return Object.keys(obj) + .filter((key) => key.startsWith(MATCH)) + .map((key) => parseInt(key.replace(MATCH, ''), 10)); +}; + const statusConditionalValue = (row) => { const matches = extractMatchValues(row); const value = matches.reduce((acc, i) => acc + row[`${MATCH}${i}`], 0); return 100 * (value / matches.length); }; -const isSelectionAvailable = (itemProposal) => { - const { price2, available } = itemProposal; - const salePrice = sale.value.price; - const { lackAlertPrice } = ticketConfig.value; - const isPriceTooHigh = (100 * price2) / salePrice > lackAlertPrice; - if (isPriceTooHigh) { - return isPriceTooHigh; +const canReplace = (itemProposal) => { + if (!canReplaceByPrice(itemProposal)) { + return false; } - const hasEnoughQuantity = - (100 * available) / Math.abs($props.itemLack.lack) < lackAlertPrice; - return hasEnoughQuantity; + return canReplaceByQuantity(itemProposal); +}; +const differenceByPrice = ({ price2: proposalPrice }) => { + const { price: salePrice } = sale.value; + const percentage = ((proposalPrice - salePrice) / salePrice) * 100; + return percentage; +}; +const canReplaceByPrice = (itemProposal) => + differenceByPrice(itemProposal) < ticketConfig.value.lackAlertPrice; + +const differenceByQuantity = ({ available }) => { + const { quantity: saleQuantity } = sale.value; + const percentage = ((saleQuantity - available) / available) * 100; + return percentage; }; +const canReplaceByQuantity = (itemProposal) => + differenceByQuantity(itemProposal) < ticketConfig.value.lackAlertPrice; + async function change(itemSubstitution) { - if (!isSelectionAvailable(itemSubstitution)) { - notify(t('notAvailable'), 'warning'); + if (!canReplaceByPrice(itemSubstitution)) { + notify(t('notAvailableByPrice'), 'warning'); + return; + } + if (!canReplaceByQuantity(itemSubstitution)) { + notify(t('notAvailableByQuantity'), 'warning'); return; } const { itemFk: substitutionFk } = itemSubstitution; @@ -277,9 +284,7 @@ async function handleTicketConfig(data) { } function filterRows(data) { - const filteredRows = data.sort( - (a, b) => isSelectionAvailable(b) - isSelectionAvailable(a), - ); + const filteredRows = data.sort((a, b) => canReplace(b) - canReplace(a)); proposalTableRef.value.CrudModelRef.formData = filteredRows; } @@ -315,6 +320,7 @@ function filterRows(data) { >