la_bot_back/kernel/app_modules/card/conditions/CashbackCondition.php

42 lines
1.3 KiB
PHP
Raw Normal View History

2025-01-26 14:42:47 +03:00
<?php
namespace kernel\app_modules\card\conditions;
use kernel\app_modules\card\models\Card;
use kernel\app_modules\card\models\forms\CreateCardTransactionForm;
use kernel\app_modules\card\services\CardTransactionService;
use kernel\helpers\Debug;
class CashbackCondition
{
public function handler(Card $card, int $amount): \kernel\app_modules\card\models\CardTransaction|false
{
$conditions = $card->cardProgram->cardProgramConditions;
if ($conditions){
foreach ($conditions as $condition){
if ($condition->type === "cashback"){
$transactionForm = new CreateCardTransactionForm();
$transactionForm->load([
'from' => 1001,
'to' => $card->id,
'amount' => $this->calcPercent($amount, $condition->value),
'type' => 1,
'status' => 1,
]);
$transactionService = new CardTransactionService();
return $transactionService->create($transactionForm);
}
}
}
return false;
}
private function calcPercent($price, $percent): int
{
return round($price * ($percent / 100));
}
}