Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Forms/CheckoutComponentValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function php($data): bool
$valid = false;
}

if(!$valid) {
if (!$valid) {
$this->form->sessionMessage(
_t(
__CLASS__ . ".InvalidDataMessage",
Expand Down
15 changes: 14 additions & 1 deletion src/ORM/FieldType/ShopCurrency.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace SilverShop\ORM\FieldType;

use SilverStripe\Forms\FormField;
use SilverStripe\Forms\NumericField;
use SilverStripe\ORM\FieldType\DBCurrency;

/**
Expand Down Expand Up @@ -38,6 +40,11 @@ class ShopCurrency extends DBCurrency
*/
private static string $negative_value_format = '<span class="negative">(%s)</span>';

/**
* Number of decimal places to use for currency display and form fields
*/
private static int $decimals = 2;

private static array $casting = [
'forTemplate' => 'HTMLFragment',
'Nice' => 'HTMLFragment',
Expand All @@ -53,7 +60,7 @@ public function Nice(): string
$symbol = $this->config()->currency_symbol;
$val = number_format(
abs($this->value),
2,
self::config()->decimals,
self::config()->decimal_delimiter,
self::config()->thousand_delimiter
);
Expand All @@ -66,6 +73,12 @@ public function Nice(): string
return $val;
}

public function scaffoldFormField(?string $title = null, array $params = []): ?FormField
{
return NumericField::create($this->getName(), $title)
->setScale(self::config()->decimals);
}

public function forTemplate(): string
{
return $this->Nice();
Expand Down
2 changes: 1 addition & 1 deletion src/Tasks/CalculateProductPopularity.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ public function viasql(): void
DB::query($sql);
}
}
}
}
2 changes: 1 addition & 1 deletion src/Tasks/CartCleanupTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ protected function log(string $msg): void
{
$this->output->writeln($msg);
}
}
}
2 changes: 1 addition & 1 deletion src/Tasks/CustomersToGroupTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ protected function execute(InputInterface $input, PolyOutput $output): int
}
return Command::SUCCESS;
}
}
}
2 changes: 1 addition & 1 deletion src/Tasks/PopulateCartTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ protected function execute(InputInterface $input, PolyOutput $output): int
$output->writeln('Cart populated with ' . $count . ' random products.');
return Command::SUCCESS;
}
}
}
2 changes: 1 addition & 1 deletion src/Tasks/PopulateShopTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,4 @@ protected function execute(InputInterface $input, PolyOutput $output): int
$this->extend('afterPopulate');
return Command::SUCCESS;
}
}
}
2 changes: 1 addition & 1 deletion src/Tasks/RecalculateAllOrdersTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ protected function execute(InputInterface $input, PolyOutput $output): int
}
return Command::SUCCESS;
}
}
}
2 changes: 1 addition & 1 deletion src/Tasks/ShopEmailPreviewTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ protected function execute(InputInterface $input, PolyOutput $output): int

return Command::SUCCESS;
}
}
}
2 changes: 1 addition & 1 deletion src/Tasks/ShopMigrationTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,4 @@ public function migrateShippingTaxValues(): void
//DB::query("ALTER TABLE \"Order\" CHANGE COLUMN \"Shipping\" \"_obsolete_Shipping\" decimal(9,2)");
//DB::query("ALTER TABLE \"Order\" CHANGE COLUMN \"AddedTax\" \"_obsolete_AddedTax\" decimal(9,2)");
}
}
}
35 changes: 35 additions & 0 deletions tests/php/ORM/FieldType/ShopCurrencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use SilverShop\ORM\FieldType\ShopCurrency;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\NumericField;

final class ShopCurrencyTest extends SapphireTest
{
Expand All @@ -20,4 +21,38 @@ public function testField(): void
$shopCurrency->setValue(-12345.56);
$this->assertEquals("- X12-345|56", $shopCurrency->Nice());
}

public function testNiceUsesDecimalsConfig(): void
{
ShopCurrency::config()->currency_symbol = "$";
ShopCurrency::config()->decimal_delimiter = ".";
ShopCurrency::config()->thousand_delimiter = ",";
ShopCurrency::config()->decimals = 4;

$shopCurrency = ShopCurrency::create("Price");
$shopCurrency->setValue(12345.5678);
$this->assertEquals("$12,345.5678", $shopCurrency->Nice());
}

public function testScaffoldFormFieldReturnsNumericField(): void
{
ShopCurrency::config()->decimals = 2;

$shopCurrency = ShopCurrency::create("Price");
$field = $shopCurrency->scaffoldFormField("Price");

$this->assertInstanceOf(NumericField::class, $field);
$this->assertEquals(2, $field->getScale());
}

public function testScaffoldFormFieldRespectsDecimalsConfig(): void
{
ShopCurrency::config()->decimals = 4;

$shopCurrency = ShopCurrency::create("Price");
$field = $shopCurrency->scaffoldFormField("Price");

$this->assertInstanceOf(NumericField::class, $field);
$this->assertEquals(4, $field->getScale());
}
}
Loading