From b2c57c944d6b90e8ea42ab1b9d454859d9a6355f Mon Sep 17 00:00:00 2001 From: Quentin Gabriele Date: Tue, 27 Aug 2024 19:14:50 +0200 Subject: [PATCH] update to elegantly money --- composer.json | 2 +- database/factories/InvoiceFactory.php | 3 ++ database/factories/InvoiceItemFactory.php | 3 ++ phpstan.neon.dist | 2 +- src/Casts/Discounts.php | 10 +++- src/Commands/DenormalizeInvoicesCommand.php | 8 ++- src/GenerateSerialNumber.php | 9 ++++ src/Invoice.php | 6 ++- src/InvoiceDiscount.php | 55 ++++++++++++++++++--- src/InvoiceItem.php | 6 ++- src/InvoiceState.php | 2 +- src/InvoiceType.php | 2 +- src/PdfInvoice.php | 2 + src/PdfInvoiceItem.php | 5 +- src/SerialNumberGenerator.php | 4 +- 15 files changed, 98 insertions(+), 21 deletions(-) diff --git a/composer.json b/composer.json index 7fc5e91..c41952e 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "require": { "php": "^8.1", "barryvdh/laravel-dompdf": "^2.0.1", - "finller/laravel-money": "^0.1.3|^1.0.0", + "elegantly/laravel-money": "^2.0.0", "illuminate/contracts": "^10.0|^11.0", "spatie/laravel-package-tools": "^1.14" }, diff --git a/database/factories/InvoiceFactory.php b/database/factories/InvoiceFactory.php index a4c9c21..eefe84b 100644 --- a/database/factories/InvoiceFactory.php +++ b/database/factories/InvoiceFactory.php @@ -7,6 +7,9 @@ use Finller\Invoice\InvoiceType; use Illuminate\Database\Eloquent\Factories\Factory; +/** + * @extends Factory + */ class InvoiceFactory extends Factory { protected $model = Invoice::class; diff --git a/database/factories/InvoiceItemFactory.php b/database/factories/InvoiceItemFactory.php index 2a52b56..7cdb3aa 100644 --- a/database/factories/InvoiceItemFactory.php +++ b/database/factories/InvoiceItemFactory.php @@ -6,6 +6,9 @@ use Finller\Invoice\InvoiceItem; use Illuminate\Database\Eloquent\Factories\Factory; +/** + * @extends Factory + */ class InvoiceItemFactory extends Factory { protected $model = InvoiceItem::class; diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 260b5e1..de458d6 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -2,7 +2,7 @@ includes: - phpstan-baseline.neon parameters: - level: 4 + level: 5 paths: - src - config diff --git a/src/Casts/Discounts.php b/src/Casts/Discounts.php index 4c6b533..cdb934f 100644 --- a/src/Casts/Discounts.php +++ b/src/Casts/Discounts.php @@ -2,21 +2,25 @@ namespace Finller\Invoice\Casts; +use Finller\Invoice\InvoiceDiscount; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; use Illuminate\Database\Eloquent\Casts\Json; use Illuminate\Database\Eloquent\Model; +/** + * @implements CastsAttributes> + */ class Discounts implements CastsAttributes { /** - * Cast the given value. - * * @param array $attributes + * @return null|InvoiceDiscount[] */ public function get(Model $model, string $key, mixed $value, array $attributes): mixed { $data = Json::decode(data_get($attributes, $key, '')); + /** @var string $class */ $class = config('invoices.discount_class'); return is_array($data) ? array_map(fn (?array $item) => $class::fromArray($item), $data) : null; @@ -25,7 +29,9 @@ public function get(Model $model, string $key, mixed $value, array $attributes): /** * Prepare the given value for storage. * + * @param null|InvoiceDiscount[] $value * @param array $attributes + * @return array */ public function set(Model $model, string $key, mixed $value, array $attributes): mixed { diff --git a/src/Commands/DenormalizeInvoicesCommand.php b/src/Commands/DenormalizeInvoicesCommand.php index 5cf71de..283600c 100644 --- a/src/Commands/DenormalizeInvoicesCommand.php +++ b/src/Commands/DenormalizeInvoicesCommand.php @@ -17,10 +17,14 @@ public function handle(): int { $ids = $this->argument('ids'); + /** + * @var string $model + */ $model = config('invoices.model_invoice'); - /** @var Builder $query */ + /** @var Builder $query */ $query = $model::query(); + $query ->with(['items']) ->when($ids, fn (Builder $q) => $q->whereIn('id', $ids)); @@ -31,7 +35,7 @@ public function handle(): int $bar = $this->output->createProgressBar($total); $query - ->chunk(200, function (Collection $invoices) use ($bar) { + ->chunk(2_000, function (Collection $invoices) use ($bar) { $invoices->each(function (Invoice $invoice) use ($bar) { $invoice->denormalize()->saveQuietly(); $bar->advance(); diff --git a/src/GenerateSerialNumber.php b/src/GenerateSerialNumber.php index 31b0010..9aa4032 100644 --- a/src/GenerateSerialNumber.php +++ b/src/GenerateSerialNumber.php @@ -14,5 +14,14 @@ public function generate( string|int|null $month = null, ): string; + /** + * @return array{ + * prefix: ?string, + * serie: ?int, + * month: ?int, + * year: ?int, + * count: ?int, + * } + */ public function parse(string $serialNumber): array; } diff --git a/src/Invoice.php b/src/Invoice.php index 18f419e..7b9a2fb 100644 --- a/src/Invoice.php +++ b/src/Invoice.php @@ -4,9 +4,10 @@ use Brick\Money\Money; use Carbon\Carbon; +use Elegantly\Money\MoneyCast; use Exception; use Finller\Invoice\Casts\Discounts; -use Finller\Money\MoneyCast; +use Finller\Invoice\Database\Factories\InvoiceFactory; use Illuminate\Contracts\Mail\Attachable; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Casts\ArrayObject; @@ -65,6 +66,9 @@ */ class Invoice extends Model implements Attachable { + /** + * @use HasFactory + */ use HasFactory; protected $attributes = [ diff --git a/src/InvoiceDiscount.php b/src/InvoiceDiscount.php index 7013e06..38f8be2 100644 --- a/src/InvoiceDiscount.php +++ b/src/InvoiceDiscount.php @@ -7,7 +7,11 @@ use Illuminate\Contracts\Support\Arrayable; use JsonSerializable; -/** @phpstan-consistent-constructor */ +/** + * @implements Arrayable + * + * @phpstan-consistent-constructor + */ class InvoiceDiscount implements Arrayable, JsonSerializable { use FormatForPdf; @@ -34,7 +38,10 @@ public function computeDiscountAmountOn(Money $amout): Money return Money::of(0, $amout->getCurrency()); } - public static function fromArray(?array $array) + /** + * @param ?array $array + */ + public static function fromArray(?array $array): static { $currency = data_get($array, 'currency', config('invoices.default_currency')); $amount_off = data_get($array, 'amount_off'); @@ -48,7 +55,16 @@ public static function fromArray(?array $array) ); } - public function toArray() + /** + * @return array{ + * name: ?string, + * code: ?string, + * amount_off: ?int, + * currency: ?string, + * percent_off: ?float, + * } + */ + public function toArray(): array { return [ 'name' => $this->name, @@ -59,17 +75,44 @@ public function toArray() ]; } - public function jsonSerialize(): mixed + /** + * @return array{ + * name: ?string, + * code: ?string, + * amount_off: ?int, + * currency: ?string, + * percent_off: ?float, + * } + */ + public function jsonSerialize(): array { return $this->toArray(); } - public function toLivewire() + /** + * @return array{ + * name: ?string, + * code: ?string, + * amount_off: ?int, + * currency: ?string, + * percent_off: ?float, + * } + */ + public function toLivewire(): array { return $this->toArray(); } - public static function fromLivewire($value) + /** + * @param ?array{ + * name: ?string, + * code: ?string, + * amount_off: ?int, + * currency: ?string, + * percent_off: ?float, + * } $value + */ + public static function fromLivewire(?array $value): static { return static::fromArray($value); } diff --git a/src/InvoiceItem.php b/src/InvoiceItem.php index e4d90a5..0003e0e 100644 --- a/src/InvoiceItem.php +++ b/src/InvoiceItem.php @@ -4,7 +4,8 @@ use Brick\Money\Money; use Carbon\Carbon; -use Finller\Money\MoneyCast; +use Elegantly\Money\MoneyCast; +use Finller\Invoice\Database\Factories\InvoiceItemFactory; use Illuminate\Database\Eloquent\Casts\ArrayObject; use Illuminate\Database\Eloquent\Casts\AsArrayObject; use Illuminate\Database\Eloquent\Factories\HasFactory; @@ -28,6 +29,9 @@ */ class InvoiceItem extends Model { + /** + * @use HasFactory + */ use HasFactory; protected $guarded = []; diff --git a/src/InvoiceState.php b/src/InvoiceState.php index 4e2a7ea..16cffe5 100644 --- a/src/InvoiceState.php +++ b/src/InvoiceState.php @@ -9,7 +9,7 @@ enum InvoiceState: string case Paid = 'paid'; case Refunded = 'refunded'; - public function trans() + public function trans(): string { return match ($this) { self::Draft => __('invoices::invoice.states.draft'), diff --git a/src/InvoiceType.php b/src/InvoiceType.php index b3322e8..cef2f1f 100644 --- a/src/InvoiceType.php +++ b/src/InvoiceType.php @@ -9,7 +9,7 @@ enum InvoiceType: string case Credit = 'credit'; case Proforma = 'proforma'; - public function trans() + public function trans(): string { return match ($this) { self::Invoice => __('invoices::invoice.types.invoice'), diff --git a/src/PdfInvoice.php b/src/PdfInvoice.php index 53a0eae..9563485 100644 --- a/src/PdfInvoice.php +++ b/src/PdfInvoice.php @@ -14,6 +14,8 @@ class PdfInvoice use FormatForPdf; /** + * @param null|array $buyer + * @param null|array $seller * @param null|PdfInvoiceItem[] $items * @param null|InvoiceDiscount[] $discounts */ diff --git a/src/PdfInvoiceItem.php b/src/PdfInvoiceItem.php index d3a981f..b553187 100644 --- a/src/PdfInvoiceItem.php +++ b/src/PdfInvoiceItem.php @@ -2,6 +2,7 @@ namespace Finller\Invoice; +use Brick\Math\RoundingMode; use Brick\Money\Currency; use Brick\Money\Money; use Exception; @@ -45,9 +46,7 @@ public function totalTaxAmount(): Money } if ($this->tax_percentage) { - [$tax] = $this->subTotalAmount()->allocate($this->tax_percentage, 100 - $this->tax_percentage); - - return $tax; + return $this->subTotalAmount()->multipliedBy($this->tax_percentage / 100, roundingMode: RoundingMode::HALF_EVEN); } return Money::ofMinor(0, $this->currency); diff --git a/src/SerialNumberGenerator.php b/src/SerialNumberGenerator.php index 732d34d..bd05663 100644 --- a/src/SerialNumberGenerator.php +++ b/src/SerialNumberGenerator.php @@ -33,7 +33,7 @@ public function generate( ); return str_pad( - $serie, + (string) $serie, $slotLength, '0', STR_PAD_LEFT @@ -51,7 +51,7 @@ public function generate( ); return str_pad( - $count, + (string) $count, $slotLength, '0', STR_PAD_LEFT