StatItemController.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. //查看财务的权限
  15. $lookMoney = \bizGhs\shop\classes\ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
  16. if ($lookMoney == 0) {
  17. util::fail('无法查看');
  18. }
  19. $get = Yii::$app->request->get();
  20. $where = [];
  21. $where['shopId'] = $this->shopId;
  22. if (isset($get['shopId']) && !empty($get['shopId'])) {
  23. $where['shopId'] = $get['shopId'];
  24. }
  25. $searchTime = $get['searchTime'] ?? 'today';
  26. if (!empty($searchTime)) {
  27. $startTime = $get['startTime'] ?? '';
  28. $endTime = $get['endTime'] ?? '';
  29. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  30. $where['time'] = ['between', [$period['startTime'], $period['endTime']]];
  31. }
  32. $list = StatItemClass::getAllByCondition($where, null, '*');
  33. if (empty($list)) {
  34. util::success(['list' => $list]);
  35. }
  36. $ids = array_column($list, 'itemId');
  37. $ids = array_unique(array_filter($ids));
  38. $productInfo = ProductClass::getByIds($ids, null, 'id');
  39. $stat = [];
  40. foreach ($list as $key => $val) {
  41. $itemId = $val['itemId'] ?? 0;
  42. $idsData[$itemId] = 0;
  43. $num = bcsub($val['num'], $val['refundNum'], 2);
  44. $amount = bcsub($val['amount'], $val['refundAmount'], 2);
  45. $name = $productInfo[$itemId]['name'] ?? '';
  46. $py = $productInfo[$itemId]['py'] ?? '';
  47. $stat[$itemId]['productId'] = $itemId;
  48. $stat[$itemId]['name'] = $name;
  49. $stat[$itemId]['py'] = $py;
  50. if (isset($stat[$itemId]['num'])) {
  51. $stat[$itemId]['num'] = bcadd($stat[$itemId]['num'], $num, 2);
  52. } else {
  53. $stat[$itemId]['num'] = $num;
  54. }
  55. if (isset($stat[$itemId]['amount'])) {
  56. $stat[$itemId]['amount'] = bcadd($stat[$itemId]['amount'], $amount, 2);
  57. } else {
  58. $stat[$itemId]['amount'] = $amount;
  59. }
  60. }
  61. $stat = arrayUtil::arraySort($stat, 'num');
  62. util::success(['list' => $stat]);
  63. }
  64. }