Compare commits
6 Commits
3cf5f6db6a
...
2644be0505
| Author | SHA1 | Date | |
|---|---|---|---|
| 2644be0505 | |||
|
|
9ddb71f03d | ||
| eadcc0b3d7 | |||
|
|
8c6fa6cb08 | ||
|
|
7bcfaff311 | ||
|
|
e04689acca |
55
app/Actions/Sales/CreateSaleAction.php
Normal file
55
app/Actions/Sales/CreateSaleAction.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
16
app/Actions/Sales/SyncAccountsAction.php
Normal file
16
app/Actions/Sales/SyncAccountsAction.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Sales;
|
||||
|
||||
use App\Models\Sale;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class SyncAccountsAction
|
||||
{
|
||||
public function __invoke(Sale $sale, array $accounts): void
|
||||
{
|
||||
DB::transaction(function () use ($sale, $accounts) {
|
||||
$sale->accounts()->sync($accounts);
|
||||
}, attempts: 2);
|
||||
}
|
||||
}
|
||||
30
app/Actions/Transactions/CreateRecordTransactionsAction.php
Normal file
30
app/Actions/Transactions/CreateRecordTransactionsAction.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Transactions;
|
||||
|
||||
use App\DataObjects\CreateTransactionDTO;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Pipeline;
|
||||
|
||||
class CreateRecordTransactionsAction
|
||||
{
|
||||
public function __invoke(Model $record, array $transactions): void
|
||||
{
|
||||
foreach ($transactions as $transaction) {
|
||||
$tData = [
|
||||
'branch_id' => $record->branch_id,
|
||||
'happened_on' => $record->happened_on,
|
||||
...$transaction,
|
||||
];
|
||||
|
||||
$payload = new CreateTransactionDTO(data: $tData, transactionable: $record);
|
||||
|
||||
Pipeline::send(passable: $payload)->through(
|
||||
[
|
||||
CreateTransactionAction::class,
|
||||
]
|
||||
)->thenReturn();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
64
app/Commands/Clients/GenerateBaseAccountCommand.php
Normal file
64
app/Commands/Clients/GenerateBaseAccountCommand.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Commands\Clients;
|
||||
|
||||
use App\Commands\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class GenerateBaseAccountCommand
|
||||
{
|
||||
/**
|
||||
* Create a new class instance.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the command.
|
||||
*/
|
||||
public function execute($client): void
|
||||
{
|
||||
DB::transaction(function () use ($client) {
|
||||
$client->accounts()->createMany([
|
||||
[
|
||||
'account_type_id' => 1,
|
||||
'account' => 'Cash',
|
||||
'normal_balance' => 'debit',
|
||||
],
|
||||
[
|
||||
'account_type_id' => 1,
|
||||
'account' => 'Input Tax',
|
||||
'normal_balance' => 'debit',
|
||||
],
|
||||
[
|
||||
'account_type_id' => 1,
|
||||
'account' => 'Creditable Withholding Tax',
|
||||
'normal_balance' => 'debit',
|
||||
],
|
||||
[
|
||||
'account_type_id' => 2,
|
||||
'account' => 'Output Tax',
|
||||
'normal_balance' => 'credit',
|
||||
],
|
||||
[
|
||||
'account_type_id' => 2,
|
||||
'account' => 'Payable Withholding Tax',
|
||||
'normal_balance' => 'credit',
|
||||
],
|
||||
[
|
||||
'account_type_id' => 5,
|
||||
'account' => 'Vat Exempt Revenue',
|
||||
'normal_balance' => 'credit',
|
||||
],
|
||||
[
|
||||
'account_type_id' => 4,
|
||||
'account' => 'Sales Discount',
|
||||
'normal_balance' => 'debit',
|
||||
],
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
27
app/Commands/Sales/CreateSaleCommand.php
Normal file
27
app/Commands/Sales/CreateSaleCommand.php
Normal 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());
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Filament\Resources\ClientResource\RelationManagers;
|
||||
|
||||
use App\Commands\Clients\GenerateBaseAccountCommand;
|
||||
use App\Filament\Exports\ClientAccountsExporter;
|
||||
use App\Models\Account;
|
||||
use Filament\Actions\Exports\Enums\ExportFormat;
|
||||
@@ -43,6 +44,20 @@ class AccountsRelationManager extends RelationManager
|
||||
//
|
||||
])
|
||||
->headerActions([
|
||||
Tables\Actions\Action::make('generate-base-accounts')
|
||||
->requiresConfirmation()
|
||||
->label('Generate Base Accounts')
|
||||
->action(function () {
|
||||
$client = $this->getOwnerRecord();
|
||||
if (! $client ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if($client->accounts()->count() > 0) {
|
||||
return;
|
||||
}
|
||||
app(GenerateBaseAccountCommand::class)->execute($client);
|
||||
}),
|
||||
Tables\Actions\ExportAction::make('Export Accounts')->exporter(ClientAccountsExporter::class)->formats([ExportFormat::Csv]),
|
||||
Tables\Actions\CreateAction::make()->label('New Account')->icon('heroicon-o-plus')->slideOver(),
|
||||
])
|
||||
|
||||
@@ -2,15 +2,13 @@
|
||||
|
||||
namespace App\Filament\Resources\ExpenseResource\Pages;
|
||||
|
||||
use App\Actions\Transactions\CreateTransactionAction;
|
||||
use App\DataObjects\CreateTransactionDTO;
|
||||
use App\Actions\Transactions\CreateRecordTransactionsAction;
|
||||
use App\Filament\Resources\ClientResource;
|
||||
use App\Filament\Resources\ExpenseResource;
|
||||
use App\Models\Client;
|
||||
use Exception;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Pipeline;
|
||||
use Symfony\Component\Console\Exception\LogicException;
|
||||
|
||||
class CreateExpense extends CreateRecord
|
||||
@@ -76,24 +74,7 @@ class CreateExpense extends CreateRecord
|
||||
$transactions = $this->form->getState()['transactions'] ?? [];
|
||||
|
||||
try {
|
||||
$branch = $this->getRecord()->branch;
|
||||
|
||||
foreach ($transactions as $transaction) {
|
||||
|
||||
$data = [
|
||||
'branch_id' => $branch->id,
|
||||
'happened_on' => $this->getRecord()->happened_on,
|
||||
...$transaction,
|
||||
];
|
||||
|
||||
$payload = new CreateTransactionDTO(data: $data, transactionable: $this->getRecord());
|
||||
|
||||
Pipeline::send(passable: $payload)->through(
|
||||
[
|
||||
CreateTransactionAction::class,
|
||||
]
|
||||
)->thenReturn();
|
||||
}
|
||||
app(CreateRecordTransactionsAction::class)($this->getRecord(), $transactions);
|
||||
|
||||
$accountIds = collect($transactions)
|
||||
->pluck('account_id')
|
||||
|
||||
@@ -2,19 +2,22 @@
|
||||
|
||||
namespace App\Filament\Resources\SaleResource\Pages;
|
||||
|
||||
use App\Actions\Transactions\CreateTransactionAction;
|
||||
use App\DataObjects\CreateTransactionDTO;
|
||||
use App\Actions\Sales\CreateSaleAction;
|
||||
use App\Actions\Sales\SyncAccountsAction;
|
||||
use App\Actions\Transactions\CreateRecordTransactionsAction;
|
||||
use App\Commands\Clients\GenerateBaseAccountCommand;
|
||||
use App\Filament\Resources\ClientResource;
|
||||
use App\Filament\Resources\SaleResource;
|
||||
use App\Models\Branch;
|
||||
use App\Models\Client;
|
||||
use App\Models\Sale;
|
||||
use App\Services\Sales\SaleService;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Forms\Components\Actions;
|
||||
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
|
||||
{
|
||||
@@ -81,47 +84,17 @@ class CreateSale extends CreateRecord
|
||||
|
||||
public function processCreate(array $data, array $transactions): Model
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$record = app(SaleService::class)->create($this->getFormDataMutation($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();
|
||||
}
|
||||
|
||||
$accountIds = collect($transactions)
|
||||
->pluck('account_id')
|
||||
->filter()
|
||||
->unique()
|
||||
->values()
|
||||
->all();
|
||||
|
||||
$record->accounts()->sync($accountIds);
|
||||
|
||||
DB::commit();
|
||||
} catch (\Exception $exception) {
|
||||
DB::rollBack();
|
||||
throw new \Exception('Failed to save transactions : '.$exception->getMessage());
|
||||
}
|
||||
$record = app(CreateSaleAction::class)($this->getFormDataMutation($data), $transactions);
|
||||
|
||||
return $record;
|
||||
}
|
||||
|
||||
protected function afterCreate(): void
|
||||
protected function getRedirectUrl(): string
|
||||
{
|
||||
$branch = Branch::find($this->data['branch_id']);
|
||||
$client = $this->getClient();
|
||||
if (! $client) {
|
||||
return parent::getRedirectUrl();
|
||||
}
|
||||
return ClientResource::getUrl('view', ['record' => $client->id]).'?activeRelationManager=3';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Commands\Clients\GenerateBaseAccountCommand;
|
||||
use App\Models\Client;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
@@ -12,45 +13,7 @@ class ClientObserver
|
||||
*/
|
||||
public function created(Client $client): void
|
||||
{
|
||||
DB::transaction(function () use ($client) {
|
||||
$client->accounts()->createMany([
|
||||
[
|
||||
'account_type_id' => 1,
|
||||
'account' => 'Cash',
|
||||
'normal_balance' => 'debit',
|
||||
],
|
||||
[
|
||||
'account_type_id' => 1,
|
||||
'account' => 'Input Tax',
|
||||
'normal_balance' => 'debit',
|
||||
],
|
||||
[
|
||||
'account_type_id' => 1,
|
||||
'account' => 'Creditable Withholding Tax',
|
||||
'normal_balance' => 'debit',
|
||||
],
|
||||
[
|
||||
'account_type_id' => 2,
|
||||
'account' => 'Output Tax',
|
||||
'normal_balance' => 'credit',
|
||||
],
|
||||
[
|
||||
'account_type_id' => 2,
|
||||
'account' => 'Payable Withholding Tax',
|
||||
'normal_balance' => 'credit',
|
||||
],
|
||||
[
|
||||
'account_type_id' => 5,
|
||||
'account' => 'Vat Exempt Revenue',
|
||||
'normal_balance' => 'credit',
|
||||
],
|
||||
[
|
||||
'account_type_id' => 4,
|
||||
'account' => 'Sales Discount',
|
||||
'normal_balance' => 'debit',
|
||||
],
|
||||
]);
|
||||
});
|
||||
app(GenerateBaseAccountCommand::class)->execute($client);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Sales;
|
||||
|
||||
use App\DataObjects\CreateSaleDTO;
|
||||
use App\Models\Sale;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class SaleService
|
||||
{
|
||||
/**
|
||||
* Create a new class instance.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function create(array $data): Sale
|
||||
{
|
||||
|
||||
$tData = new CreateSaleDTO(
|
||||
reference_number: $data['current_series'],
|
||||
happened_on: Carbon::parse($data['happened_on']),
|
||||
branch_id: $data['branch_id'],
|
||||
user_id: Auth::user()->id,
|
||||
);
|
||||
return Sale::create($tData->toArray());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user