transaction

This commit is contained in:
Kavalar 2025-01-20 00:12:30 +03:00
parent 1ffa0581bf
commit 485c11de5f
16 changed files with 589 additions and 1 deletions

View File

@ -2,7 +2,19 @@
namespace app\modules\tgbot\controllers;
use app\modules\tgbot\models\Tgbot;
class TgBotRestController extends \kernel\app_modules\tgbot\controllers\TgBotRestController
{
public function actionGetScanBtn(int $id): void
{
$dialog = Tgbot::where("dialog_id", $id)->first();
if ($dialog){
$this->renderApi([
'html' => '<a class="btn btn-primary" href="/miniapp/scan">Сканировать</a>',
]);
}
}
}

View File

@ -2,6 +2,7 @@
namespace app\modules\tgbot\controllers;
use app\modules\tgbot\models\Tgbot;
use Cassandra\Decimal;
use kernel\app_modules\tag\service\TagService;
use kernel\Controller;

View File

@ -0,0 +1 @@
<a class="btn btn-primary" href="/miniapp/scan">Сканировать</a>

View File

@ -64,6 +64,13 @@ class CardModule extends Module
"slug" => "card_program_conditions",
"parent_slug" => "card"
]);
$this->menuService->createItem([
"label" => "Транзакции",
"url" => "/admin/card_transaction",
"slug" => "card_transaction",
"parent_slug" => "card"
]);
}
/**
@ -76,6 +83,7 @@ class CardModule extends Module
$this->menuService->removeItemBySlug("card_template");
$this->menuService->removeItemBySlug("card_program");
$this->menuService->removeItemBySlug("card_program_conditions");
$this->menuService->removeItemBySlug("card_transaction");
$this->migrationService->rollbackAtPath("{KERNEL_APP_MODULES}/card/migrations");
$this->consoleService->runComposerRemove("dragon-code/card-number");
$this->consoleService->runComposerRemove("endroid/qr-code");

View File

@ -0,0 +1,120 @@
<?php
namespace kernel\app_modules\card\controllers;
use Exception;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
use kernel\app_modules\card\models\CardTemplate;
use kernel\app_modules\card\models\CardTransaction;
use kernel\app_modules\card\models\forms\CreateCardTemplateForm;
use kernel\app_modules\card\models\forms\CreateCardTransactionForm;
use kernel\app_modules\card\services\CardService;
use kernel\app_modules\card\services\CardTemplateService;
use kernel\app_modules\card\services\CardTransactionService;
use kernel\FileUpload;
use kernel\Flash;
class CardTransactionController extends AdminController
{
private CardTransactionService $cardTransactionService;
protected function init(): void
{
parent::init();
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/card/views/card_transaction/";
$this->cardTransactionService = new CardTransactionService();
}
public function actionCreate(): void
{
$this->cgView->render("form.php");
}
#[NoReturn] public function actionAdd(): void
{
$cardForm = new CreateCardTransactionForm();
$cardForm->load($_REQUEST);
if ($cardForm->validate()){
$cardTransaction = $this->cardTransactionService->create($cardForm);
if ($cardTransaction){
Flash::setMessage("success", "Card transaction created");
$this->redirect("/admin/card_transaction/" . $cardTransaction->id);
}
Flash::setMessage("error", $this->cardTransactionService->getErrorsString());
$this->redirect("/admin/card_transaction/create");
}
Flash::setMessage("error", $cardForm->getErrorsStr());
$this->redirect("/admin/card_transaction/create");
}
public function actionIndex($page_number = 1): void
{
$this->cgView->render("index.php", ['page_number' => $page_number]);
}
/**
* @throws Exception
*/
public function actionView($id): void
{
$card = CardTransaction::find($id);
if (!$card){
throw new Exception(message: "The card transaction not found");
}
$this->cgView->render("view.php", ['card_transaction' => $card]);
}
/**
* @throws Exception
*/
public function actionUpdate($id): void
{
$model = CardTransaction::find($id);
if (!$model){
throw new Exception(message: "The card transaction not found");
}
$this->cgView->render("form.php", ['model' => $model]);
}
/**
* @throws Exception
*/
public function actionEdit($id): void
{
$cardTransaction = CardTransaction::find($id);
if (!$cardTransaction){
throw new Exception(message: "The card transaction not found");
}
$cardTransactionForm = new CreateCardTransactionForm();
$cardTransactionForm->load($_REQUEST);
if ($cardTransactionForm->validateForUpdate()) {
$card = $this->cardTransactionService->update($cardTransactionForm, $cardTransaction);
if ($card) {
$this->redirect("/admin/card_transaction/" . $card->id);
}
}
Flash::setMessage("error", $cardTransaction->getErrorsStr());
$this->redirect("/admin/card_transaction/update/" . $id);
}
#[NoReturn] public function actionDelete($id): void
{
$card = CardTemplate::where("id", $id)->first();
if (!$card){
Flash::setMessage("error", "Card transaction not found");
}
else {
$card->delete();
}
$this->redirect("/admin/card_template/");
}
}

