195 lines
9.5 KiB
PHP
195 lines
9.5 KiB
PHP
<?php
|
|
|
|
namespace Yunshop\ShareholderDividend\services;
|
|
|
|
use app\common\models\Order;
|
|
use app\common\models\OrderGoods;
|
|
use Carbon\Carbon;
|
|
use Yunshop\ShareholderDividend\models\ExceptionLog;
|
|
use Yunshop\ShareholderDividend\models\GoodsShareholderDividend;
|
|
use Yunshop\StoreCashier\common\models\balance\PluginBalanceOrder;
|
|
use Yunshop\StoreCashier\common\models\CashierGoods;
|
|
use Yunshop\StoreCashier\common\models\Store;
|
|
use Yunshop\StoreCashier\common\models\StoreGoods;
|
|
use Yunshop\StoreCashier\common\models\StoreSetting;
|
|
|
|
|
|
/**
|
|
* Author:
|
|
* Date: 2017/8/29
|
|
* Time: 下午5:50
|
|
*/
|
|
class CycleAmounyService
|
|
{
|
|
// 根据周期 获取分红金额
|
|
public static function getCycleAmount($cycle, $method){
|
|
switch ($cycle) {
|
|
case 0:
|
|
$time_end = strtotime(Carbon::today()->toDateTimeString());
|
|
$time_start = strtotime(Carbon::yesterday()->toDateTimeString());
|
|
break;
|
|
case 1:
|
|
$time_end = strtotime(Carbon::today()->toDateTimeString());
|
|
$time_start = Carbon::today()->modify('-1 week')->timestamp;
|
|
|
|
break;
|
|
case 2:
|
|
$time_end = Carbon::now()->startOfMonth()->timestamp;
|
|
$time_start = Carbon::now()->modify('-1 month')->startOfMonth()->timestamp;
|
|
break;
|
|
}
|
|
ExceptionLog::create([
|
|
'uniacid' => \YunShop::app()->uniacid,
|
|
'comment' => "cycle[{$cycle}]method[{$method}]",
|
|
'time' => date('Y-m-d H:i:s', time())
|
|
]);
|
|
$orders = Order::select()->uniacid()
|
|
->whereBetween('created_at', [$time_start, $time_end])
|
|
->whereIn('status',[Order::WAIT_SEND,Order::WAIT_RECEIVE,Order::COMPLETE]) // 统计已支付-待发货,已支付-待收货,已完成订单
|
|
->get();
|
|
|
|
return self::amountHandle($orders,$method);
|
|
}
|
|
|
|
// 根据订单 获取分红金额
|
|
public static function getOrderAmount($orderId, $method){
|
|
$orders = Order::select()->uniacid()
|
|
->where('id',$orderId)
|
|
->whereIn('status',[Order::WAIT_SEND,Order::WAIT_RECEIVE,Order::COMPLETE]) // 统计已支付-待发货,已支付-待收货,已完成订单
|
|
->get();
|
|
|
|
return self::amountHandle($orders,$method);
|
|
}
|
|
|
|
// 统一处理
|
|
public static function amountHandle($orders,$method){
|
|
$order_ids = $orders->pluck('id');
|
|
|
|
if ($method) {
|
|
$price = $orders->sum(function ($order) {
|
|
if (\YunShop::plugin()->get('store-cashier')) {
|
|
$cashier = CashierGoods::all();
|
|
$store_goods = StoreGoods::with('store')->get();
|
|
if ($order->plugin_id == 31) {
|
|
return $order->hasManyOrderGoods->sum(function ($order_goods) use ($cashier, $store_goods) {
|
|
$cashier_good = $cashier->where('goods_id', $order_goods->goods_id)->first();
|
|
if ($cashier_good) {
|
|
$price = proportionMath($order_goods->payment_amount,$cashier_good->shop_commission);
|
|
if (app('plugins')->isEnabled('consumer-reward') && \Setting::get('plugin.consumer_reward.is_open')==1) {
|
|
if (\Setting::get('plugin.consumer_reward.cashier_profit')) {
|
|
$price = proportionMath($order_goods->goods_price,$cashier_good->shop_commission);
|
|
$price = \Yunshop\ConsumerReward\common\services\AmountDividendService::amountPrice($order_goods->order_id, $price);
|
|
}
|
|
}
|
|
return $price;
|
|
}
|
|
});
|
|
} elseif ($order->plugin_id == 32) {
|
|
return $order->hasManyOrderGoods->sum(function ($order_goods) use ($cashier, $store_goods) {
|
|
$store_good = $store_goods->where('goods_id', $order_goods->goods_id)->first();
|
|
if ($store_good) {
|
|
$store_setting = StoreSetting::where('store_id', $store_good->store->id)->where('key', 'store')->first();
|
|
$shop_commission = (integer)$store_setting->value['shop_commission'];
|
|
$price = proportionMath($order_goods->payment_amount , $shop_commission);
|
|
if (app('plugins')->isEnabled('consumer-reward') && \Setting::get('plugin.consumer_reward.is_open')==1) {
|
|
if (\Setting::get('plugin.consumer_reward.store_profit')) {
|
|
$amount_price = $order_goods->payment_amount - $order_goods->goods_cost_price;
|
|
$price = \Yunshop\ConsumerReward\common\services\AmountDividendService::amountPrice($order_goods->order_id, $amount_price);
|
|
}
|
|
}
|
|
return $price;
|
|
}
|
|
});
|
|
} elseif ($order->plugin_id == 39) {
|
|
// plugin_id == 39 门店余额充值订单
|
|
$balanceOrder = PluginBalanceOrder::select()->where('order_id', $order->id)->first();
|
|
$storeSetting = StoreSetting::getStoreSettingByStoreId($balanceOrder->store_id)
|
|
->where('key', 'store_balance')
|
|
->first();
|
|
$storeSettingValue = $storeSetting->value;
|
|
|
|
return $order->hasManyOrderGoods->sum(function ($order_goods) use ($cashier, $store_goods, $storeSettingValue) {
|
|
$price = proportionMath($order_goods->payment_amount, $storeSettingValue['shop_commission']);
|
|
if (app('plugins')->isEnabled('consumer-reward') && \Setting::get('plugin.consumer_reward.is_open')==1) {
|
|
if (\Setting::get('plugin.consumer_reward.store_profit')) {
|
|
$amount_price = $order_goods->payment_amount - $order_goods->goods_cost_price;
|
|
$price = \Yunshop\ConsumerReward\common\services\AmountDividendService::amountPrice($order_goods->order_id, $amount_price);
|
|
}
|
|
}
|
|
return $price;
|
|
});
|
|
} else {
|
|
return $order->hasManyOrderGoods->sum(function ($order_goods) {
|
|
$res = $order_goods->payment_amount - $order_goods->goods_cost_price;
|
|
return $res > 0 ? $res : 0;
|
|
});
|
|
}
|
|
} else {
|
|
return $order->hasManyOrderGoods->sum(function ($order_goods) {
|
|
$res = $order_goods->payment_amount - $order_goods->goods_cost_price;
|
|
return $res > 0 ? $res : 0;
|
|
});
|
|
}
|
|
});
|
|
} else {
|
|
$price = $orders->sum('price') - $orders->sum('dispatch_price');
|
|
}
|
|
|
|
$no_count_price = self::getCycleGoodsAmount( $method, $order_ids) ?: 0;
|
|
|
|
$price = $price - $no_count_price;
|
|
|
|
return $price;
|
|
}
|
|
|
|
|
|
public static function getCycleGoodsAmount($method, $order_ids)
|
|
{
|
|
$goods = GoodsShareholderDividend::uniacid()->where('is_no_count', 1);
|
|
$is_store = false;
|
|
$store_goods = [];
|
|
|
|
if (\YunShop::plugin()->get('store-cashier')) {
|
|
$store = StoreSetting::where('key', 'shareholder_dividend')->get();
|
|
$store = $store->filter(function ($item) {
|
|
return $item->value['is_no_count'] == 1;
|
|
});
|
|
$store_ids = $store->pluck('store_id');
|
|
$store_goods = StoreGoods::whereIn('store_id',$store_ids)->get();
|
|
$store_goods = $store_goods->pluck('goods_id')->toArray();
|
|
$goods->whereNotIn('store_id',$store_goods);
|
|
$is_store = true;
|
|
}
|
|
$goods = $goods->get();
|
|
$goods_ids = $goods->pluck('goods_id')->toArray();
|
|
if ($store_goods) {
|
|
$goods_ids = array_unique(array_merge($goods_ids, $store_goods));
|
|
}
|
|
|
|
|
|
$order_goods = OrderGoods::whereIn('order_id',$order_ids)->whereIn('goods_id',$goods_ids)->get();
|
|
if ($method) {
|
|
$price = $order_goods->sum(function ($order_goods) use ($goods, $is_store) {
|
|
$cashier = CashierGoods::all();
|
|
$store = StoreGoods::all();
|
|
if ($store_goods = $store->where('goods_id',$order_goods->goods_id)->first()) {
|
|
$store_setting = StoreSetting::where('store_id', $store_goods->store_id)->where('key', 'store')->first();
|
|
$shop_commission = (integer)$store_setting->value['shop_commission'];
|
|
return $order_goods->payment_amount * $shop_commission / 100;
|
|
}
|
|
elseif ($cashier_good = $cashier->where('goods_id', $order_goods->goods_id)->first()) {
|
|
return $order_goods->payment_amount * $cashier_good->shop_commission / 100;
|
|
} else {
|
|
$res = $order_goods->payment_amount - $order_goods->goods_cost_price;
|
|
return $res > 0 ? $res : 0;
|
|
}
|
|
});
|
|
} else {
|
|
$price = $order_goods->sum('payment_amount');
|
|
}
|
|
|
|
return $price;
|
|
|
|
}
|
|
|
|
} |