StatController.php 2.7 KB

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