StatController.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. $mainId = $this->mainId;
  17. $respond = StatSaleClass::profile($mainId);
  18. $income = $respond['income'] ?? 0;
  19. $expend = $respond['expend'] ?? 0;
  20. $balance = $respond['balance'] ?? 0;
  21. util::success(['income' => $income, 'expend' => $expend, 'balance' => floatval($balance)]);
  22. }
  23. public function actionWastage()
  24. {
  25. $get = Yii::$app->request->get();
  26. $where = [];
  27. $where['mainId'] = $this->mainId;
  28. $searchTime = $get['searchTime'] ?? 'today';
  29. if (!empty($searchTime)) {
  30. $startTime = $get['startTime'] ?? '';
  31. $endTime = $get['endTime'] ?? '';
  32. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  33. $start = date("Y-m-d 00:00:00", strtotime($period['startTime']));
  34. $end = date("Y-m-d 23:59:59", strtotime($period['endTime']));
  35. $where['addTime'] = ['between', [$start, $end]];
  36. }
  37. $wastageList = StockWastageOrderItemClass::getAllByCondition($where, null, '*', null, true);
  38. $stat = [];
  39. $totalCost = 0;
  40. $totalNum = 0;
  41. if (!empty($wastageList)) {
  42. foreach ($wastageList as $waste) {
  43. $name = $waste->name ?? '';
  44. $shortCover = $waste->cover ?? '';
  45. $cover = imgUtil::groupImg($shortCover);
  46. $num = $waste->itemNum ?? 0;
  47. $cost = $waste->itemCost ?? 0;
  48. $currentCost = bcmul($cost, $num, 2);
  49. $productId = $waste->productId ?? 0;
  50. if (isset($stat[$productId]) == false) {
  51. $stat[$productId]['name'] = $name;
  52. $stat[$productId]['cover'] = $cover;
  53. $stat[$productId]['num'] = 0;
  54. $stat[$productId]['amount'] = 0;
  55. }
  56. $stat[$productId]['num'] = bcadd($stat[$productId]['num'], $num, 2);
  57. $stat[$productId]['amount'] = bcadd($stat[$productId]['amount'], $currentCost, 2);
  58. $totalNum = bcadd($totalNum, $num, 2);
  59. $totalCost = bcadd($totalCost, $currentCost, 2);
  60. }
  61. $stat = array_values($stat);
  62. $stat = arrayUtil::arraySort($stat, 'num');
  63. }
  64. util::success(['stat' => $stat, 'totalNum' => $totalNum, 'totalCost' => $totalCost]);
  65. }
  66. }