ConsoleController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\custom\classes\CustomClass;
  4. use bizGhs\item\classes\ItemClass;
  5. use bizGhs\order\classes\OrderClass;
  6. use bizGhs\order\classes\StockWastageOrderClass;
  7. use bizGhs\shop\classes\ShopAdminClass;
  8. use bizGhs\stat\classes\StatKdClass;
  9. use bizGhs\stat\classes\StatSaleClass;
  10. use bizHd\purchase\classes\PurchaseClass;
  11. //use bizHd\stat\classes\StatVisitClass;
  12. use bizGhs\stat\classes\StatVisitClass;
  13. use common\components\dict;
  14. use common\components\util;
  15. use Yii;
  16. class ConsoleController extends BaseController
  17. {
  18. public $guestAccess = ['init', 'profile', 'Stroke-count', 'stat'];
  19. //总览
  20. public function actionStat()
  21. {
  22. //$where = ['shopId' => $this->shopId, 'debt' => 1]; // 重新赋值
  23. //$pfDebt = OrderClass::sum($where, 'remainDebtPrice'); // 总欠款
  24. //$pfDebt = $pfDebt === null ? 0 : $pfDebt;
  25. //$lsDebt = \bizHd\order\classes\OrderClass::sum(['mainId' => $this->mainId, 'debt' => 1], 'remainDebtPrice');
  26. //$lsDebt = $lsDebt === null ? 0 : $lsDebt;
  27. //$totalDebt = bcadd($pfDebt, $lsDebt, 2);
  28. // 优化慢查询:直接在数据库层聚合库存和成本,避免取出全量数据到PHP内存中循环计算
  29. $stat = ItemClass::getByCondition(
  30. ['mainId' => $this->mainId, 'virtualStock' => 0, 'delStatus' => 0],
  31. false,
  32. false,
  33. 'SUM(stock) as totalItemNum, SUM(stock * avCost) as totalCost'
  34. );
  35. $totalItemNum = floor($stat['totalItemNum'] ?? 0);
  36. $totalCost = round($stat['totalCost'] ?? 0, 2);
  37. $totalBalance = CustomClass::sum(['ownMainId'=>$this->mainId],'balance');
  38. $may = $totalBalance<0 ? abs($totalBalance) : 0;
  39. $overview = [
  40. ['name' => "库存数", "url" => '/admin/stat/stock', 'value' => $totalItemNum, 'type' => 1],
  41. ['name' => "库存值", "url" => '/admin/stat/stock', 'value' => $totalCost, 'type' => 1],
  42. ['name' => "应收款", "url" => '/admin/shop/debtChange', 'value' => $may, 'type' => 1],
  43. ['name' => "未命名", "url" => '/admin/home/member', 'value' => 0, 'type' => 4],
  44. ['name' => "总访客", "url" => '/admin/home/member', 'value' => 0, 'type' => 4],
  45. ['name' => "总订单", "url" => '/admin/home/order', 'value' => 0, 'type' => 4],
  46. ];
  47. util::success(['overview' => $overview]);
  48. }
  49. //系统收入、客服收入、欠款
  50. public function actionStrokeCount()
  51. {
  52. if (empty($this->mainId)) {
  53. util::success([
  54. 'systemIncome' => 0,
  55. 'systemCount' => 0,
  56. 'debtCount' => 0,
  57. 'debtIncome' => 0,
  58. 'kfWxCount' => 0,
  59. 'kfWxIncome' => 0,
  60. 'cashCount' => 0,
  61. 'cashIncome' => 0,
  62. ]);
  63. }
  64. $return = StatKdClass::eachChannelIncome($this->mainId, $this->shop);
  65. $incomeList = $return['incomeList'];
  66. $systemIncome1 = $incomeList['system']['category']['ls']['amount'] ?? 0;
  67. $systemCount1 = $incomeList['system']['category']['ls']['num'] ?? 0;
  68. $systemIncome2 = $incomeList['system']['category']['pf']['amount'] ?? 0;
  69. $systemCount2 = $incomeList['system']['category']['pf']['num'] ?? 0;
  70. $systemCount = bcadd($systemCount1, $systemCount2);
  71. $systemIncome = bcadd($systemIncome1, $systemIncome2, 2);
  72. $debtIncome1 = $incomeList['debt']['category']['ls']['amount'] ?? 0;
  73. $debtCount1 = $incomeList['debt']['category']['ls']['num'] ?? 0;
  74. $debtIncome2 = $incomeList['debt']['category']['pf']['amount'] ?? 0;
  75. $debtCount2 = $incomeList['debt']['category']['pf']['num'] ?? 0;
  76. $debtIncome = bcadd($debtIncome1, $debtIncome2, 2);
  77. $debtCount = bcadd($debtCount1, $debtCount2);
  78. $kfWxIncome1 = $incomeList['kfWx']['category']['ls']['amount'] ?? 0;
  79. $kfWxCount1 = $incomeList['kfWx']['category']['ls']['num'] ?? 0;
  80. $kfWxIncome2 = $incomeList['kfWx']['category']['pf']['amount'] ?? 0;
  81. $kfWxCount2 = $incomeList['kfWx']['category']['pf']['num'] ?? 0;
  82. $kfWxIncome = bcadd($kfWxIncome1, $kfWxIncome2, 2);
  83. $kfWxCount = bcadd($kfWxCount1, $kfWxCount2);
  84. $cashIncome1 = $incomeList['cash']['category']['ls']['amount'] ?? 0;
  85. $cashCount1 = $incomeList['cash']['category']['ls']['num'] ?? 0;
  86. $cashIncome2 = $incomeList['cash']['category']['pf']['amount'] ?? 0;
  87. $cashCount2 = $incomeList['cash']['category']['pf']['num'] ?? 0;
  88. $cashIncome = bcadd($cashIncome1, $cashIncome2, 2);
  89. $cashCount = bcadd($cashCount1, $cashCount2);
  90. util::success([
  91. 'systemIncome' => $systemIncome,
  92. 'systemCount' => $systemCount,
  93. 'debtCount' => $debtCount,
  94. 'debtIncome' => $debtIncome,
  95. 'kfWxCount' => $kfWxCount,
  96. 'kfWxIncome' => $kfWxIncome,
  97. 'cashCount' => $cashCount,
  98. 'cashIncome' => $cashIncome,
  99. ]);
  100. }
  101. /**
  102. * 昨日各渠道收入合计(与 stat-kd/profile、kdIncome 页「合计」口径一致)
  103. */
  104. public function actionGetYesterdayIncome()
  105. {
  106. if (empty($this->mainId)) {
  107. util::success(['income' => 0]);
  108. return;
  109. }
  110. // 固定查昨日,避免与 kdIncome 选「昨天」日期不一致
  111. Yii::$app->request->setQueryParams(array_merge(Yii::$app->request->get(), [
  112. 'searchTime' => 'yesterday',
  113. ]));
  114. $contain = intval(Yii::$app->request->get('contain', 0));
  115. $return = StatKdClass::eachChannelIncome($this->mainId, $this->shop, $contain);
  116. $incomeList = $return['incomeList'] ?? [];
  117. $totalIncome = '0.00';
  118. if (!empty($incomeList)) {
  119. foreach ($incomeList as $item) {
  120. $amount = $item['amount'] ?? 0;
  121. $totalIncome = bcadd($totalIncome, $amount, 2);
  122. }
  123. }
  124. util::success(['income' => floatval($totalIncome)]);
  125. }
  126. //今日概况 ssh 20220723
  127. public function actionProfile()
  128. {
  129. $get = Yii::$app->request->get();
  130. $version = $get['version'] ?? 1;
  131. $notice = [];
  132. $shop = $this->shop;
  133. if (!empty($shop)) {
  134. //到期前25天提醒
  135. $deadline = $shop->deadline;
  136. $currentTime = time();
  137. $deadTime = strtotime($deadline);
  138. $remainingSeconds = $deadTime - $currentTime;
  139. $remainingDays = ceil($remainingSeconds / 86400);
  140. // 到期前7天提醒
  141. if ($remainingSeconds <= 604800) {
  142. if ($remainingSeconds <= 0) {
  143. $notice[] = ['title' => '系统已到期', 'action' => '续费', 'page' => '/admin/shop/renew'];
  144. } else {
  145. $notice[] = ['title' => "系统{$remainingDays}天后到期,请及时续费", 'action' => '续费', 'page' => '/admin/shop/renew'];
  146. }
  147. }
  148. }
  149. //$notice[] = ['title' => '客户多个订单开始支持一键分享', 'action' => '查看', 'page' => '/admin/book/detail?id=calc'];
  150. //$notice[] = ['title' => '如果你发现一个问题很重要,一直没给你处理,在小群多反应几次,用反应次数强调重要性,目前批发店多反应问题多,都在排队。', 'action' => '', 'page' => ''];
  151. //$notice[] = ['title' => '新功能:客户端和后台使用跑腿的流程', 'action' => '', 'page' => '/admin/book/detail?id=use_pt'];
  152. //$notice[] = ['title' => '明年1月1号起,所有人介绍新批发店的分红政策,将会统一调整,请知悉', 'action' => '', 'page' => ''];
  153. //$notice[] = ['title' => '急事请连续打二次电话 15280215347', 'action' => '', 'page' => ''];
  154. $mainId = $this->mainId;
  155. $respond = StatSaleClass::profile($mainId);
  156. $incomeList = $respond['income'] ?? [];
  157. $todaySale = 0;
  158. if (!empty($incomeList)) {
  159. //这里有二个地方同时要修改,请搜索关键词:index_show_income
  160. foreach ($incomeList as $item) {
  161. if (isset($item['id']) && $item['id'] == 'allot') {
  162. continue;
  163. }
  164. $todaySale = bcadd($todaySale, $item['amount'], 2);
  165. }
  166. }
  167. $expendList = $respond['expend'] ?? [];
  168. $todayOut = 0;
  169. if (!empty($expendList)) {
  170. foreach ($expendList as $item) {
  171. $todayOut = bcadd($todayOut, $item['amount'], 2);
  172. }
  173. }
  174. $orderNum = $respond['totalOrderNum'] ?? 0;
  175. $wastage = StockWastageOrderClass::getTodayWaste($this->mainId);
  176. $visitCustom = StatVisitClass::getVisitNum($this->mainId);
  177. $menu = [
  178. ["name" => "商城", "img" => "ghs/home/shop3.png", "url" => "/admin/home/mall"],
  179. ["name" => "改价", "img" => "ghs/home/gj.png", "url" => "/admin/changePrice/list"],
  180. ["name" => "花材", "img" => "ghs/home/hcgl.png", "url" => "/admin/item/list"],
  181. ["name" => "花材(简)", "img" => "ghs/home/hcgl.png", "url" => "/admin/item/simple"],
  182. ["name" => "改库存", "img" => "ghs/home/kcyjs.png", "url" => "/admin/changePrice/changeStock"],
  183. ["name" => "采购", "img" => "ghs/home/icon_ghs.png", "url" => "/pagesPurchase/supplier"],
  184. ["name" => "采购记录", "img" => "ghs/home/icon_caigou.png", "url" => "/pagesPurchase/order"],
  185. ["name" => "调拨入库", "img" => "ghs/home/dbrk.png", "url" => "/pagesStorehouse/allot/allotPut"],
  186. ["name" => "计算器", "img" => "ghs/home/kcyjs.png", "url" => "/admin/tools/calculator"],
  187. ["name" => "买花", "img" => "ghs/home/icon_buy_item.png", "url" => "buyGhs"],
  188. ["name" => "买花记录", "img" => "ghs/home/icon_buy_record.png", "url" => "buyRecord", 'remindNum' => 0],
  189. ["name" => "调拨出库", "img" => "ghs/home/dbck.png", "url" => "/pagesStorehouse/allot/allotEx"],
  190. ["name" => "支出", "img" => "ghs/home/kcyjs.png", "url" => "/admin/expend/list"],
  191. ["name" => "员工", "img" => "ghs/home/yggl.png", "url" => "/admin/staff/list"],
  192. ["name" => "库存预警", "img" => "ghs/home/kcyjs.png", "url" => "/admin/item/list?warning=1"],
  193. ["name" => "盘点", "img" => "ghs/home/pandian.png", "url" => "/pagesStorehouse/inventory/list"],
  194. ["name" => "损耗", "img" => "ghs/home/kcyjs.png", "url" => "/admin/breakage/list"],
  195. ];
  196. $menuV2 = [
  197. ["name" => "商城码", "img" => "ghs/home/shop3.png", "url" => "/admin/home/mall"],
  198. ["name" => "改价", "img" => "ghs/home/gj.png", "url" => "/admin/changePrice/list2"],
  199. ["name" => "花材", "img" => "ghs/home/hcgl.png", "url" => "/admin/item/list2"],
  200. ["name" => "配送片区", "img" => "ghs/home/kcyjs.png", "url" => "/admin/order/orderListByDist"],
  201. ["name" => "改库存", "img" => "ghs/home/kcyjs.png", "url" => "/admin/changePrice/changeStock2"],
  202. ["name" => "采购", "img" => "ghs/home/icon_ghs.png", "url" => "/pagesPurchase/supplier"],
  203. ["name" => "采购记录", "img" => "ghs/home/icon_caigou.png", "url" => "/pagesPurchase/order"],
  204. ["name" => "调拨入库", "img" => "ghs/home/dbrk.png", "url" => "/pagesStorehouse/allot/allotPut"],
  205. ["name" => "计算器", "img" => "ghs/home/kcyjs.png", "url" => "/admin/tools/calculator"],
  206. ["name" => "买花", "img" => "ghs/home/icon_buy_item.png", "url" => "buyGhs"],
  207. ["name" => "买花记录", "img" => "ghs/home/icon_buy_record.png", "url" => "buyRecord", 'remindNum' => 0],
  208. ["name" => "调拨出库", "img" => "ghs/home/dbck.png", "url" => "/pagesStorehouse/allot/allotEx"],
  209. ["name" => "支出", "img" => "ghs/home/kcyjs.png", "url" => "/admin/expend/list"],
  210. ["name" => "员工", "img" => "ghs/home/yggl.png", "url" => "/admin/staff/list"],
  211. ["name" => "库存预警", "img" => "ghs/home/kcyjs.png", "url" => "/admin/item/list?warning=1"],
  212. ["name" => "盘点", "img" => "ghs/home/pandian.png", "url" => "/pagesStorehouse/inventory/list"],
  213. ["name" => "报损", "img" => "ghs/home/kcyjs.png", "url" => "/admin/breakage/list"],
  214. ["name" => "拆散", "img" => "ghs/home/kcyjs.png", "url" => "/admin/part/list"],
  215. ["name" => "收款流水", "img" => "ghs/home/kcyjs.png", "url" => "/admin/order/scanPay"],
  216. ["name" => "收款码", "img" => "ghs/home/shop.png", "url" => "/admin/cg/code"]
  217. ];
  218. $menuV3 = [
  219. ["name" => "商城码", "img" => "ghs/home/shop3.png", "url" => "/admin/home/mall"],
  220. ["name" => "改价", "img" => "ghs/home/gj.png", "url" => "/admin/changePrice/list2"],
  221. ["name" => "花材", "img" => "ghs/home/hcgl.png", "url" => "/admin/item/list2"],
  222. ["name" => "配送片区", "img" => "ghs/home/kcyjs.png", "url" => "/admin/order/orderListByDist"],
  223. ["name" => "改库存", "img" => "ghs/home/kcyjs.png", "url" => "/admin/changePrice/changeStock2"],
  224. ["name" => "采购", "img" => "ghs/home/icon_ghs.png", "url" => "/pagesPurchase/supplier"],
  225. ["name" => "采购记录", "img" => "ghs/home/icon_caigou.png", "url" => "/pagesPurchase/order"],
  226. ["name" => "调拨入库", "img" => "ghs/home/dbrk.png", "url" => "/pagesStorehouse/allot/allotPut"],
  227. ["name" => "计算器", "img" => "ghs/home/kcyjs.png", "url" => "/admin/tools/calculator"],
  228. ["name" => "买花", "img" => "ghs/home/icon_buy_item.png", "url" => "buyGhs"],
  229. ["name" => "买花记录", "img" => "ghs/home/icon_buy_record.png", "url" => "buyRecord", 'remindNum' => 0],
  230. ["name" => "调拨出库", "img" => "ghs/home/dbck.png", "url" => "/pagesStorehouse/allot/allotEx"],
  231. ["name" => "支出", "img" => "ghs/home/kcyjs.png", "url" => "/admin/expend/list"],
  232. ["name" => "员工", "img" => "ghs/home/yggl.png", "url" => "/admin/staff/list"],
  233. ["name" => "库存预警", "img" => "ghs/home/kcyjs.png", "url" => "/admin/item/warning"],
  234. ["name" => "盘点", "img" => "ghs/home/pandian.png", "url" => "/pagesStorehouse/inventory/list"],
  235. ["name" => "报损", "img" => "ghs/home/kcyjs.png", "url" => "/admin/breakage/list"],
  236. ["name" => "拆散", "img" => "ghs/home/kcyjs.png", "url" => "/admin/part/list"],
  237. ["name" => "收款流水", "img" => "ghs/home/kcyjs.png", "url" => "/admin/order/scanPay"],
  238. ["name" => "收款码", "img" => "ghs/home/shop.png", "url" => "/admin/cg/code"]
  239. ];
  240. $menuIconMap = [
  241. '商城码' => 'mall_code',
  242. '改价' => 'modify_price',
  243. '花材' => 'item',
  244. '配送片区' => 'send_dist',
  245. '改库存' => 'modify_stock',
  246. '采购' => 'purchase',
  247. '采购记录' => 'purchase_record',
  248. '调拨入库' => 'in_stock',
  249. '计算器' => 'calculator',
  250. '买花' => 'stock_in',
  251. '买花记录' => 'stock_in_record',
  252. '调拨出库' => 'out_stock',
  253. '支出' => 'expend',
  254. '员工' => 'staff',
  255. '库存预警' => 'stock_warning',
  256. '盘点' => 'check',
  257. '报损' => 'breakage',
  258. '损耗' => 'breakage',
  259. '拆散' => 'break_up',
  260. '收款流水' => 'gather_record',
  261. '收款码' => 'gather_code',
  262. '商城' => 'mall_code',
  263. '花材(简)' => 'item',
  264. ];
  265. if ($version == 2) {
  266. $menu = $menuV2;
  267. } elseif ($version == 3) {
  268. $menu = [];
  269. foreach ($menuV2 as $item) {
  270. $menuItem = $item;
  271. unset($menuItem['img']);
  272. $menuItem['icon'] = $menuIconMap[$item['name']] ?? 'item';
  273. $menu[] = $menuItem;
  274. }
  275. }elseif($version == 4) {
  276. $menu = [];
  277. foreach ($menuV3 as $item) {
  278. $menuItem = $item;
  279. unset($menuItem['img']);
  280. $menuItem['icon'] = $menuIconMap[$item['name']] ?? 'item';
  281. $menu[] = $menuItem;
  282. }
  283. }
  284. //查看财务的权限
  285. $lookMoney = ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
  286. $warning = '';
  287. $warningUrl = '';
  288. //$warning = '本页右上方,暂不显示昨天收入金额';
  289. //$warningUrl = '/admin/notice/warning';
  290. util::success([
  291. 'asset' => $this->main,
  292. 'notice' => $notice,
  293. 'warning' => $warning,
  294. 'warningUrl' => $warningUrl,
  295. 'todayData' => [
  296. 'income' => $todaySale,
  297. 'order' => $orderNum,
  298. 'expend' => $todayOut,
  299. 'visitCustom' => $visitCustom,
  300. 'wastage' => $wastage
  301. ],
  302. 'menu' => $menu,
  303. 'lookMoney' => $lookMoney,
  304. ]);
  305. }
  306. //常用数据初始化
  307. public function actionInit()
  308. {
  309. $dict = dict::get();
  310. $shop = isset($this->shop) && !empty($this->shop) ? $this->shop->attributes : [];
  311. util::success(['dict' => $dict, 'shop' => $shop]);
  312. }
  313. }