123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- namespace App\Console\Commands\Finance;
- use App\Repositories\Enums\Finance\CheckStatusEnum;
- use App\Repositories\Enums\Finance\IncomeTypeEnum;
- use App\Repositories\Enums\Finance\OrderTypeEnum;
- use App\Repositories\Enums\Finance\StatisticalTypeEnum;
- use App\Repositories\Models\Finance\Bill;
- use App\Repositories\Models\Finance\Order;
- use App\Repositories\Models\Finance\Shop;
- use App\Repositories\Models\Finance\Statisticals;
- use Carbon\Carbon;
- use Illuminate\Console\Command;
- class StatisticalCommand extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'statistical:order';
- /**
- * 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 int
- */
- public function handle()
- {
- $day = Carbon::yesterday()->toDateString();
- $day = Carbon::now()->toDateString();
- $pipelines = Order::query()->where('check_status', CheckStatusEnum::SUCCESS)->where('type', OrderTypeEnum::FOLLOW)->whereDate('check_time', $day)->get()->groupBy('shop_id')->toArray();
- $data = [];
- foreach ($pipelines as $id => $shop) {
- $shop_id = $id;
- $platform = $shop[0]['platform'];
- $type = StatisticalTypeEnum::SHOP_DAY;
- $order_nums = 0;
- $refund_nums = 0;
- $buy_nums = 0;
- $cost_money = 0.00;
- $order_money = 0.00;
- $refund_money = 0.00;
- $refund_cost_money = 0.00;
- $other_money = 0.00;
- $postage_money = 0.00;
- $profits_money = 0.00;
- $compensate = 0.00;
- $return_money = 0.00;
- $refund_postage_money = 0.00;
- $koudian = Shop::query()->where('id', $shop_id)->value('koudian');
- foreach ($shop as $order) {
- if ($order['refund_order_money'] > 0.00) {
- $refund_nums++;
- }
- if ($order['buy_id'] > 0) {
- $buy_nums++;
- }
- $order_nums += $order['nums'];
- $cost_money = bcadd($order['cost_money'], $cost_money, 2);
- $order_money = bcadd($order['order_money'], $order_money, 2);
- $refund_money = bcadd($order['refund_order_money'], $refund_money, 2);
- $refund_cost_money = bcadd($order['refund_cost_money'], $refund_cost_money, 2);
- $other_money = bcadd($order['other_money'], $other_money, 2);
- $postage_money = bcadd($order['postage_money'], $postage_money, 2);
- $profits_money = bcadd($order['profits_money'], $profits_money, 2);
- $refund_postage_money = bcadd($order['refund_postage_money'], $refund_postage_money, 2);
- $return_money = bcadd($order['return_money'], $return_money, 2);
- $compensate = bcadd($order['compensate'], $compensate, 2);
- }
- $bills = Bill::query()->where('check_status', CheckStatusEnum::SUCCESS)->whereDate('check_time', $day)->where('shop_id', $shop_id)->select(['id', 'money', 'type_id', 'income_type'])->get()->append('type_name')->toArray();
- $in_money = Bill::query()->where('check_status', CheckStatusEnum::SUCCESS)->whereDate('check_time', $day)->where('shop_id', $shop_id)->where('income_type', IncomeTypeEnum::IN)->sum('money');
- $out_money = Bill::query()->where('check_status', CheckStatusEnum::SUCCESS)->whereDate('check_time', $day)->where('shop_id', $shop_id)->where('income_type', IncomeTypeEnum::OUT)->sum('money');
- $data[] = [
- 'shop_id' => $shop_id,
- 'platform' => $platform,
- 'day' => $day,
- 'type' => $type,
- 'body' => [
- 'order_nums' => $order_nums,
- 'refund_nums' => $refund_nums,
- 'buy_nums' => $buy_nums,
- 'cost_money' => $cost_money,
- 'order_money' => $order_money,
- 'refund_money' => $refund_money,
- 'refund_cost_money' => $refund_cost_money,
- 'other_money' => $order_money,
- 'postage_money' => $postage_money,
- 'profits_money' => $profits_money,
- 'refund_postage_money' => $refund_postage_money,
- 'return_money' => $return_money,
- 'compensate' => $compensate,
- 'koudian' => $koudian,
- 'money' => bcmul($profits_money, $koudian / 100, 2),
- 'bills' => $bills,
- 'in_money' => $in_money,
- 'out_money' => $out_money,
- 'bill_money' => bcsub($in_money, $out_money, 2),
- 'profits' => bcadd(bcmul($profits_money, $koudian / 100, 2), bcsub($in_money, $out_money, 2), 2)
- ],
- 'created_at' => now(),
- 'updated_at' => now(),
- ];
- }
- foreach ($data as $d) {
- $model = new Statisticals($d);
- $model->save();
- }
- //平台每天收益
- $statisticals = Statisticals::query()->where('day', $day)->where('type', StatisticalTypeEnum::SHOP_DAY)->get()->groupBy('platform')->toArray();
- $platformData = [];
- foreach ($statisticals as $p_id => $statistical) {
- $shop_id = 0;
- $platform = $p_id;
- $type = StatisticalTypeEnum::PLATFORM_DAY;
- $order_nums = 0;
- $refund_nums = 0;
- $buy_nums = 0;
- $cost_money = 0.00;
- $order_money = 0.00;
- $refund_money = 0.00;
- $refund_cost_money = 0.00;
- $other_money = 0.00;
- $postage_money = 0.00;
- $profits_money = 0.00;
- $compensate = 0.00;
- $return_money = 0.00;
- $refund_postage_money = 0.00;
- $money = 0.00;
- $in_money = 0.00;
- $out_money = 0.00;
- $bill_money = 0.00;
- $profits = 0.00;
- foreach ($statistical as $order) {
- $refund_nums += $order['body']['refund_nums'];
- $buy_nums += $order['body']['buy_nums'];
- $order_nums += $order['body']['order_nums'];
- $cost_money = bcadd($order['body']['cost_money'], $cost_money, 2);
- $order_money = bcadd($order['body']['order_money'], $order_money, 2);
- $refund_money = bcadd($order['body']['refund_money'], $refund_money, 2);
- $refund_cost_money = bcadd($order['body']['refund_cost_money'], $refund_cost_money, 2);
- $other_money = bcadd($order['body']['other_money'], $other_money, 2);
- $postage_money = bcadd($order['body']['postage_money'], $postage_money, 2);
- $profits_money = bcadd($order['body']['profits_money'], $profits_money, 2);
- $compensate = bcadd($order['body']['compensate'], $compensate, 2);
- $return_money = bcadd($order['body']['return_money'], $return_money, 2);
- $refund_postage_money = bcadd($order['body']['refund_postage_money'], $refund_postage_money, 2);
- $money = bcadd($order['body']['money'], $money, 2);
- $in_money = bcadd($order['body']['in_money'], $in_money, 2);
- $out_money = bcadd($order['body']['out_money'], $out_money, 2);
- $bill_money = bcadd($order['body']['bill_money'], $bill_money, 2);
- $profits = bcadd($order['body']['profits'], $profits, 2);
- }
- $platformData[] = [
- 'shop_id' => $shop_id,
- 'platform' => $platform,
- 'day' => $day,
- 'type' => $type,
- 'body' => [
- 'order_nums' => $order_nums,
- 'refund_nums' => $refund_nums,
- 'buy_nums' => $buy_nums,
- 'cost_money' => $cost_money,
- 'order_money' => $order_money,
- 'refund_money' => $refund_money,
- 'refund_cost_money' => $refund_cost_money,
- 'other_money' => $order_money,
- 'postage_money' => $postage_money,
- 'profits_money' => $profits_money,
- 'refund_postage_money' => $refund_postage_money,
- 'return_money' => $return_money,
- 'compensate' => $compensate,
- 'money' => $money,
- 'in_money' => $in_money,
- 'out_money' => $out_money,
- 'bill_money' => $bill_money,
- 'profits' => $profits,
- ],
- 'created_at' => now(),
- 'updated_at' => now(),
- ];
- }
- foreach ($platformData as $d) {
- $model = new Statisticals($d);
- $model->save();
- }
- //每天收益
- $totals = Statisticals::query()->where('day', $day)->where('type', StatisticalTypeEnum::PLATFORM_DAY)->get()->toArray();
- $totalData = [];
- $shop_id = 0;
- $platform = 0;
- $type = StatisticalTypeEnum::DAY;
- $order_nums = 0;
- $refund_nums = 0;
- $buy_nums = 0;
- $cost_money = 0.00;
- $order_money = 0.00;
- $refund_money = 0.00;
- $refund_cost_money = 0.00;
- $other_money = 0.00;
- $postage_money = 0.00;
- $profits_money = 0.00;
- $compensate = 0.00;
- $return_money = 0.00;
- $refund_postage_money = 0.00;
- $money = 0.00;
- $out_money = 0.00;
- $in_money = 0.00;
- $bill_money = 0.00;
- $profits = 0.00;
- foreach ($totals as $p_id => $order) {
- $refund_nums += $order['body']['refund_nums'];
- $buy_nums += $order['body']['buy_nums'];
- $order_nums += $order['body']['order_nums'];
- $cost_money = bcadd($order['body']['cost_money'], $cost_money, 2);
- $order_money = bcadd($order['body']['order_money'], $order_money, 2);
- $refund_money = bcadd($order['body']['refund_money'], $refund_money, 2);
- $refund_cost_money = bcadd($order['body']['refund_cost_money'], $refund_cost_money, 2);
- $other_money = bcadd($order['body']['other_money'], $other_money, 2);
- $postage_money = bcadd($order['body']['postage_money'], $postage_money, 2);
- $profits_money = bcadd($order['body']['profits_money'], $profits_money, 2);
- $compensate = bcadd($order['body']['compensate'], $compensate, 2);
- $return_money = bcadd($order['body']['return_money'], $return_money, 2);
- $refund_postage_money = bcadd($order['body']['refund_postage_money'], $refund_postage_money, 2);
- $money = bcadd($order['body']['money'], $money, 2);
- $in_money = bcadd($order['body']['in_money'], $in_money, 2);
- $out_money = bcadd($order['body']['out_money'], $out_money, 2);
- $bill_money = bcadd($order['body']['bill_money'], $bill_money, 2);
- $profits = bcadd($order['body']['profits'], $profits, 2);
- }
- $totalData = [
- 'shop_id' => $shop_id,
- 'platform' => $platform,
- 'day' => $day,
- 'type' => $type,
- 'body' => [
- 'order_nums' => $order_nums,
- 'refund_nums' => $refund_nums,
- 'buy_nums' => $buy_nums,
- 'cost_money' => $cost_money,
- 'order_money' => $order_money,
- 'refund_money' => $refund_money,
- 'refund_cost_money' => $refund_cost_money,
- 'other_money' => $order_money,
- 'postage_money' => $postage_money,
- 'profits_money' => $profits_money,
- 'refund_postage_money' => $refund_postage_money,
- 'return_money' => $return_money,
- 'compensate' => $compensate,
- 'money' => $money,
- 'in_money' => $in_money,
- 'out_money' => $out_money,
- 'bill_money' => $bill_money,
- 'profits' => $profits,
- ],
- 'created_at' => now(),
- 'updated_at' => now(),
- ];
- $model = new Statisticals($totalData);
- $model->save();
- }
- }
|