115 lines
4.9 KiB
PHP
115 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\SalesReport;
|
|
use App\Models\Seller;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use RuLong\Order\Models\Order;
|
|
use RuLong\Order\Models\OrderDetail;
|
|
|
|
class OrderDataCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'rulong:OrderDataCommand';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Command description';
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function handle()
|
|
{
|
|
$sellerids = Seller::get();
|
|
$starta = Carbon::yesterday()->toDateString();
|
|
foreach ($sellerids as $key => $seller) {
|
|
$seller_id = $seller->id;
|
|
$start = date($starta, strtotime('-1 day'));
|
|
$end = date($starta);
|
|
$orderids[$key] = Order::when($seller_id, function ($q) use ($seller_id) {$q->where('seller_id', $seller_id);})
|
|
->whereHas('logs', function ($query) use ($start, $end) {
|
|
$query->where('state', 'DELIVERED|SIGNED')->when($start && $end, function ($query) use ($start, $end) {
|
|
$query->whereBetween('created_at', [$start, date('Y-m-d', strtotime("+1 days", strtotime($end)))]);
|
|
})
|
|
->when($start && !$end, function ($query) use ($start) {
|
|
$query->where('created_at', '>=', $start);
|
|
})
|
|
->when((!$start && $end), function ($query) use ($end) {
|
|
$query->where('created_at', '<=', date('Y-m-d', strtotime("+1 days", strtotime($end))));
|
|
});
|
|
})->where('state', '<>',Order::ORDER_CLOSED)->pluck('id');
|
|
if (!empty($orderids[$key])) {
|
|
self::insertReport($orderids[$key], 'SIGNED', $starta, $seller_id);
|
|
}
|
|
|
|
$orderDeliveredids[$key] = Order::when($seller_id, function ($q) use ($seller_id) {$q->where('seller_id', $seller_id);})
|
|
->whereHas('logs', function ($query) use ($start, $end) {
|
|
$query->where('state', 'PAID|DELIVERED')->when($start && $end, function ($query) use ($start, $end) {
|
|
$query->whereBetween('created_at', [$start, date('Y-m-d', strtotime("+1 days", strtotime($end)))]);
|
|
})
|
|
->when($start && !$end, function ($query) use ($start) {
|
|
$query->where('created_at', '>=', $start);
|
|
})
|
|
->when((!$start && $end), function ($query) use ($end) {
|
|
$query->where('created_at', '<=', date('Y-m-d', strtotime("+1 days", strtotime($end))));
|
|
});
|
|
})->where('state', '<>',Order::ORDER_CLOSED)->pluck('id');
|
|
if (!empty($orderids[$key])) {
|
|
self::insertReport($orderDeliveredids[$key], 'DELIVERED', $starta, $seller_id);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private static function insertReport($orderids, $type, $starta, $seller_id)
|
|
{
|
|
$detail = OrderDetail::select(DB::raw('sum(number) as number_sum, sum(score * number) as score_sum, sum(price * number) as price_sum, sum(seller_price * number) as seller_price_sum'))
|
|
->whereIn('order_id', $orderids)->first();
|
|
if (!empty($detail) && $detail->number_sum) {
|
|
$order = Order::select(DB::raw('count(*) as order_total, sum(freight) as freight_sum, sum(seller_freight) as seller_freight_sum, sum(amount) as orders_price, sum(score) as orders_score,sum(seller_amount) as seller_amount_sum'))
|
|
->whereIn('id', $orderids)->first();
|
|
$report = SalesReport::where(['seller_id' => $seller_id, 'state' => $type, 'action_time' => $starta])->first();
|
|
if (empty($report)) {
|
|
SalesReport::create([
|
|
'state' => $type,
|
|
'action_time' => $starta,
|
|
'goods_total' => $detail->number_sum,
|
|
'goods_price_total' => $detail->price_sum,
|
|
'goods_score_total' => $detail->score_sum,
|
|
'orders_total' => $order->order_total,
|
|
'orders_price' => $order->orders_price,
|
|
'orders_score' => $order->orders_score,
|
|
'orders_freight' => $order->freight_sum,
|
|
'seller_id' => $seller_id,
|
|
'seller_total' => $order->seller_amount_sum,
|
|
'seller_freight' => $order->seller_freight_sum,
|
|
]);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|