Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

インボイス対応 #762

Merged
merged 25 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
28af248
インボイス対応
nanasess Sep 1, 2023
1077a19
Fix reduced total
nanasess Sep 3, 2023
9fde0f2
税額集計と内訳出力のメソッドを分割
nanasess Sep 8, 2023
70717b9
Add unit test
nanasess Sep 8, 2023
c654b70
debian-stretch が archive へ移動したため sources.list を修正
nanasess Sep 13, 2023
4a12b8b
Add PHP8.2
nanasess Sep 16, 2023
f2e7bb9
Merge pull request #127 from nanasess/add-php8.2-image
nanasess Sep 16, 2023
065f6f7
Merge pull request #128 from nanasess/use-debian-archive
nanasess Sep 16, 2023
b134893
build-args を修正
nanasess Sep 16, 2023
8f50a61
Merge pull request #129 from nanasess/use-debian-archive
nanasess Sep 16, 2023
9e56bdf
build-args を修正
nanasess Sep 16, 2023
e24f48d
Merge branch 'master' into use-debian-archive
nanasess Sep 16, 2023
5ef4289
Merge pull request #130 from nanasess/use-debian-archive
nanasess Sep 16, 2023
0945ae1
build-args を修正
nanasess Sep 16, 2023
14e026c
Merge branch 'master' into use-debian-archive
nanasess Sep 16, 2023
0b47729
Merge pull request #131 from nanasess/use-debian-archive
nanasess Sep 16, 2023
988f46c
Fix Division by zero on PHP8.0+
nanasess Sep 19, 2023
ca3834f
Merge remote-tracking branch 'origin/master' into improve/invoice
nanasess Oct 24, 2023
06c4356
Merge branch 'master' into improve/invoice
nanasess Oct 24, 2023
f9e1d19
Update data/class/helper/SC_Helper_TaxRule.php
nanasess Jan 23, 2024
29ed7c5
Update data/class/helper/SC_Helper_TaxRule.php
nanasess Jan 24, 2024
61c481c
Update data/class/SC_Fpdf.php
nanasess Jan 29, 2024
4899651
Merge remote-tracking branch 'upstream/master' into improve/invoice
nanasess Feb 4, 2024
ae08724
Fix divison by zero and format
nanasess Feb 4, 2024
f99e6c0
再現テスト追加
nanasess Feb 4, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

<!--{/section}-->
-------------------------------------------------
小 計 ¥<!--{$arrOrder.subtotal|n2s|default:0}--> <!--{if 0 < $arrOrder.tax}-->(うち消費税 ¥<!--{$arrOrder.tax|n2s|default:0}-->)<!--{/if}-->
小 計 ¥<!--{$arrOrder.subtotal|n2s|default:0}-->

<!--{if $arrOrder.use_point > 0}-->
値引き ¥<!--{$arrOrder.use_point*$smarty.const.POINT_VALUE+$arrOrder.discount|n2s|default:0}-->
Expand Down
2 changes: 1 addition & 1 deletion data/Smarty/templates/mobile/mail_templates/order_mail.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

<!--{/section}-->

小 計 ¥<!--{$arrOrder.subtotal|n2s|default:0}--> <!--{if 0 < $arrOrder.tax}-->(うち消費税 ¥<!--{$arrOrder.tax|n2s|default:0}-->)<!--{/if}-->
小 計 ¥<!--{$arrOrder.subtotal|n2s|default:0}-->

<!--{if $arrOrder.use_point > 0}-->
値引き ¥<!--{$arrOrder.use_point*$smarty.const.POINT_VALUE+$arrOrder.discount|n2s|default:0}-->
Expand Down
37 changes: 36 additions & 1 deletion data/class/SC_Fpdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@
//ロゴ画像
$logo_file = PDF_TEMPLATE_REALDIR . 'logo.png';
$this->Image($logo_file, 124, 46, 40);

