mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 04:23:55 +00:00
supplier bill payment refund module
This commit is contained in:
@@ -21,9 +21,11 @@ use App\Classes\Modules\Documents\Services\CreatesDocument;
|
||||
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
|
||||
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject;
|
||||
use App\Classes\Modules\Transactions\Services\CreatesTransaction;
|
||||
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
|
||||
use App\Classes\ValueObjects\Constants\PaymentMethodType;
|
||||
use App\Classes\ValueObjects\Constants\TransactionType;
|
||||
use App\Models\BillGroup;
|
||||
use App\Models\Transaction;
|
||||
|
||||
class CreateSupplierBillGroupLogic extends AbstractControllerLogic
|
||||
{
|
||||
@@ -57,6 +59,9 @@ class CreateSupplierBillGroupLogic extends AbstractControllerLogic
|
||||
/** @var UpdateGroupLogic */
|
||||
private $updateGroupLogic;
|
||||
|
||||
/** @var UpdatesTransactionStatus */
|
||||
private $updatesTransactionStatus;
|
||||
|
||||
|
||||
/**
|
||||
* CreateSupplierBillGroupLogic constructor.
|
||||
@@ -66,8 +71,9 @@ class CreateSupplierBillGroupLogic extends AbstractControllerLogic
|
||||
* @param CreatesFiles $createsFile
|
||||
* @param GeneratesTransactionBillNumber $generatesTransactionBillNumber
|
||||
* @param UpdateGroupLogic $updateGroupLogic
|
||||
* @param UpdatesTransactionStatus $updatesTransactionStatus
|
||||
*/
|
||||
public function __construct(FetchesCompany $fetchesCompany, CreatesTransaction $createsTransaction, CreatesDocument $createsDocument, CreatesFiles $createsFile, GeneratesTransactionBillNumber $generatesTransactionBillNumber, UpdateGroupLogic $updateGroupLogic)
|
||||
public function __construct(FetchesCompany $fetchesCompany, CreatesTransaction $createsTransaction, CreatesDocument $createsDocument, CreatesFiles $createsFile, GeneratesTransactionBillNumber $generatesTransactionBillNumber, UpdateGroupLogic $updateGroupLogic, UpdatesTransactionStatus $updatesTransactionStatus)
|
||||
{
|
||||
$this->fetchesCompany = $fetchesCompany;
|
||||
$this->createsTransaction = $createsTransaction;
|
||||
@@ -75,6 +81,7 @@ class CreateSupplierBillGroupLogic extends AbstractControllerLogic
|
||||
$this->createsFile = $createsFile;
|
||||
$this->generatesTransactionBillNumber = $generatesTransactionBillNumber;
|
||||
$this->updateGroupLogic = $updateGroupLogic;
|
||||
$this->updatesTransactionStatus = $updatesTransactionStatus;
|
||||
}
|
||||
|
||||
public function logic(Request $request) : JsonResponse
|
||||
@@ -83,6 +90,19 @@ class CreateSupplierBillGroupLogic extends AbstractControllerLogic
|
||||
$supplier = $this->fetchesCompany->execute(['id' => $request->route('id')]);
|
||||
|
||||
$payments = $request->input('payments');
|
||||
$supplierRefunds = $request->input('supplierRefunds');
|
||||
|
||||
foreach ($supplierRefunds as $supplierRefund) {
|
||||
$refund = Transaction::find($supplierRefund['id']);
|
||||
|
||||
if ($refund->owner->transactions()->where('type', TransactionType::BILL)->first()->issuer !== $supplier->id) {
|
||||
throw new MalformedRequestException('The supplier refund and bill group does not belongs to same supplier.');
|
||||
}
|
||||
|
||||
if ($refund->type !== TransactionType::SUPPLIER_REFUND) {
|
||||
throw new MalformedRequestException('Only transaction type supplier refund can be used for bill refund.');
|
||||
}
|
||||
}
|
||||
|
||||
$amount = 0;
|
||||
$original_amount = 0;
|
||||
@@ -129,6 +149,41 @@ class CreateSupplierBillGroupLogic extends AbstractControllerLogic
|
||||
$billGroup->groups()->sync($payment['id'], false);
|
||||
}
|
||||
|
||||
//create bill refund
|
||||
foreach ($supplierRefunds as $supplierRefund) {
|
||||
$refund = Transaction::find($supplierRefund['id']);
|
||||
$deductedRefunds = $refund->transactions()->where('type', TransactionType::BILL_REFUND)->where('status', ApprovalStatus::APPROVED)->get();
|
||||
$refundDeductableAmount = $refund->amount - $deductedRefunds->sum('amount');
|
||||
$refundDeductableOriginalAmount = $refund->original_amount - $deductedRefunds->sum('original_amount');
|
||||
|
||||
$amount -= round($refundDeductableAmount, 2);
|
||||
$original_amount -= round($refundDeductableOriginalAmount, 2);
|
||||
|
||||
if ($amount > 0) {
|
||||
$deductedRefundAmount = $refundDeductableAmount;
|
||||
$deductedRefundOriginalAmount = $refundDeductableOriginalAmount;
|
||||
|
||||
$this->updatesTransactionStatus->execute($refund, ApprovalStatus::COMPLETED);
|
||||
}
|
||||
|
||||
if ($amount < 0) {
|
||||
$deductedRefundAmount = $refundDeductableAmount + $amount;
|
||||
$deductedRefundOriginalAmount = $refundDeductableOriginalAmount + $original_amount;
|
||||
}
|
||||
|
||||
$billNumber = $this->generatesTransactionBillNumber->execute('BRFD-');
|
||||
|
||||
$object = new TransactionObject($billNumber, TransactionType::BILL_REFUND, $supplier->id, 1,
|
||||
1, PaymentMethodType::CASH,
|
||||
$deductedRefundAmount, $deductedRefundOriginalAmount, $refund->currency_id,
|
||||
$refund->original_currency_id, $deductedRefundOriginalAmount / $deductedRefundAmount,
|
||||
0, 0, null, ApprovalStatus::APPROVED, []);
|
||||
|
||||
$transaction = $this->createsTransaction->execute($refund, $object);
|
||||
|
||||
$billGroup->billRefunds()->sync($transaction->id, false);
|
||||
}
|
||||
|
||||
return $this->response([]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user