StatItemController.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace hd\controllers;
  3. use biz\stat\classes\StatItemClass;
  4. use bizGhs\product\classes\ProductClass;
  5. use common\components\arrayUtil;
  6. use common\components\dateUtil;
  7. use Yii;
  8. use common\components\util;
  9. class StatItemController extends BaseController
  10. {
  11. //花材销量 ssh 20211021
  12. public function actionProfile()
  13. {
  14. $get = Yii::$app->request->get();
  15. $where = [];
  16. $where['shopId'] = $this->shopId;
  17. if (isset($get['shopId']) && !empty($get['shopId'])) {
  18. $where['shopId'] = $get['shopId'];
  19. }
  20. $searchTime = $get['searchTime'] ?? 'today';
  21. if (!empty($searchTime)) {
  22. $startTime = $get['startTime'] ?? '';
  23. $endTime = $get['endTime'] ?? '';
  24. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  25. $where['time'] = ['between', [$period['startTime'], $period['endTime']]];
  26. }
  27. $list = StatItemClass::getAllByCondition($where, null, '*');
  28. if (empty($list)) {
  29. util::success(['list' => $list]);
  30. }
  31. $ids = array_column($list, 'itemId');
  32. $ids = array_unique(array_filter($ids));
  33. $productInfo = ProductClass::getByIds($ids, null, 'id');
  34. $stat = [];
  35. foreach ($list as $key => $val) {
  36. $itemId = $val['itemId'] ?? 0;
  37. $idsData[$itemId] = 0;
  38. $num = bcsub($val['num'], $val['refundNum'], 2);
  39. $amount = bcsub($val['amount'], $val['refundAmount'], 2);
  40. $name = $productInfo[$itemId]['name'] ?? '';
  41. $py = $productInfo[$itemId]['py'] ?? '';
  42. $stat[$itemId]['productId'] = $itemId;
  43. $stat[$itemId]['name'] = $name;
  44. $stat[$itemId]['py'] = $py;
  45. if (isset($stat[$itemId]['num'])) {
  46. $stat[$itemId]['num'] = bcadd($stat[$itemId]['num'], $num, 2);
  47. } else {
  48. $stat[$itemId]['num'] = $num;
  49. }
  50. if (isset($stat[$itemId]['amount'])) {
  51. $stat[$itemId]['amount'] = bcadd($stat[$itemId]['amount'], $amount, 2);
  52. } else {
  53. $stat[$itemId]['amount'] = $amount;
  54. }
  55. }
  56. $stat = arrayUtil::arraySort($stat, 'num');
  57. util::success(['list' => $stat]);
  58. }
  59. }