la_bot_back/app/modules/tgbot/controllers/TgMainController.php
2025-01-27 20:03:58 +03:00

128 lines
4.6 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\modules\tgbot\controllers;
use app\modules\tgbot\models\forms\CardActionStep2Form;
use app\modules\tgbot\models\Tgbot;
use app\modules\tgbot\services\TgBotService;
use Cassandra\Decimal;
use kernel\app_modules\card\conditions\CashbackCondition;
use kernel\app_modules\card\models\Card;
use kernel\app_modules\card\models\CardTransaction;
use kernel\app_modules\card\services\CardService;
use kernel\app_modules\card\services\CardTransactionService;
use kernel\app_modules\tag\service\TagService;
use kernel\app_modules\tgbot\models\forms\CreateTgbotNotificationForm;
use kernel\app_modules\tgbot\models\TgbotNotification;
use kernel\app_modules\tgbot\services\TgbotNotificationService;
use kernel\Controller;
use kernel\Flash;
use kernel\helpers\Debug;
use kernel\modules\post\models\Post;
use kernel\Request;
class TgMainController extends Controller
{
protected function init(): void
{
parent::init();
$this->cgView->viewPath = APP_DIR . "/modules/tgbot/views/tgbot/main/";
$this->cgView->layout = "main.php";
$this->cgView->layoutPath = APP_DIR . "/modules/tgbot/views/tgbot/layout/";
$this->cgView->addVarToLayout("resources", "/resources/main");
}
public function actionMain(): void
{
$this->cgView->render("index.php");
}
public function actionNews(): void
{
$news = TagService::getEntityByTagSlug("novosti", Post::class);
$this->cgView->render("news.php", ['news' => $news]);
}
public function actionPromo(): void
{
$news = TagService::getEntityByTagSlug("akcii", Post::class);
$this->cgView->render("news.php", ['news' => $news]);
}
public function actionScanner(): void
{
$this->cgView->render("scanner.php");
}
public function actionCardActionStep1(int $cardId): void
{
$card = Card::where("id", $cardId)->first();
$this->cgView->render("card_action_step_1.php", ['card' => $card]);
}
public function actionCardActionStep2(): void
{
$params = new CardActionStep2Form();
$request = new Request();
$params->load($request->get());
$this->cgView->render("card_action_step_2.php", ['params' => $params]);
}
public function actionCardActionStep3(): void
{
$params = new CardActionStep2Form();
$request = new Request();
$params->load($request->post());
$card = CardService::getCardById($params->getItem("card_id"));
if ($params->getItem('type') === 'add_money') {
Flash::setMessage("success", "Баланс пополнен на " . $params->getItem('amount'));
$transaction = CardService::addMoneyToCard($card, (int)$params->getItem('amount'));
}
if ($params->getItem('type') === 'withdraw') {
Flash::setMessage("success", "С карты списано " . $params->getItem('amount'));
$transaction = CardService::withdrawMoneyFromCard($card, (int)$params->getItem('amount'));
}
if ($params->getItem('type') === 'add_purchase') {
// Flash::setMessage("success", "С карты списано " . $params->getItem('amount'));
// CardService::withdrawMoneyFromCard($card, (int)$params->getItem('amount'));
$cashback = new CashbackCondition();
$transaction = $cashback->handler($card, (int)$params->getItem('amount'));
Flash::setMessage("success", "Начислено кешбэк " . $transaction->amount);
}
$card = $card->fresh();
$tgbot = Tgbot::where("user_id", $card->user_id)->first();
if ($tgbot) {
$notificationForm = new CreateTgbotNotificationForm();
$notificationForm->load([
'bot_id' => $tgbot->bot_id,
'dialog_id' => $tgbot->dialog_id,
'content' => "На вашу карту начисленно " . $transaction->amount . " бонуса.",
'status' => TgbotNotification::TO_SEND_STATUS,
]);
$notificationService = new TgbotNotificationService();
$notificationService->create($notificationForm);
}
$this->cgView->render("card_action_step_3.php", ['card' => $card, 'transaction' => $transaction ?? null]);
}
public function actionCardInfo(int $cardId): void
{
$card = Card::where("id", $cardId)->first();
$transactions = CardTransaction::where("from", $cardId)->orWhere("to", $cardId)->get();
$this->cgView->render("card_info.php", ['card' => $card, 'transactions' => $transactions]);
}
}