You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.7 KiB
52 lines
1.7 KiB
<?php
|
|
|
|
namespace App\Domains\Billing\Actions\Invoicing;
|
|
|
|
use App\Domains\Billing\DTOs\Invoicing\InvoiceDTO;
|
|
use App\Domains\Billing\Enums\Invoicing\InvoiceStatus;
|
|
use App\Domains\Billing\Models\Invoice;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
|
|
class DraftInvoice
|
|
{
|
|
public function __construct(
|
|
protected GenerateInvoiceCode $generateInvoiceCode,
|
|
protected AttachItemsToInvoice $attachItemsToInvoice
|
|
) {}
|
|
|
|
/**
|
|
* @throws Throwable
|
|
*/
|
|
public function execute(InvoiceDTO $dto): Invoice
|
|
{
|
|
try {
|
|
return DB::transaction(function() use ($dto) {
|
|
$code = $this->generateInvoiceCode->execute($dto->client_id);
|
|
|
|
$invoice = Invoice::create([
|
|
'code' => $code,
|
|
'name' => $dto->name,
|
|
'client_detail' => $dto->client_detail,
|
|
'client_ref_id' => $dto->client_ref_id,
|
|
'type' => $dto->type,
|
|
'gross_amount' => $dto->pricing->gross_amount,
|
|
'discount_amount' => $dto->pricing->discount_amount,
|
|
'net_amount' => $dto->pricing->net_amount,
|
|
'status' => InvoiceStatus::DRAFT,
|
|
'client_id' => $dto->client_id
|
|
]);
|
|
|
|
foreach ($dto->items as $item) {
|
|
$this->attachItemsToInvoice->execute(invoiceId: $invoice->id, item: $item);
|
|
}
|
|
|
|
return $invoice;
|
|
});
|
|
} catch (\Throwable $th) {
|
|
info($th->getMessage());
|
|
throw new Exception(__('domains/system.exception.something_wrong'));
|
|
}
|
|
}
|
|
}
|
|
|