Files
exchange-2.0/app/Classes/Modules/Questionnaires/Processors/FetchNextQuestionV1AdminWFProcessor.php
T

149 lines
6.5 KiB
PHP

<?php
namespace App\Classes\Modules\Questionnaires\Processors;
use App\Classes\Modules\Questionnaires\Services\FetchesQuestion;
use App\Classes\Modules\Questionnaires\Services\ListsQuestions;
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;
use App\Models\QAUserAnswerSelected;
class FetchNextQuestionV1AdminWFProcessor
{
/** @var FetchesQuestion */
private $fetchesQuestion;
/** @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, CalculatesBookingOutstanding $calculatesBookingOutstanding, CalculatesBookingRefundAmount $calculatesBookingRefundAmount, FetchesBookingQuotation $fetchBookingQuotation)
{
$this->fetchesQuestion = $fetchesQuestion;
$this->listsQuestions = $listsQuestions;
$this->calculatesBookingOutstanding = $calculatesBookingOutstanding;
$this->calculatesBookingRefundAmount = $calculatesBookingRefundAmount;
$this->fetchBookingQuotation = $fetchBookingQuotation;
}
public function execute(array $currentQuestion, ?array $currentQuestionAnswer, array $questionContext, QAUserAnswerSelected $previousAnswer, ?Booking $booking, bool $isPrevious){
$isEnd = 0;
$questionId = 0;
$nextQuestionNumber = "";
$nextNestedQuestion = "";
$nextMainQuestion= "";
$questionnaireSetId = 0;
$questionType = QAType::DEFAULT;
$returnQuestion = null;
if ($currentQuestion && array_key_exists('id', $currentQuestion)) {
$questionId = $currentQuestion['id'];
$isEnd = $currentQuestion['is_end'];
$questionType = $currentQuestion['question_type'];
$nextNestedQuestion = $currentQuestion['next_nested_question'];
$nextMainQuestion = $currentQuestion['next_main_question'];
$questionnaireSetId = $currentQuestion['questionnaire_set_id'];
}
if ($currentQuestionAnswer && $currentQuestionAnswer['next_question_number']) {
$nextQuestionNumber = $currentQuestionAnswer['next_question_number'];
}
if ($isPrevious) {
if($previousAnswer)
{
$previousAnswer->delete();
}
else{
return null;
}
//Get previous question
$previousQuestion = $this->fetchesQuestion->execute(['questionnaire_set_id' => $questionnaireSetId, 'id' => $previousAnswer['question_id']]);
$returnQuestion = $previousQuestion;
}
else{
//Get next question
$nextQuestion = null;
$question_number = "";
if($nextQuestionNumber !== ""){
$question_number = $nextQuestionNumber;
}
else if($nextNestedQuestion !== ""){
$question_number = $nextNestedQuestion;
}
else {
$question_number = $nextMainQuestion;
}
if($question_number !== ""){
//Check if question exists
$questions = $this->listsQuestions->execute(['questionnaire_set_id' => $questionnaireSetId, 'question_number' => $question_number]);
if($previousAnswer['answer'] === '1688_order_verified' && count($questions) == 0){
$questionMetadata = $questionContext['questionMetadata'] ?? null;
$amountProcessed = $questionMetadata['amount_processed'];
$amountPaid = $questionMetadata['paid_amount'];
$wallet = $booking->company->wallets()->first();
$walletAmount = $wallet->amount;
if ($amountProcessed === $amountPaid) {
$question_number = '1688_proceed_order';
}
else if ($amountProcessed > $amountPaid) {
$outstanding = $this->calculatesBookingOutstanding->execute($booking) + $this->calculatesBookingRefundAmount->execute($booking, $booking->fix_currency_id);
$differenceUnderPayinCNY = $amountProcessed - $amountPaid;
$conversionObject = new CurrencyConversionObject(floatval(str_replace(',', '', $differenceUnderPayinCNY)), $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);
$amountUnderpay = $configurations->getTotal();
// if($amountUnderpay > round($outstanding, 2)) {
// $question_number = '1688_underpaid_order_3';
// }
if($walletAmount > $amountUnderpay) {
$question_number = '1688_underpaid_order_1';
}
else {
$question_number = '1688_underpaid_order_2';
}
}
else {
$question_number = '1688_overpaid_order';
}
}
$nextQuestion = $this->fetchesQuestion->execute(['questionnaire_set_id' => $questionnaireSetId, 'question_number' => $question_number]);
$returnQuestion = $nextQuestion;
}
}
return $returnQuestion;
}
}