Skip to content

Commit

Permalink
update to elegantly money
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Aug 27, 2024
1 parent 89b997e commit b2c57c9
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 21 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
3 changes: 3 additions & 0 deletions database/factories/InvoiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Finller\Invoice\InvoiceType;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends Factory<Invoice>
*/
class InvoiceFactory extends Factory
{
protected $model = Invoice::class;
Expand Down
3 changes: 3 additions & 0 deletions database/factories/InvoiceItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use Finller\Invoice\InvoiceItem;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends Factory<InvoiceItem>
*/
class InvoiceItemFactory extends Factory
{
protected $model = InvoiceItem::class;
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ includes:
- phpstan-baseline.neon

parameters:
level: 4
level: 5
paths:
- src
- config
Expand Down
10 changes: 8 additions & 2 deletions src/Casts/Discounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<null|InvoiceDiscount[], array<string, mixed>>
*/
class Discounts implements CastsAttributes
{
/**
* Cast the given value.
*
* @param array<string, mixed> $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;
Expand All @@ -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<string, mixed> $attributes
* @return array<string, mixed>
*/
public function set(Model $model, string $key, mixed $value, array $attributes): mixed
{
Expand Down
8 changes: 6 additions & 2 deletions src/Commands/DenormalizeInvoicesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<Invoice> $query */
$query = $model::query();

$query
->with(['items'])
->when($ids, fn (Builder $q) => $q->whereIn('id', $ids));
Expand All @@ -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();
Expand Down
9 changes: 9 additions & 0 deletions src/GenerateSerialNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 5 additions & 1 deletion src/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,6 +66,9 @@
*/
class Invoice extends Model implements Attachable
{
/**
* @use HasFactory<InvoiceFactory>
*/
use HasFactory;

protected $attributes = [
Expand Down
55 changes: 49 additions & 6 deletions src/InvoiceDiscount.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
use Illuminate\Contracts\Support\Arrayable;
use JsonSerializable;

/** @phpstan-consistent-constructor */
/**
* @implements Arrayable<string, mixed>
*
* @phpstan-consistent-constructor
*/
class InvoiceDiscount implements Arrayable, JsonSerializable
{
use FormatForPdf;
Expand All @@ -34,7 +38,10 @@ public function computeDiscountAmountOn(Money $amout): Money
return Money::of(0, $amout->getCurrency());
}

public static function fromArray(?array $array)
/**
* @param ?array<int|string, mixed> $array
*/
public static function fromArray(?array $array): static
{
$currency = data_get($array, 'currency', config('invoices.default_currency'));
$amount_off = data_get($array, 'amount_off');
Expand All @@ -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,
Expand All @@ -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);
}
Expand Down
6 changes: 5 additions & 1 deletion src/InvoiceItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +29,9 @@
*/
class InvoiceItem extends Model
{
/**
* @use HasFactory<InvoiceItemFactory>
*/
use HasFactory;

protected $guarded = [];
Expand Down
2 changes: 1 addition & 1 deletion src/InvoiceState.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
2 changes: 1 addition & 1 deletion src/InvoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
2 changes: 2 additions & 0 deletions src/PdfInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class PdfInvoice
use FormatForPdf;

/**
* @param null|array<string, mixed> $buyer
* @param null|array<string, mixed> $seller
* @param null|PdfInvoiceItem[] $items
* @param null|InvoiceDiscount[] $discounts
*/
Expand Down
5 changes: 2 additions & 3 deletions src/PdfInvoiceItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Finller\Invoice;

use Brick\Math\RoundingMode;
use Brick\Money\Currency;
use Brick\Money\Money;
use Exception;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/SerialNumberGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function generate(
);

return str_pad(
$serie,
(string) $serie,
$slotLength,
'0',
STR_PAD_LEFT
Expand All @@ -51,7 +51,7 @@ public function generate(
);

return str_pad(
$count,
(string) $count,
$slotLength,
'0',
STR_PAD_LEFT
Expand Down

0 comments on commit b2c57c9

Please sign in to comment.