0
0
Files
Babyclass/app/Models/WindupReport.php
2020-08-04 10:09:42 +08:00

69 lines
2.8 KiB
PHP

<?php
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use RuLong\Order\Models\Order;
class WindupReport extends Model
{
public function orders()
{
return $this->hasMany(Order::class);
}
public function seller()
{
return $this->belongsTo(Seller::class);
}
public function autoReport()
{
$today_arr = explode('-', Carbon::today()->toDateString());
if ($today_arr[2] > 15) {
//本月1=15
$start = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), 1, date("Y")));
$end = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), 15, date("Y")));
} else {
$start = date('Y-m-16 00:00:00', strtotime('-1 month'));
$end = date("Y-m-d 23:59:59", strtotime(-date('d') . 'day'));
}
$sellerids = Seller::get();
foreach ($sellerids as $key => $seller) {
$seller_id = $seller->id;
$orderids = Order::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, $end]);
});
})
->where('state', '<>',Order::ORDER_CLOSED)
->pluck('id');
$windup = Order::select(DB::raw('count(*) as orders_count, sum(freight) as freight_sum, sum(seller_freight) as seller_freight_sum, sum(amount) as amount_sum, sum(score) as score_sum,sum(seller_amount) as seller_amount_sum'))
->whereIn('id', $orderids)->first();
$report = WindupReport::where(['seller_id' => $seller_id, 'start_time' => $start, 'end_time' => $end])->first();
if ($orderids->count()>0 && empty($report)) {
DB::transaction(function () use ($seller_id, $start, $end, $windup, $orderids) {
$windup_report = WindupReport::create([
'seller_id' => $seller_id,
'start_time' => $start,
'end_time' => $end,
'orders_count' => $windup->orders_count ?? 0,
'freight_sum' => $windup->freight_sum ?? 0,
'amount_sum' => $windup->amount_sum ?? 0,
'score_sum' => $windup->score_sum ?? 0,
'seller_freight_sum' => $windup->seller_freight_sum ?? 0,
'seller_amount_sum' => $windup->seller_amount_sum ?? 0,
'orderids' => $orderids
]);
Order::whereIn('id', $orderids)->update(['windup_report_id' => $windup_report->id]);
});
}
}
}
}