refactor(sales): replace service with command and action pattern

- Replace SaleService with CreateSaleCommand and CreateSaleAction for better separation of concerns
- Move sale creation logic into dedicated command class following command pattern
- Update CreateSale.php to use new action instead of direct service call
- Wrap sale creation in database transaction for data consistency
This commit is contained in:
Jp
2026-02-16 01:22:00 +08:00
parent 3cf5f6db6a
commit e04689acca
4 changed files with 51 additions and 32 deletions

View File

@@ -0,0 +1,27 @@
<?php
namespace App\Commands\Sales;
use App\Commands\Command;
use App\DataObjects\CreateSaleDTO;
use App\Models\Sale;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class CreateSaleCommand implements Command
{
public function execute(array $data): Sale
{
return DB::transaction(function () use ($data, &$sale) {
$tData = new CreateSaleDTO(
reference_number: $data['current_series'],
happened_on: \Carbon\Carbon::parse($data['happened_on']),
branch_id: $data['branch_id'],
user_id: Auth::user()->id,
);
return Sale::create($tData->toArray());
});
}
}