65 lines
1.7 KiB
PHP
65 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Component\Models;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class ClientsBonus extends Model
|
|
{
|
|
protected $table = 'clients_bonuses';
|
|
protected $guarded = [];
|
|
|
|
/**
|
|
* Bonus info.
|
|
*/
|
|
public static function getBonus($guid)
|
|
{
|
|
$bonus = ClientsBonus::where('client_guid', $guid)->first();
|
|
if (isset($bonus)) {
|
|
$bonus = $bonus['value'];
|
|
} else {
|
|
$bonus = 0;
|
|
}
|
|
return $bonus;
|
|
}
|
|
|
|
/**
|
|
* Save bonus value.
|
|
*/
|
|
public static function bonusReg($guid, $value, $type)
|
|
{
|
|
$bonus_value = ClientsBonus::where('client_guid', $guid)->first();
|
|
if ($type == 'in') {
|
|
ClientsBonus::updateOrCreate(
|
|
['client_guid' => $guid],
|
|
['value' => $bonus_value['value'] + $value]
|
|
);
|
|
} elseif ($type == 'out') {
|
|
ClientsBonus::updateOrCreate(
|
|
['client_guid' => $guid],
|
|
['value' => $bonus_value['value'] - $value]
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Bonus log.
|
|
*/
|
|
public static function bonusLog($guid, $value, $time, $staff_id)
|
|
{
|
|
if ($value > 0) {
|
|
$action_name = 'Зачиление бонусов';
|
|
} else {
|
|
$action_name = 'Списание бонусов';
|
|
}
|
|
$action = new ClientsActions();
|
|
$action->action = $action_name;
|
|
$action->created = Carbon::createFromTimestampUTC($time)->timezone('Europe/Minsk');
|
|
$action->user_id = $guid;
|
|
$action->action_value = abs($value);
|
|
$action->action_type = 1;
|
|
$action->who = $staff_id;
|
|
$action->save();
|
|
}
|
|
} |