From 0d4e396a548c4fc3365280b3ef4a5b242560c5a1 Mon Sep 17 00:00:00 2001 From: glovetleong Date: Tue, 22 Mar 2022 14:59:17 +0800 Subject: [PATCH] group transaction --- .../CreateSupplierTransactionLogic.php | 42 ++++++++++ app/Models/Group.php | 13 +++ app/Models/GroupTransaction.php | 10 +++ .../2022_03_20_170628_create_groups_table.php | 40 ++++++++++ ...170641_create_group_transactions_table.php | 31 +++++++ .../RecoverGroupTransactionTableSeeder.php | 80 +++++++++++++++++++ 6 files changed, 216 insertions(+) create mode 100644 app/Models/Group.php create mode 100644 app/Models/GroupTransaction.php create mode 100644 database/migrations/2022_03_20_170628_create_groups_table.php create mode 100644 database/migrations/2022_03_20_170641_create_group_transactions_table.php create mode 100644 database/seeds/RecoverGroupTransactionTableSeeder.php diff --git a/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php index 1cf71643..c8f2813e 100644 --- a/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php +++ b/app/Classes/Modules/Transactions/ControllersLogic/CreateSupplierTransactionLogic.php @@ -5,6 +5,9 @@ namespace App\Classes\Modules\Transactions\ControllersLogic; use App\Classes\Modules\Transactions\Processors\CreateSupplierTransactionProcessor; use App\Models\Document; + +use App\Models\Group; + use Illuminate\Http\Request; use Illuminate\Http\JsonResponse; use Meneses\LaravelMpdf\Facades\LaravelMpdf; @@ -71,6 +74,44 @@ class CreateSupplierTransactionLogic extends AbstractControllerLogic if(!count($this->createSupplierTransactionProcessor->getBills())) return $this->response([]); + $group = new Group(); + $group->save(); + + $issuer = ''; + $receiver = ''; + $amount = 0; + $original_amount = 0; + $currency_id = 0; + $original_currency_id = ''; + $currency_rate = ''; + $tax = 0; + $service_charge = 0; + + foreach ($this->createSupplierTransactionProcessor->getBills() as $key => $row) { + $group->transaction()->sync($row->id, false); + $issuer = $row->issuer; + $receiver = $row->receiver; + $amount += $row->amount; + $original_amount += $row->original_amount; + $currency_id = $row->currency_id; + $original_currency_id = $row->original_currency_id; + $currency_rate = $row->currency_rate; + $tax += $row->tax; + $service_charge += $row->service_charge; + } + + $group->issuer = $issuer; + $group->receiver = $receiver; + $group->amount = $amount; + $group->original_amount = $original_amount; + $group->currency_id = $currency_id; + $group->original_currency_id = $original_currency_id; + $group->currency_rate = $currency_rate; + $group->tax = $tax; + $group->service_charge = $service_charge; + + $group->update(); + $pdf = LaravelMpdf::loadView('pages.pdfs.currency_vendor_order', ['transactions' => $this->createSupplierTransactionProcessor->getBills(), 'transferFeeTransactions' => $this->createSupplierTransactionProcessor->getTransferTransactions(), 'supplier' => $supplier]); $object = new DocumentObject( @@ -85,6 +126,7 @@ class CreateSupplierTransactionLogic extends AbstractControllerLogic $document = $this->createsDocument->execute($supplier, $object); $this->createsFile->execute($document, $object); + return $this->response([]); } } diff --git a/app/Models/Group.php b/app/Models/Group.php new file mode 100644 index 00000000..eff309e2 --- /dev/null +++ b/app/Models/Group.php @@ -0,0 +1,13 @@ +belongsToMany('App\Models\Transaction', 'group_transaction'); + } +} diff --git a/app/Models/GroupTransaction.php b/app/Models/GroupTransaction.php new file mode 100644 index 00000000..c8622ef9 --- /dev/null +++ b/app/Models/GroupTransaction.php @@ -0,0 +1,10 @@ +id(); + $table->foreignId('issuer')->unsigned(); + $table->foreignId('receiver')->unsigned(); + $table->decimal('amount', 14, 5)->default(0.00); + $table->decimal('original_amount', 14, 5)->default(0.00); + $table->foreignId('currency_id')->unsigned(); + $table->foreignId('original_currency_id')->unsigned(); + $table->decimal('currency_rate', 14, 5)->default(0.00); + $table->decimal('tax', 14, 5)->default(0.00); + $table->decimal('service_charge', 14, 5)->default(0.00); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('groups'); + } +} diff --git a/database/migrations/2022_03_20_170641_create_group_transactions_table.php b/database/migrations/2022_03_20_170641_create_group_transactions_table.php new file mode 100644 index 00000000..6ce7cedc --- /dev/null +++ b/database/migrations/2022_03_20_170641_create_group_transactions_table.php @@ -0,0 +1,31 @@ +foreignId('group_id')->unsigned(); + $table->foreignId('transaction_id')->unsigned(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('group_transaction'); + } +} diff --git a/database/seeds/RecoverGroupTransactionTableSeeder.php b/database/seeds/RecoverGroupTransactionTableSeeder.php new file mode 100644 index 00000000..ac1f01d7 --- /dev/null +++ b/database/seeds/RecoverGroupTransactionTableSeeder.php @@ -0,0 +1,80 @@ +get(); + $transaction_group = Transaction:: + select('issuer', 'currency_rate', 'type', DB::raw('count(DISTINCT id) as total'), DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d %H:%i') as new_date")) + ->where('type', 3) + ->groupBy( + 'issuer', + 'currency_rate', + 'new_date' + ) + ->get(); + + foreach ($transaction_group as $key => $row) { + $transaction = Transaction:: + where('type', 3) + ->where('issuer', $row->issuer) + ->where('currency_rate', $row->currency_rate) + ->where(DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d %H:%i')"), $row->new_date) + ->get(); + + $group = new Group(); + $group->save(); + + $issuer = ''; + $receiver = ''; + $amount = 0; + $original_amount = 0; + $currency_id = 0; + $original_currency_id = ''; + $currency_rate = ''; + $tax = 0; + $service_charge = 0; + + foreach ($transaction as $key_2 => $row_2) { + $group->transaction()->sync($row_2->id, false); + $issuer = $row_2->issuer; + $receiver = $row_2->receiver; + $amount += $row_2->amount; + $original_amount += $row_2->original_amount; + $currency_id = $row_2->currency_id; + $original_currency_id = $row_2->original_currency_id; + $currency_rate = $row_2->currency_rate; + $tax += $row_2->tax; + $service_charge += $row_2->service_charge; + } + + $group->issuer = $issuer; + $group->receiver = $receiver; + $group->amount = $amount; + $group->original_amount = $original_amount; + $group->currency_id = $currency_id; + $group->original_currency_id = $original_currency_id; + $group->currency_rate = $currency_rate; + $group->tax = $tax; + $group->service_charge = $service_charge; + + $group->update(); + } + + DB::commit(); + } +}