176 lines
6 KiB
PHP
176 lines
6 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Created by Reliese Model.
|
|
*/
|
|
|
|
namespace App\Models;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use App\Models\ShoppingOrder;
|
|
use App\User;
|
|
|
|
/**
|
|
* Class UserSalesVolume
|
|
*
|
|
* @property int $id
|
|
* @property int $user_id
|
|
* @property int|null $shopping_order_id
|
|
* @property int|null $month
|
|
* @property int|null $year
|
|
* @property Carbon|null $date
|
|
* @property int|null $points
|
|
* @property int|null $month_points
|
|
* @property float|null $total_net
|
|
* @property float|null $month_total_net
|
|
* @property string|null $message
|
|
* @property int $status
|
|
* @property Carbon|null $created_at
|
|
* @property Carbon|null $updated_at
|
|
* @property ShoppingOrder|null $shopping_order
|
|
* @property User $user
|
|
* @package App\Models
|
|
* @property int|null $user_invoice_id
|
|
* @property int|null $month_shop_points
|
|
* @property float|null $month_shop_total_net
|
|
* @property-read \App\Models\UserInvoice|null $user_invoice
|
|
* @method static \Illuminate\Database\Eloquent\Builder|UserSalesVolume newModelQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|UserSalesVolume newQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|UserSalesVolume query()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|UserSalesVolume whereCreatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|UserSalesVolume whereDate($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|UserSalesVolume whereId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|UserSalesVolume whereMessage($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|UserSalesVolume whereMonth($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|UserSalesVolume whereMonthPoints($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|UserSalesVolume whereMonthShopPoints($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|UserSalesVolume whereMonthShopTotalNet($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|UserSalesVolume whereMonthTotalNet($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|UserSalesVolume wherePoints($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|UserSalesVolume whereShoppingOrderId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|UserSalesVolume whereStatus($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|UserSalesVolume whereTotalNet($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|UserSalesVolume whereUpdatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|UserSalesVolume whereUserId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|UserSalesVolume whereUserInvoiceId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|UserSalesVolume whereYear($value)
|
|
* @mixin \Eloquent
|
|
*/
|
|
class UserSalesVolume extends Model
|
|
{
|
|
protected $table = 'user_sales_volumes';
|
|
|
|
protected $casts = [
|
|
'user_id' => 'int',
|
|
'shopping_order_id' => 'int',
|
|
'user_invoice_id' => 'int',
|
|
'month' => 'int',
|
|
'year' => 'int',
|
|
'points' => 'int',
|
|
'month_points' => 'int',
|
|
'month_shop_points' => 'int',
|
|
'total_net' => 'float',
|
|
'month_total_net' => 'float',
|
|
'month_shop_total_net' => 'float',
|
|
'status' => 'int'
|
|
];
|
|
|
|
protected $dates = [
|
|
'date'
|
|
];
|
|
|
|
protected $fillable = [
|
|
'user_id',
|
|
'shopping_order_id',
|
|
'user_invoice_id',
|
|
'month',
|
|
'year',
|
|
'date',
|
|
'points',
|
|
'month_points',
|
|
'month_shop_points',
|
|
'total_net',
|
|
'month_total_net',
|
|
'month_shop_total_net',
|
|
'message',
|
|
'status'
|
|
];
|
|
|
|
|
|
public static $statusTypes = [
|
|
0 => 'nicht zugewiesen',
|
|
1 => 'hinzugefügt aus Bestellung',
|
|
2 => 'hinzugefügt aus Shop',
|
|
3 => 'hinzugefügt aus Shop / pending',
|
|
10 => ''
|
|
];
|
|
|
|
public function shopping_order()
|
|
{
|
|
return $this->belongsTo(ShoppingOrder::class);
|
|
}
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function user_invoice()
|
|
{
|
|
return $this->belongsTo(UserInvoice::class);
|
|
}
|
|
|
|
public function getDateAttribute(){
|
|
return $this->attributes['date'] ? Carbon::parse($this->attributes['date'])->format(\Util::formatDateDB()) : '';
|
|
}
|
|
public function setDateAttribute( $value ) {
|
|
$this->attributes['date'] = isset($value) ? (new Carbon($value))->format('Y-m-d') : NULL;
|
|
}
|
|
public function getDateRaw(){
|
|
return isset($this->attributes['date']) ? $this->attributes['date'] : NULL;
|
|
}
|
|
|
|
public function getStatusType(){
|
|
return isset(self::$statusTypes[$this->status]) ? self::$statusTypes[$this->status] : "";
|
|
}
|
|
|
|
public static function getStatusByOrder($ShoppingOrder){
|
|
if($ShoppingOrder->payment_for){
|
|
if($ShoppingOrder->payment_for === 6){ //Kunde-Shop
|
|
if($ShoppingOrder->shopping_user && $ShoppingOrder->shopping_user->is_like){
|
|
return 3; //shop Kunden, berater zuordnen
|
|
}
|
|
return 2;
|
|
}
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public function getFormatedMonthYear(){
|
|
return str_pad($this->month, 2, "0", STR_PAD_LEFT)."/".$this->year;
|
|
}
|
|
|
|
public function setToUserAndCalculate($user_id){
|
|
|
|
$month = $this->month;
|
|
$year = $this->year;
|
|
$month_shop_points = UserSalesVolume::where('user_id', $user_id)->where('status', 2)->where('month', $month)->where('year', $year)->sum('points');
|
|
$month_shop_total_net = UserSalesVolume::where('user_id', $user_id)->where('status', 2)->where('month', $month)->where('year', $year)->sum('total_net');
|
|
$month_points = UserSalesVolume::where('user_id', $user_id)->where('status', 1)->where('month', $month)->where('year', $year)->sum('points');
|
|
$month_total_net = UserSalesVolume::where('user_id', $user_id)->where('status', 1)->where('month', $month)->where('year', $year)->sum('total_net');
|
|
|
|
$month_shop_points += $this->points;
|
|
$month_shop_total_net += $this->total_net;
|
|
|
|
$this->user_id = $user_id;
|
|
$this->month_shop_points = $month_shop_points;
|
|
$this->month_shop_total_net = $month_shop_total_net;
|
|
$this->month_points = $month_points;
|
|
$this->month_total_net = $month_total_net;
|
|
$this->status = 2;
|
|
$this->save();
|
|
|
|
}
|
|
}
|