42 lines
1.3 KiB
PHP
42 lines
1.3 KiB
PHP
|
<?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));
|
||
|
}
|
||
|
|
||
|
}
|