StatController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\order\classes\StockWastageOrderClass;
  4. use bizGhs\order\classes\StockWastageOrderItemClass;
  5. use bizGhs\stat\classes\StatSaleClass;
  6. use common\components\arrayUtil;
  7. use common\components\imgUtil;
  8. use Yii;
  9. use common\components\util;
  10. use common\components\dateUtil;
  11. class StatController extends BaseController
  12. {
  13. public function actionProfit()
  14. {
  15. ini_set('memory_limit', '2045M');
  16. set_time_limit(0);
  17. $shopAdmin = $this->shopAdmin;
  18. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  19. util::fail('超管才能查看');
  20. }
  21. //惠雅鲜花员工不能看利润表
  22. $shopAdmin = $this->shopAdmin;
  23. $adminId = $shopAdmin->adminId ?? 0;
  24. if (in_array($adminId, [2366, 2812, 4004, 3405, 3407])) {
  25. util::fail('暂无权限');
  26. }
  27. $mainId = $this->mainId;
  28. $respond = StatSaleClass::profile($mainId);
  29. $income = $respond['income'] ?? 0;
  30. $expend = $respond['expend'] ?? 0;
  31. $balance = $respond['balance'] ?? 0;
  32. util::success(['income' => $income, 'expend' => $expend, 'balance' => floatval($balance)]);
  33. }
  34. //按月统计损耗情况
  35. public function actionWaste()
  36. {
  37. ini_set('memory_limit', '2045M');
  38. set_time_limit(0);
  39. $mainId = $this->mainId;
  40. $list = StockWastageOrderClass::getAllByCondition(['mainId' => $mainId], null, '*');
  41. $arr = [];
  42. if (!empty($list)) {
  43. foreach ($list as $order) {
  44. $addTime = $order['addTime'];
  45. $time = date("Ym", strtotime($addTime));
  46. $date = date("Y年n月", strtotime($addTime));
  47. $bigNum = $order['bigNum'];
  48. $cost = $order['cost'];
  49. if (isset($arr[$time])) {
  50. $arr[$time]['num'] = bcadd($arr[$time]['num'], $bigNum);
  51. $arr[$time]['cost'] = bcadd($arr[$time]['cost'], $cost, 2);
  52. $arr[$time]['time'] = $date;
  53. } else {
  54. $arr[$time]['num'] = $bigNum;
  55. $arr[$time]['cost'] = $cost;
  56. $arr[$time]['time'] = $date;
  57. }
  58. }
  59. }
  60. util::success(['list' => $arr]);
  61. }
  62. //损耗花材
  63. public function actionWastage()
  64. {
  65. $get = Yii::$app->request->get();
  66. $where = [];
  67. $where['mainId'] = $this->mainId;
  68. $searchTime = $get['searchTime'] ?? 'today';
  69. if (!empty($searchTime)) {
  70. $startTime = $get['startTime'] ?? '';
  71. $endTime = $get['endTime'] ?? '';
  72. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  73. $start = date("Y-m-d 00:00:00", strtotime($period['startTime']));
  74. $end = date("Y-m-d 23:59:59", strtotime($period['endTime']));
  75. $where['addTime'] = ['between', [$start, $end]];
  76. }
  77. $wastageList = StockWastageOrderItemClass::getAllByCondition($where, null, '*', null, true);
  78. $stat = [];
  79. $totalCost = 0;
  80. $totalNum = 0;
  81. if (!empty($wastageList)) {
  82. foreach ($wastageList as $waste) {
  83. $name = $waste->name ?? '';
  84. $shortCover = $waste->cover ?? '';
  85. $cover = imgUtil::groupImg($shortCover);
  86. $num = $waste->itemNum ?? 0;
  87. $cost = $waste->itemCost ?? 0;
  88. $currentCost = bcmul($cost, $num, 2);
  89. $productId = $waste->productId ?? 0;
  90. if (isset($stat[$productId]) == false) {
  91. $stat[$productId]['name'] = $name;
  92. $stat[$productId]['cover'] = $cover;
  93. $stat[$productId]['num'] = 0;
  94. $stat[$productId]['amount'] = 0;
  95. }
  96. $stat[$productId]['num'] = bcadd($stat[$productId]['num'], $num, 2);
  97. $stat[$productId]['amount'] = bcadd($stat[$productId]['amount'], $currentCost, 2);
  98. $totalNum = bcadd($totalNum, $num, 2);
  99. $totalCost = bcadd($totalCost, $currentCost, 2);
  100. }
  101. $stat = array_values($stat);
  102. $stat = arrayUtil::arraySort($stat, 'num');
  103. }
  104. util::success(['stat' => $stat, 'totalNum' => $totalNum, 'totalCost' => $totalCost]);
  105. }
  106. }