Skip to content

Commit

Permalink
Fix currency formatting (#814)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
wernerkrauss committed Feb 20, 2024
1 parent cd9cb74 commit 653f67c
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 30 deletions.
9 changes: 9 additions & 0 deletions client/dist/css/shopcms.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lang/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ de:
ShipTo: 'Lieferung an'
SubTotal: Zwischensumme
Total: Gesamt
TotalNice: Gesamt
TotalOutstanding: 'Gesamt ausstehend'
TotalPrice: 'Gesamtpreis'
TotalPriceWithCurrency: 'Gesamtpreis ({Currency})'
Expand Down
1 change: 1 addition & 0 deletions lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions lang/hr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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})'
Expand Down
1 change: 1 addition & 0 deletions lang/nb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ nb:
ShipTo: 'Send til'
SubTotal: Sum total
Total: Pris
TotalNice: Pris
TotalOutstanding: 'Total utestående'
TotalPrice: 'Pris'
TotalPriceWithCurrency: 'Pris ({Currency})'
Expand Down
1 change: 1 addition & 0 deletions lang/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ nl:
ShipTo: 'Verzend naar'
SubTotal: Subtotaal
Total: Totaal
TotalNice: Totaal
TotalOutstanding: 'Totaal openstaand'
TotalPrice: 'Totaalprijs'
TotalPriceWithCurrency: 'Totaalprijs ({Currency})'
Expand Down
1 change: 1 addition & 0 deletions lang/nn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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})'
Expand Down
40 changes: 20 additions & 20 deletions src/Model/Modifiers/OrderModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];

Expand Down Expand Up @@ -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.
Expand Down
24 changes: 22 additions & 2 deletions src/Model/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class Order extends DataObject
'Placed',
'Name',
'LatestEmail',
'Total',
'TotalNice',
'StatusI18N',
];

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Reports/AbandonedCartReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public function columns()
return [
'FilterPeriod' => $period,
'Count' => 'Count',
'TotalValue' => 'Total Value',
'TotalValue' => [
'title' => 'Total Value',
'casting' => 'Currency->Nice'
]
];
}

Expand Down
5 changes: 4 additions & 1 deletion src/Reports/CustomerReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
10 changes: 8 additions & 2 deletions src/Reports/ProductReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@ public function columns()
'title' => 'Title',
'formatting' => '<a href=\"admin/catalog/Product/EditForm/field/Product/item/$ID/edit\" target=\"_new\">$Title</a>',
],
'BasePrice' => 'Price',
'BasePrice' => [
'title' => 'Price',
'casting' => 'Currency->Nice'
],
'Quantity' => 'Quantity',
'Sales' => 'Sales',
'Sales' => [
'title' => 'Total Sales',
'casting' => 'Currency->Nice'
],
];
}

Expand Down
5 changes: 4 additions & 1 deletion src/Reports/ShopSalesReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public function columns()
return [
'FilterPeriod' => $period,
'Count' => 'Order Count',
'Sales' => 'Total Sales',
'Sales' => [
'title' => 'Total Sales',
'casting' => 'Currency->Nice'
],
];
}

Expand Down
15 changes: 12 additions & 3 deletions src/Reports/TaxReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
];
}

Expand All @@ -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');
Expand Down

0 comments on commit 653f67c

Please sign in to comment.