Merge pull request #2 from kingjaypee12/update/ledger
refactor: streamline sales and expenses management in client resource
This commit is contained in:
@@ -15,7 +15,7 @@ class CreateBalanceAction extends BaseAction
|
||||
|
||||
public function __invoke(Data $payload, Closure $next)
|
||||
{
|
||||
$this->createBalanceCommand->execute($payload->balanceDTO->except('amount')->toArray());
|
||||
$this->createBalanceCommand->execute($payload->balanceDTO->except('amount', 'type')->toArray());
|
||||
|
||||
return $next($payload);
|
||||
}
|
||||
|
||||
@@ -2,31 +2,13 @@
|
||||
|
||||
namespace App\Filament\Resources\ClientResource\RelationManagers;
|
||||
|
||||
use App\Actions\Transactions\CreateTransactionAction;
|
||||
use App\Commands\Expenses\GenerateVoucher;
|
||||
use App\DataObjects\CreateTransactionDTO;
|
||||
use App\Models\Account;
|
||||
use App\Models\Branch;
|
||||
use App\Models\Client;
|
||||
use Awcodes\TableRepeater\Components\TableRepeater;
|
||||
use Awcodes\TableRepeater\Header;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Components\DatePicker;
|
||||
use Filament\Forms\Components\Hidden;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use App\Filament\Resources\ExpenseResource;
|
||||
use App\Models\Expense;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Forms\Get;
|
||||
use Filament\Forms\Set;
|
||||
use Filament\Resources\RelationManagers\RelationManager;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Pipeline;
|
||||
|
||||
class ExpensesRelationManager extends RelationManager
|
||||
{
|
||||
@@ -37,155 +19,7 @@ class ExpensesRelationManager extends RelationManager
|
||||
public function form(Form $form): Form
|
||||
{
|
||||
return $form
|
||||
->schema([
|
||||
Select::make('branch_id')
|
||||
->relationship('branch', 'code', fn (Builder $query) => $query->where('client_id', $this->getOwnerRecord()->id))
|
||||
->afterStateUpdated(function ($set, $get) {
|
||||
$set('voucher_number', static::getVoucherNumber($get));
|
||||
$set('transactions.*.branch_id', $get('branch_id'));
|
||||
})
|
||||
->required()
|
||||
->live(),
|
||||
TextInput::make('supplier')->label('Supplier Name')->required(),
|
||||
TextInput::make('reference_number')->label('Reference Number'),
|
||||
TextInput::make('voucher_number')->label('Voucher Number')
|
||||
->default(fn ($get) => static::getVoucherNumber($get))
|
||||
->readOnly(),
|
||||
DatePicker::make('happened_on')->label('Date')
|
||||
->required()
|
||||
->afterStateUpdated(function ($set, $get) {
|
||||
$set('transactions.*.happened_on', $get('happened_on'));
|
||||
})
|
||||
->live()
|
||||
->native(false),
|
||||
|
||||
TableRepeater::make('transactions')
|
||||
->headers(fn (Get $get): array => static::getTransactionTableHeader($get))
|
||||
->relationship('transactions')
|
||||
->schema(fn (Get $get): array => $this->getTransactionTableFormSchema($get))
|
||||
->visible(fn (Get $get) => $get('branch_id') != null)
|
||||
->columnSpan('full'),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getVoucherNumber(Get $get): string
|
||||
{
|
||||
$branch = Branch::find($get('branch_id'));
|
||||
|
||||
if ($branch) {
|
||||
return GenerateVoucher::execute($branch);
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
private static function getTransactionTableHeader(Get $get): array
|
||||
{
|
||||
return [
|
||||
Header::make('Charge Account'),
|
||||
Header::make('Description'),
|
||||
Header::make('Gross Amount'),
|
||||
Header::make('Exempt'),
|
||||
Header::make('Zero Rated'),
|
||||
Header::make('Vatable Amount'),
|
||||
Header::make('Input Tax'),
|
||||
Header::make('Withholding Tax'),
|
||||
Header::make('Net Amount'),
|
||||
];
|
||||
}
|
||||
|
||||
private function getTransactionTableFormSchema(Get $get): array
|
||||
{
|
||||
return [
|
||||
Select::make('account_id')->options(fn ($get) => $this->getAccountOptions($get)),
|
||||
TextInput::make('description')->label('Description'),
|
||||
Hidden::make('branch_id')->default(fn (Get $get) => $get('../../branch_id')),
|
||||
TextInput::make('gross_amount')
|
||||
->numeric()
|
||||
->live(false, 500)
|
||||
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
|
||||
$this->setDefaultFormValues($get, $set, $old, $state);
|
||||
})->default(0),
|
||||
TextInput::make('exempt')
|
||||
->numeric()
|
||||
->live()
|
||||
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
|
||||
$this->setDefaultFormValues($get, $set, $old, $state);
|
||||
})->default(0),
|
||||
TextInput::make('zero_rated')
|
||||
->numeric()
|
||||
->live()
|
||||
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
|
||||
|
||||
$this->setDefaultFormValues($get, $set, $old, $state);
|
||||
})->default(0),
|
||||
TextInput::make('vatable_amount')
|
||||
->numeric()
|
||||
->nullable()
|
||||
->live()
|
||||
->readOnly()
|
||||
->default(0),
|
||||
Hidden::make('happened_on')->default(fn (Get $get) => $get('../../happened_on')),
|
||||
TextInput::make('input_tax')
|
||||
->numeric()
|
||||
->live()
|
||||
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
|
||||
|
||||
$this->setDefaultFormValues($get, $set, $old, $state);
|
||||
})->default(0),
|
||||
TextInput::make('payable_withholding_tax')
|
||||
->numeric()
|
||||
->live()
|
||||
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
|
||||
|
||||
$this->setDefaultFormValues($get, $set, $old, $state);
|
||||
})->default(0),
|
||||
TextInput::make('net_amount')->numeric()->default(0),
|
||||
];
|
||||
}
|
||||
|
||||
private function getAccountOptions(Get $get): Collection
|
||||
{
|
||||
$query = Account::query();
|
||||
|
||||
$query->where([
|
||||
'client_id' => $this->getOwnerRecord()->id,
|
||||
]);
|
||||
|
||||
if ($get('../../branch_id')) {
|
||||
$query->whereHas('balances', function ($query) use ($get) {
|
||||
return $query->where('branch_id', $get('../../branch_id'));
|
||||
});
|
||||
}
|
||||
|
||||
$query->whereHas('accountType', function ($query) {
|
||||
return $query->where('type', 'Expenses');
|
||||
});
|
||||
|
||||
return $query->get()->pluck('account', 'id');
|
||||
}
|
||||
|
||||
private function setDefaultFormValues(Get $get, Set $set, ?string $old, ?string $state): void
|
||||
{
|
||||
$exempt = (float) $get('exempt');
|
||||
$withHoldingTax = (float) $get('payable_withholding_tax');
|
||||
$vatableSales = $get('gross_amount');
|
||||
$vatableAmount = 0;
|
||||
if ($vatableSales) {
|
||||
$vatableAmount = $vatableSales / 1.12;
|
||||
}
|
||||
|
||||
$inputTax = $vatableAmount * 0.12;
|
||||
|
||||
$netAmount = (int) $vatableSales - $get('payable_withholding_tax');
|
||||
|
||||
if ($this->getOwnerRecord()->vatable) {
|
||||
$netAmount = ($vatableAmount + $exempt) - $withHoldingTax;
|
||||
}
|
||||
|
||||
$set('input_tax', number_format($inputTax, 2, '.', ''));
|
||||
$set('vatable_amount', number_format($vatableAmount, 2, '.', ''));
|
||||
$set('net_amount', number_format($netAmount, 2, '.', ''));
|
||||
->schema([]);
|
||||
}
|
||||
|
||||
public function table(Table $table): Table
|
||||
@@ -204,40 +38,11 @@ class ExpensesRelationManager extends RelationManager
|
||||
])
|
||||
->headerActions([
|
||||
Tables\Actions\CreateAction::make()
|
||||
->using(function (array $data, string $model) {
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
$data = Arr::except($data, ['transactions']);
|
||||
|
||||
$record = $model::create($data);
|
||||
|
||||
try {
|
||||
$branch = $record->branch;
|
||||
|
||||
foreach ($transactions as $transaction) {
|
||||
|
||||
$tData = [
|
||||
'branch_id' => $branch->id,
|
||||
'happened_on' => $record->happened_on,
|
||||
...$transaction,
|
||||
];
|
||||
|
||||
$payload = new CreateTransactionDTO(data: $tData, transactionable: $record);
|
||||
|
||||
Pipeline::send(passable: $payload)->through(
|
||||
[
|
||||
CreateTransactionAction::class,
|
||||
]
|
||||
)->thenReturn();
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
throw new \Exception('Failed to save transactions : '.$exception->getMessage());
|
||||
}
|
||||
|
||||
return $record;
|
||||
}),
|
||||
->url(fn () => ExpenseResource::getUrl('create', ['client_id' => $this->getOwnerRecord()->id])),
|
||||
])
|
||||
->actions([
|
||||
Tables\Actions\EditAction::make(),
|
||||
Tables\Actions\EditAction::make()
|
||||
->url(fn (Expense $record) => ExpenseResource::getUrl('edit', ['record' => $record])),
|
||||
Tables\Actions\DeleteAction::make(),
|
||||
])
|
||||
->bulkActions([
|
||||
|
||||
@@ -2,31 +2,14 @@
|
||||
|
||||
namespace App\Filament\Resources\ClientResource\RelationManagers;
|
||||
|
||||
use App\Actions\Transactions\CreateTransactionAction;
|
||||
use App\DataObjects\CreateTransactionDTO;
|
||||
use Illuminate\Support\Facades\Pipeline;
|
||||
use App\Models\Account;
|
||||
use App\Models\Branch;
|
||||
use App\Models\Client;
|
||||
use Awcodes\TableRepeater\Components\TableRepeater;
|
||||
use Awcodes\TableRepeater\Header;
|
||||
use App\Filament\Resources\SaleResource;
|
||||
use App\Models\Sale;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Components\Checkbox;
|
||||
use Filament\Forms\Components\DatePicker;
|
||||
use Filament\Forms\Components\Hidden;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Forms\Get;
|
||||
use Filament\Forms\Set;
|
||||
use Filament\Resources\RelationManagers\RelationManager;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class SalesRelationManager extends RelationManager
|
||||
{
|
||||
@@ -36,178 +19,7 @@ class SalesRelationManager extends RelationManager
|
||||
|
||||
public function form(Form $form): Form
|
||||
{
|
||||
return $form
|
||||
->schema([
|
||||
Select::make('branch_id')
|
||||
->relationship('branch', 'code', fn (Builder $query) => $query->where('client_id', $this->getOwnerRecord()->id))
|
||||
->required()
|
||||
->afterStateUpdated(function ($set, $get) {
|
||||
$set('current_series', static::getSeries($get));
|
||||
$set('transactions.*.branch_id', $get('branch_id'));
|
||||
})
|
||||
->live(),
|
||||
TextInput::make('current_series')
|
||||
->label('Series')
|
||||
->disabled(),
|
||||
DatePicker::make('happened_on')->label('Date')
|
||||
->required()
|
||||
->afterStateUpdated(function ($set, $get) {
|
||||
$set('transactions.*.happened_on', $get('happened_on'));
|
||||
})
|
||||
->native(false),
|
||||
Checkbox::make('with_discount')->label('With Discount?')->default(false)->live(),
|
||||
|
||||
TableRepeater::make('transactions')
|
||||
->headers(fn (Get $get): array => static::getTransactionTableHeader($get))
|
||||
->relationship('transactions')
|
||||
->schema(fn (Get $get): array => $this->getTransactionTableFormSchema($get))
|
||||
->visible(fn (Get $get) => $get('branch_id') != null)
|
||||
->columnSpan('full'),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getSeries(Get $get): string
|
||||
{
|
||||
$branch = Branch::find($get('branch_id'));
|
||||
|
||||
if ($branch) {
|
||||
$currentSeries = $branch->current_series;
|
||||
|
||||
return str_pad($currentSeries + 1, 6, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
return '';
|
||||
|
||||
}
|
||||
|
||||
private static function getTransactionTableHeader(Get $get): array
|
||||
{
|
||||
if ($get('with_discount')) {
|
||||
return [
|
||||
Header::make('Charge Account'),
|
||||
Header::make('Description'),
|
||||
Header::make('Gross Amount'),
|
||||
Header::make('Exempt'),
|
||||
Header::make('Vatable Amount'),
|
||||
Header::make('Output Tax'),
|
||||
Header::make('Withholding Tax'),
|
||||
Header::make('Discount'),
|
||||
Header::make('Net Amount'),
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
Header::make('Charge Account'),
|
||||
Header::make('Description'),
|
||||
Header::make('Gross Amount'),
|
||||
Header::make('Exempt'),
|
||||
Header::make('Vatable Amount'),
|
||||
Header::make('Output Tax'),
|
||||
Header::make('Withholding Tax'),
|
||||
Header::make('Net Amount'),
|
||||
];
|
||||
}
|
||||
|
||||
private function getTransactionTableFormSchema(Get $get): array
|
||||
{
|
||||
return [
|
||||
Select::make('account_id')->options(fn ($get) => $this->getAccountOptions($get)),
|
||||
TextInput::make('description')->label('Description'),
|
||||
Hidden::make('branch_id')->default(fn (Get $get) => $get('../../branch_id')),
|
||||
TextInput::make('gross_amount')
|
||||
->numeric()
|
||||
->live(false, 500)
|
||||
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
|
||||
$this->setDefaultFormValues($get, $set, $old, $state);
|
||||
})->default(0),
|
||||
TextInput::make('exempt')
|
||||
->numeric()
|
||||
->live()
|
||||
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
|
||||
$this->setDefaultFormValues($get, $set, $old, $state);
|
||||
})->default(0),
|
||||
TextInput::make('vatable_amount')
|
||||
->numeric()
|
||||
->nullable()
|
||||
->live()
|
||||
->readOnly()
|
||||
->default(0),
|
||||
Hidden::make('happened_on')->default(fn (Get $get) => $get('../../happened_on')),
|
||||
Hidden::make('with_discount')->default(fn (Get $get) => $get('../../with_discount')),
|
||||
TextInput::make('output_tax')
|
||||
->numeric()
|
||||
->live()
|
||||
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
|
||||
|
||||
$this->setDefaultFormValues($get, $set, $old, $state);
|
||||
})->default(0),
|
||||
TextInput::make('payable_withholding_tax')
|
||||
->numeric()
|
||||
->live()
|
||||
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
|
||||
|
||||
$this->setDefaultFormValues($get, $set, $old, $state);
|
||||
})->default(0),
|
||||
TextInput::make('discount')
|
||||
->numeric()
|
||||
->readOnly()
|
||||
->visible(fn (Get $get) => $get('../../with_discount'))
|
||||
->live(),
|
||||
TextInput::make('net_amount')->numeric()->default(0),
|
||||
];
|
||||
}
|
||||
|
||||
private function getAccountOptions($get)
|
||||
{
|
||||
$query = Account::query();
|
||||
|
||||
$query->where([
|
||||
'client_id' => $this->getOwnerRecord()->id,
|
||||
]);
|
||||
|
||||
if ($get('../../branch_id')) {
|
||||
$query->whereHas('balances', function ($query) use ($get) {
|
||||
return $query->where('branch_id', $get('../../branch_id'));
|
||||
});
|
||||
}
|
||||
|
||||
$query->whereHas('accountType', function ($query) {
|
||||
return $query->where('type', 'Revenue');
|
||||
});
|
||||
|
||||
return $query->get()->pluck('account', 'id');
|
||||
}
|
||||
|
||||
private function setDefaultFormValues(Get $get, Set $set, ?string $old, ?string $state)
|
||||
{
|
||||
$exempt = (float) $get('exempt');
|
||||
$withHoldingTax = (float) $get('payable_withholding_tax');
|
||||
$vatableSales = $get('gross_amount');
|
||||
$vatableAmount = 0;
|
||||
if ($vatableSales) {
|
||||
$vatableAmount = $vatableSales / 1.12;
|
||||
}
|
||||
|
||||
$discount = $exempt * .20;
|
||||
$outputTax = $vatableAmount * 0.12;
|
||||
|
||||
//default net amount
|
||||
$netAmount = (int) $vatableSales - $get('payable_withholding_tax');
|
||||
|
||||
//net amount if vatable
|
||||
if ($this->getOwnerRecord()->vatable) {
|
||||
$netAmount = ($vatableAmount + $exempt) - $withHoldingTax;
|
||||
}
|
||||
|
||||
//if discounted
|
||||
if ($get('../../with_discount')) {
|
||||
$netAmount = $netAmount - $discount;
|
||||
}
|
||||
|
||||
$set('output_tax', number_format($outputTax, 2, '.', ''));
|
||||
$set('discount', number_format($discount, 2, '.', ''));
|
||||
$set('vatable_amount', number_format($vatableAmount, 2, '.', ''));
|
||||
$set('net_amount', number_format($netAmount, 2, '.', ''));
|
||||
return $form->schema([]);
|
||||
}
|
||||
|
||||
public function table(Table $table): Table
|
||||
@@ -231,39 +43,11 @@ class SalesRelationManager extends RelationManager
|
||||
])
|
||||
->headerActions([
|
||||
Tables\Actions\CreateAction::make()
|
||||
->using(function (array $data, string $model) {
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
$data = Arr::except($data, ['transactions']);
|
||||
|
||||
$record = $model::create($data);
|
||||
|
||||
try {
|
||||
$branch = $record->branch;
|
||||
|
||||
foreach ($transactions as $transaction) {
|
||||
$tData = [
|
||||
'branch_id' => $branch->id,
|
||||
'happened_on' => $record->happened_on,
|
||||
...$transaction,
|
||||
];
|
||||
|
||||
$payload = new CreateTransactionDTO(data: $tData, transactionable: $record);
|
||||
|
||||
Pipeline::send(passable: $payload)->through(
|
||||
[
|
||||
CreateTransactionAction::class,
|
||||
]
|
||||
)->thenReturn();
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
throw new \Exception('Failed to save transactions : '.$exception->getMessage());
|
||||
}
|
||||
|
||||
return $record;
|
||||
}),
|
||||
->url(fn () => SaleResource::getUrl('create', ['client_id' => $this->getOwnerRecord()->id])),
|
||||
])
|
||||
->actions([
|
||||
Tables\Actions\EditAction::make(),
|
||||
Tables\Actions\EditAction::make()
|
||||
->url(fn (Sale $record) => SaleResource::getUrl('edit', ['record' => $record])),
|
||||
Tables\Actions\DeleteAction::make(),
|
||||
])
|
||||
->bulkActions([
|
||||
|
||||
@@ -43,6 +43,7 @@ class ExpenseResource extends Resource
|
||||
{
|
||||
return [
|
||||
Select::make('client')
|
||||
->default(request()->query('client_id'))
|
||||
->options(Client::query()->get()->pluck('company', 'id'))
|
||||
->afterStateUpdated(function ($set, $get) {
|
||||
$set('branch_id', '');
|
||||
|
||||
@@ -28,7 +28,7 @@ class CreateExpense extends CreateRecord
|
||||
|
||||
protected function afterCreate(): void
|
||||
{
|
||||
$transactions = Arr::only($this->form->getState(), ['transactions']);
|
||||
$transactions = $this->form->getState()['transactions'] ?? [];
|
||||
|
||||
try {
|
||||
$branch = $this->getRecord()->branch;
|
||||
@@ -38,7 +38,7 @@ class CreateExpense extends CreateRecord
|
||||
$data = [
|
||||
'branch_id' => $branch->id,
|
||||
'happened_on' => $this->getRecord()->happened_on,
|
||||
...Arr::first($transaction),
|
||||
...$transaction,
|
||||
];
|
||||
|
||||
$payload = new CreateTransactionDTO(data: $data, transactionable: $this->getRecord());
|
||||
|
||||
@@ -35,6 +35,7 @@ class SaleResource extends Resource
|
||||
return $form
|
||||
->schema([
|
||||
Select::make('client')
|
||||
->default(request()->query('client_id'))
|
||||
->options(Client::query()->get()->pluck('company', 'id'))
|
||||
->afterStateUpdated(function ($set, $get) {
|
||||
$set('branch_id', '');
|
||||
|
||||
@@ -2,10 +2,16 @@
|
||||
|
||||
namespace App\Filament\Resources\SaleResource\Pages;
|
||||
|
||||
use App\Actions\Transactions\CreateTransactionAction;
|
||||
use App\DataObjects\CreateTransactionDTO;
|
||||
use App\Filament\Resources\SaleResource;
|
||||
use App\Models\Branch;
|
||||
use App\Models\Sale;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Pipeline;
|
||||
|
||||
class CreateSale extends CreateRecord
|
||||
{
|
||||
@@ -16,11 +22,48 @@ class CreateSale extends CreateRecord
|
||||
return $this->getFormDataMutation($data);
|
||||
}
|
||||
|
||||
protected function handleRecordCreation(array $data): Model
|
||||
{
|
||||
$transactions = $this->data['transactions'] ?? [];
|
||||
return $this->processCreate($data, $transactions);
|
||||
}
|
||||
|
||||
public function getFormDataMutation(array $data): array
|
||||
{
|
||||
return Arr::except($data, ['client', 'transactions', 'with_discount']);
|
||||
}
|
||||
|
||||
public function processCreate(array $data, array $transactions): Model
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$record = Sale::create($data);
|
||||
$branch = $record->branch;
|
||||
|
||||
foreach ($transactions as $transaction) {
|
||||
$tData = [
|
||||
'branch_id' => $branch->id,
|
||||
'happened_on' => $record->happened_on,
|
||||
...$transaction,
|
||||
];
|
||||
|
||||
$payload = new CreateTransactionDTO(data: $tData, transactionable: $record);
|
||||
|
||||
Pipeline::send(passable: $payload)->through(
|
||||
[
|
||||
CreateTransactionAction::class,
|
||||
]
|
||||
)->thenReturn();
|
||||
}
|
||||
DB::commit();
|
||||
} catch (\Exception $exception) {
|
||||
DB::rollBack();
|
||||
throw new \Exception('Failed to save transactions : '.$exception->getMessage());
|
||||
}
|
||||
|
||||
return $record;
|
||||
}
|
||||
|
||||
protected function afterCreate(): void
|
||||
{
|
||||
$branch = Branch::find($this->data['branch_id']);
|
||||
|
||||
@@ -2,9 +2,15 @@
|
||||
|
||||
namespace App\Filament\Resources\SaleResource\Pages;
|
||||
|
||||
use App\Actions\Transactions\CreateTransactionAction;
|
||||
use App\DataObjects\CreateTransactionDTO;
|
||||
use App\Filament\Resources\SaleResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Pipeline;
|
||||
|
||||
class EditSale extends EditRecord
|
||||
{
|
||||
@@ -16,4 +22,61 @@ class EditSale extends EditRecord
|
||||
Actions\DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function mutateFormDataBeforeSave(array $data): array
|
||||
{
|
||||
return $this->getFormDataMutation($data);
|
||||
}
|
||||
|
||||
protected function handleRecordUpdate(Model $record, array $data): Model
|
||||
{
|
||||
$transactions = $this->data['transactions'] ?? [];
|
||||
return $this->processUpdate($record, $data, $transactions);
|
||||
}
|
||||
|
||||
public function getFormDataMutation(array $data): array
|
||||
{
|
||||
return Arr::except($data, ['client', 'transactions', 'with_discount']);
|
||||
}
|
||||
|
||||
public function processUpdate(Model $record, array $data, array $transactions): Model
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$record->update($data);
|
||||
$branch = $record->branch;
|
||||
|
||||
// Delete existing transactions and their related ledgers/balances
|
||||
$record->transactions->each(function ($transaction) {
|
||||
$transaction->ledgers->each(function ($ledger) {
|
||||
$ledger->balances()->delete();
|
||||
$ledger->delete();
|
||||
});
|
||||
$transaction->delete();
|
||||
});
|
||||
|
||||
// Create new transactions
|
||||
foreach ($transactions as $transaction) {
|
||||
$tData = [
|
||||
'branch_id' => $branch->id,
|
||||
'happened_on' => $record->happened_on,
|
||||
...$transaction,
|
||||
];
|
||||
|
||||
$payload = new CreateTransactionDTO(data: $tData, transactionable: $record);
|
||||
|
||||
Pipeline::send(passable: $payload)->through(
|
||||
[
|
||||
CreateTransactionAction::class,
|
||||
]
|
||||
)->thenReturn();
|
||||
}
|
||||
DB::commit();
|
||||
} catch (\Exception $exception) {
|
||||
DB::rollBack();
|
||||
throw new \Exception('Failed to save transactions : '.$exception->getMessage());
|
||||
}
|
||||
|
||||
return $record;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user