View File

@ -60,6 +60,17 @@ return new class extends Migration
$table->timestamps();
});
\kernel\App::$db->schema->create('card_transaction', function (Blueprint $table) {
$table->increments('id');
$table->integer('from')->nullable(false);
$table->integer('to')->nullable(false);
$table->integer('amount')->nullable(false);
$table->integer('type')->nullable(false)->default(1);
$table->integer('status')->default(1);
$table->timestamps();
});
\kernel\App::$db->schema->create('card', function (Blueprint $table) {
$table->increments('id')->from(105545);
$table->integer('user_id')->nullable(false);

View File

@ -0,0 +1,56 @@
<?php
namespace kernel\app_modules\card\models;
use Illuminate\Database\Eloquent\Model;
/**
* @property integer $from
* @property integer $to
* @property integer $amount
* @property integer $type
* @property integer $status
*/
class CardTransaction extends Model
{
const DISABLE_STATUS = 0;
const SUCCESS_STATUS = 1;
const IN_PROGRESS_STATUS = 2;
protected $table = 'card_transaction';
protected $fillable = ['from', 'to', 'amount', 'type', 'status'];
public static function labels(): array
{
// Заполнить массив
// Пример: [
// 'label' => 'Заголовок',
// 'entity' => 'Сущность',
// 'slug' => 'Slug',
// 'status' => 'Статус',
// ]
return [
'from' => 'От',
'to' => 'Кому',
'amount' => 'Количество',
'type' => 'Тип операции',
'status' => 'Статус',
];
}
/**
* @return string[]
*/
public static function getStatus(): array
{
return [
self::DISABLE_STATUS => "Не активный",
self::SUCCESS_STATUS => "Завершен",
self::IN_PROGRESS_STATUS => "В процессе",
];
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace kernel\app_modules\card\models\forms;
use kernel\FormModel;
class CreateCardTransactionForm extends FormModel
{
public function rules(): array
{
// Заполнить массив правил
// Пример:
// return [
// 'label' => 'required|min-str-len:5|max-str-len:30',
// 'entity' => 'required',
// 'slug' => '',
// 'status' => ''
// ];
return [
'from' => 'required|alpha-numeric',
'to' => 'required|alpha-numeric',
'amount' => 'required|alpha-numeric',
'type' => 'required|alpha-numeric',
'status' => ''
];
}
}

View File

@ -64,6 +64,21 @@ App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $router
});
});
App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $router) {
App::$collector->group(["before" => "auth"], function (RouteCollector $router) {
App::$collector->group(["prefix" => "card_transaction"], function (CGRouteCollector $router) {
App::$collector->get('/', [\kernel\app_modules\card\controllers\CardTransactionController::class, 'actionIndex']);
App::$collector->get('/page/{page_number}', [\kernel\app_modules\card\controllers\CardTransactionController::class, 'actionIndex']);
App::$collector->get('/create', [\kernel\app_modules\card\controllers\CardTransactionController::class, 'actionCreate']);
App::$collector->post("/", [\kernel\app_modules\card\controllers\CardTransactionController::class, 'actionAdd']);
App::$collector->get('/{id}', [\kernel\app_modules\card\controllers\CardTransactionController::class, 'actionView']);
App::$collector->any('/update/{id}', [\kernel\app_modules\card\controllers\CardTransactionController::class, 'actionUpdate']);
App::$collector->any("/edit/{id}", [\kernel\app_modules\card\controllers\CardTransactionController::class, 'actionEdit']);
App::$collector->get('/delete/{id}', [\kernel\app_modules\card\controllers\CardTransactionController::class, 'actionDelete']);
});
});
});
App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router){
App::$collector->group(['before' => 'bearer'], function (CgRouteCollector $router){
$router->rest("card", [\kernel\app_modules\card\controllers\CardRestController::class]);

View File

@ -0,0 +1,108 @@
<?php
namespace kernel\app_modules\card\services;
use kernel\app_modules\card\models\Card;
use kernel\app_modules\card\models\CardTemplate;
use kernel\app_modules\card\models\CardTransaction;
use kernel\FormModel;
class CardTransactionService
{
protected array $errors = [];
public function create(FormModel $form_model): false|CardTransaction
{
$model = new CardTransaction();
$fromId = (int)$form_model->getItem('from');
$toId = (int)$form_model->getItem('to');
if ($fromId !== 1001){
$fromCard = Card::where("id", $form_model->getItem('from'))->first();
if (!$fromCard){
$this->errors[] = ["error_id" => 3, "error_msg" => "Sender not found"];
return false;
}
}
if ($toId !== 1001){
$toCard = Card::where("id", $form_model->getItem('to'))->first();
if (!$toCard){
$this->errors[] = ["error_id" => 3, "error_msg" => "Recipient not found"];
return false;
}
}
if ($fromId !== 1001){
if ($fromCard->balance < $form_model->getItem('amount')){
$this->errors[] = ["error_id" => 3, "error_msg" => "Insufficient funds of the sender"];
}
}
// Пример заполнения:
$model->from = $form_model->getItem('from');
$model->to = $form_model->getItem('to');
$model->status = $form_model->getItem('status');
$model->type = $form_model->getItem('type');
$model->amount = $form_model->getItem('amount');
// $model->slug = Slug::createSlug($form_model->getItem('title'), Card::class); // Генерация уникального slug
if ($model->save()) {
if ($fromCard){
$fromCard->balance = $fromCard->balance - (int)$form_model->getItem('amount');
$fromCard->save();
}
if ($toCard){
$toCard->balance = $toCard->balance + (int)$form_model->getItem('amount');
$toCard->save();
}
return $model;
}
return false;
}
public function update(FormModel $form_model, CardTransaction $cardTransaction): false|CardTransaction
{
$fromCard = Card::where("id", $form_model->getItem('from'))->first();
if (!$fromCard){
$this->errors[] = ["error_id" => 3, "error_msg" => "Sender not found"];
return false;
}
$toCard = Card::where("id", $form_model->getItem('to'))->first();
if (!$toCard){
$this->errors[] = ["error_id" => 3, "error_msg" => "Recipient not found"];
return false;
}
if ($fromCard->balance < $form_model->getItem('amount')){
$this->errors[] = ["error_id" => 3, "error_msg" => "Insufficient funds of the sender"];
}
// Пример обновления:
$cardTransaction->from = $form_model->getItem('from');
$cardTransaction->to = $form_model->getItem('to');
$cardTransaction->amount = $form_model->getItem('amount');
$cardTransaction->type = $form_model->getItem('type');
$cardTransaction->status = $form_model->getItem('status');
if ($cardTransaction->save()) {
return $cardTransaction;
}
return false;
}
public function getErrorsString(): string
{
$errStr = "";
foreach ($this->errors as $error){
$errStr .= $error['error_msg'] . ". ";
}
return $errStr;
}
}

View File

@ -0,0 +1,80 @@
<?php
/**
* @var \kernel\app_modules\card\models\CardTransaction $model
*/
use kernel\app_modules\card\models\Card;
$form = new \itguild\forms\ActiveForm();
$form->beginForm(isset($model) ? "/admin/card_transaction/edit/" . $model->id : "/admin/card_transaction", 'multipart/form-data');
// Пример формы:
$form->field(\itguild\forms\inputs\TextInput::class, 'from', [
'class' => "form-control",
'placeholder' => 'От кого',
'value' => $model->from ?? ''
])
->setLabel("От кого")
->render();
$form->field(\itguild\forms\inputs\TextInput::class, 'to', [
'class' => "form-control",
'placeholder' => 'Кому',
'value' => $model->to ?? ''
])
->setLabel("Кому")
->render();
$form->field(\itguild\forms\inputs\TextInput::class, 'amount', [
'class' => "form-control",
'placeholder' => 'Количество',
'value' => $model->amount ?? ''
])
->setLabel("Количество")
->render();
$form->field(class: \itguild\forms\inputs\Select::class, name: "type", params: [
'class' => "form-control",
'value' => $model->type ?? ''
])
->setLabel("Тип операции")
->setOptions([1 => "Зачисление", 2 => "Списание"])
->render();
$form->field(class: \itguild\forms\inputs\Select::class, name: "status", params: [
'class' => "form-control",
'value' => $model->status ?? ''
])
->setLabel("Статус")
->setOptions(\kernel\app_modules\card\models\CardTransaction::getStatus())
->render();
?>
<div class="row">
<div class="col-sm-2">
<?php
$form->field(\itguild\forms\inputs\Button::class, name: "btn-submit", params: [
'class' => "btn btn-primary ",
'value' => 'Отправить',
'typeInput' => 'submit'
])
->render();
?>
</div>
<div class="col-sm-2">
<?php
$form->field(\itguild\forms\inputs\Button::class, name: "btn-reset", params: [
'class' => "btn btn-warning",
'value' => 'Сбросить',
'typeInput' => 'reset'
])
->render();
?>
</div>
</div>
<?php
$form->endForm();

View File

@ -0,0 +1,77 @@
<?php
/**
* @var \Illuminate\Database\Eloquent\Collection $card
* @var int $page_number
* @var \kernel\CgView $view
*/
use kernel\app_modules\card\models\Card;
use Itguild\EloquentTable\EloquentDataProvider;
use Itguild\EloquentTable\ListEloquentTable;
use kernel\widgets\IconBtn\IconBtnCreateWidget;
use kernel\widgets\IconBtn\IconBtnDeleteWidget;
use kernel\widgets\IconBtn\IconBtnEditWidget;
use kernel\widgets\IconBtn\IconBtnViewWidget;
$view->setTitle("Список транзакций");
$view->setMeta([
'description' => 'Список транзакций системы'
]);
//Для использования таблицы с моделью, необходимо создать таблицу в базе данных
$table = new ListEloquentTable(new EloquentDataProvider(\kernel\app_modules\card\models\CardTransaction::class, [
'currentPage' => $page_number,
'perPage' => 8,
'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/card_transaction"
]));
//$table = new \Itguild\Tables\ListJsonTable(json_encode(
// [
// 'meta' => [
// 'total' => 0,
// 'totalWithFilters' => 0,
// 'columns' => [
// 'title',
// 'slug',
// 'status',
// ],
// 'perPage' => 5,
// 'currentPage' => 1,
// 'baseUrl' => '/admin/some',
// 'params' => [
// 'class' => 'table table-bordered',
// 'border' => 2
// ]
// ],
// 'filters' => [],
// 'data' => [],
// ]
//));
$table->beforePrint(function () {
return IconBtnCreateWidget::create(['url' => '/admin/card_transaction/create'])->run();
});
$table->columns([
'status' => [
'value' => function ($data) {
return \kernel\app_modules\card\models\CardTransaction::getStatus()[$data];
}
],
]);
$table->addAction(function($row) {
return IconBtnViewWidget::create(['url' => '/admin/card_transaction/' . $row['id']])->run();
});
//$table->addAction(function($row) {
// return IconBtnEditWidget::create(['url' => '/admin/card_transaction/update/' . $row['id']])->run();
//});
//$table->addAction(function($row) {
// return IconBtnDeleteWidget::create(['url' => '/admin/card_transaction/delete/' . $row['id']])->run();
//});
$table->create();
$table->render();

View File

@ -0,0 +1,34 @@
<?php
/**
* @var \Illuminate\Database\Eloquent\Collection $card_transaction
*/
use Itguild\EloquentTable\ViewEloquentTable;
use Itguild\EloquentTable\ViewJsonTableEloquentModel;
use kernel\widgets\IconBtn\IconBtnDeleteWidget;
use kernel\widgets\IconBtn\IconBtnEditWidget;
use kernel\widgets\IconBtn\IconBtnListWidget;
$table = new ViewEloquentTable(new ViewJsonTableEloquentModel($card_transaction, [
'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/card_program",
]));
$table->rows([
'status' => [
'value' => function ($data) {
return \kernel\app_modules\card\models\CardProgram::getStatus()[$data];
}
]
]);
$table->beforePrint(function () use ($card_transaction) {
$btn = IconBtnListWidget::create(['url' => '/admin/card_transaction'])->run();
// $btn .= IconBtnEditWidget::create(['url' => '/admin/card_transaction/update/' . $card_transaction->id])->run();
// $btn .= IconBtnDeleteWidget::create(['url' => '/admin/card_transaction/delete/' . $card_transaction->id])->run();
return $btn;
});
$table->create();
$table->render();

View File

@ -26,5 +26,6 @@ App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router){
$router->post('/tg-bot/create-card', [\app\modules\tgbot\controllers\TgBotRestController::class, 'actionCreateCard']);
$router->get('/tg-bot/get-by-dialog/{dialog_id}/{bot_id}', [\app\modules\tgbot\controllers\TgBotRestController::class, 'actionGetByDialog']);
});
$router->get('/tg-bot/get-scan-btn/{id}', [\app\modules\tgbot\controllers\TgBotRestController::class, 'actionGetScanBtn']);
$router->get('/tg-bot/get-card-by-dialog/{dialog_id}/{bot_id}', [\app\modules\tgbot\controllers\TgBotRestController::class, 'actionGetCardByDialog']);
});

