StatController.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\order\classes\StockWastageOrderItemClass;
  4. use bizGhs\stat\classes\StatSaleClass;
  5. use common\components\arrayUtil;
  6. use common\components\imgUtil;
  7. use Yii;
  8. use common\components\util;
  9. use common\components\dateUtil;
  10. class StatController extends BaseController
  11. {
  12. public function actionProfit()
  13. {
  14. ini_set('memory_limit', '2045M');
  15. set_time_limit(0);
  16. $shopAdmin = $this->shopAdmin;
  17. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  18. util::fail('超管才能查看');
  19. }
  20. $shopAdmin = $this->shopAdmin;
  21. $staffId = $shopAdmin->id ?? 0;
  22. if (in_array($staffId, [126921, 128024, 128026])) {
  23. util::fail('暂无权限');
  24. }
  25. $mainId = $this->mainId;
  26. $respond = StatSaleClass::profile($mainId);
  27. $income = $respond['income'] ?? 0;
  28. $expend = $respond['expend'] ?? 0;
  29. $balance = $respond['balance'] ?? 0;
  30. util::success(['income' => $income, 'expend' => $expend, 'balance' => floatval($balance)]);
  31. }
  32. public function actionWastage()
  33. {
  34. $get = Yii::$app->request->get();
  35. $where = [];
  36. $where['mainId'] = $this->mainId;
  37. $searchTime = $get['searchTime'] ?? 'today';
  38. if (!empty($searchTime)) {
  39. $startTime = $get['startTime'] ?? '';
  40. $endTime = $get['endTime'] ?? '';
  41. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  42. $start = date("Y-m-d 00:00:00", strtotime($period['startTime']));
  43. $end = date("Y-m-d 23:59:59", strtotime($period['endTime']));
  44. $where['addTime'] = ['between', [$start, $end]];
  45. }
  46. $wastageList = StockWastageOrderItemClass::getAllByCondition($where, null, '*', null, true);
  47. $stat = [];
  48. $totalCost = 0;
  49. $totalNum = 0;
  50. if (!empty($wastageList)) {
  51. foreach ($wastageList as $waste) {
  52. $name = $waste->name ?? '';
  53. $shortCover = $waste->cover ?? '';
  54. $cover = imgUtil::groupImg($shortCover);
  55. $num = $waste->itemNum ?? 0;
  56. $cost = $waste->itemCost ?? 0;
  57. $currentCost = bcmul($cost, $num, 2);
  58. $productId = $waste->productId ?? 0;
  59. if (isset($stat[$productId]) == false) {
  60. $stat[$productId]['name'] = $name;
  61. $stat[$productId]['cover'] = $cover;
  62. $stat[$productId]['num'] = 0;
  63. $stat[$productId]['amount'] = 0;
  64. }
  65. $stat[$productId]['num'] = bcadd($stat[$productId]['num'], $num, 2);
  66. $stat[$productId]['amount'] = bcadd($stat[$productId]['amount'], $currentCost, 2);
  67. $totalNum = bcadd($totalNum, $num, 2);
  68. $totalCost = bcadd($totalCost, $currentCost, 2);
  69. }
  70. $stat = array_values($stat);
  71. $stat = arrayUtil::arraySort($stat, 'num');
  72. }
  73. util::success(['stat' => $stat, 'totalNum' => $totalNum, 'totalCost' => $totalCost]);
  74. }
  75. }