StatItemController.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace ghs\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['mainId'] = $this->mainId;
  17. $searchTime = $get['searchTime'] ?? 'today';
  18. if (!empty($searchTime)) {
  19. $startTime = $get['startTime'] ?? '';
  20. $endTime = $get['endTime'] ?? '';
  21. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  22. $where['time'] = ['between', [$period['startTime'], $period['endTime']]];
  23. }
  24. $list = StatItemClass::getAllByCondition($where, null, '*');
  25. if (empty($list)) {
  26. util::success(['list' => $list]);
  27. }
  28. $ids = array_column($list, 'itemId');
  29. $ids = array_unique(array_filter($ids));
  30. $productInfo = ProductClass::getByIds($ids, null, 'id');
  31. $stat = [];
  32. foreach ($list as $key => $val) {
  33. $itemId = $val['itemId'] ?? 0;
  34. $idsData[$itemId] = 0;
  35. $num = bcsub($val['num'], $val['refundNum'], 2);
  36. $amount = bcsub($val['amount'], $val['refundAmount'], 2);
  37. $name = $productInfo[$itemId]['name'] ?? '';
  38. $py = $productInfo[$itemId]['py'] ?? '';
  39. $stat[$itemId]['productId'] = $itemId;
  40. $stat[$itemId]['name'] = $name;
  41. $stat[$itemId]['py'] = $py;
  42. if (isset($stat[$itemId]['num'])) {
  43. $stat[$itemId]['num'] = bcadd($stat[$itemId]['num'], $num, 2);
  44. } else {
  45. $stat[$itemId]['num'] = $num;
  46. }
  47. if (isset($stat[$itemId]['amount'])) {
  48. $stat[$itemId]['amount'] = bcadd($stat[$itemId]['amount'], $amount, 2);
  49. } else {
  50. $stat[$itemId]['amount'] = $amount;
  51. }
  52. }
  53. $stat = arrayUtil::arraySort($stat, 'num');
  54. util::success(['list' => $stat]);
  55. }
  56. }