if (defined('INVOICE_REGISTRATION_NUM')) {
$text = '登録番号: '.INVOICE_REGISTRATION_NUM;
$this->lfText(125, 87, $text, 8);
}
}

private function setMessageData()
Expand Down Expand Up @@ -228,6 +233,8 @@
$monetary_unit = '円';
$point_unit = 'Pt';

$arrTaxableTotal = [];
$defaultTaxRule = SC_Helper_TaxRule_Ex::getTaxRule();
// 購入商品情報
for ($i = 0; $i < count($this->arrDisp['quantity']); $i++) {
// 購入数量
Expand All @@ -249,9 +256,18 @@
$arrOrder[$i][0] .= ' * '.$this->arrDisp['classcategory_name2'][$i].' ]';
}
}

// 標準税率より低い税率は軽減税率として※を付与
if ($this->arrDisp['tax_rate'][$i] < $defaultTaxRule['tax_rate']) {
$arrOrder[$i][0] .= ' ※';
}
$arrOrder[$i][1] = number_format($data[0]);
$arrOrder[$i][2] = number_format($data[1]).$monetary_unit;
$arrOrder[$i][3] = number_format($data[2]).$monetary_unit;
if (array_key_exists($this->arrDisp['tax_rate'][$i], $arrTaxableTotal) === false) {
$arrTaxableTotal[$this->arrDisp['tax_rate'][$i]] = 0;
}
$arrTaxableTotal[$this->arrDisp['tax_rate'][$i]] += $data[2];
}

$arrOrder[$i][0] = '';
Expand All @@ -270,18 +286,21 @@
$arrOrder[$i][1] = '';
$arrOrder[$i][2] = '送料';
$arrOrder[$i][3] = number_format($this->arrDisp['deliv_fee']).$monetary_unit;
$arrTaxableTotal[intval($defaultTaxRule['tax_rate'])] += $this->arrDisp['deliv_fee'];

$i++;
$arrOrder[$i][0] = '';
$arrOrder[$i][1] = '';
$arrOrder[$i][2] = '手数料';
$arrOrder[$i][3] = number_format($this->arrDisp['charge']).$monetary_unit;
$arrTaxableTotal[intval($defaultTaxRule['tax_rate'])] += $this->arrDisp['charge'];

$i++;
$arrOrder[$i][0] = '';
$arrOrder[$i][1] = '';
$arrOrder[$i][2] = '値引き';
$arrOrder[$i][3] = '- '.number_format(($this->arrDisp['use_point'] * POINT_VALUE) + $this->arrDisp['discount']).$monetary_unit;
$discount_total = ($this->arrDisp['use_point'] * POINT_VALUE) + $this->arrDisp['discount'];
$arrOrder[$i][3] = '- '.number_format($discount_total).$monetary_unit;

$i++;
$arrOrder[$i][0] = '';
Expand Down Expand Up @@ -311,6 +330,22 @@
}

$this->FancyTable($this->label_cell, $arrOrder, $this->width_cell);

$this->SetLineWidth(.3);
$this->SetFont('SJIS', '', 6);

$this->Cell(0, 0, '', 0, 1, 'C', 0, '');
// 行頭近くの場合、表示崩れがあるためもう一個字下げする
if (270 <= $this->GetY()) {
$this->Cell(0, 0, '', 0, 1, 'C', 0, '');
}
$width = array_reduce($this->width_cell, function ($n, $w) {
return $n + $w;
});
$this->SetX(20);

$message = SC_Helper_TaxRule_Ex::getTaxDetail($arrTaxableTotal, $discount_total);
$this->MultiCell($width, 4, $message, 0, 'R', 0, '');

Check failure on line 348 in data/class/SC_Fpdf.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method PDF_Japanese::MultiCell() invoked with 7 parameters, 3-6 required.
nanasess marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
56 changes: 56 additions & 0 deletions data/class/helper/SC_Helper_TaxRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,62 @@
return SC_Helper_TaxRule_Ex::calcTax($price, $arrTaxRule['tax_rate'], $arrTaxRule['tax_rule'], $arrTaxRule['tax_adjust']);
}

