dmin Workflow - Fix Reported Issues - Automatically deduct funds from customer wallet when customer underpaid

This commit is contained in:
Dillon Ngo
2025-02-22 19:18:35 +08:00
parent 0a5d9c1f95
commit f2ba89d3e4
10 changed files with 385 additions and 220 deletions
@@ -5,10 +5,16 @@ namespace App\Classes\Modules\Questionnaires\Processors;
use App\Classes\Modules\Questionnaires\Services\FetchesQuestion;
use App\Classes\Modules\Questionnaires\Services\ListsQuestions;
use Illuminate\Http\Request;
use App\Classes\Modules\Bookings\Services\CalculatesBookingOutstanding;
use App\Classes\Modules\Bookings\Services\CalculatesBookingRefundAmount;
use App\Classes\Modules\Bookings\Services\FetchesBookingQuotation;
use App\Classes\Modules\Currencies\DataTransferObjects\CurrencyConversionObject;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Classes\ValueObjects\Constants\QAType;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use App\Models\Booking;
class FetchNextQuestionV1AdminWFProcessor
{
@@ -18,18 +24,33 @@ class FetchNextQuestionV1AdminWFProcessor
/** @var ListsQuestions */
private $listsQuestions;
/** @var CalculatesBookingOutstanding */
private $calculatesBookingOutstanding;
/** @var CalculatesBookingRefundAmount */
private $calculatesBookingRefundAmount;
/** @var FetchesBookingQuotation */
private $fetchBookingQuotation;
/**
* FetchNextQuestionV1AdminWFProcessor constructor.
* @param FetchesQuestion $fetchesQuestion
* @param ListsQuestions $listsQuestions
* @param CalculatesBookingOutstanding $calculatesBookingOutstanding
* @param CalculatesBookingRefundAmount $calculatesBookingRefundAmount
* @param FetchesBookingQuotation $fetchBookingQuotation
*/
public function __construct(FetchesQuestion $fetchesQuestion, ListsQuestions $listsQuestions)
public function __construct(FetchesQuestion $fetchesQuestion, ListsQuestions $listsQuestions, CalculatesBookingOutstanding $calculatesBookingOutstanding, CalculatesBookingRefundAmount $calculatesBookingRefundAmount, FetchesBookingQuotation $fetchBookingQuotation)
{
$this->fetchesQuestion = $fetchesQuestion;
$this->listsQuestions = $listsQuestions;
$this->calculatesBookingOutstanding = $calculatesBookingOutstanding;
$this->calculatesBookingRefundAmount = $calculatesBookingRefundAmount;
$this->fetchBookingQuotation = $fetchBookingQuotation;
}
public function execute(Request $request, $previousAnswer){
public function execute(Request $request, $previousAnswer, $booking){
$isEnd = 0;
$questionId = 0;
$nextQuestionNumber = "";
@@ -62,8 +83,6 @@ class FetchNextQuestionV1AdminWFProcessor
return null;
}
Log::info("Previous Answer question id: ".$previousAnswer['question_id']);
//Get previous question
$previousQuestion = $this->fetchesQuestion->execute(['questionnaire_set_id' => $questionnaireSetId, 'id' => $previousAnswer['question_id']]);
$returnQuestion = $previousQuestion;
@@ -85,31 +104,39 @@ class FetchNextQuestionV1AdminWFProcessor
if($question_number !== ""){
//Check if question exists
$questions = $this->listsQuestions->execute(['questionnaire_set_id' => $questionnaireSetId, 'question_number' => $question_number]);
if($previousAnswer['answer'] === '1688_underpaid_overpaid' && count($questions) == 0){
$extra = $request->extra;
$questionMetadata = $extra['questionMetadata'] ?? null;
$processed = round((floatval($questionMetadata['amount_processed']) + PHP_FLOAT_EPSILON) * 1000) / 1000;
$amount = round((floatval($questionMetadata['amount']) + PHP_FLOAT_EPSILON) * 1000) / 1000;
// $processed = round((floatval($questionMetadata['amount_processed']) + PHP_FLOAT_EPSILON) * 1000) / 1000;
// $amount = round((floatval($questionMetadata['amount']) + PHP_FLOAT_EPSILON) * 1000) / 1000;
$processed = $questionMetadata['amount_processed'];
$amount = $questionMetadata['amount'];
$wallet = $booking->company->wallets()->first();
$walletAmount = $wallet->amount;
//1688_underpaid_order_1, 1688_underpaid_order_2,1688_overpaid_order
if ($processed === $amount) {
Log::info('The amounts are exactly the same:', [
'amount_processed' => $questionMetadata['amount_processed'],
'amount' => $questionMetadata['amount']
]);
$question_number = '1688_underpaid_order_1';
} elseif ($processed < $amount) {
Log::info('The processed amount is under the expected amount:', [
'amount_processed' => $questionMetadata['amount_processed'],
'amount' => $questionMetadata['amount']
]);
$question_number = '1688_underpaid_order_2';
$question_number = '1688_order_paid_precisely';
}
else if ($processed < $amount) {
$outstanding = $this->calculatesBookingOutstanding->execute($booking) + $this->calculatesBookingRefundAmount->execute($booking, $booking->fix_currency_id);
$differenceUnder = $amount - $processed;
$conversionObject = new CurrencyConversionObject(floatval(str_replace(',', '', $differenceUnder)), $booking->convertible_currency_id, $booking->service_id, $booking->fix_currency_id === 1 ? 0:1, PaymentMethodType::PAYMENT_METHODS['wallet']);
$configurations = $this->fetchBookingQuotation->execute($booking->company, $conversionObject);
$underpay = $configurations->getTotal();
if($underpay > round($outstanding, 2)) {
// throw new MalformedRequestException('Your payment must not be greater than '. $outstanding .'.');
$question_number = '1688_underpaid_order_3';
}
else if($walletAmount > $underpay) {
$question_number = '1688_underpaid_order_1';
}
else {
$question_number = '1688_underpaid_order_2';
}
} else {
Log::info('The processed amount is over the expected amount:', [
'amount_processed' => $questionMetadata['amount_processed'],
'amount' => $questionMetadata['amount']
]);
$question_number = '1688_overpaid_order';
}
}
@@ -117,14 +144,8 @@ class FetchNextQuestionV1AdminWFProcessor
$nextQuestion = $this->fetchesQuestion->execute(['questionnaire_set_id' => $questionnaireSetId, 'question_number' => $question_number]);
$returnQuestion = $nextQuestion;
}
//cief todo: 74
else if (array_key_exists('id', $request->input('question'))) {
dd('cbal todo');
$nextQuestion = $this->fetchesQuestion->execute(['questionnaire_set_id' => $questionnaireSetId, 'id_next' => $questionId, 'next_main_question' => null]);
$returnQuestion = $nextQuestion;
}
}
return $returnQuestion;
}
}
}