View File

@ -2,6 +2,14 @@
text-align: center;
}
#btnBox {
margin-bottom: 10px;
}
#btnBox a{
width: 100%;
}
.card {
margin-bottom: 10px;
}

View File

@ -4,12 +4,16 @@ class TgApp {
constructor(containerId, userId) {
this.container = document.getElementById(containerId);
this.userId = userId;
this.btnBox = this.createBox("btnBox");
this.container.appendChild(this.btnBox);
console.log(userId)
}
actionMain(){
console.log("main 123")
this.createCardBox();
// this.createDefaultBox();
this.getScanBtn();
this.getCard();
}
@ -46,11 +50,34 @@ class TgApp {
})
.then((response) => response.json())
.then((data) => {
console.log(data)
this.cardBox.innerHTML = templates.cardBox(data.card_file.file, data.balance)
// {title: "foo", body: "bar", userId: 1, id: 101}
})
}
getScanBtn(){
fetch(config.config.apiUrl + `api/tg-bot/get-scan-btn/${this.userId}`, {
method: 'GET', // Здесь так же могут быть GET, PUT, DELETE
headers: {
// Добавляем необходимые заголовки
'Content-type': 'text/html; charset=UTF-8',
},
})
.then((response) => response.json())
.then((data) => {
this.btnBox.innerHTML = data.html
// {title: "foo", body: "bar", userId: 1, id: 101}
})
}
createBox(id){
let box = document.createElement("div");
box.setAttribute("id", id);
return box;
}
getAction(action){
if (action === "actionMain"){
return this.actionMain;