From 653f67c7b805a55213becc2724181e90850e291f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Werner=20Krau=C3=9F?= Date: Wed, 17 Jan 2024 08:47:26 +0100 Subject: [PATCH] Fix currency formatting (#814) * Order: helper method for nice formatting of Total in GridFields See #811 Can be removed if Total() works like other DB Fields * OrderModifier: disable Amount() method See #811 * OrderModifier: show short class name in summary fields * shop reports: format currency values as currency * fix linting errors --- client/dist/css/shopcms.css | 9 ++++++ lang/de.yml | 1 + lang/en.yml | 1 + lang/hr.yml | 1 + lang/nb.yml | 1 + lang/nl.yml | 1 + lang/nn.yml | 1 + src/Model/Modifiers/OrderModifier.php | 40 +++++++++++++-------------- src/Model/Order.php | 24 ++++++++++++++-- src/Reports/AbandonedCartReport.php | 5 +++- src/Reports/CustomerReport.php | 5 +++- src/Reports/ProductReport.php | 10 +++++-- src/Reports/ShopSalesReport.php | 5 +++- src/Reports/TaxReport.php | 15 ++++++++-- 14 files changed, 89 insertions(+), 30 deletions(-) diff --git a/client/dist/css/shopcms.css b/client/dist/css/shopcms.css index f51fadd77..c8ee31824 100644 --- a/client/dist/css/shopcms.css +++ b/client/dist/css/shopcms.css @@ -43,3 +43,12 @@ Integrated webfont. Created via http://fontello.com/ .shop-order__address { vertical-align: top; } + + td.col-TotalNice, + td.col-TotalValue, + td.col-Amount-Nice, + td.col-BasePrice, + td.col-Sales, + td.col-Spent { + text-align: right; + } diff --git a/lang/de.yml b/lang/de.yml index 8df4ba880..612c7076c 100644 --- a/lang/de.yml +++ b/lang/de.yml @@ -243,6 +243,7 @@ de: ShipTo: 'Lieferung an' SubTotal: Zwischensumme Total: Gesamt + TotalNice: Gesamt TotalOutstanding: 'Gesamt ausstehend' TotalPrice: 'Gesamtpreis' TotalPriceWithCurrency: 'Gesamtpreis ({Currency})' diff --git a/lang/en.yml b/lang/en.yml index 70e61e23a..8b39af8c7 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -261,6 +261,7 @@ en: db_Status: Status db_Surname: Surname db_Total: Total + TotalNice: Total has_many_Items: Items has_many_Modifiers: Modifiers has_many_OrderStatusLogs: 'Order Status Logs' diff --git a/lang/hr.yml b/lang/hr.yml index 2e1eb5b24..199078011 100644 --- a/lang/hr.yml +++ b/lang/hr.yml @@ -211,6 +211,7 @@ hr: ShipTo: 'Pošalji na' SubTotal: Međuzbroj Total: Ukupno + TotalNice: Ukupno TotalOutstanding: 'Ukupno nepodmireno' TotalPrice: 'Ukupna cijena' TotalPriceWithCurrency: 'Ukupna cijena ({Currency})' diff --git a/lang/nb.yml b/lang/nb.yml index d08ce8ec4..7f7c67d3a 100644 --- a/lang/nb.yml +++ b/lang/nb.yml @@ -243,6 +243,7 @@ nb: ShipTo: 'Send til' SubTotal: Sum total Total: Pris + TotalNice: Pris TotalOutstanding: 'Total utestående' TotalPrice: 'Pris' TotalPriceWithCurrency: 'Pris ({Currency})' diff --git a/lang/nl.yml b/lang/nl.yml index 8e5fced49..e58aad621 100644 --- a/lang/nl.yml +++ b/lang/nl.yml @@ -243,6 +243,7 @@ nl: ShipTo: 'Verzend naar' SubTotal: Subtotaal Total: Totaal + TotalNice: Totaal TotalOutstanding: 'Totaal openstaand' TotalPrice: 'Totaalprijs' TotalPriceWithCurrency: 'Totaalprijs ({Currency})' diff --git a/lang/nn.yml b/lang/nn.yml index 7d1eb345c..80393ccc3 100644 --- a/lang/nn.yml +++ b/lang/nn.yml @@ -241,6 +241,7 @@ nn: ShipTo: 'Send sending til:' SubTotal: Sum total Total: Total + TotalNice: Total TotalOutstanding: 'Total uteståande' TotalPrice: 'Total pris' TotalPriceWithCurrency: 'Total pris ({Currency})' diff --git a/src/Model/Modifiers/OrderModifier.php b/src/Model/Modifiers/OrderModifier.php index d448359b2..27aea588f 100644 --- a/src/Model/Modifiers/OrderModifier.php +++ b/src/Model/Modifiers/OrderModifier.php @@ -48,8 +48,8 @@ class OrderModifier extends OrderAttribute private static $summary_fields = [ 'Order.ID' => 'Order ID', 'TableTitle' => 'Table Title', - 'ClassName' => 'Type', - 'Amount' => 'Amount', + 'ClassName.ShortName' => 'Type', + 'Amount.Nice' => 'Amount', 'Type' => 'Type', ]; @@ -121,24 +121,24 @@ public function valid() return true; } - /** - * This function is always called to determine the - * amount this modifier needs to charge or deduct. - * - * If the modifier exists in the DB, in which case it - * already exists for a given order, we just return - * the Amount data field from the DB. This is for - * existing orders. - * - * If this is a new order, and the modifier doesn't - * exist in the DB ($this->ID is 0), so we return - * the amount from $this->LiveAmount() which is a - * calculation based on the order and it's items. - */ - public function Amount() - { - return $this->Amount; - } +// /** +// * This function is always called to determine the +// * amount this modifier needs to charge or deduct. +// * +// * If the modifier exists in the DB, in which case it +// * already exists for a given order, we just return +// * the Amount data field from the DB. This is for +// * existing orders. +// * +// * If this is a new order, and the modifier doesn't +// * exist in the DB ($this->ID is 0), so we return +// * the amount from $this->LiveAmount() which is a +// * calculation based on the order and it's items. +// */ +// public function Amount() +// { +// return $this->Amount; +// } /** * Monetary to use in templates. diff --git a/src/Model/Order.php b/src/Model/Order.php index 64c1f35e7..73cde315e 100644 --- a/src/Model/Order.php +++ b/src/Model/Order.php @@ -142,7 +142,7 @@ class Order extends DataObject 'Placed', 'Name', 'LatestEmail', - 'Total', + 'TotalNice', 'StatusI18N', ]; @@ -343,6 +343,7 @@ public function fieldLabels($includerelations = true) $labels['Name'] = _t('SilverShop\Generic.Customer', 'Customer'); $labels['LatestEmail'] = _t(__CLASS__ . '.db_Email', 'Email'); $labels['StatusI18N'] = _t(__CLASS__ . '.db_Status', 'Status'); + $labels['TotalNice'] = _t(__CLASS__ . '.TotalNice', 'Total'); return $labels; } @@ -482,6 +483,25 @@ public function GrandTotal() return $this->Total(); } + /** + * Helper method for getting nice currency-formatted values. + * + * This is needed, cause Total() conflicts with casting to Currency. + * Related Issue: https://github.com/silvershop/silvershop-core/issues/811 + * + * Can be deprecated and removed once the issue is resolved. + * + * @return string + */ + public function TotalNice(): string + { + $total = $this->dbObject('Total'); + if ($total instanceof DBCurrency) { + return $total->Nice(); + } + return ''; + } + /** * Calculate how much is left to be paid on the order. * Enforces rounding precision. @@ -518,7 +538,7 @@ public function getStatusI18N() public function Link() { $link = CheckoutPage::find_link(false, 'order', $this->ID); - + if (Security::getCurrentUser()) { $link = Controller::join_links(AccountPage::find_link(), 'order', $this->ID); } diff --git a/src/Reports/AbandonedCartReport.php b/src/Reports/AbandonedCartReport.php index b39361657..7a39d81fd 100644 --- a/src/Reports/AbandonedCartReport.php +++ b/src/Reports/AbandonedCartReport.php @@ -29,7 +29,10 @@ public function columns() return [ 'FilterPeriod' => $period, 'Count' => 'Count', - 'TotalValue' => 'Total Value', + 'TotalValue' => [ + 'title' => 'Total Value', + 'casting' => 'Currency->Nice' + ] ]; } diff --git a/src/Reports/CustomerReport.php b/src/Reports/CustomerReport.php index e1eb3ed5e..4c7f2de1f 100644 --- a/src/Reports/CustomerReport.php +++ b/src/Reports/CustomerReport.php @@ -28,7 +28,10 @@ public function columns() 'Surname' => 'Surname', 'Email' => 'Email', 'Created' => 'Joined', - 'Spent' => 'Spent', + 'Spent' => [ + 'title' =>'Spent', + 'casting' => 'Currency->Nice' + ], 'Orders' => 'Orders', 'edit' => [ 'title' => 'Edit', diff --git a/src/Reports/ProductReport.php b/src/Reports/ProductReport.php index 70584364d..2bbd00925 100644 --- a/src/Reports/ProductReport.php +++ b/src/Reports/ProductReport.php @@ -25,9 +25,15 @@ public function columns() 'title' => 'Title', 'formatting' => '$Title', ], - 'BasePrice' => 'Price', + 'BasePrice' => [ + 'title' => 'Price', + 'casting' => 'Currency->Nice' + ], 'Quantity' => 'Quantity', - 'Sales' => 'Sales', + 'Sales' => [ + 'title' => 'Total Sales', + 'casting' => 'Currency->Nice' + ], ]; } diff --git a/src/Reports/ShopSalesReport.php b/src/Reports/ShopSalesReport.php index eb40c4686..360dba1e8 100644 --- a/src/Reports/ShopSalesReport.php +++ b/src/Reports/ShopSalesReport.php @@ -31,7 +31,10 @@ public function columns() return [ 'FilterPeriod' => $period, 'Count' => 'Order Count', - 'Sales' => 'Total Sales', + 'Sales' => [ + 'title' => 'Total Sales', + 'casting' => 'Currency->Nice' + ], ]; } diff --git a/src/Reports/TaxReport.php b/src/Reports/TaxReport.php index 605de284c..124f24afb 100644 --- a/src/Reports/TaxReport.php +++ b/src/Reports/TaxReport.php @@ -29,8 +29,14 @@ public function columns() return [ 'FilterPeriod' => $period, 'Count' => 'Order Count', - 'Sales' => 'Total Sales', - 'Tax' => 'Total Tax', + 'Sales' => [ + 'title' => 'Total Sales', + 'casting' => 'Currency->Nice' + ], + 'Tax' => [ + 'title' => 'Total Tax', + 'casting' => 'Currency->Nice' + ] ]; } @@ -41,7 +47,10 @@ public function query($params) 'SilverShop_OrderAttribute', '"SilverShop_OrderAttribute"."OrderID" = "SilverShop_Order"."ID" AND "SilverShop_OrderAttribute"."ClassName" LIKE \'%TaxModifier\'' ) - ->addInnerJoin('SilverShop_OrderModifier', '"SilverShop_OrderModifier"."ID" = "SilverShop_OrderAttribute"."ID"') + ->addInnerJoin( + 'SilverShop_OrderModifier', + '"SilverShop_OrderModifier"."ID" = "SilverShop_OrderAttribute"."ID"' + ) ->selectField('COUNT("SilverShop_Order"."ID")', 'Count') ->selectField('SUM("SilverShop_OrderModifier"."Amount")', 'Tax') ->selectField('SUM("SilverShop_Order"."Total")', 'Sales');