- Add discount_type column to transactions table via migration - Update Sale model to use fillable instead of guarded for better security - Implement discount account ledger creation when discount is applied - Fix net amount calculation to include discount in CreateSaleAction - Remove unused "Exempt" column from sale transaction table - Make discount_type required when discount is enabled in form - Update form data mutation to properly handle discount calculations
55 lines
1.5 KiB
PHP
55 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\Sales;
|
|
|
|
use App\Actions\Transactions\CreateRecordTransactionsAction;
|
|
use App\Commands\Sales\CreateSaleCommand;
|
|
use App\Commands\Series\CreateSeriesCommand;
|
|
use App\Models\Sale;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class CreateSaleAction
|
|
{
|
|
/**
|
|
* Create a new class instance.
|
|
*/
|
|
public function __construct(
|
|
private CreateSaleCommand $createSaleCommand,
|
|
private CreateSeriesCommand $createSeriesCommand,
|
|
){ }
|
|
|
|
public function __invoke(array $data, array $transactions): Sale
|
|
{
|
|
$record = $this->createSaleCommand->execute($data);
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
//create transactions for the sale
|
|
app(CreateRecordTransactionsAction::class)($record, $transactions);
|
|
|
|
$accountIds = collect($transactions)
|
|
->pluck('account_id')
|
|
->filter()
|
|
->unique()
|
|
->values()
|
|
->all();
|
|
|
|
//sync accounts to sale
|
|
app(SyncAccountsAction::class)($record, $accountIds);
|
|
|
|
//increment current_series
|
|
$this->createSeriesCommand->execute([
|
|
'branch_id' => $record->branch_id,
|
|
'series' => $record->reference_number,
|
|
]);
|
|
|
|
DB::commit();
|
|
} catch (\Exception $exception) {
|
|
DB::rollBack();
|
|
throw new \Exception('Failed to save transactions : '.$exception->getMessage());
|
|
}
|
|
|
|
return $record;
|
|
}
|
|
}
|