Skip to content

Commit 6b21b0d

Browse files
committed
fix: round error on refunded taxes
Change-Id: Icbe4dbafa324cfc9241fc8809dfa866adfaaed29
1 parent b15e2bd commit 6b21b0d

File tree

8 files changed

+76
-11
lines changed

8 files changed

+76
-11
lines changed

Libs/ModelSerializers/AbstractSerializer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ private function get_class_lineage($object):array
234234
const StringType = 'json_string';
235235
const IntType = 'json_int';
236236
const FloatType = 'json_float';
237+
const MoneyType = 'json_money';
237238
const ObfuscatedEmailType = 'json_obfuscated_email';
238239
const UrlType = 'json_url';
239240
const ColorType = 'json_color';
@@ -249,6 +250,7 @@ private function get_class_lineage($object):array
249250
self::UrlType,
250251
self::ColorType,
251252
self::JsonStringArray,
253+
self::MoneyType
252254
];
253255

254256
/**
@@ -352,7 +354,7 @@ public function serialize($expand = null, array $fields = [], array $relations =
352354
break;
353355
case 'json_money':
354356
{
355-
$value = JsonUtils::toJsonMoney($value);
357+
$value = JsonUtils::toJsonMoney($value, count($mapping) > 2 ? intval($mapping[2]) : 2);
356358
}
357359
break;
358360
case 'json_obfuscated_email':

Libs/Utils/JsonUtils.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php namespace libs\utils;
2+
use Aws\S3\Exception\PermanentRedirectException;
3+
24
/**
35
* Copyright 2015 OpenStack Foundation
46
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -113,13 +115,14 @@ public static function toJsonFloat($value)
113115
}
114116

115117
/**
116-
* @param string $value
117-
* @return float|null
118+
* @param $value
119+
* @param int $precision
120+
* @return float
118121
*/
119-
public static function toJsonMoney($value)
122+
public static function toJsonMoney($value, int $precision = 2 )
120123
{
121124
if(empty($value)) return 0.00;
122-
return floatval(round($value, 2, PHP_ROUND_HALF_UP));
125+
return floatval(round($value, $precision, PHP_ROUND_HALF_UP));
123126
}
124127

125128
/**

app/ModelSerializers/Summit/Registration/Refunds/SummitRefundRequestSerializer.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ class SummitRefundRequestSerializer extends SilverStripeSerializer
2828
'RequestedById' => 'requested_by_id:json_int',
2929
'ActionById' => 'action_by_id:json_int',
3030
'ActionDate' => 'action_date:datetime_epoch',
31-
'RefundedAmount' => 'refunded_amount:json_float',
32-
'TaxesRefundedAmount' => 'taxes_refunded_amount:json_float',
33-
'TotalRefundedAmount' => 'total_refunded_amount:json_float',
31+
'RefundedAmount' => 'refunded_amount:json_money',
32+
'RefundedAmountInCents' => 'refunded_amount_in_cents:json_int',
33+
'TaxesRefundedAmount' => 'taxes_refunded_amount:json_money',
34+
'TaxesRefundedAmountInCents' => 'taxes_refunded_amount_in_cents:json_int',
35+
'TotalRefundedAmount' => 'total_refunded_amount:json_money',
36+
'TotalRefundedAmountInCents' => 'total_refunded_amount_in_cents:json_int',
3437
'Notes' => 'notes:json_string',
3538
'PaymentGatewayResult' => 'payment_gateway_result:json_string',
3639
];

app/ModelSerializers/Summit/Registration/Refunds/SummitTaxRefundSerializer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ final class SummitTaxRefundSerializer extends SilverStripeSerializer
2323
protected static $array_mappings = [
2424
'TaxId' => 'tax_id:json_int',
2525
'RefundRequestId' => 'refund_request_id:json_int',
26-
'RefundedAmount' => 'refunded_amount:json_float',
26+
'RefundedAmount' => 'refunded_amount:json_money:10',
27+
'RefundedAmountInCents' => 'refunded_amount_in_cents:json_int',
2728
];
2829

2930
protected static $expand_mappings = [

app/Models/Foundation/Summit/Registration/Refunds/SummitRefundRequest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,14 @@ public function getTotalRefundedAmount(): float {
242242
return $this->refunded_amount + $this->getTaxesRefundedAmount();
243243
}
244244

245+
/*
246+
* Total amount refunded Ticket Price + Tax/Fee price
247+
* @return int
248+
*/
249+
public function getTotalRefundedAmountInCents(): int {
250+
return self::convertToCents($this->getTotalRefundedAmount());
251+
}
252+
245253
public function getTaxesRefundedAmount():float{
246254
$taxes_refund_amount = 0.0;
247255
foreach ($this->refunded_taxes as $tax_refund)

app/Models/Foundation/Summit/Registration/Refunds/SummitTaxRefund.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public function getRefundedAmount(): float
7878
return $this->refunded_amount;
7979
}
8080

81+
public function getRefundedAmountInCents(): int
82+
{
83+
return self::convertToCents($this->refunded_amount);
84+
}
85+
8186
public function getTax(): SummitTaxType
8287
{
8388
return $this->tax;

app/Models/Utils/Traits/FinancialTrait.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ static function isZeroDecimalCurrency(string $currency): bool
5050

5151
/**
5252
* @param float $value
53+
* @param bool $should_round
5354
* @return int
5455
*/
55-
static function convertToCents(float $value):int{
56-
return intval(round($value * 100));
56+
static function convertToCents(float $value, bool $should_round = true):int{
57+
return $should_round ? intval(round($value * 100)) : intval($value * 100);
5758
}
5859

5960
/**
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php namespace Database\Migrations\Model;
2+
/**
3+
* Copyright 2024 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
use Doctrine\Migrations\AbstractMigration;
15+
use Doctrine\DBAL\Schema\Schema as Schema;
16+
/**
17+
* Class Version20240410135620
18+
* @package Database\Migrations\Model
19+
*/
20+
final class Version20240410135620 extends AbstractMigration
21+
{
22+
/**
23+
* @param Schema $schema
24+
*/
25+
public function up(Schema $schema): void
26+
{
27+
$sql = <<<SQL
28+
ALTER TABLE `SummitTaxRefund` CHANGE `RefundedAmount` `RefundedAmount` DECIMAL(32,10) NOT NULL DEFAULT '0.0000000000';
29+
ALTER TABLE `SummitRefundRequest` CHANGE `RefundedAmount` `RefundedAmount` DECIMAL(32,10) NOT NULL DEFAULT '0.0000000000';
30+
SQL;
31+
32+
$this->addSql($sql);
33+
}
34+
35+
/**
36+
* @param Schema $schema
37+
*/
38+
public function down(Schema $schema): void
39+
{
40+
41+
}
42+
}

0 commit comments

Comments
 (0)