/**
* 消費税の内訳を返す.
*
* 税率ごとに以下のような連想配列を返す.
* - total: 値引後の税込み合計金額
* - tax: 値引後の税額
* 値引額合計は税率ごとに按分する.
* 課税規則は標準税率の設定を使用する.
*
* @param array{8?:int, 10?:int} $arrTaxableTotal 税率ごとのお支払い合計金額
* @param int $discount_total 値引額合計
* @return array{8?:array{total:int,tax:int}, 10?:array{total:int,tax:int}}
*/
public static function getTaxPerTaxRate(array $arrTaxableTotal, $discount_total = 0)
{
$arrDefaultTaxRule = static::getTaxRule();

ksort($arrTaxableTotal);
$tax = [];
$taxable_total = array_sum($arrTaxableTotal);
$result = [];
foreach ($arrTaxableTotal as $rate => $total) {
if ($taxable_total > 0) {
$reduced_total = $total - $discount_total * $total / $taxable_total;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$reduced_total = $total - $discount_total * $total / $taxable_total;
$reduced_total = $total - static::roundByCalcRule($discount_total * $total / $taxable_total, $arrDefaultTaxRule['calc_rule']);

}

$tax = $reduced_total * ($rate / (100 + $rate));

Check failure on line 91 in data/class/helper/SC_Helper_TaxRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Variable $reduced_total might not be defined.
$result[$rate] = [
'total' => intval(static::roundByCalcRule($reduced_total, $arrDefaultTaxRule['calc_rule'])),

Check failure on line 93 in data/class/helper/SC_Helper_TaxRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Variable $reduced_total might not be defined.
'tax' => intval(static::roundByCalcRule($tax, $arrDefaultTaxRule['calc_rule'])),
];
}

return $result;
}
nanasess marked this conversation as resolved.
Show resolved Hide resolved

/**
* 消費税の内訳の文字列を返す.
*
* 複数の税率がある場合は改行で区切る.
*
* @param array{8?:int, 10?:int} $arrTaxableTotal 税率ごとのお支払い合計金額
* @param int $discount_total 値引額合計
* @return string (<税率>%対象: <値引後税込合計>円 内消費税: <値引後税額>円)
*/
public static function getTaxDetail($arrTaxableTotal, $discount_total = 0)
{
$arrTaxPerTaxRate = static::getTaxPerTaxRate($arrTaxableTotal, $discount_total);
$result = '';
foreach ($arrTaxPerTaxRate as $rate => $item) {
$result .= '('.$rate .'%対象: '. number_format($item['total']).'円 内消費税: '.number_format($item['tax']).'円)'.PHP_EOL;
}

return $result;
}

