StatisticalCommand.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. namespace App\Console\Commands\Finance;
  3. use App\Repositories\Enums\Finance\CheckStatusEnum;
  4. use App\Repositories\Enums\Finance\IncomeTypeEnum;
  5. use App\Repositories\Enums\Finance\OrderTypeEnum;
  6. use App\Repositories\Enums\Finance\StatisticalTypeEnum;
  7. use App\Repositories\Models\Finance\Bill;
  8. use App\Repositories\Models\Finance\Order;
  9. use App\Repositories\Models\Finance\Shop;
  10. use App\Repositories\Models\Finance\Statisticals;
  11. use Carbon\Carbon;
  12. use Illuminate\Console\Command;
  13. class StatisticalCommand extends Command
  14. {
  15. /**
  16. * The name and signature of the console command.
  17. *
  18. * @var string
  19. */
  20. protected $signature = 'statistical:order';
  21. /**
  22. * The console command description.
  23. *
  24. * @var string
  25. */
  26. protected $description = 'Command description';
  27. /**
  28. * Create a new command instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct()
  33. {
  34. parent::__construct();
  35. }
  36. /**
  37. * Execute the console command.
  38. *
  39. * @return int
  40. */
  41. public function handle()
  42. {
  43. $day = Carbon::yesterday()->toDateString();
  44. $day = Carbon::now()->toDateString();
  45. $pipelines = Order::query()->where('check_status', CheckStatusEnum::SUCCESS)->where('type', OrderTypeEnum::FOLLOW)->whereDate('check_time', $day)->get()->groupBy('shop_id')->toArray();
  46. $data = [];
  47. foreach ($pipelines as $id => $shop) {
  48. $shop_id = $id;
  49. $platform = $shop[0]['platform'];
  50. $type = StatisticalTypeEnum::SHOP_DAY;
  51. $order_nums = 0;
  52. $refund_nums = 0;
  53. $buy_nums = 0;
  54. $cost_money = 0.00;
  55. $order_money = 0.00;
  56. $refund_money = 0.00;
  57. $refund_cost_money = 0.00;
  58. $other_money = 0.00;
  59. $postage_money = 0.00;
  60. $profits_money = 0.00;
  61. $compensate = 0.00;
  62. $return_money = 0.00;
  63. $refund_postage_money = 0.00;
  64. $koudian = Shop::query()->where('id', $shop_id)->value('koudian');
  65. foreach ($shop as $order) {
  66. if ($order['refund_order_money'] > 0.00) {
  67. $refund_nums++;
  68. }
  69. if ($order['buy_id'] > 0) {
  70. $buy_nums++;
  71. }
  72. $order_nums += $order['nums'];
  73. $cost_money = bcadd($order['cost_money'], $cost_money, 2);
  74. $order_money = bcadd($order['order_money'], $order_money, 2);
  75. $refund_money = bcadd($order['refund_order_money'], $refund_money, 2);
  76. $refund_cost_money = bcadd($order['refund_cost_money'], $refund_cost_money, 2);
  77. $other_money = bcadd($order['other_money'], $other_money, 2);
  78. $postage_money = bcadd($order['postage_money'], $postage_money, 2);
  79. $profits_money = bcadd($order['profits_money'], $profits_money, 2);
  80. $refund_postage_money = bcadd($order['refund_postage_money'], $refund_postage_money, 2);
  81. $return_money = bcadd($order['return_money'], $return_money, 2);
  82. $compensate = bcadd($order['compensate'], $compensate, 2);
  83. }
  84. $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();
  85. $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');
  86. $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');
  87. $data[] = [
  88. 'shop_id' => $shop_id,
  89. 'platform' => $platform,
  90. 'day' => $day,
  91. 'type' => $type,
  92. 'body' => [
  93. 'order_nums' => $order_nums,
  94. 'refund_nums' => $refund_nums,
  95. 'buy_nums' => $buy_nums,
  96. 'cost_money' => $cost_money,
  97. 'order_money' => $order_money,
  98. 'refund_money' => $refund_money,
  99. 'refund_cost_money' => $refund_cost_money,
  100. 'other_money' => $order_money,
  101. 'postage_money' => $postage_money,
  102. 'profits_money' => $profits_money,
  103. 'refund_postage_money' => $refund_postage_money,
  104. 'return_money' => $return_money,
  105. 'compensate' => $compensate,
  106. 'koudian' => $koudian,
  107. 'money' => bcmul($profits_money, $koudian / 100, 2),
  108. 'bills' => $bills,
  109. 'in_money' => $in_money,
  110. 'out_money' => $out_money,
  111. 'bill_money' => bcsub($in_money, $out_money, 2),
  112. 'profits' => bcadd(bcmul($profits_money, $koudian / 100, 2), bcsub($in_money, $out_money, 2), 2)
  113. ],
  114. 'created_at' => now(),
  115. 'updated_at' => now(),
  116. ];
  117. }
  118. foreach ($data as $d) {
  119. $model = new Statisticals($d);
  120. $model->save();
  121. }
  122. //平台每天收益
  123. $statisticals = Statisticals::query()->where('day', $day)->where('type', StatisticalTypeEnum::SHOP_DAY)->get()->groupBy('platform')->toArray();
  124. $platformData = [];
  125. foreach ($statisticals as $p_id => $statistical) {
  126. $shop_id = 0;
  127. $platform = $p_id;
  128. $type = StatisticalTypeEnum::PLATFORM_DAY;
  129. $order_nums = 0;
  130. $refund_nums = 0;
  131. $buy_nums = 0;
  132. $cost_money = 0.00;
  133. $order_money = 0.00;
  134. $refund_money = 0.00;
  135. $refund_cost_money = 0.00;
  136. $other_money = 0.00;
  137. $postage_money = 0.00;
  138. $profits_money = 0.00;
  139. $compensate = 0.00;
  140. $return_money = 0.00;
  141. $refund_postage_money = 0.00;
  142. $money = 0.00;
  143. $in_money = 0.00;
  144. $out_money = 0.00;
  145. $bill_money = 0.00;
  146. $profits = 0.00;
  147. foreach ($statistical as $order) {
  148. $refund_nums += $order['body']['refund_nums'];
  149. $buy_nums += $order['body']['buy_nums'];
  150. $order_nums += $order['body']['order_nums'];
  151. $cost_money = bcadd($order['body']['cost_money'], $cost_money, 2);
  152. $order_money = bcadd($order['body']['order_money'], $order_money, 2);
  153. $refund_money = bcadd($order['body']['refund_money'], $refund_money, 2);
  154. $refund_cost_money = bcadd($order['body']['refund_cost_money'], $refund_cost_money, 2);
  155. $other_money = bcadd($order['body']['other_money'], $other_money, 2);
  156. $postage_money = bcadd($order['body']['postage_money'], $postage_money, 2);
  157. $profits_money = bcadd($order['body']['profits_money'], $profits_money, 2);
  158. $compensate = bcadd($order['body']['compensate'], $compensate, 2);
  159. $return_money = bcadd($order['body']['return_money'], $return_money, 2);
  160. $refund_postage_money = bcadd($order['body']['refund_postage_money'], $refund_postage_money, 2);
  161. $money = bcadd($order['body']['money'], $money, 2);
  162. $in_money = bcadd($order['body']['in_money'], $in_money, 2);
  163. $out_money = bcadd($order['body']['out_money'], $out_money, 2);
  164. $bill_money = bcadd($order['body']['bill_money'], $bill_money, 2);
  165. $profits = bcadd($order['body']['profits'], $profits, 2);
  166. }
  167. $platformData[] = [
  168. 'shop_id' => $shop_id,
  169. 'platform' => $platform,
  170. 'day' => $day,
  171. 'type' => $type,
  172. 'body' => [
  173. 'order_nums' => $order_nums,
  174. 'refund_nums' => $refund_nums,
  175. 'buy_nums' => $buy_nums,
  176. 'cost_money' => $cost_money,
  177. 'order_money' => $order_money,
  178. 'refund_money' => $refund_money,
  179. 'refund_cost_money' => $refund_cost_money,
  180. 'other_money' => $order_money,
  181. 'postage_money' => $postage_money,
  182. 'profits_money' => $profits_money,
  183. 'refund_postage_money' => $refund_postage_money,
  184. 'return_money' => $return_money,
  185. 'compensate' => $compensate,
  186. 'money' => $money,
  187. 'in_money' => $in_money,
  188. 'out_money' => $out_money,
  189. 'bill_money' => $bill_money,
  190. 'profits' => $profits,
  191. ],
  192. 'created_at' => now(),
  193. 'updated_at' => now(),
  194. ];
  195. }
  196. foreach ($platformData as $d) {
  197. $model = new Statisticals($d);
  198. $model->save();
  199. }
  200. //每天收益
  201. $totals = Statisticals::query()->where('day', $day)->where('type', StatisticalTypeEnum::PLATFORM_DAY)->get()->toArray();
  202. $totalData = [];
  203. $shop_id = 0;
  204. $platform = 0;
  205. $type = StatisticalTypeEnum::DAY;
  206. $order_nums = 0;
  207. $refund_nums = 0;
  208. $buy_nums = 0;
  209. $cost_money = 0.00;
  210. $order_money = 0.00;
  211. $refund_money = 0.00;
  212. $refund_cost_money = 0.00;
  213. $other_money = 0.00;
  214. $postage_money = 0.00;
  215. $profits_money = 0.00;
  216. $compensate = 0.00;
  217. $return_money = 0.00;
  218. $refund_postage_money = 0.00;
  219. $money = 0.00;
  220. $out_money = 0.00;
  221. $in_money = 0.00;
  222. $bill_money = 0.00;
  223. $profits = 0.00;
  224. foreach ($totals as $p_id => $order) {
  225. $refund_nums += $order['body']['refund_nums'];
  226. $buy_nums += $order['body']['buy_nums'];
  227. $order_nums += $order['body']['order_nums'];
  228. $cost_money = bcadd($order['body']['cost_money'], $cost_money, 2);
  229. $order_money = bcadd($order['body']['order_money'], $order_money, 2);
  230. $refund_money = bcadd($order['body']['refund_money'], $refund_money, 2);
  231. $refund_cost_money = bcadd($order['body']['refund_cost_money'], $refund_cost_money, 2);
  232. $other_money = bcadd($order['body']['other_money'], $other_money, 2);
  233. $postage_money = bcadd($order['body']['postage_money'], $postage_money, 2);
  234. $profits_money = bcadd($order['body']['profits_money'], $profits_money, 2);
  235. $compensate = bcadd($order['body']['compensate'], $compensate, 2);
  236. $return_money = bcadd($order['body']['return_money'], $return_money, 2);
  237. $refund_postage_money = bcadd($order['body']['refund_postage_money'], $refund_postage_money, 2);
  238. $money = bcadd($order['body']['money'], $money, 2);
  239. $in_money = bcadd($order['body']['in_money'], $in_money, 2);
  240. $out_money = bcadd($order['body']['out_money'], $out_money, 2);
  241. $bill_money = bcadd($order['body']['bill_money'], $bill_money, 2);
  242. $profits = bcadd($order['body']['profits'], $profits, 2);
  243. }
  244. $totalData = [
  245. 'shop_id' => $shop_id,
  246. 'platform' => $platform,
  247. 'day' => $day,
  248. 'type' => $type,
  249. 'body' => [
  250. 'order_nums' => $order_nums,
  251. 'refund_nums' => $refund_nums,
  252. 'buy_nums' => $buy_nums,
  253. 'cost_money' => $cost_money,
  254. 'order_money' => $order_money,
  255. 'refund_money' => $refund_money,
  256. 'refund_cost_money' => $refund_cost_money,
  257. 'other_money' => $order_money,
  258. 'postage_money' => $postage_money,
  259. 'profits_money' => $profits_money,
  260. 'refund_postage_money' => $refund_postage_money,
  261. 'return_money' => $return_money,
  262. 'compensate' => $compensate,
  263. 'money' => $money,
  264. 'in_money' => $in_money,
  265. 'out_money' => $out_money,
  266. 'bill_money' => $bill_money,
  267. 'profits' => $profits,
  268. ],
  269. 'created_at' => now(),
  270. 'updated_at' => now(),
  271. ];
  272. $model = new Statisticals($totalData);
  273. $model->save();
  274. }
  275. }