Skip to content

Commit

Permalink
Bugfix for the inconsistient prices naming strategy (#2918) + `final_…
Browse files Browse the repository at this point in the history
…price` being used for catalog rules (#3099)
  • Loading branch information
pkarw committed Jun 25, 2019
1 parent 0a3aa2e commit e1f15ff
Showing 1 changed file with 134 additions and 54 deletions.
188 changes: 134 additions & 54 deletions core/modules/catalog/helpers/tax.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Logger } from '@vue-storefront/core/lib/logger'

function isSpecialPriceActive (fromDate, toDate) {
function isSpecialPriceActive(fromDate, toDate) {
const now = new Date()
fromDate = fromDate ? new Date(fromDate) : false
toDate = toDate ? new Date(toDate) : false

if (!fromDate && !toDate) {
return true
}

if (fromDate && toDate) {
return fromDate < now && toDate > now
}
Expand All @@ -19,39 +21,69 @@ function isSpecialPriceActive (fromDate, toDate) {
}

export function updateProductPrices (product, rate, sourcePriceInclTax = false) {
const rateFactor = parseFloat(rate.rate) / 100
const rate_factor = parseFloat(rate.rate) / 100
product.final_price = parseFloat(product.final_price)
product.price = parseFloat(product.price)
product.special_price = parseFloat(product.special_price)

let priceExclTax = product.price
if (product.final_price) {
if (product.final_price < product.price) { // compare the prices with the product final price if provided; final prices is used in case of active catalog promo rules for example
if (product.final_price < product.special_price) { // for VS - special_price is any price lowered than regular price (`price`); in Magento there is a separate mechanism for setting the `special_prices`
product.price = product.special_price // if the `final_price` is lower than the original `special_price` - it means some catalog rules were applied over it
}
product.special_to_date = null
product.special_from_date = null
product.special_price = product.final_price
} else {
product.price = product.final_price
}
}

let price_excl_tax = product.price
if (sourcePriceInclTax) {
priceExclTax = product.price / (1 + rateFactor)
product.price = priceExclTax
price_excl_tax = product.price / (1 + rate_factor)
product.price = price_excl_tax
}

product.priceTax = priceExclTax * rateFactor
product.priceInclTax = priceExclTax + product.priceTax
product.price_tax = price_excl_tax * rate_factor
product.price_incl_tax = price_excl_tax + product.price_tax

let specialPriceExclTax = product.special_price
let special_price_excl_tax = product.special_price
if (sourcePriceInclTax) {
specialPriceExclTax = product.special_price / (1 + rateFactor)
product.special_price = specialPriceExclTax
special_price_excl_tax = product.special_price / (1 + rate_factor)
product.special_price = special_price_excl_tax
}

product.specialPriceTax = specialPriceExclTax * rateFactor
product.specialPriceInclTax = specialPriceExclTax + product.specialPriceTax
product.special_price_tax = special_price_excl_tax * rate_factor
product.special_price_incl_tax = special_price_excl_tax + product.special_price_tax

/** BEGIN @deprecated - inconsitent naming kept just for the backward compatibility */
product.priceTax = product.price_tax
product.priceInclTax = product.price_incl_tax
product.specialPriceTax = product.special_price_tax
product.specialPriceInclTax = product.special_price_incl_tax
/** END */

if (product.special_price && (product.special_price < product.price)) {
if (!isSpecialPriceActive(product.special_from_date, product.special_to_date)) {
product.special_price = 0 // out of the dates period
} else {
product.originalPrice = priceExclTax
product.originalPriceInclTax = product.priceInclTax
product.originalPriceTax = product.priceTax

product.price = specialPriceExclTax
product.priceInclTax = product.specialPriceInclTax
product.priceTax = product.specialPriceTax
product.original_price = price_excl_tax
product.original_price_incl_tax = product.price_incl_tax
product.original_price_tax = product.price_tax

product.price = special_price_excl_tax
product.price_incl_tax = product.special_price_incl_tax
product.price_tax = product.special_price_tax

/** BEGIN @deprecated - inconsitent naming kept just for the backward compatibility */
product.price = special_price_excl_tax
product.priceInclTax = product.price_incl_tax
product.priceTax = product.price_tax
product.originalPrice = product.original_price
product.originalPriceInclTax = product.original_price_incl_tax
product.originalPriceTax = product.original_price_tax
/** END */
}
} else {
product.special_price = 0 // the same price as original; it's not a promotion
Expand All @@ -66,52 +98,91 @@ export function updateProductPrices (product, rate, sourcePriceInclTax = false)
}
configurableChild.price = parseFloat(configurableChild.price)
configurableChild.special_price = parseFloat(configurableChild.special_price)
configurableChild.final_price = parseFloat(configurableChild.final_price)

if (configurableChild.final_price) {
if (configurableChild.final_price < configurableChild.price) { // compare the prices with the product final price if provided; final prices is used in case of active catalog promo rules for example
if (configurableChild.final_price < configurableChild.special_price) { // for VS - special_price is any price lowered than regular price (`price`); in Magento there is a separate mechanism for setting the `special_prices`
configurableChild.price = configurableChild.special_price // if the `final_price` is lower than the original `special_price` - it means some catalog rules were applied over it
}
configurableChild.special_to_date = null
configurableChild.special_from_date = null
configurableChild.special_price = product.final_price
} else {
configurableChild.price = configurableChild.final_price
}
}

let priceExclTax = configurableChild.price
let price_excl_tax = configurableChild.price
if (sourcePriceInclTax) {
priceExclTax = configurableChild.price / (1 + rateFactor)
configurableChild.price = priceExclTax
price_excl_tax = configurableChild.price / (1 + rate_factor)
configurableChild.price = price_excl_tax
}

configurableChild.priceTax = priceExclTax * rateFactor
configurableChild.priceInclTax = priceExclTax + configurableChild.priceTax

let specialPriceExclTax = parseFloat(configurableChild.special_price)
configurableChild.price_tax = price_excl_tax * rate_factor
configurableChild.price_incl_tax = price_excl_tax + configurableChild.price_tax
let special_price_excl_tax = parseFloat(configurableChild.special_price)

if (sourcePriceInclTax) {
specialPriceExclTax = configurableChild.special_price / (1 + rateFactor)
configurableChild.special_price = specialPriceExclTax
special_price_excl_tax = configurableChild.special_price / (1 + rate_factor)
configurableChild.special_price = special_price_excl_tax
}

configurableChild.specialPriceTax = specialPriceExclTax * rateFactor
configurableChild.specialPriceInclTax = specialPriceExclTax + configurableChild.specialPriceTax
configurableChild.special_price_tax = special_price_excl_tax * rate_factor
configurableChild.special_price_incl_tax = special_price_excl_tax + configurableChild.special_price_tax

/** BEGIN @deprecated - inconsitent naming kept just for the backward compatibility */
configurableChild.priceTax = configurableChild.price_tax
configurableChild.priceInclTax = configurableChild.price_incl_tax
configurableChild.specialPriceTax = configurableChild.special_price_tax
configurableChild.specialPriceInclTax = configurableChild.special_price_incl_tax
/** END */

if (configurableChild.special_price && (configurableChild.special_price < configurableChild.price)) {
if (!isSpecialPriceActive(configurableChild.special_from_date, configurableChild.special_to_date)) {
configurableChild.special_price = 0 // out of the dates period
} else {
configurableChild.originalPrice = priceExclTax
configurableChild.originalPriceInclTax = configurableChild.priceInclTax
configurableChild.originalPriceTax = configurableChild.priceTax

configurableChild.price = specialPriceExclTax
configurableChild.priceInclTax = configurableChild.specialPriceInclTax
configurableChild.priceTax = configurableChild.specialPriceTax
configurableChild.original_price = price_excl_tax
configurableChild.original_price_incl_tax = configurableChild.price_incl_tax
configurableChild.original_price_tax = configurableChild.price_tax

configurableChild.price = special_price_excl_tax
configurableChild.price_incl_tax = configurableChild.special_price_incl_tax
configurableChild.price_tax = configurableChild.special_price_tax

/** BEGIN @deprecated - inconsitent naming kept just for the backward compatibility */
configurableChild.originalPrice = configurableChild.original_price
configurableChild.originalPriceInclTax = configurableChild.original_price_incl_tax
configurableChild.originalPriceTax = configurableChild.original_price_tax
configurableChild.priceInclTax = configurableChild.price_incl_tax
configurableChild.priceTax = configurableChild.price_tax
/** END */
}
} else {
configurableChild.special_price = 0
}

if (configurableChild.priceInclTax < product.priceInclTax || product.price === 0) { // always show the lowest price
product.priceInclTax = configurableChild.priceInclTax
product.priceTax = configurableChild.priceTax
if (configurableChild.price_incl_tax < product.price_incl_tax || product.price === 0) { // always show the lowest price
product.price_incl_tax = configurableChild.price_incl_tax
product.price_tax = configurableChild.price_tax
product.price = configurableChild.price
product.special_price = configurableChild.special_price
product.specialPriceInclTax = configurableChild.specialPriceInclTax
product.specialPriceTax = configurableChild.specialPriceTax
product.originalPrice = configurableChild.originalPrice
product.originalPriceInclTax = configurableChild.originalPriceInclTax
product.originalPriceTax = configurableChild.originalPriceTax
product.special_price_incl_tax = configurableChild.special_price_incl_tax
product.special_price_tax = configurableChild.special_price_tax
product.original_price = configurableChild.original_price
product.original_price_incl_tax = configurableChild.original_price_incl_tax
product.original_price_tax = configurableChild.original_price_tax

/** BEGIN @deprecated - inconsitent naming kept just for the backward compatibility */
product.priceInclTax = product.price_incl_tax
product.priceTax = product.price_tax
product.specialPriceInclTax = product.special_price_incl_tax
product.specialPriceTax = product.special_price_tax
product.originalPrice = product.original_price
product.originalPriceInclTax = product.original_price_incl_tax
product.originalPriceTax = product.original_price_tax
/** END */
}
}
}
Expand All @@ -126,30 +197,39 @@ export function calculateProductTax (product, taxClasses, taxCountry = 'PL', tax
if (rate.tax_country_id === taxCountry && (rate.region_name === taxRegion || rate.tax_region_id === 0 || !rate.region_name)) {
updateProductPrices(product, rate, sourcePriceInclTax)
rateFound = true
Logger.debug('Tax rate ' + rate.code + ' = ' + rate.rate + '% found for ' + taxCountry + ' / ' + taxRegion, 'helper-tax')()
break
}
}
} else {
Logger.debug('No such tax class id: ' + product.tax_class_id, 'helper-tax')()
}
} else {
Logger.debug('No tax class set for: ' + product.sku, 'helper-tax')()
}
if (!rateFound) {
Logger.log('No such tax class id: ' + product.tax_class_id + ' or rate not found for ' + taxCountry + ' / ' + taxRegion, 'helper-tax')()
updateProductPrices(product, {rate: 0})

product.price_incl_tax = product.price
product.price_tax = 0
product.special_price_incl_tax = 0
product.special_price_tax = 0

/** BEGIN @deprecated - inconsitent naming kept just for the backward compatibility */
product.priceInclTax = product.price
product.priceTax = 0
product.specialPriceInclTax = 0
product.specialPriceTax = 0
/** END */

if (product.configurable_children) {
for (let configurableChildren of product.configurable_children) {
configurableChildren.price_incl_tax = configurableChildren.price
configurableChildren.price_tax = 0
configurableChildren.special_price_incl_tax = 0
configurableChildren.special_price_tax = 0

/** BEGIN @deprecated - inconsitent naming kept just for the backward compatibility */
configurableChildren.priceInclTax = configurableChildren.price
configurableChildren.priceTax = 0
configurableChildren.specialPriceInclTax = 0
configurableChildren.specialPriceTax = 0
configurableChildren.specialPriceTax = 0
/** END */
}
}
}
Expand Down

0 comments on commit e1f15ff

Please sign in to comment.