StatKdController.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace console\controllers;
  3. use biz\shop\classes\ShopClass;
  4. use biz\stat\classes\StatKdClass;
  5. use biz\stat\classes\StatRefundClass;
  6. use bizGhs\order\classes\OrderClass;
  7. use bizGhs\order\models\Order;
  8. use bizGhs\order\models\RefundOrder;
  9. use yii\console\Controller;
  10. use Yii;
  11. class StatKdController extends Controller
  12. {
  13. //销售数据更新 ./yii stat-kd/update
  14. public function actionUpdate()
  15. {
  16. ini_set('memory_limit', '2045M');
  17. set_time_limit(0);
  18. $sql = 'TRUNCATE `xhStatKd`;TRUNCATE `xhStatKdMonth`;';
  19. Yii::$app->db->createCommand($sql)->execute();
  20. $query = new \yii\db\Query();
  21. $query->from(Order::tableName());
  22. foreach ($query->batch() as $batch) {
  23. foreach ($batch as $order) {
  24. $status = $order['status'] ?? 0;
  25. if ($status == 1 || $status == 5) {
  26. continue;
  27. }
  28. $payWay = $order['payWay'] ?? 0;
  29. $amount = $order['actPrice'] ?? 0;
  30. $refundPrice = $order['refundPrice'] ?? 0;
  31. if ($refundPrice > 0) {
  32. $amount = bcadd($amount, $refundPrice, 2);
  33. }
  34. $shopId = $order['shopId'] ?? 0;
  35. $shop = ShopClass::getLockById($shopId);
  36. if (empty($shop)) {
  37. continue;
  38. }
  39. $date = date('Ymd', strtotime($order['addTime']));
  40. //各渠道收入金额统计
  41. StatKdClass::replace($shop, $amount, $payWay, $date);
  42. }
  43. }
  44. $sql = 'TRUNCATE `xhStatRefund`;TRUNCATE `xhStatRefundMonth`;';
  45. Yii::$app->db->createCommand($sql)->execute();
  46. $query = new \yii\db\Query();
  47. $query->from(RefundOrder::tableName());
  48. foreach ($query->batch() as $batch) {
  49. foreach ($batch as $refund) {
  50. $relateOrderSn = $refund['relateOrderSn'] ?? '';
  51. $order = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  52. $payWay = $order->payWay ?? 0;
  53. $shopId = $refund['shopId'] ?? 0;
  54. $shop = ShopClass::getLockById($shopId);
  55. $amount = $refund['refundPrice'] ?? 0;
  56. $date = date('Ymd', strtotime($refund['addTime']));
  57. StatRefundClass::replace($shop, $payWay, $amount, $date);
  58. }
  59. }
  60. }
  61. }