accounting bank mapping part - mapping unknown transactions

This commit is contained in:
edmondlang
2023-05-21 22:59:48 +08:00
parent 1ede3edc08
commit dc9f09b18a
5 changed files with 274 additions and 34 deletions
@@ -3,15 +3,23 @@
namespace App\Classes\Modules\Accounting\ControllersLogic;
use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Accounting\Services\UpdatesBankStatementDetails;
use App\Classes\Modules\Accounting\Services\FetchesBankStatementDetails;
use App\Classes\Modules\Accounting\Standards\Rules\CanUpdateCompany;
use App\Classes\Modules\Accounting\DataTransferObjects\BankStatementDetailObject;
use App\Classes\Modules\Accounting\DataTransferObjects\BankStatementTransactionObject;
use App\Http\Resources\BankStatementDetailResource;
use App\Classes\Modules\Accounting\Services\CreatesBankStatementTransactionOwner;
use App\Classes\Modules\Accounting\Services\FetchesBankStatementTransaction;
use App\Classes\ValueObjects\Constants\StatementTransactionOwnerType;
use App\Classes\ValueObjects\Constants\SystemType;
use App\Models\Transaction;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use ErrorException;
use Illuminate\Support\Facades\Log;
class UpdateBankStatementDetailLogic extends AbstractControllerLogic
{
@@ -35,15 +43,25 @@ class UpdateBankStatementDetailLogic extends AbstractControllerLogic
/** @var FetchesBankStatementDetails */
private $fetchesBankStatementDetails;
/** @var FetchesBankStatementTransaction */
private $fetchesBankStatementTransaction;
/** @var CreatesBankStatementTransactionOwner */
private $createsBankStatementTransactionOwner;
/**
* UpdateBankStatementDetailLogic constructor.
* @param UpdatesBankStatementDetails $updatesBankStatementDetails
* @param FetchesBankStatementDetails $fetchesBankStatementDetails
* @param fetchesBankStatementTransaction $fetchesBankStatementTransaction
* @param CreatesBankStatementTransactionOwner $createsBankStatementTransactionOwner
*/
public function __construct(UpdatesBankStatementDetails $updatesBankStatementDetails, FetchesBankStatementDetails $fetchesBankStatementDetails)
public function __construct(UpdatesBankStatementDetails $updatesBankStatementDetails, FetchesBankStatementDetails $fetchesBankStatementDetails, FetchesBankStatementTransaction $fetchesBankStatementTransaction, CreatesBankStatementTransactionOwner $createsBankStatementTransactionOwner)
{
$this->updatesBankStatementDetails = $updatesBankStatementDetails;
$this->fetchesBankStatementDetails = $fetchesBankStatementDetails;
$this->fetchesBankStatementTransaction = $fetchesBankStatementTransaction;
$this->createsBankStatementTransactionOwner = $createsBankStatementTransactionOwner;
}
@@ -52,15 +70,142 @@ class UpdateBankStatementDetailLogic extends AbstractControllerLogic
* @return JsonResponse
* @throws ErrorException
*/
public function logic(Request $request) : JsonResponse
public function logic(Request $request): JsonResponse
{
$object = new BankStatementDetailObject($request->input('pay_for'), $request->input('system_references'));
$bankStatementTransaction = $this->fetchesBankStatementTransaction->execute(['id' => $request->route('id')]);
$query = $this->fetchesBankStatementDetails->execute(['id' => $request->route('id')]);
$owner_type = null;
$owner_id = null;
$statementTransactionOwnerType = null;
$system = null;
$owner_reference = null;
$query = $this->updatesBankStatementDetails->execute($query, $object);
switch ($request->input('pay_for')) {
case 'sales':
if (in_array($request->input('system_references'), ['exchange', 'izyim'])) {
$transaction = $this->verifyBillNumber($request->input('transaction_reference'), $request->input('system_references'));
return $this->resourceResponse(new BankStatementDetailResource($query));
// todo-new: update / test on izyim system
$owner_type = Transaction::class;
$owner_id = $transaction->id;
// todo-new: owner_reference
// booking number / order number
$owner_reference = null;
$statementTransactionOwnerType = StatementTransactionOwnerType::SALES;
$system = $request->input('system_references');
} else {
switch ($request->input('system_references')) {
case 'lite':
$statementTransactionOwnerType = StatementTransactionOwnerType::SALES_LITE;
break;
case 'cntr':
$statementTransactionOwnerType = StatementTransactionOwnerType::SALES_CNTR;
break;
case 'probashi':
$statementTransactionOwnerType = StatementTransactionOwnerType::SALES_PROBASHI;
break;
case 'pets':
$statementTransactionOwnerType = StatementTransactionOwnerType::SALES_PETS;
break;
default:
throw new MalformedRequestException('System Reference not allowed');
}
}
break;
case 'top_up':
$transaction = $this->verifyBillNumber($request->input('transaction_reference'), $request->input('system_references'));
$owner_type = Transaction::class;
$owner_id = $transaction->id;
// todo-new: owner_reference
// top_up - store customer marking
$owner_reference = null;
$statementTransactionOwnerType = StatementTransactionOwnerType::WALLET_TOP_UP;
$system = $request->input('system_references');
break;
case 'internal_bank_transfer':
$statementTransactionOwnerType = StatementTransactionOwnerType::INTERNAL_BANK_TRANSFER_IN; // if negative -> out, positive -> in
$system = $request->input('system_references');
break;
case 'others':
$owner_reference = $request->input('transaction_reference');
$statementTransactionOwnerType = StatementTransactionOwnerType::NON_OPERATIONAL;
$system = $request->input('system_references');
break;
default:
throw new MalformedRequestException('Transaction Type Not Allowed');
}
$system == null ? '' : $system = SystemType::SYSTEM_NAMES[$system];
dd([
'type' => $statementTransactionOwnerType,
'system' => $system,
// 'owner_type' => Transaction::class,
// todo-new: make sure owner_type is a class
'owner_type' => $owner_type,
'owner_id' => $owner_id,
'owner_reference' => $owner_reference
]);
$bankStatementTransaction->owners()->firstOrCreate([
'type' => $statementTransactionOwnerType,
'system' => $system,
// 'owner_type' => Transaction::class,
// todo-new: make sure owner_type is a class
'owner_type' => $owner_type,
'owner_id' => $owner_id,
'owner_reference' => $owner_reference
]);
dd($bankStatementTransaction->owners()->first());
// $this->canCreateUser->passes($user_object);
// $user = $this->createsBankStatementTransactionOwner->execute($bankStatementTransactionObject);
// return $this->resourceResponse(new BankStatementDetailResource($query));
}
public function verifyBillNumber($bill_no, $system_reference)
{
if ($system_reference == 'izyim') {
try {
$url = 'https://izyim.cief-malaysia.com/public/api/v1/transactions/query';
$client = new \GuzzleHttp\Client(['verify' => false]);
$response = $client->request('GET', $url . '?api-key=510acd13d8d24375cf038ad626c282565451461a9c2399357e0b65365300787e&filters={"bill_no":' . $bill_no . '}');
$body = $response->getBody();
$data = json_decode($body, true);
dd($data);
$payload = $data['payload'];
$transactions2 = $payload['data'];
dd($transactions2);
return $transactions2;
} catch (\Exception $exception) {
Log::error($exception);
dd($exception);
preg_match('/\{.*\}/s', $exception->getMessage(), $matches);
$jsonError = json_decode($matches[0]);
// Retrieved Transactions failed
throw new MalformedRequestException($jsonError->title);
}
}
if ($system_reference == 'exchange') {
$transaction = Transaction::where('bill_no', $bill_no)->first();
if ($transaction) {
return $transaction;
}
}
// if not found
throw new MalformedRequestException('Bill Number Not Found.');
}
}
@@ -0,0 +1,34 @@
<?php
namespace App\Classes\Modules\Accounting\Services;
use App\Classes\General\Eloquent\AbstractUpdateRecord;
use App\Classes\Modules\Accounting\DataTransferObjects\BankStatementTransactionObject;
use App\Models\StatementTransactionOwner;
class CreatesBankStatementTransactionOwner extends AbstractUpdateRecord
{
/**
* @param StatementTransactionOwner $model
* @param BankStatementDetailsObject $object
* @return \Illuminate\Database\Eloquent\Model
* @throws \App\Classes\Exceptions\MalformedRequestException
*/
public function execute(BankStatementTransactionObject $object)
{
$model = new StatementTransactionOwner();
$model->statement_transaction_id = $object->getStatementTransactionId();
$model->type = $object->getType();
$model->system = $object->getSystem();
$model->owner_type = $object->getOwnerType();
$model->owner_id = $object->getOwnerId();
$model->invoice_reference = $object->getInvoiceReference();
$model->receipt_reference = $object->getReceipteReference();
$model->is_auto_mapped = $object->getIsAutoMapped();
$model->status = $object->getStatus();
return $this->handler($model);
}
}
@@ -34,4 +34,12 @@ final class StatementTransactionOwnerType {
public const NON_OPERATIONAL = 14;
public const SALES_LITE = 15;
public const SALES_CNTR = 16;
public const SALES_PROBASHI = 17;
public const SALES_PETS = 18;
}
@@ -0,0 +1,16 @@
<?php
namespace App\Classes\ValueObjects\Constants;
final class SystemType {
public const EXCHANGE = 'EXCHANGE';
public const SHIPPING_PORTAL = 'SHIPPING_PORTAL';
public const SYSTEM_NAMES = [
'exchange' => self::EXCHANGE,
'shipping_portal' => self::SHIPPING_PORTAL,
'izyim' => self::SHIPPING_PORTAL,
];
}
@@ -1,26 +1,6 @@
<template>
<div class="row ">
<div class="col bg-white padding-15">
<div class="row">
<div class="col">
<div class="row">
<div class="col">
<validation-wrapper-component :validator="$v.pay_for">
<label>Pay For</label>
<input type="text" class="form-control" v-model="pay_for" >
</validation-wrapper-component>
</div>
</div>
<div class="row">
<div class="col">
<validation-wrapper-component :validator="$v.system_references">
<label>System References</label>
<input type="text" class="form-control" v-model="system_references" >
</validation-wrapper-component>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="form-group">
@@ -43,6 +23,36 @@
</div>
</div>
</div>
<div class="row m-b-15">
<div class="col">
<div class="row">
<div class="col">
<validation-wrapper-component selectable :validator="$v.pay_for_value">
<label>Transaction Type</label>
<select-component :options="['Sales','Top Up', 'Internal Bank Transfer', 'Others']" v-model="pay_for_value"></select-component>
</validation-wrapper-component>
</div>
</div>
<div class="row m-t-15" v-if="['Sales', 'Top Up'].includes(pay_for_value)">
<div class="col">
<validation-wrapper-component selectable :key="custom_options_key" :validator="$v.system_references">
<label>Service</label>
<select-component :options="customOptions()" v-model="system_references"></select-component>
</validation-wrapper-component>
</div>
</div>
<div class="row m-t-15" v-if="['Sales', 'Top Up', 'Others'].includes(pay_for_value)">
<div class="col">
<validation-wrapper-component :validator="$v.transaction_reference">
<label>Transaction ID / Reference</label>
<input type="text" class="form-control" v-model.lazy="transaction_reference">
</validation-wrapper-component>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col">
<div class="row">
@@ -62,15 +72,21 @@
<script>
import componentHandler from '../../../general/mixins/componentHandler';
// import ModalFormHandler from '../../../general/mixins/modalFormHandler';
import { required } from "vuelidate/lib/validators";
// import FormHandler from '../../../general/mixins/formHandler';
import { required } from "vuelidate/lib/validators";
export default {
data(){
return {
error: '',
pay_for: "",
system_references: ""
system_references: "",
transaction_reference: "",
pay_for_value: "",
custom_options_key: 1,
pay_for: {
status: false,
options: ['sales', 'top_up', 'internal_bank_transfer', 'others'],
},
}
},
created() {
@@ -78,26 +94,47 @@
this.system_references = this.item.system_references
},
validations: {
pay_for: { required },
system_references: { required }
system_references: { },
transaction_reference: { },
pay_for_value: { required },
},
watch: {
'data': function() {
this.pay_for = this.item.pay_for;
this.system_references = this.item.system_references;
},
pay_for_value(newVal, oldVal) {
this.custom_options_key ++;
this.transaction_reference = '';
}
},
methods: {
submitForm(){
this.parameters = {pay_for : this.pay_for, system_references: this.system_references};
this.item.pay_for = this.pay_for;
this.item.system_references = this.system_references;
this.pay_for_value = this.pay_for_value.replace(/\s/g, "_").toLowerCase();
this.system_references = this.system_references ? this.system_references.toLowerCase() : '';
this.parameters = {
pay_for : this.pay_for_value,
system_references : this.system_references,
transaction_reference : this.transaction_reference
};
this.submit(this.route('api.accounting.statement.details.update', this.data.id), 'put', this.section, true, true);
},
successHandler(){
// this.closeModal();
// this.formHandler('');
},
customOptions(){
switch (this.pay_for_value) {
case 'Sales':
return ['Exchange', 'Izyim', 'Lite', 'Cntr', 'Probashi', 'Pets'];
case 'Top Up':
return ['Exchange', 'Izyim'];
default:
return [];
}
},
},
mixins: [componentHandler]
}