/**
* 設定情報IDに基づいて税金付与した金額を返す
* (受注データのようにルールが決まっている場合用)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @author Nobuhiko Kimoto
* @version $Id$
*/
class SC_Helper_TaxRule_TestBase extends Common_TestCase
abstract class SC_Helper_TaxRule_TestBase extends Common_TestCase
{

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
<?php
require __DIR__.'/SC_Helper_TaxRule_TestBase.php';

class SC_Helper_TaxRule_getDetailTest extends SC_Helper_TaxRule_TestBase
{
private $taxs = [];
protected function setUp()
{
parent::setUp();
}

protected function tearDown()
{
parent::tearDown();
}

public function testGetTaxPerTaxRateWithRound()
{
$this->setUpTaxRule([
[
'tax_rule_id' => 1004,
'apply_date' => '2019-10-01 00:00:00',
'tax_rate' => '10',
'calc_rule' => '1',
'product_id' => '0',
'product_class_id' => '0',
'del_flg' => '0',
'member_id' => 1,
'create_date' => '2000-01-01 00:00:00',
'update_date' => '2000-01-01 00:00:00',
],
]);

$arrTaxableTotal = [
10 => 724431,
8 => 65756,
];
$discount_total = 7159;

$actual = SC_Helper_TaxRule_Ex::getTaxPerTaxRate($arrTaxableTotal, $discount_total);
self::assertSame(
[
8 => [
'total' => 65160,
'tax' => 4827
],
10 => [
'total' => 717868,
'tax' => 65261
]
],
$actual
);

self::assertSame(
'(8%対象: 65,160円 内消費税: 4,827円)'.PHP_EOL.
'(10%対象: 717,868円 内消費税: 65,261円)'.PHP_EOL,
SC_Helper_TaxRule_Ex::getTaxDetail($arrTaxableTotal, $discount_total)
);
}

public function testGetTaxPerTaxRateWithZero()
{
$this->setUpTaxRule([
[
'tax_rule_id' => 1004,
'apply_date' => '2019-10-01 00:00:00',
'tax_rate' => '10',
'calc_rule' => '1',
'product_id' => '0',
'product_class_id' => '0',
'del_flg' => '0',
'member_id' => 1,
'create_date' => '2000-01-01 00:00:00',
'update_date' => '2000-01-01 00:00:00',
],
]);

$arrTaxableTotal = [
10 => 0,
8 => 0,
];
$discount_total = 0;

$actual = SC_Helper_TaxRule_Ex::getTaxPerTaxRate($arrTaxableTotal, $discount_total);
self::assertSame(
[
8 => [
'total' => 0,
'tax' => 0
],
10 => [
'total' => 0,
'tax' => 0
]
],
$actual
);

self::assertSame(
'(8%対象: 0円 内消費税: 0円)'.PHP_EOL.
'(10%対象: 0円 内消費税: 0円)'.PHP_EOL,
SC_Helper_TaxRule_Ex::getTaxDetail($arrTaxableTotal, $discount_total)
);
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGetTaxPerTaxRateWithFloor()
{
self::markTestSkipped('Skip this test because @runInSeparateProcess does not work properly');

$this->setUpTaxRule([
[
'tax_rule_id' => 1004,
'apply_date' => '2019-10-01 00:00:00',
'tax_rate' => '10',
'calc_rule' => '2', // floor
'product_id' => '0',
'product_class_id' => '0',
'del_flg' => '0',
'member_id' => 1,
'create_date' => '2000-01-01 00:00:00',
'update_date' => '2000-01-01 00:00:00',
],
]);

$arrTaxableTotal = [
10 => 724431,
8 => 65756,
];
$discount_total = 7159;

$actual = SC_Helper_TaxRule_Ex::getTaxPerTaxRate($arrTaxableTotal, $discount_total);
self::assertSame(
[
8 => [
'total' => 65160,
'tax' => 4826
],
10 => [
'total' => 717867,
'tax' => 65260
]
],
$actual
);
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGetTaxPerTaxRateWithCeil()
{
self::markTestSkipped('Skip this test because @runInSeparateProcess does not work properly');

$this->setUpTaxRule([
[
'tax_rule_id' => 1004,
'apply_date' => '2019-10-01 00:00:00',
'tax_rate' => '10',
'calc_rule' => '3', // ceil
'product_id' => '0',
'product_class_id' => '0',
'del_flg' => '0',
'member_id' => 1,
'create_date' => '2000-01-01 00:00:00',
'update_date' => '2000-01-01 00:00:00',
],
]);

$arrTaxableTotal = [
10 => 724431,
8 => 65756,
];
$discount_total = 7159;

$actual = SC_Helper_TaxRule_Ex::getTaxPerTaxRate($arrTaxableTotal, $discount_total);
self::assertSame(
[
8 => [
'total' => 65161,
'tax' => 4827
],
10 => [
'total' => 717868,
'tax' => 65261
]
],
$actual
);
}

protected function setUpTaxRule(array $taxs = [])
{
$this->objQuery->delete('dtb_tax_rule');
foreach ($taxs as $key => $item) {
$this->objQuery->insert('dtb_tax_rule', $item);
}
}
}
Loading