Skip to content

Commit

Permalink
fix: round error on refunded taxes
Browse files Browse the repository at this point in the history
Change-Id: Icbe4dbafa324cfc9241fc8809dfa866adfaaed29
  • Loading branch information
smarcet committed Apr 10, 2024
1 parent b15e2bd commit 6b21b0d
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 11 deletions.
4 changes: 3 additions & 1 deletion Libs/ModelSerializers/AbstractSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ private function get_class_lineage($object):array
const StringType = 'json_string';
const IntType = 'json_int';
const FloatType = 'json_float';
const MoneyType = 'json_money';
const ObfuscatedEmailType = 'json_obfuscated_email';
const UrlType = 'json_url';
const ColorType = 'json_color';
Expand All @@ -249,6 +250,7 @@ private function get_class_lineage($object):array
self::UrlType,
self::ColorType,
self::JsonStringArray,
self::MoneyType
];

/**
Expand Down Expand Up @@ -352,7 +354,7 @@ public function serialize($expand = null, array $fields = [], array $relations =
break;
case 'json_money':
{
$value = JsonUtils::toJsonMoney($value);
$value = JsonUtils::toJsonMoney($value, count($mapping) > 2 ? intval($mapping[2]) : 2);
}
break;
case 'json_obfuscated_email':
Expand Down
11 changes: 7 additions & 4 deletions Libs/Utils/JsonUtils.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace libs\utils;
use Aws\S3\Exception\PermanentRedirectException;

/**
* Copyright 2015 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -113,13 +115,14 @@ public static function toJsonFloat($value)
}

/**
* @param string $value
* @return float|null
* @param $value
* @param int $precision
* @return float
*/
public static function toJsonMoney($value)
public static function toJsonMoney($value, int $precision = 2 )
{
if(empty($value)) return 0.00;
return floatval(round($value, 2, PHP_ROUND_HALF_UP));
return floatval(round($value, $precision, PHP_ROUND_HALF_UP));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ class SummitRefundRequestSerializer extends SilverStripeSerializer
'RequestedById' => 'requested_by_id:json_int',
'ActionById' => 'action_by_id:json_int',
'ActionDate' => 'action_date:datetime_epoch',
'RefundedAmount' => 'refunded_amount:json_float',
'TaxesRefundedAmount' => 'taxes_refunded_amount:json_float',
'TotalRefundedAmount' => 'total_refunded_amount:json_float',
'RefundedAmount' => 'refunded_amount:json_money',
'RefundedAmountInCents' => 'refunded_amount_in_cents:json_int',
'TaxesRefundedAmount' => 'taxes_refunded_amount:json_money',
'TaxesRefundedAmountInCents' => 'taxes_refunded_amount_in_cents:json_int',
'TotalRefundedAmount' => 'total_refunded_amount:json_money',
'TotalRefundedAmountInCents' => 'total_refunded_amount_in_cents:json_int',
'Notes' => 'notes:json_string',
'PaymentGatewayResult' => 'payment_gateway_result:json_string',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ final class SummitTaxRefundSerializer extends SilverStripeSerializer
protected static $array_mappings = [
'TaxId' => 'tax_id:json_int',
'RefundRequestId' => 'refund_request_id:json_int',
'RefundedAmount' => 'refunded_amount:json_float',
'RefundedAmount' => 'refunded_amount:json_money:10',
'RefundedAmountInCents' => 'refunded_amount_in_cents:json_int',
];

protected static $expand_mappings = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,14 @@ public function getTotalRefundedAmount(): float {
return $this->refunded_amount + $this->getTaxesRefundedAmount();
}

/*
* Total amount refunded Ticket Price + Tax/Fee price
* @return int
*/
public function getTotalRefundedAmountInCents(): int {
return self::convertToCents($this->getTotalRefundedAmount());
}

public function getTaxesRefundedAmount():float{
$taxes_refund_amount = 0.0;
foreach ($this->refunded_taxes as $tax_refund)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public function getRefundedAmount(): float
return $this->refunded_amount;
}

public function getRefundedAmountInCents(): int
{
return self::convertToCents($this->refunded_amount);
}

public function getTax(): SummitTaxType
{
return $this->tax;
Expand Down
5 changes: 3 additions & 2 deletions app/Models/Utils/Traits/FinancialTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ static function isZeroDecimalCurrency(string $currency): bool

/**
* @param float $value
* @param bool $should_round
* @return int
*/
static function convertToCents(float $value):int{
return intval(round($value * 100));
static function convertToCents(float $value, bool $should_round = true):int{
return $should_round ? intval(round($value * 100)) : intval($value * 100);
}

/**
Expand Down
42 changes: 42 additions & 0 deletions database/migrations/model/Version20240410135620.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php namespace Database\Migrations\Model;
/**
* Copyright 2024 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use Doctrine\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema as Schema;
/**
* Class Version20240410135620
* @package Database\Migrations\Model
*/
final class Version20240410135620 extends AbstractMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema): void
{
$sql = <<<SQL
ALTER TABLE `SummitTaxRefund` CHANGE `RefundedAmount` `RefundedAmount` DECIMAL(32,10) NOT NULL DEFAULT '0.0000000000';
ALTER TABLE `SummitRefundRequest` CHANGE `RefundedAmount` `RefundedAmount` DECIMAL(32,10) NOT NULL DEFAULT '0.0000000000';
SQL;

$this->addSql($sql);
}

/**
* @param Schema $schema
*/
public function down(Schema $schema): void
{

}
}

0 comments on commit 6b21b0d

Please sign in to comment.