148 lines
3.7 KiB
PHP
148 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
/**
|
|
* Class IncentivePointsLog
|
|
*
|
|
* @property int $id
|
|
* @property int $participant_id
|
|
* @property string $type
|
|
* @property string $source_type
|
|
* @property int $source_id
|
|
* @property string $source_label
|
|
* @property int $month
|
|
* @property int $year
|
|
* @property int $points_onetime
|
|
* @property int $points_accumulated
|
|
* @property bool $is_storno
|
|
* @property int|null $storno_of_id
|
|
* @property int|null $user_sales_volume_id
|
|
* @property int|null $incentive_new_partner_id
|
|
* @property int|null $incentive_new_abo_id
|
|
* @property Carbon|null $created_at
|
|
* @property Carbon|null $updated_at
|
|
* @property-read IncentiveParticipant $participant
|
|
* @property-read UserSalesVolume|null $salesVolume
|
|
* @property-read IncentiveNewPartner|null $incentiveNewPartner
|
|
* @property-read IncentiveNewAbo|null $incentiveNewAbo
|
|
* @property-read IncentivePointsLog|null $stornoOf
|
|
*
|
|
* @method static \Illuminate\Database\Eloquent\Builder|IncentivePointsLog newModelQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|IncentivePointsLog newQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|IncentivePointsLog query()
|
|
*
|
|
* @mixin \Eloquent
|
|
*/
|
|
class IncentivePointsLog extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $table = 'incentive_points_log';
|
|
|
|
protected $casts = [
|
|
'participant_id' => 'int',
|
|
'source_id' => 'int',
|
|
'month' => 'int',
|
|
'year' => 'int',
|
|
'points_onetime' => 'int',
|
|
'points_accumulated' => 'int',
|
|
'is_storno' => 'bool',
|
|
'storno_of_id' => 'int',
|
|
'user_sales_volume_id' => 'int',
|
|
'incentive_new_partner_id' => 'int',
|
|
'incentive_new_abo_id' => 'int',
|
|
];
|
|
|
|
protected $fillable = [
|
|
'participant_id',
|
|
'type',
|
|
'source_type',
|
|
'source_id',
|
|
'source_label',
|
|
'month',
|
|
'year',
|
|
'points_onetime',
|
|
'points_accumulated',
|
|
'is_storno',
|
|
'storno_of_id',
|
|
'user_sales_volume_id',
|
|
'incentive_new_partner_id',
|
|
'incentive_new_abo_id',
|
|
];
|
|
|
|
public static $types = [
|
|
'partner' => 'partner',
|
|
'abo' => 'abo',
|
|
];
|
|
|
|
// Relationships
|
|
|
|
public function participant()
|
|
{
|
|
return $this->belongsTo(IncentiveParticipant::class, 'participant_id');
|
|
}
|
|
|
|
public function salesVolume()
|
|
{
|
|
return $this->belongsTo(UserSalesVolume::class, 'user_sales_volume_id');
|
|
}
|
|
|
|
public function incentiveNewPartner()
|
|
{
|
|
return $this->belongsTo(IncentiveNewPartner::class, 'incentive_new_partner_id');
|
|
}
|
|
|
|
public function incentiveNewAbo()
|
|
{
|
|
return $this->belongsTo(IncentiveNewAbo::class, 'incentive_new_abo_id');
|
|
}
|
|
|
|
public function stornoOf()
|
|
{
|
|
return $this->belongsTo(self::class, 'storno_of_id');
|
|
}
|
|
|
|
public function stornoEntries()
|
|
{
|
|
return $this->hasMany(self::class, 'storno_of_id');
|
|
}
|
|
|
|
// Scopes
|
|
|
|
public function scopePartner($query)
|
|
{
|
|
return $query->where('type', 'partner');
|
|
}
|
|
|
|
public function scopeAbo($query)
|
|
{
|
|
return $query->where('type', 'abo');
|
|
}
|
|
|
|
public function scopeActive($query)
|
|
{
|
|
return $query->where('is_storno', false);
|
|
}
|
|
|
|
public function scopeForMonth($query, int $month, int $year)
|
|
{
|
|
return $query->where('month', $month)->where('year', $year);
|
|
}
|
|
|
|
// Helpers
|
|
|
|
public function getTotalPoints(): int
|
|
{
|
|
return $this->points_onetime + $this->points_accumulated;
|
|
}
|
|
|
|
public function getFormattedMonthYear(): string
|
|
{
|
|
return str_pad($this->month, 2, '0', STR_PAD_LEFT).'/'.$this->year;
|
|
}
|
|
}
|