-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* B2B-158
- Loading branch information
Showing
27 changed files
with
1,991 additions
and
476 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
database/migrations/2024_09_04_000001_add_vat_rate_to_order.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use App\Models\Order; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up(): void | ||
{ | ||
Schema::table('orders', function (Blueprint $table) { | ||
$table->string('vat_rate', 9)->default('0'); | ||
}); | ||
|
||
Order::query()->with('salesChannel')->chunkById(100, function (Collection $orders) { | ||
/** @var Order $order */ | ||
foreach ($orders as $order) { | ||
$order->update(['vat_rate' => $order->salesChannel?->vat_rate ?? '0']); | ||
} | ||
}); | ||
|
||
Schema::table('prices', function (Blueprint $table) { | ||
$table->boolean('is_net')->default(true)->change(); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::table('orders', function (Blueprint $table) { | ||
$table->dropColumn('vat_rate'); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Domain\Order\Dtos; | ||
|
||
use Brick\Math\BigDecimal; | ||
use Brick\Math\RoundingMode; | ||
use Brick\Money\Money; | ||
use Domain\Currency\Currency; | ||
use Domain\SalesChannel\Models\SalesChannel; | ||
use Domain\SalesChannel\SalesChannelService; | ||
use InvalidArgumentException; | ||
use Spatie\LaravelData\Attributes\WithCast; | ||
use Spatie\LaravelData\Attributes\WithTransformer; | ||
use Spatie\LaravelData\Casts\EnumCast; | ||
use Spatie\LaravelData\Data; | ||
use Support\LaravelData\Casts\MoneyCast; | ||
use Support\LaravelData\Transformers\MoneyToAmountTransformer; | ||
|
||
final class OrderPriceDto extends Data | ||
{ | ||
public function __construct( | ||
#[WithTransformer(MoneyToAmountTransformer::class)] | ||
#[WithCast(MoneyCast::class)] | ||
public Money $net, | ||
#[WithTransformer(MoneyToAmountTransformer::class)] | ||
#[WithCast(MoneyCast::class)] | ||
public Money $gross, | ||
#[WithCast(EnumCast::class, Currency::class)] | ||
public Currency $currency, | ||
public BigDecimal $vat_rate, | ||
) { | ||
$this->vat_rate = $this->vat_rate->toScale(2, RoundingMode::HALF_UP); | ||
} | ||
|
||
public static function fromMoneyAndSalesChannel(Money $price, SalesChannel $salesChannel, bool $is_net = true): self | ||
{ | ||
if ($salesChannel->priceMap === null) { | ||
throw new InvalidArgumentException(); | ||
} | ||
|
||
return self::fromMoneyAndVatRate($price, app(SalesChannelService::class)->getVatRate($salesChannel), $is_net); | ||
} | ||
|
||
public static function fromMoneyAndVatRate(Money $price, BigDecimal|float|string $vat_rate, bool $is_net = true): self | ||
{ | ||
if (!$vat_rate instanceof BigDecimal) { | ||
$vat_rate = BigDecimal::of($vat_rate)->multipliedBy(0.01)->abs(); | ||
} | ||
|
||
if ($is_net) { | ||
$net = $price; | ||
$gross = app(SalesChannelService::class)->addVat($price, $vat_rate); | ||
} else { | ||
$net = app(SalesChannelService::class)->removeVat($price, $vat_rate); | ||
$gross = $price; | ||
} | ||
|
||
return self::from([ | ||
'net' => $net, | ||
'gross' => $gross, | ||
'currency' => $price->getCurrency()->getCurrencyCode(), | ||
'vat_rate' => $vat_rate, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.