Promotion Backend v1
This commit is contained in:
parent
0ed47d3553
commit
f0da981737
43 changed files with 2765 additions and 45 deletions
|
|
@ -117,6 +117,13 @@ use Illuminate\Database\Eloquent\SoftDeletes;
|
|||
* @method static \Illuminate\Database\Eloquent\Builder|Product wherePartnerCommission($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|Product whereSingleCommission($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|Product whereValueCommission($value)
|
||||
* @property bool|null $shipping_addon
|
||||
* @property bool|null $max_buy
|
||||
* @property int|null $max_buy_num
|
||||
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ProductBuy[] $product_buys
|
||||
* @property-read int|null $product_buys_count
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|Product whereMaxBuy($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|Product whereMaxBuyNum($value)
|
||||
*/
|
||||
class Product extends Model
|
||||
{
|
||||
|
|
@ -340,10 +347,13 @@ class Product extends Model
|
|||
|
||||
/*price by user Factor*/
|
||||
private function calcPriceUserFactor($price){
|
||||
/*
|
||||
Nicht in benutzung, die margin errechnet sich im Warenkorb, wegen der Staffelprovision
|
||||
if(\Auth::user() && \Auth::user()->user_level){
|
||||
$margin = ((\Auth::user()->user_level->margin -100)*-1) / 100;
|
||||
$price = $price * $margin;
|
||||
}
|
||||
*/
|
||||
return $price;
|
||||
}
|
||||
/*price net*/
|
||||
|
|
|
|||
181
app/Models/PromotionAdmin.php
Normal file
181
app/Models/PromotionAdmin.php
Normal file
|
|
@ -0,0 +1,181 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Created by Reliese Model.
|
||||
*/
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use App\Services\Util;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
/**
|
||||
* Class PromotionAdmin
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property string|null $description
|
||||
* @property Carbon|null $from
|
||||
* @property Carbon|null $to
|
||||
* @property float|null $max_bugdet
|
||||
* @property int|null $max_items
|
||||
* @property bool $active
|
||||
* @property Carbon|null $created_at
|
||||
* @property Carbon|null $updated_at
|
||||
* @property Collection|Product[] $products
|
||||
* @property Collection|PromotionUserProduct[] $promotion_user_products
|
||||
* @property Collection|PromotionUser[] $promotion_users
|
||||
* @package App\Models
|
||||
* @property-read int|null $products_count
|
||||
* @property-read int|null $promotion_user_products_count
|
||||
* @property-read int|null $promotion_users_count
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin newModelQuery()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin newQuery()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin query()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin whereActive($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin whereCreatedAt($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin whereDescription($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin whereFrom($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin whereId($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin whereMaxBugdet($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin whereMaxItems($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin whereName($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin whereTo($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin whereUpdatedAt($value)
|
||||
* @mixin \Eloquent
|
||||
*/
|
||||
class PromotionAdmin extends Model
|
||||
{
|
||||
protected $table = 'promotion_admins';
|
||||
|
||||
protected $casts = [
|
||||
'max_bugdet' => 'float',
|
||||
'max_items' => 'int',
|
||||
'active' => 'bool',
|
||||
'type' => 'int',
|
||||
'shop' => 'bool',
|
||||
|
||||
|
||||
];
|
||||
|
||||
protected $dates = [
|
||||
'from',
|
||||
'to'
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'type',
|
||||
'name',
|
||||
'description',
|
||||
'from',
|
||||
'to',
|
||||
'shop',
|
||||
'max_bugdet',
|
||||
'max_items',
|
||||
'active'
|
||||
];
|
||||
|
||||
public $promotionType = [
|
||||
1 => 'verschenken',
|
||||
2 => 'Nachlass (not built in!)',
|
||||
3 => 'Gewinnspiel (not built in!)',
|
||||
];
|
||||
|
||||
public function products()
|
||||
{
|
||||
return $this->belongsToMany(Product::class, 'promotion_admin_products')
|
||||
->withPivot('id', 'own_price', 'price', 'tax', 'price_old', 'calcu_commission', 'max_items', 'used_items', 'shipping', 'active')
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
|
||||
public function promotion_admin_products()
|
||||
{
|
||||
return $this->hasMany(PromotionAdminProduct::class);
|
||||
}
|
||||
|
||||
public function promotion_admin_products_active()
|
||||
{
|
||||
return $this->hasMany(PromotionAdminProduct::class)->where('active', 1);
|
||||
}
|
||||
|
||||
public function promotion_user_products()
|
||||
{
|
||||
return $this->hasMany(PromotionUserProduct::class);
|
||||
}
|
||||
|
||||
public function promotion_users()
|
||||
{
|
||||
return $this->hasMany(PromotionUser::class);
|
||||
}
|
||||
|
||||
public function getPromotionType(){
|
||||
if(isset($this->promotionType[$this->type])){
|
||||
return $this->promotionType[$this->type];
|
||||
}
|
||||
}
|
||||
|
||||
public function getFromAttribute($value)
|
||||
{
|
||||
if(!$value){ return ""; }
|
||||
return Carbon::parse($value)->format(\Util::formatDateDB());
|
||||
}
|
||||
|
||||
public function getFromRaw(){
|
||||
return isset($this->attributes['from']) ? $this->attributes['from'] : NULL;
|
||||
}
|
||||
|
||||
public function setFromAttribute( $value ) {
|
||||
$this->attributes['from'] = isset($value) ? (new Carbon($value))->format('Y-m-d') : NULL;
|
||||
}
|
||||
|
||||
public function getToAttribute($value)
|
||||
{
|
||||
if(!$value){ return ""; }
|
||||
return Carbon::parse($value)->format(\Util::formatDateDB());
|
||||
}
|
||||
|
||||
public function getToRaw(){
|
||||
return isset($this->attributes['to']) ? $this->attributes['to'] : NULL;
|
||||
}
|
||||
|
||||
public function setToAttribute( $value ) {
|
||||
$this->attributes['to'] = isset($value) ? (new Carbon($value))->format('Y-m-d') : NULL;
|
||||
}
|
||||
|
||||
|
||||
public function setMaxBugdetAttribute( $value ) {
|
||||
|
||||
$this->attributes['max_bugdet'] = $value ? Util::reFormatNumber($value) : null;
|
||||
}
|
||||
|
||||
public function getFormattedMaxBugdet()
|
||||
{
|
||||
return isset($this->attributes['max_bugdet']) ? Util::formatNumber($this->attributes['max_bugdet']) : "";
|
||||
}
|
||||
|
||||
|
||||
public function canDelete(){
|
||||
|
||||
if($this->promotion_users->count() === 0 && $this->promotion_user_products->count() === 0){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static function getActiveAdminPromotionsAsArray(){
|
||||
$query = PromotionAdmin::where('active', true)
|
||||
->where(function ($query) {
|
||||
$query->where('from', '<', Carbon::now())
|
||||
->orWhereNull('from');
|
||||
})
|
||||
->where(function ($query) {
|
||||
$query->where('to', '>=', Carbon::now())
|
||||
->orWhereNull('to');
|
||||
});
|
||||
return $query->pluck('name', 'id')->toArray();
|
||||
}
|
||||
}
|
||||
194
app/Models/PromotionAdminProduct.php
Normal file
194
app/Models/PromotionAdminProduct.php
Normal file
|
|
@ -0,0 +1,194 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Created by Reliese Model.
|
||||
*/
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use App\Services\Util;
|
||||
use Auth;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
/**
|
||||
* Class PromotionAdminProduct
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $promotion_admin_id
|
||||
* @property int $product_id
|
||||
* @property bool $own_price
|
||||
* @property float|null $price
|
||||
* @property float|null $tax
|
||||
* @property float|null $price_old
|
||||
* @property bool $calcu_commission
|
||||
* @property int|null $max_items
|
||||
* @property int|null $used_items
|
||||
* @property bool $shipping
|
||||
* @property bool $active
|
||||
* @property Carbon|null $created_at
|
||||
* @property Carbon|null $updated_at
|
||||
* @property Product $product
|
||||
* @property PromotionAdmin $promotion_admin
|
||||
* @property Collection|PromotionUserProduct[] $promotion_user_products
|
||||
* @package App\Models
|
||||
* @property-read int|null $promotion_user_products_count
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct newModelQuery()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct newQuery()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct query()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct whereActive($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct whereCalcuCommission($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct whereCreatedAt($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct whereId($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct whereMaxItems($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct whereOwnPrice($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct wherePrice($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct wherePriceOld($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct whereProductId($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct wherePromotionAdminId($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct whereShipping($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct whereTax($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct whereUpdatedAt($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct whereUsedItems($value)
|
||||
* @mixin \Eloquent
|
||||
*/
|
||||
class PromotionAdminProduct extends Model
|
||||
{
|
||||
protected $table = 'promotion_admin_products';
|
||||
|
||||
protected $casts = [
|
||||
'promotion_admin_id' => 'int',
|
||||
'product_id' => 'int',
|
||||
'own_price' => 'bool',
|
||||
'price' => 'float',
|
||||
'tax' => 'float',
|
||||
'price_old' => 'float',
|
||||
'calcu_commission' => 'bool',
|
||||
'max_items' => 'int',
|
||||
'used_items' => 'int',
|
||||
'shipping' => 'bool',
|
||||
'active' => 'bool'
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'promotion_admin_id',
|
||||
'product_id',
|
||||
'own_price',
|
||||
'price',
|
||||
'tax',
|
||||
'price_old',
|
||||
'calcu_commission',
|
||||
'max_items',
|
||||
'used_items',
|
||||
'shipping',
|
||||
'active'
|
||||
];
|
||||
|
||||
public function product()
|
||||
{
|
||||
return $this->belongsTo(Product::class);
|
||||
}
|
||||
|
||||
public function promotion_admin()
|
||||
{
|
||||
return $this->belongsTo(PromotionAdmin::class);
|
||||
}
|
||||
|
||||
public function promotion_user_products()
|
||||
{
|
||||
return $this->hasMany(PromotionUserProduct::class);
|
||||
}
|
||||
|
||||
public function setPriceAttribute( $value ) {
|
||||
|
||||
$this->attributes['price'] = $value ? Util::reFormatNumber($value) : null;
|
||||
}
|
||||
|
||||
public function getFormattedPrice()
|
||||
{
|
||||
return isset($this->attributes['price']) ? Util::formatNumber($this->attributes['price']) : "";
|
||||
}
|
||||
|
||||
public function getRealPrice()
|
||||
{
|
||||
if($this->own_price && $this->price){
|
||||
return $this->price;
|
||||
}
|
||||
if($this->product && $this->product->price){
|
||||
return $this->product->price;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public function getFormattedRealPrice()
|
||||
{
|
||||
return Util::formatNumber($this->getRealPrice());
|
||||
}
|
||||
|
||||
/*price calcu Marign single_commission Factor*/
|
||||
private function calcPriceCommissionFactor($price){
|
||||
//einzelrabatt aus produkt
|
||||
$margin = 100;
|
||||
if(isset($this->product) && $this->product->single_commission){
|
||||
$margin = $this->product->value_commission;
|
||||
}else{
|
||||
//rabatt aus user level
|
||||
$user = Auth::user();
|
||||
if($user && $user->user_level && $user->user_level->user_level_margins){
|
||||
if($user_level_margin = $user->user_level->user_level_margins_re->first()){
|
||||
$margin = $user_level_margin->trading_margin;
|
||||
}
|
||||
}
|
||||
}
|
||||
$margin = (100 - $margin) / 100;
|
||||
$price = $price * $margin;
|
||||
return $price;
|
||||
}
|
||||
/*price net*/
|
||||
private function calcPriceNet($price){
|
||||
$tax = isset($this->product->tax) ? $this->product->tax : 0;
|
||||
$tax_rate = ($tax + 100) / 100;
|
||||
return $price / $tax_rate;
|
||||
}
|
||||
//price calu with
|
||||
public function getPriceWith(Bool $net = true){
|
||||
$price = $this->getRealPrice();
|
||||
$price = $net ? $this->calcPriceNet($price) : $price;
|
||||
$price = $this->calcu_commission ? $this->calcPriceCommissionFactor($price) : $price;
|
||||
return round($price, 2);
|
||||
}
|
||||
/*out*/
|
||||
public function getFormattedPriceWith(Bool $net = true)
|
||||
{
|
||||
return Util::formatNumber($this->getPriceWith($net));
|
||||
}
|
||||
|
||||
public function canDelete(){
|
||||
if($this->promotion_user_products->count() === 0){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
public function getPromotionUserProducts($user_promotion, $key = false)
|
||||
{
|
||||
$user_promotion_product = $user_promotion->hasPromotionUserProducts($this->id);
|
||||
if($user_promotion_product){
|
||||
if(!$key){
|
||||
return $user_promotion_product;
|
||||
}
|
||||
if(isset($user_promotion_product->{$key})){
|
||||
return $user_promotion_product->{$key};
|
||||
}else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
//return $this->hasMany(PromotionUserProduct::class)->where('promotion_admin_product_id', $promotion_admin_product->id)->first();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
222
app/Models/PromotionUser.php
Normal file
222
app/Models/PromotionUser.php
Normal file
|
|
@ -0,0 +1,222 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Created by Reliese Model.
|
||||
*/
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Class PromotionUser
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $promotion_admin_id
|
||||
* @property string $name
|
||||
* @property string|null $description
|
||||
* @property string|null $url
|
||||
* @property bool $pick_up
|
||||
* @property float|null $used_budget_total
|
||||
* @property int|null $sell_items_total
|
||||
* @property bool $active
|
||||
* @property Carbon|null $user_deleted_at
|
||||
* @property Carbon|null $created_at
|
||||
* @property Carbon|null $updated_at
|
||||
* @property PromotionAdmin $promotion_admin
|
||||
* @property Collection|Product[] $products
|
||||
* @package App\Models
|
||||
* @property-read int|null $products_count
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUser newModelQuery()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUser newQuery()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUser query()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUser whereActive($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUser whereCreatedAt($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUser whereDescription($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUser whereId($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUser whereName($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUser wherePickUp($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUser wherePromotionAdminId($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUser whereSellItemsTotal($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUser whereUpdatedAt($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUser whereUrl($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUser whereUsedBudgetTotal($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUser whereUserDeletedAt($value)
|
||||
* @mixin \Eloquent
|
||||
*/
|
||||
class PromotionUser extends Model
|
||||
{
|
||||
protected $table = 'promotion_users';
|
||||
|
||||
protected $casts = [
|
||||
'promotion_admin_id' => 'int',
|
||||
'user_id' => 'int',
|
||||
'pick_up' => 'bool',
|
||||
'used_budget_total' => 'float',
|
||||
'sell_items_total' => 'int',
|
||||
'active' => 'bool'
|
||||
];
|
||||
|
||||
protected $dates = [
|
||||
'user_deleted_at'
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'promotion_admin_id',
|
||||
'user_id',
|
||||
'name',
|
||||
'description',
|
||||
'url',
|
||||
'pick_up',
|
||||
'used_budget_total',
|
||||
'sell_items_total',
|
||||
'active',
|
||||
'user_deleted_at'
|
||||
];
|
||||
|
||||
public function promotion_admin()
|
||||
{
|
||||
return $this->belongsTo(PromotionAdmin::class);
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(\App\User::class);
|
||||
}
|
||||
|
||||
public function products()
|
||||
{
|
||||
return $this->belongsToMany(Product::class, 'promotion_user_products')
|
||||
->withPivot('id', 'promotion_admin_id', 'promotion_admin_product_id', 'open_items', 'sell_items', 'used_budget_total', 'active')
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
public function promotion_user_products()
|
||||
{
|
||||
return $this->hasMany(PromotionUserProduct::class);
|
||||
}
|
||||
|
||||
public function promotion_user_products_active()
|
||||
{
|
||||
return $this->hasMany(PromotionUserProduct::class)->where('active', 1);
|
||||
}
|
||||
|
||||
|
||||
public function getUrlPreview()
|
||||
{
|
||||
return $this->url ? config('app.promo_url').$this->url : "";
|
||||
}
|
||||
|
||||
public function canDelete(){
|
||||
return true;
|
||||
}
|
||||
|
||||
public function hasPromotionUserProducts($promotion_admin_product_id)
|
||||
{
|
||||
return $this->hasMany(PromotionUserProduct::class)->where('promotion_admin_product_id', $promotion_admin_product_id)->first();
|
||||
}
|
||||
public function calculateSell(){
|
||||
$price = 0;
|
||||
$price_net = 0;
|
||||
//used_budget_total
|
||||
//von den user produkte, inkl. der nicht aktiven!
|
||||
//ToDo der Preis vom verkauf muss eingesetzt werden, wird gespeichert beim Sale
|
||||
if($this->promotion_user_products){
|
||||
foreach($this->promotion_user_products as $promotion_user_product){
|
||||
$qty = $promotion_user_product->sell_items;
|
||||
$price += $promotion_user_product->promotion_admin_product->getPriceWith(false) * intval($qty);
|
||||
$price_net += $promotion_user_product->promotion_admin_product->getPriceWith(true) * intval($qty);
|
||||
|
||||
}
|
||||
}
|
||||
return ['price' => $price, 'price_net' => $price_net];
|
||||
}
|
||||
|
||||
public function calculateCart(){
|
||||
$price = 0;
|
||||
$price_net = 0;
|
||||
if(isset($this->promotion_admin->promotion_admin_products_active)){
|
||||
foreach($this->promotion_admin->promotion_admin_products_active as $promotion_admin_product){
|
||||
if($promotion_user_product = $this->hasPromotionUserProducts($promotion_admin_product->id)){
|
||||
if($promotion_user_product->active){
|
||||
$qty = $promotion_user_product->open_items;
|
||||
$price += $promotion_admin_product->getPriceWith(false) * intval($qty);
|
||||
$price_net += $promotion_admin_product->getPriceWith(true) * intval($qty);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ['price' => $price, 'price_net' => $price_net];
|
||||
}
|
||||
|
||||
public function getCountOpenItems(){
|
||||
//hier die aktiven
|
||||
return $this->promotion_user_products_active->sum('open_items');
|
||||
}
|
||||
|
||||
public function getCountSellItems(){
|
||||
//hier alle zusammenziehen
|
||||
return $this->promotion_user_products->sum('sell_items');
|
||||
}
|
||||
|
||||
public function checkPaymentCredit()
|
||||
{
|
||||
if($this->promotion_user_products_active->count() > 0 && $this->getCountOpenItems() > 0){
|
||||
$payment_credit = \Auth::user()->payment_credit;
|
||||
if($payment_credit <= 0){
|
||||
return "empty";
|
||||
}
|
||||
|
||||
$prices = $this->calculateCart();
|
||||
if(isset($prices['price']) && $prices['price'] > 0){
|
||||
if($payment_credit >= $prices['price']){
|
||||
return "okay";
|
||||
}
|
||||
}
|
||||
return 'not';
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public static function preCheckPaymentCredit($values){
|
||||
if($values['count_items'] > 0 && $values['sum_items'] > 0){
|
||||
$payment_credit = \Auth::user()->payment_credit;
|
||||
if($payment_credit <= 0){
|
||||
return "empty";
|
||||
}
|
||||
if(isset($values['price']) && $values['price'] > 0){
|
||||
if($payment_credit >= $values['price']){
|
||||
return "okay";
|
||||
}
|
||||
}
|
||||
return 'not';
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static function preCalculateCart($value, $by = ''){
|
||||
$price = 0;
|
||||
$price_net = 0;
|
||||
$count_items = 0;
|
||||
$sum_items = 0;
|
||||
if($by === 'products'){
|
||||
foreach($value as $product){
|
||||
if(isset($product['product_id']) && isset($product['qty'])){
|
||||
if($promotion_admin_product = PromotionAdminProduct::find($product['product_id'])){
|
||||
$count_items ++;
|
||||
$sum_items += intval($product['qty']);
|
||||
$price += $promotion_admin_product->getPriceWith(false) * intval($product['qty']);
|
||||
$price_net += $promotion_admin_product->getPriceWith(true) * intval($product['qty']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if($by === 'user_promotion'){
|
||||
return $value->calculateCart();
|
||||
}
|
||||
return ['price' => $price, 'price_net' => $price_net, 'count_items' => $count_items, 'sum_items' => $sum_items];
|
||||
|
||||
}
|
||||
}
|
||||
92
app/Models/PromotionUserProduct.php
Normal file
92
app/Models/PromotionUserProduct.php
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Created by Reliese Model.
|
||||
*/
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Class PromotionUserProduct
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $promotion_admin_id
|
||||
* @property int $promotion_user_id
|
||||
* @property int $promotion_admin_product_id
|
||||
* @property int $product_id
|
||||
* @property int|null $open_items
|
||||
* @property int|null $sell_items
|
||||
* @property float|null $used_budget_total
|
||||
* @property bool $active
|
||||
* @property Carbon|null $created_at
|
||||
* @property Carbon|null $updated_at
|
||||
* @property Product $product
|
||||
* @property PromotionAdmin $promotion_admin
|
||||
* @property PromotionAdminProduct $promotion_admin_product
|
||||
* @property PromotionUser $promotion_user
|
||||
* @package App\Models
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUserProduct newModelQuery()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUserProduct newQuery()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUserProduct query()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUserProduct whereActive($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUserProduct whereCreatedAt($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUserProduct whereId($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUserProduct whereOpenItems($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUserProduct whereProductId($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUserProduct wherePromotionAdminId($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUserProduct wherePromotionAdminProductId($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUserProduct wherePromotionUserId($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUserProduct whereSellItems($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUserProduct whereUpdatedAt($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|PromotionUserProduct whereUsedBudgetTotal($value)
|
||||
* @mixin \Eloquent
|
||||
*/
|
||||
class PromotionUserProduct extends Model
|
||||
{
|
||||
protected $table = 'promotion_user_products';
|
||||
|
||||
protected $casts = [
|
||||
'promotion_admin_id' => 'int',
|
||||
'promotion_user_id' => 'int',
|
||||
'promotion_admin_product_id' => 'int',
|
||||
'product_id' => 'int',
|
||||
'open_items' => 'int',
|
||||
'sell_items' => 'int',
|
||||
'used_budget_total' => 'float',
|
||||
'active' => 'bool'
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'promotion_admin_id',
|
||||
'promotion_user_id',
|
||||
'promotion_admin_product_id',
|
||||
'product_id',
|
||||
'open_items',
|
||||
'sell_items',
|
||||
'used_budget_total',
|
||||
'active'
|
||||
];
|
||||
|
||||
public function product()
|
||||
{
|
||||
return $this->belongsTo(Product::class);
|
||||
}
|
||||
|
||||
public function promotion_admin()
|
||||
{
|
||||
return $this->belongsTo(PromotionAdmin::class);
|
||||
}
|
||||
|
||||
public function promotion_admin_product()
|
||||
{
|
||||
return $this->belongsTo(PromotionAdminProduct::class);
|
||||
}
|
||||
|
||||
public function promotion_user()
|
||||
{
|
||||
return $this->belongsTo(PromotionUser::class);
|
||||
}
|
||||
}
|
||||
|
|
@ -47,6 +47,7 @@ class UserPayCredit extends Model
|
|||
1 => 'add from payment',
|
||||
2 => 'deduction from payment',
|
||||
3 => 'manually added credit',
|
||||
4 => 'return from order',
|
||||
|
||||
];
|
||||
protected $table = 'user_pay_credits';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue