ConsoleController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace hd\controllers;
  3. use bizGhs\stat\classes\StatSaleClass;
  4. use bizHd\custom\classes\CustomClass;
  5. use bizHd\goods\classes\GoodsClass;
  6. use bizHd\order\classes\OrderClass;
  7. use bizHd\stat\classes\StatVisitClass;
  8. use bizGhs\item\classes\ItemClass;
  9. use common\components\dateUtil;
  10. use common\components\dict;
  11. use common\components\util;
  12. class ConsoleController extends BaseController
  13. {
  14. public $guestAccess = ['init', 'profile'];
  15. //总览
  16. public function actionStat()
  17. {
  18. $cacheKey = 'hd_console_stat_overview:' . $this->mainId . '_' . $this->shopId;
  19. $cache = \Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  20. $cached = !empty($cache) ? json_decode($cache, true) : null;
  21. if (is_array($cached)
  22. && isset($cached['totalItemNum'], $cached['totalCost'], $cached['goodsCount'], $cached['customCount'], $cached['orderCount'], $cached['debtCount'])) {
  23. $overview = [
  24. ['name' => "花材数", "url" => '/admin/stat/stock', 'value' => $cached['totalItemNum'], 'type' => 1],
  25. ['name' => "花材值", "url" => '/admin/stat/stock', 'value' => $cached['totalCost'], 'type' => 1],
  26. ['name' => "花束", "url" => '/admin/custom/showTotalBalance', 'value' => $cached['goodsCount'], 'type' => 1],
  27. ['name' => "总客户", "url" => '/admin/stat/stock', 'value' => $cached['customCount'], 'type' => 1],
  28. ['name' => "总订单", "url" => '/admin/home/member', 'value' => $cached['orderCount'], 'type' => 4],
  29. ['name' => "总赊账", "url" => '/admin/home/order', 'value' => $cached['debtCount'], 'type' => 4],
  30. ];
  31. util::success(['overview' => $overview]);
  32. }
  33. // 缓存未命中时计算
  34. $itemList = ItemClass::getAllByCondition(['mainId' => $this->mainId], null, 'id,stock,cost,virtualStock,delStatus', null, true);
  35. $totalItemNum = 0;
  36. $totalCost = 0;
  37. if (!empty($itemList)) {
  38. foreach ($itemList as $item) {
  39. if ($item->virtualStock == 0 && $item->delStatus == 0) {
  40. $stock = $item->stock ?? 0;
  41. $cost = $item->cost ?? 0;
  42. $totalItemNum = bcadd($stock, $totalItemNum, 2);
  43. $currentCost = bcmul($stock, $cost, 2);
  44. $totalCost = bcadd($currentCost, $totalCost, 2);
  45. }
  46. }
  47. }
  48. $totalItemNum = floor($totalItemNum);
  49. $goodsCount = GoodsClass::getCount(['mainId'=>$this->mainId]);
  50. $customCount = CustomClass::getCount(['shopId'=>$this->shopId]);
  51. $orderCount = OrderClass::getCount(['mainId'=>$this->mainId, 'status'=>4]);
  52. $debtCount = OrderClass::debtCount($this->mainId);
  53. $overview = [
  54. ['name' => "花材数", "url" => '/admin/stat/stock', 'value' => $totalItemNum, 'type' => 1],
  55. ['name' => "花材值", "url" => '/admin/stat/stock', 'value' => $totalCost, 'type' => 1],
  56. ['name' => "花束", "url" => '/admin/goods/list', 'value' => $goodsCount, 'type' => 1],
  57. ['name' => "总客户", "url" => '/admin/home/member', 'value' => $customCount, 'type' => 1],
  58. ['name' => "总订单", "url" => '/admin/home/order', 'value' => $orderCount, 'type' => 4],
  59. ['name' => "总赊账", "url" => '/admin/home/order', 'value' => $debtCount, 'type' => 4],
  60. ];
  61. $numbers = [
  62. 'totalItemNum' => $totalItemNum,
  63. 'totalCost' => $totalCost,
  64. 'goodsCount' => $goodsCount,
  65. 'customCount' => $customCount,
  66. 'orderCount' => $orderCount,
  67. 'debtCount' => $debtCount,
  68. ];
  69. // 缓存一小时
  70. \Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 3600, json_encode($numbers, JSON_UNESCAPED_UNICODE)]);
  71. util::success(['overview' => $overview]);
  72. }
  73. //概况
  74. public function actionProfile()
  75. {
  76. //查看财务的权限
  77. $lookMoney = \bizGhs\shop\classes\ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
  78. $lsIncome = 0;
  79. $orderCount = 0;
  80. $yesterdayIncome = 0;
  81. if ($lookMoney == 1) {
  82. $mainId = $this->mainId;
  83. $todayStr = date('Y-m-d');
  84. // 今天零售订单总数与收入
  85. $cacheKey = 'hd_console_profile:' . 'today_'.$todayStr.'_income_' . $this->shopId;
  86. $cache = \Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  87. if ($cache) {
  88. $cacheData = json_decode($cache, true);
  89. $lsIncome = $cacheData['lsIncome'];
  90. $orderCount = $cacheData['orderCount'];
  91. } else {
  92. $period = dateUtil::formatTime('today', '', '', true);
  93. $start = $period['startTime'];
  94. $end = $period['endTime'];
  95. $currentStartDate = date('Y-m-d', strtotime($start));
  96. $currentEndDate = date('Y-m-d', strtotime($end));
  97. $currentStartTime = $currentStartDate . ' 00:00:00';
  98. $currentEndTime = $currentEndDate . ' 23:59:59';
  99. $where['time'] = ['between', [$start, $end]];
  100. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  101. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  102. $orderCount = OrderClass::getCount($where);
  103. // $lsIncome = 0;
  104. // $hdOrderList = OrderClass::getAllByCondition($where, null, 'mainPay, forward, cash');
  105. // foreach ($hdOrderList as $hdKey => $hdOrder) {
  106. // $actPrice = $hdOrder['mainPay'] ?? 0;
  107. // $forward = $hdOrder['forward'] ?? 0;
  108. // if ($forward == 1) { //零售售后付款
  109. // // $lsAfterSale = bcadd($lsAfterSale, $actPrice, 2);
  110. // } else {
  111. // $cash = $hdOrder['cash'] ?? 0;
  112. // $lsIncome = bcadd($actPrice, $lsIncome, 2);
  113. // $lsIncome = bcadd($lsIncome, $cash, 2);
  114. // }
  115. // }
  116. $mainId = $this->mainId;
  117. $respond = StatSaleClass::profile($mainId);
  118. $incomeList = $respond['income'] ?? [];
  119. $todaySale = 0;
  120. if (!empty($incomeList)) {
  121. //这里有二个地方同时要修改,请搜索关键词:index_show_income
  122. foreach ($incomeList as $item) {
  123. if (isset($item['id']) && $item['id'] == 'allot') {
  124. continue;
  125. }
  126. $todaySale = bcadd($todaySale, $item['amount'], 2);
  127. }
  128. }
  129. $cacheData = [
  130. 'lsIncome' => $todaySale,
  131. 'orderCount' => $orderCount,
  132. ];
  133. \Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 1800, json_encode($cacheData, JSON_UNESCAPED_UNICODE)]);
  134. }
  135. // 昨天零售收入
  136. $cacheKey = 'hd_console_profile:' . 'yesterday_'.$todayStr.'_income_' . $this->shopId;
  137. $cache = \Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  138. if ($cache) {
  139. $cacheData = json_decode($cache, true);
  140. $yesterdayIncome = $cacheData['yesterdayIncome'];
  141. } else {
  142. // $period = dateUtil::formatTime('yesterday', '', '', true);
  143. // $start = $period['startTime'];
  144. // $end = $period['endTime'];
  145. // $currentStartDate = date('Y-m-d', strtotime($start));
  146. // $currentEndDate = date('Y-m-d', strtotime($end));
  147. // $currentStartTime = $currentStartDate . ' 00:00:00';
  148. // $currentEndTime = $currentEndDate . ' 23:59:59';
  149. // $where['time'] = ['between', [$start, $end]];
  150. // $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  151. // $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  152. // $yesterdayIncome = 0;
  153. // $hdOrderList = OrderClass::getAllByCondition($where, null, 'mainPay, forward, cash');
  154. // foreach ($hdOrderList as $hdKey => $hdOrder) {
  155. // $actPrice = $hdOrder['mainPay'] ?? 0;
  156. // $forward = $hdOrder['forward'] ?? 0;
  157. // if ($forward == 1) { //零售售后付款
  158. // // $lsAfterSale = bcadd($lsAfterSale, $actPrice, 2);
  159. // } else {
  160. // $cash = $hdOrder['cash'] ?? 0;
  161. // $yesterdayIncome = bcadd($actPrice, $yesterdayIncome, 2);
  162. // $yesterdayIncome = bcadd($yesterdayIncome, $cash, 2);
  163. // }
  164. // }
  165. $mainId = $this->mainId;
  166. $_GET['searchTime'] = 'yesterday'; // 必须要有的,让其获取昨天的收入
  167. $respond = StatSaleClass::profile($mainId);
  168. $incomeList = $respond['income'] ?? [];
  169. $yesterdayIncome = 0;
  170. if (!empty($incomeList)) {
  171. //这里有二个地方同时要修改,请搜索关键词:index_show_income
  172. foreach ($incomeList as $item) {
  173. if (isset($item['id']) && $item['id'] == 'allot') {
  174. continue;
  175. }
  176. $yesterdayIncome = bcadd($yesterdayIncome, $item['amount'], 2);
  177. }
  178. }
  179. $cacheData = [
  180. 'yesterdayIncome' => $yesterdayIncome,
  181. ];
  182. \Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 86400, json_encode($cacheData, JSON_UNESCAPED_UNICODE)]);
  183. }
  184. }
  185. //访客数
  186. $visitCustom = StatVisitClass::getVisitNum($this->shopId);
  187. // $wastage = \bizGhs\order\classes\StockWastageOrderClass::getTodayWaste($this->mainId);
  188. // $hsReturn = StatOrderClass::statHsNum($this->mainId);
  189. // $hsNum = $hsReturn['num'] ?? 0;
  190. $menu = [
  191. ["name" => "商城", "img" => "ghs/home/shop2.png", "url" => "/admin/cg/mall", 'pf' => 1,],
  192. ["name" => "改价", "img" => "ghs/home/gj2.png", "url" => "/admin/changePrice/list2", 'pf' => 1,],
  193. ["name" => "绿植花材", "img" => "ghs/home/hcgl2.png", "url" => "/admin/item/list2", 'pf' => 1,],
  194. ["name" => "接花束", "img" => "ghs/home/shop2.png", "url" => "/admin/order/workOrder", 'pf' => 1,],
  195. ["name" => "拆散", "img" => "ghs/home/kcyjs2.png", "url" => "/admin/part/list", 'pf' => 1,],
  196. ["name" => "损耗", "img" => "ghs/home/kcyjs2.png", "url" => "/admin/breakage/list", 'pf' => 1,],
  197. ["name" => "任务", "img" => "ghs/home/kcyjs2.png", "url" => "/admin/work/list", 'pf' => 1,],
  198. ["name" => "买花", "img" => "ghs/home/shop2.png", "url" => "/pagesPurchase/order", 'pf' => 1,],
  199. ["name" => "买花记录", "img" => "ghs/home/shop2.png", "url" => "/pagesPurchase/shopping", 'pf' => 1,],
  200. ["name" => "花束", "img" => "ghs/home/kcyjs2.png", "url" => "/admin/goods/list", 'pf' => 1,],
  201. ["name" => "客户充值", "img" => "ghs/home/kcyjs2.png", "url" => "/admin/recharge/rechargeCode", 'pf' => 1,],
  202. ];
  203. $warning = '';
  204. //$warning = '本月4号凌晨4点~5点系统升级,暂停使用';
  205. util::success([
  206. 'warning' => $warning,
  207. 'todayData' => [
  208. 'visit' => $visitCustom,
  209. 'income' => $lsIncome, //$todaySale,
  210. 'order' => $orderCount, //$orderNum,
  211. // 'wastage' => $wastage,
  212. // 'bouquet' => $hsNum
  213. ],
  214. 'yesterdayIncome' => $yesterdayIncome,
  215. 'menu' => $menu,
  216. 'lookMoney' => $lookMoney,
  217. ]);
  218. }
  219. //常用初始化
  220. public function actionInit()
  221. {
  222. $dict = dict::get($this->mainId);
  223. $shop = isset($this->shop) && !empty($this->shop) ? $this->shop->attributes : [];
  224. util::success(['dict' => $dict, 'shop' => $shop]);
  225. }
  226. //系统收入、客服收入、欠款
  227. public function actionStrokeCount()
  228. {
  229. if (empty($this->mainId)) {
  230. util::success([
  231. 'systemIncome' => 0,
  232. 'systemCount' => 0,
  233. 'debtCount' => 0,
  234. 'debtIncome' => 0,
  235. 'kfWxCount' => 0,
  236. 'kfWxIncome' => 0,
  237. 'cashCount' => 0,
  238. 'cashIncome' => 0,
  239. ]);
  240. }
  241. $return = \bizGhs\stat\classes\StatKdClass::eachChannelIncome($this->mainId, $this->shop);
  242. $incomeList = $return['incomeList'];
  243. $systemIncome1 = $incomeList['system']['category']['ls']['amount'] ?? 0;
  244. $systemCount1 = $incomeList['system']['category']['ls']['num'] ?? 0;
  245. $systemIncome2 = $incomeList['system']['category']['pf']['amount'] ?? 0;
  246. $systemCount2 = $incomeList['system']['category']['pf']['num'] ?? 0;
  247. $systemCount = bcadd($systemCount1, $systemCount2);
  248. $systemIncome = bcadd($systemIncome1, $systemIncome2, 2);
  249. $debtIncome1 = $incomeList['debt']['category']['ls']['amount'] ?? 0;
  250. $debtCount1 = $incomeList['debt']['category']['ls']['num'] ?? 0;
  251. $debtIncome2 = $incomeList['debt']['category']['pf']['amount'] ?? 0;
  252. $debtCount2 = $incomeList['debt']['category']['pf']['num'] ?? 0;
  253. $debtIncome = bcadd($debtIncome1, $debtIncome2, 2);
  254. $debtCount = bcadd($debtCount1, $debtCount2);
  255. $kfWxIncome1 = $incomeList['kfWx']['category']['ls']['amount'] ?? 0;
  256. $kfWxCount1 = $incomeList['kfWx']['category']['ls']['num'] ?? 0;
  257. $kfWxIncome2 = $incomeList['kfWx']['category']['pf']['amount'] ?? 0;
  258. $kfWxCount2 = $incomeList['kfWx']['category']['pf']['num'] ?? 0;
  259. $kfWxIncome = bcadd($kfWxIncome1, $kfWxIncome2, 2);
  260. $kfWxCount = bcadd($kfWxCount1, $kfWxCount2);
  261. $cashIncome1 = $incomeList['cash']['category']['ls']['amount'] ?? 0;
  262. $cashCount1 = $incomeList['cash']['category']['ls']['num'] ?? 0;
  263. $cashIncome2 = $incomeList['cash']['category']['pf']['amount'] ?? 0;
  264. $cashCount2 = $incomeList['cash']['category']['pf']['num'] ?? 0;
  265. $cashIncome = bcadd($cashIncome1, $cashIncome2, 2);
  266. $cashCount = bcadd($cashCount1, $cashCount2);
  267. util::success([
  268. 'systemIncome' => $systemIncome,
  269. 'systemCount' => $systemCount,
  270. 'debtCount' => $debtCount,
  271. 'debtIncome' => $debtIncome,
  272. 'kfWxCount' => $kfWxCount,
  273. 'kfWxIncome' => $kfWxIncome,
  274. 'cashCount' => $cashCount,
  275. 'cashIncome' => $cashIncome,
  276. ]);
  277. }
  278. }