Skip to content

Commit

Permalink
Fix the order information display
Browse files Browse the repository at this point in the history
BrickLink generates broken VATCHARGES for orders with a coupon.
Until this is fixed on BrickLink's side (which could take a long time),
we calculate the correct net/gross/vat charge values from the given
fields.

NOTE: This fixup code has to be removed once BL fixes things on their
      side!
  • Loading branch information
rgriebl committed Jan 21, 2022
1 parent e213376 commit bee7846
Show file tree
Hide file tree
Showing 7 changed files with 544 additions and 334 deletions.
4 changes: 3 additions & 1 deletion src/bricklink/order.cpp
Expand Up @@ -263,7 +263,9 @@ double Order::usSalesTax() const

double Order::grandTotal() const
{
return d->m_grandTotal;
// return d->m_grandTotal; // this is sometimes off by 1 cent
return orderTotal() + shipping() + insurance() + additionalCharges1() + additionalCharges2()
- credit() - creditCoupon() + usSalesTax();
}

double Order::vatChargeSeller() const
Expand Down
68 changes: 42 additions & 26 deletions src/desktop/orderinformationdialog.cpp
Expand Up @@ -87,35 +87,51 @@ OrderInformationDialog::OrderInformationDialog(const BrickLink::Order *order, QW
% order->paymentCurrencyCode());
}

static auto setVatLabel = [](QLabel *l, const BrickLink::Order *order, double vatCharge) {
double gt = order->grandTotal() + order->credit() + order->creditCoupon();
double gtNet = gt - vatCharge;
bool vatFromSeller = !qFuzzyIsNull(order->vatChargeSeller());
double vatCharge = vatFromSeller ? order->vatChargeSeller() : order->vatChargeBrickLink();
bool hasVat = !qFuzzyIsNull(vatCharge);
double coupon = order->creditCoupon();
double gtGross = order->grandTotal() + coupon; // see below for why creditCoupon is needed here
double gtNet = gtGross - vatCharge;
double vatPercent = Utility::roundTo(100 * (gtGross / (qFuzzyIsNull(gtNet) ? gtGross : gtNet) - 1.0), 0);

double p = Utility::roundTo(100 * (gt / (qFuzzyIsNull(gtNet) ? gt : gtNet) - 1.0), 1);
l->setText(l->text().arg(QLocale().toString(p, 'f', QLocale::FloatingPointShortest) % u'%'));
};
if (!qFuzzyIsNull(coupon)) {
// the vatCharge values are wrong when a orderCoupon is active
// we can however re-calculate this value
double netFix = coupon * 100 / (100 + vatPercent);
gtNet -= netFix;
gtGross -= coupon;
vatCharge = gtGross - gtNet;
vatPercent = Utility::roundTo(100 * (gtGross / (qFuzzyIsNull(gtNet) ? gtGross : gtNet) - 1.0), 0);
}

setVatLabel(w_vatSellerLabel, order, order->vatChargeSeller());
setVatLabel(w_vatBLLabel, order, order->vatChargeBrickLink());
w_vatInfoLabel->setVisible(hasVat);
w_vatSeparator->setVisible(hasVat);
w_vatLabel->setText(w_vatLabel->text()
.arg(QLocale().toString(vatPercent, 'f', QLocale::FloatingPointShortest) % u'%')
.arg(vatFromSeller ? tr("Seller", "x% VAT (Seller)")
: tr("BrickLink", "x% VAT (BrickLink)")));

setup(w_shipping, w_shippingCopy, Currency::toDisplayString(order->shipping(), { }, 2),
!qFuzzyIsNull(order->shipping()), w_shippingLabel);
setup(w_insurance, w_insuranceCopy, Currency::toDisplayString(order->insurance(), { }, 2),
!qFuzzyIsNull(order->insurance()), w_insuranceLabel);
setup(w_addCharges1, w_addCharges1Copy, Currency::toDisplayString(order->additionalCharges1(), { }, 2),
setup(w_shipping, w_shippingCopy, Currency::toDisplayString(order->shipping(), { }, 2),
!qFuzzyIsNull(order->shipping()), w_shippingLabel);
setup(w_insurance, w_insuranceCopy, Currency::toDisplayString(order->insurance(), { }, 2),
!qFuzzyIsNull(order->insurance()), w_insuranceLabel);
setup(w_addCharges1, w_addCharges1Copy, Currency::toDisplayString(order->additionalCharges1(), { }, 2),
!qFuzzyIsNull(order->additionalCharges1()), w_addCharges1Label);
setup(w_addCharges2, w_addCharges2Copy, Currency::toDisplayString(order->additionalCharges2(), { }, 2),
setup(w_addCharges2, w_addCharges2Copy, Currency::toDisplayString(order->additionalCharges2(), { }, 2),
!qFuzzyIsNull(order->additionalCharges2()), w_addCharges2Label);
setup(w_credit, w_creditCopy, Currency::toDisplayString(order->credit(), { }, 2),
!qFuzzyIsNull(order->credit()), w_creditLabel);
setup(w_creditCoupon, w_creditCouponCopy, Currency::toDisplayString(order->creditCoupon(), { }, 2),
!qFuzzyIsNull(order->creditCoupon()), w_creditCouponLabel);
setup(w_total, w_totalCopy, Currency::toDisplayString(order->orderTotal(), { }, 2));
setup(w_salesTax, w_salesTaxCopy, Currency::toDisplayString(order->usSalesTax(), { }, 2),
!qFuzzyIsNull(order->usSalesTax()), w_salesTaxLabel);
setup(w_grandTotal, w_grandTotalCopy, Currency::toDisplayString(order->grandTotal(), { }, 2));
setup(w_vatSeller, w_vatSellerCopy, Currency::toDisplayString(order->vatChargeSeller(), { }, 2),
!qFuzzyIsNull(order->vatChargeSeller()), w_vatSellerLabel);
setup(w_vatBL, w_vatBLCopy, Currency::toDisplayString(order->vatChargeBrickLink(), { }, 2),
!qFuzzyIsNull(order->vatChargeBrickLink()), w_vatBLLabel);
setup(w_credit, w_creditCopy, Currency::toDisplayString(order->credit(), { }, 2),
!qFuzzyIsNull(order->credit()), w_creditLabel);
setup(w_creditCoupon, w_creditCouponCopy, Currency::toDisplayString(order->creditCoupon(), { }, 2),
!qFuzzyIsNull(order->creditCoupon()), w_creditCouponLabel);
setup(w_total, w_totalCopy, Currency::toDisplayString(order->orderTotal(), { }, 2));
setup(w_salesTax, w_salesTaxCopy, Currency::toDisplayString(order->usSalesTax(), { }, 2),
!qFuzzyIsNull(order->usSalesTax()), w_salesTaxLabel);
setup(w_grandTotal, w_grandTotalCopy, Currency::toDisplayString(order->grandTotal(), { }, 2));
setup(w_grossGrandTotal, w_grossGrandTotalCopy, Currency::toDisplayString(gtGross, { }, 2),
hasVat, w_grossGrandTotalLabel);
setup(w_netGrandTotal, w_netGrandTotalCopy, Currency::toDisplayString(gtNet, { }, 2),
hasVat, w_netGrandTotalLabel);
setup(w_vat, w_vatCopy, Currency::toDisplayString(vatCharge, { }, 2),
hasVat, w_vatLabel);
}

0 comments on commit bee7846

Please sign in to comment.