StatYjClass.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace bizGhs\stat\classes;
  3. use bizGhs\order\classes\OrderClass;
  4. use bizGhs\shop\classes\ShopAdminClass;
  5. use bizGhs\shop\models\ShopAdmin;
  6. use common\components\arrayUtil;
  7. use common\components\dateUtil;
  8. use Yii;
  9. use bizGhs\base\classes\BaseClass;
  10. class StatYjClass extends BaseClass
  11. {
  12. public static $baseFile = '\bizGhs\stat\models\StatYj';
  13. public static function profile($mainId)
  14. {
  15. $get = Yii::$app->request->get();
  16. $where = [];
  17. $where['mainId'] = $mainId;
  18. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  19. $startTime = $get['startTime'] ?? '';
  20. $endTime = $get['endTime'] ?? '';
  21. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  22. $start = $period['startTime'];
  23. $end = $period['endTime'];
  24. $where['time'] = ['between', [$start, $end]];
  25. $currentStartDate = date('Y-m-d', strtotime($start));
  26. $currentEndDate = date('Y-m-d', strtotime($end));
  27. $currentStartTime = $currentStartDate . ' 00:00:00';
  28. $currentEndTime = $currentEndDate . ' 23:59:59';
  29. $arr = [];
  30. //批发开单
  31. $where = ['mainId' => $mainId];
  32. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  33. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  34. if (!empty($ghsOrderList)) {
  35. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  36. $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
  37. $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
  38. $actPrice = $ghsOrder['actPrice'] ?? 0;
  39. $arr[$shopAdminId]['id'] = $shopAdminId;
  40. $arr[$shopAdminId]['name'] = $shopAdminName;
  41. if (isset($arr[$shopAdminId]['pfAmount']) == false) {
  42. $arr[$shopAdminId]['pfAmount'] = 0;
  43. $arr[$shopAdminId]['pfNum'] = 0;
  44. }
  45. if (isset($arr[$shopAdminId]['amount']) == false) {
  46. $arr[$shopAdminId]['amount'] = 0;
  47. $arr[$shopAdminId]['num'] = 0;
  48. }
  49. $arr[$shopAdminId]['pfAmount'] = bcadd($arr[$shopAdminId]['pfAmount'], $actPrice, 2);
  50. $arr[$shopAdminId]['pfNum'] = bcadd($arr[$shopAdminId]['pfNum'], 1);
  51. $arr[$shopAdminId]['num'] = bcadd($arr[$shopAdminId]['num'], 1);
  52. $arr[$shopAdminId]['amount'] = bcadd($arr[$shopAdminId]['amount'], $actPrice, 2);
  53. }
  54. }
  55. $hdWhere = ['mainId' => $mainId];
  56. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  57. $hdOrderList = \bizHd\order\classes\OrderClass::getAllByCondition($hdWhere, null, '*');
  58. if (!empty($hdOrderList)) {
  59. foreach ($hdOrderList as $hdKey => $hdOrder) {
  60. $shopAdminId = $hdOrder['shopAdminId'] ?? 0;
  61. $shopAdminName = $hdOrder['shopAdminName'] ?? '';
  62. $actPrice = $hdOrder['actPrice'] ?? 0;
  63. $arr[$shopAdminId]['id'] = $shopAdminId;
  64. $arr[$shopAdminId]['name'] = $shopAdminName;
  65. if (isset($arr[$shopAdminId]['lsAmount']) == false) {
  66. $arr[$shopAdminId]['lsAmount'] = 0;
  67. $arr[$shopAdminId]['lsNum'] = 0;
  68. }
  69. if (isset($arr[$shopAdminId]['amount']) == false) {
  70. $arr[$shopAdminId]['amount'] = 0;
  71. $arr[$shopAdminId]['num'] = 0;
  72. }
  73. $arr[$shopAdminId]['lsAmount'] = bcadd($arr[$shopAdminId]['lsAmount'], $actPrice, 2);
  74. $arr[$shopAdminId]['lsNum'] = bcadd($arr[$shopAdminId]['lsNum'], 1);
  75. $arr[$shopAdminId]['num'] = bcadd($arr[$shopAdminId]['num'], 1);
  76. $arr[$shopAdminId]['amount'] = bcadd($arr[$shopAdminId]['amount'], $actPrice, 2);
  77. }
  78. }
  79. if (!empty($arr)) {
  80. $ids = array_keys($arr);
  81. $staffList = ShopAdminClass::getByIds($ids, null, 'id');
  82. foreach ($arr as $shopAdminId => $val) {
  83. $name = $staffList[$shopAdminId]['name'] ?? '商城';
  84. $arr[$shopAdminId]['name'] = $name;
  85. }
  86. }
  87. return array_values($arr);
  88. }
  89. //业绩列表 ssh 2021.3.15
  90. public static function getYjList($where)
  91. {
  92. $data = self::getList('*', $where, 'addTime DESC');
  93. $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
  94. if (empty($list)) {
  95. return $data;
  96. }
  97. $arr = [];
  98. foreach ($data['list'] as $key => $val) {
  99. $shopAdminId = $val['shopAdminId'];
  100. if (isset($arr[$shopAdminId]) == false) {
  101. $arr[$shopAdminId] = ['amount' => 0, 'num' => 0];
  102. }
  103. $arr[$shopAdminId]['amount'] = bcadd($val['amount'], $arr[$shopAdminId]['amount'], 2);
  104. $arr[$shopAdminId]['num'] = bcadd($val['num'], $arr[$shopAdminId]['num']);
  105. }
  106. $ids = array_keys($arr);
  107. $admin = ShopAdminClass::getByIds($ids);
  108. $show = [];
  109. foreach ($admin as $key => $val) {
  110. $adminId = $val['id'];
  111. $name = $val['name'] ?? '';
  112. $amount = $arr[$adminId]['amount'] ?? 0.00;
  113. $num = $arr[$adminId]['num'] ?? 0;
  114. $show[] = [
  115. 'name' => $name,
  116. 'amount' => $amount,
  117. 'num' => $num,
  118. 'id' => $val['id'],
  119. ];
  120. }
  121. //排序
  122. $show = arrayUtil::arraySort($show, 'amount');
  123. return ['list' => $show];
  124. }
  125. //增加业绩 ssh 2021.3.20
  126. public static function addYj($main, $shop, $shopAdminId, $amount, $date = null)
  127. {
  128. $sjId = $shop->sjId ?? 0;
  129. $mainId = $main->id ?? 0;
  130. $date = isset($date) ? $date : date("Ymd");
  131. $stat = self::getByCondition(['shopAdminId' => $shopAdminId, 'mainId' => $mainId, 'sjId' => $sjId, 'time' => $date], true);
  132. if (empty($stat)) {
  133. $stat = self::add(['sjId' => $sjId, 'shopAdminId' => $shopAdminId, 'mainId' => $mainId, 'time' => $date], true);
  134. }
  135. $stat->num += 1;
  136. $stat->totalNum += 1;
  137. $stat->amount = bcadd($amount, $stat->amount, 2);
  138. $stat->totalAmount = bcadd($amount, $stat->totalAmount, 2);
  139. $stat->save();
  140. StatYjMonthClass::addYj($main, $shop, $shopAdminId, $amount);
  141. }
  142. }