StatYjClass.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. // timeType=bill(默认记账 payTime)/ send(发货 sendTime)
  31. // 发货口径须补 payStatus=1:sendTime 不代表成交
  32. $timeType = isset($get['timeType']) ? strval($get['timeType']) : 'bill';
  33. $saleTimeField = ($timeType === 'send') ? 'sendTime' : 'payTime';
  34. //批发开单
  35. $where = ['mainId' => $mainId];
  36. $where[$saleTimeField] = ['between', [$currentStartTime, $currentEndTime]];
  37. // sendTime 不代表成交,发货口径只统计已确认付款
  38. if ($timeType === 'send') {
  39. $where['payStatus'] = 1;
  40. }
  41. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  42. if (!empty($ghsOrderList)) {
  43. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  44. $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
  45. $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
  46. $actPrice = $ghsOrder['actPrice'] ?? 0;
  47. $arr[$shopAdminId]['id'] = $shopAdminId;
  48. $arr[$shopAdminId]['name'] = $shopAdminName;
  49. if (isset($arr[$shopAdminId]['pfAmount']) == false) {
  50. $arr[$shopAdminId]['pfAmount'] = 0;
  51. $arr[$shopAdminId]['pfNum'] = 0;
  52. }
  53. if (isset($arr[$shopAdminId]['amount']) == false) {
  54. $arr[$shopAdminId]['amount'] = 0;
  55. $arr[$shopAdminId]['num'] = 0;
  56. }
  57. $arr[$shopAdminId]['pfAmount'] = bcadd($arr[$shopAdminId]['pfAmount'], $actPrice, 2);
  58. $arr[$shopAdminId]['pfNum'] = bcadd($arr[$shopAdminId]['pfNum'], 1);
  59. $arr[$shopAdminId]['num'] = bcadd($arr[$shopAdminId]['num'], 1);
  60. $arr[$shopAdminId]['amount'] = bcadd($arr[$shopAdminId]['amount'], $actPrice, 2);
  61. }
  62. }
  63. $hdWhere = ['mainId' => $mainId];
  64. $hdWhere[$saleTimeField] = ['between', [$currentStartTime, $currentEndTime]];
  65. // sendTime 不代表成交,发货口径只统计已确认付款(xhOrder.payStatus=1)
  66. if ($timeType === 'send') {
  67. $hdWhere['payStatus'] = 1;
  68. }
  69. $hdOrderList = \bizHd\order\classes\OrderClass::getAllByCondition($hdWhere, null, '*');
  70. if (!empty($hdOrderList)) {
  71. foreach ($hdOrderList as $hdKey => $hdOrder) {
  72. $shopAdminId = $hdOrder['shopAdminId'] ?? 0;
  73. $shopAdminName = $hdOrder['shopAdminName'] ?? '';
  74. $actPrice = $hdOrder['actPrice'] ?? 0;
  75. $arr[$shopAdminId]['id'] = $shopAdminId;
  76. $arr[$shopAdminId]['name'] = $shopAdminName;
  77. if (isset($arr[$shopAdminId]['lsAmount']) == false) {
  78. $arr[$shopAdminId]['lsAmount'] = 0;
  79. $arr[$shopAdminId]['lsNum'] = 0;
  80. }
  81. if (isset($arr[$shopAdminId]['amount']) == false) {
  82. $arr[$shopAdminId]['amount'] = 0;
  83. $arr[$shopAdminId]['num'] = 0;
  84. }
  85. $arr[$shopAdminId]['lsAmount'] = bcadd($arr[$shopAdminId]['lsAmount'], $actPrice, 2);
  86. $arr[$shopAdminId]['lsNum'] = bcadd($arr[$shopAdminId]['lsNum'], 1);
  87. $arr[$shopAdminId]['num'] = bcadd($arr[$shopAdminId]['num'], 1);
  88. $arr[$shopAdminId]['amount'] = bcadd($arr[$shopAdminId]['amount'], $actPrice, 2);
  89. }
  90. }
  91. if (!empty($arr)) {
  92. $ids = array_keys($arr);
  93. $staffList = ShopAdminClass::getByIds($ids, null, 'id');
  94. foreach ($arr as $shopAdminId => $val) {
  95. $name = $staffList[$shopAdminId]['name'] ?? '商城';
  96. $arr[$shopAdminId]['name'] = $name;
  97. }
  98. }
  99. return array_values($arr);
  100. }
  101. //业绩列表 ssh 2021.3.15
  102. public static function getYjList($where)
  103. {
  104. $data = self::getList('*', $where, 'addTime DESC');
  105. $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
  106. if (empty($list)) {
  107. return $data;
  108. }
  109. $arr = [];
  110. foreach ($data['list'] as $key => $val) {
  111. $shopAdminId = $val['shopAdminId'];
  112. if (isset($arr[$shopAdminId]) == false) {
  113. $arr[$shopAdminId] = ['amount' => 0, 'num' => 0];
  114. }
  115. $arr[$shopAdminId]['amount'] = bcadd($val['amount'], $arr[$shopAdminId]['amount'], 2);
  116. $arr[$shopAdminId]['num'] = bcadd($val['num'], $arr[$shopAdminId]['num']);
  117. }
  118. $ids = array_keys($arr);
  119. $admin = ShopAdminClass::getByIds($ids);
  120. $show = [];
  121. foreach ($admin as $key => $val) {
  122. $adminId = $val['id'];
  123. $name = $val['name'] ?? '';
  124. $amount = $arr[$adminId]['amount'] ?? 0.00;
  125. $num = $arr[$adminId]['num'] ?? 0;
  126. $show[] = [
  127. 'name' => $name,
  128. 'amount' => $amount,
  129. 'num' => $num,
  130. 'id' => $val['id'],
  131. ];
  132. }
  133. //排序
  134. $show = arrayUtil::arraySort($show, 'amount');
  135. return ['list' => $show];
  136. }
  137. //增加业绩 ssh 2021.3.20
  138. public static function addYj($main, $shop, $shopAdminId, $amount, $date = null)
  139. {
  140. $sjId = $shop->sjId ?? 0;
  141. $mainId = $main->id ?? 0;
  142. $date = isset($date) ? $date : date("Ymd");
  143. $stat = self::getByCondition(['shopAdminId' => $shopAdminId, 'mainId' => $mainId, 'sjId' => $sjId, 'time' => $date], true);
  144. if (empty($stat)) {
  145. $stat = self::add(['sjId' => $sjId, 'shopAdminId' => $shopAdminId, 'mainId' => $mainId, 'time' => $date], true);
  146. }
  147. $stat->num += 1;
  148. $stat->totalNum += 1;
  149. $stat->amount = bcadd($amount, $stat->amount, 2);
  150. $stat->totalAmount = bcadd($amount, $stat->totalAmount, 2);
  151. $stat->save();
  152. StatYjMonthClass::addYj($main, $shop, $shopAdminId, $amount);
  153. }
  154. }