StatKdClass.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <?php
  2. namespace bizGhs\stat\classes;
  3. use biz\shop\classes\ShopAdminClass;
  4. use bizGhs\order\services\NextDayRefundService;
  5. use bizGhs\order\classes\OrderClass;
  6. use bizGhs\order\classes\OrderItemClass;
  7. use bizGhs\order\classes\StockOutOrderClass;
  8. use bizHd\order\classes\SettleClass;
  9. use common\components\dateUtil;
  10. use common\components\dict;
  11. use bizHd\order\classes\OrderClass as HdOrderClass;
  12. use bizGhs\clear\classes\ClearClass;
  13. use bizGhs\base\classes\BaseClass;
  14. use common\components\noticeUtil;
  15. use Yii;
  16. class StatKdClass extends BaseClass
  17. {
  18. public static $baseFile = '\bizGhs\stat\models\StatKd';
  19. //当天各渠道收入 ssh 20220723
  20. public static function eachChannelIncome($mainId, $shop, $contain = 1)
  21. {
  22. $ghsShopId = $shop->id ?? 0;
  23. $lsShopId = $shop->lsShopId ?? 0;
  24. //批发开单 pf 零售开单 ls 批发结账 pfJz 零售尾款 lsWk
  25. $get = Yii::$app->request->get();
  26. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  27. $startTime = $get['startTime'] ?? '';
  28. $endTime = $get['endTime'] ?? '';
  29. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  30. $start = $period['startTime'];
  31. $end = $period['endTime'];
  32. $currentStartDate = date('Y-m-d', strtotime($start));
  33. $currentEndDate = date('Y-m-d', strtotime($end));
  34. $currentStartTime = $currentStartDate . ' 00:00:00';
  35. $currentEndTime = $currentEndDate . ' 23:59:59';
  36. $staffList = ShopAdminClass::getAllByCondition(['mainId' => $mainId, 'isPt' => 0], null, '*', 'id', true);
  37. $kf = [];
  38. if (!empty($staffList)) {
  39. foreach ($staffList as $staff) {
  40. $name = $staff->name ?? '';
  41. $staffId = $staff->id ?? 0;
  42. $kf[$staffId]['name'] = $name;
  43. $kf[$staffId]['key'] = $staffId;
  44. $kf[$staffId]['num'] = 0;
  45. $kf[$staffId]['amount'] = 0;
  46. $kf[$staffId]['itemList']['pf'] = ['name' => '批发', 'key' => 'pf', 'num' => 0, 'amount' => 0];
  47. $kf[$staffId]['itemList']['ls'] = ['name' => '零售', 'key' => 'ls', 'num' => 0, 'amount' => 0];
  48. $kf[$staffId]['itemList']['pfJz'] = ['name' => '批发结账', 'key' => 'pfJz', 'num' => 0, 'amount' => 0];
  49. $kf[$staffId]['itemList']['lsWk'] = ['name' => '零售尾款', 'key' => 'lsWk', 'num' => 0, 'amount' => 0];
  50. }
  51. }
  52. $incomeList = [
  53. //系统收入
  54. 'system' => [
  55. 'amount' => 0,
  56. 'num' => 0,
  57. 'name' => '系统收款',
  58. 'category' => [
  59. 'pf' => ['name' => '批发', 'key' => 'pf', 'num' => 0, 'amount' => 0],
  60. 'ls' => ['name' => '零售', 'key' => 'ls', 'num' => 0, 'amount' => 0],
  61. 'pfJz' => ['name' => '批发结账', 'key' => 'pfJz', 'num' => 0, 'amount' => 0],
  62. 'lsWk' => ['name' => '零售尾款', 'key' => 'lsWk', 'num' => 0, 'amount' => 0],
  63. ],
  64. ],
  65. 'kfWx' => [
  66. 'amount' => 0,
  67. 'num' => 0,
  68. 'name' => '线下微信',
  69. 'class' => $kf,
  70. 'category' => [
  71. 'pf' => ['name' => '批发', 'key' => 'pf', 'num' => 0, 'amount' => 0],
  72. 'ls' => ['name' => '零售', 'key' => 'ls', 'num' => 0, 'amount' => 0],
  73. 'pfJz' => ['name' => '批发结账', 'key' => 'pfJz', 'num' => 0, 'amount' => 0],
  74. 'lsWk' => ['name' => '零售尾款', 'key' => 'lsWk', 'num' => 0, 'amount' => 0],
  75. ]
  76. ],
  77. 'debt' => [
  78. 'amount' => 0,
  79. 'num' => 0,
  80. 'name' => '挂账(未结)',
  81. 'category' => [
  82. 'pf' => ['name' => '批发', 'key' => 'pf', 'num' => 0, 'amount' => 0],
  83. 'ls' => ['name' => '零售', 'key' => 'ls', 'num' => 0, 'amount' => 0],
  84. ],
  85. ],
  86. 'debtClear' => [
  87. 'amount' => 0,
  88. 'num' => 0,
  89. 'name' => '挂账(已结)',
  90. 'category' => [
  91. 'pf' => ['name' => '批发', 'key' => 'pf', 'num' => 0, 'amount' => 0],
  92. 'ls' => ['name' => '零售', 'key' => 'ls', 'num' => 0, 'amount' => 0],
  93. ],
  94. ],
  95. 'balancePay' => [
  96. 'amount' => 0,
  97. 'name' => '余额支付',
  98. 'num' => 0,
  99. 'category' => [
  100. 'pf' => ['name' => '批发', 'key' => 'pf', 'num' => 0, 'amount' => 0],
  101. 'ls' => ['name' => '零售', 'key' => 'ls', 'num' => 0, 'amount' => 0],
  102. 'pfJz' => ['name' => '批发结账', 'key' => 'pfJz', 'num' => 0, 'amount' => 0],
  103. 'lsWk' => ['name' => '零售尾款', 'key' => 'lsWk', 'num' => 0, 'amount' => 0],
  104. ],
  105. ],
  106. 'cash' => [
  107. 'amount' => 0,
  108. 'name' => '现金',
  109. 'num' => 0,
  110. 'category' => [
  111. 'pf' => ['name' => '批发', 'key' => 'pf', 'num' => 0, 'amount' => 0],
  112. 'ls' => ['name' => '零售', 'key' => 'ls', 'num' => 0, 'amount' => 0],
  113. 'pfJz' => ['name' => '批发结账', 'key' => 'pfJz', 'num' => 0, 'amount' => 0],
  114. 'lsWk' => ['name' => '零售尾款', 'key' => 'lsWk', 'num' => 0, 'amount' => 0],
  115. ],
  116. ],
  117. 'aliPay' => [
  118. 'amount' => 0,
  119. 'num' => 0,
  120. 'name' => '线下支付宝',
  121. 'category' => [
  122. 'pf' => ['name' => '批发', 'key' => 'pf', 'num' => 0, 'amount' => 0],
  123. 'ls' => ['name' => '零售', 'key' => 'ls', 'num' => 0, 'amount' => 0],
  124. 'pfJz' => ['name' => '批发结账', 'key' => 'pfJz', 'num' => 0, 'amount' => 0],
  125. 'lsWk' => ['name' => '零售尾款', 'key' => 'lsWk', 'num' => 0, 'amount' => 0],
  126. ],
  127. ],
  128. 'bankCard' => [
  129. 'amount' => 0,
  130. 'name' => '银行卡',
  131. 'num' => 0,
  132. 'category' => [
  133. 'pf' => ['name' => '批发', 'key' => 'pf', 'num' => 0, 'amount' => 0],
  134. 'ls' => ['name' => '零售', 'key' => 'ls', 'num' => 0, 'amount' => 0],
  135. 'pfJz' => ['name' => '批发结账', 'key' => 'pfJz', 'num' => 0, 'amount' => 0],
  136. 'lsWk' => ['name' => '零售尾款', 'key' => 'lsWk', 'num' => 0, 'amount' => 0],
  137. ],
  138. ],
  139. 'mt' => ['amount' => 0, 'num' => 0, 'name' => '美团'],
  140. 'other' => [
  141. 'amount' => 0,
  142. 'num' => 0,
  143. 'name' => '其它',
  144. 'category' => [
  145. 'pf' => ['name' => '批发', 'key' => 'pf', 'num' => 0, 'amount' => 0],
  146. 'ls' => ['name' => '零售', 'key' => 'ls', 'num' => 0, 'amount' => 0],
  147. 'pfJz' => ['name' => '批发结账', 'key' => 'pfJz', 'num' => 0, 'amount' => 0],
  148. 'lsWk' => ['name' => '零售尾款', 'key' => 'lsWk', 'num' => 0, 'amount' => 0],
  149. ],
  150. ],
  151. ];
  152. $staffAmountList = [];
  153. //批发开单
  154. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  155. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  156. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  157. if (!empty($ghsOrderList)) {
  158. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  159. $payWay = $ghsOrder['payWay'] ?? 0;
  160. $actPrice = $ghsOrder['actPrice'] ?? 0;
  161. $getStaffId = $ghsOrder['getStaffId'] ?? 0;
  162. $debtPrice = $ghsOrder['debtPrice'] ?? 0;
  163. $remainDebtPrice = $ghsOrder['remainDebtPrice'] ?? 0;
  164. $onlinePay = $ghsOrder['onlinePay'] ?? dict::getDict('onlinePay', 'not');
  165. $status = $ghsOrder['status'] ?? 0;
  166. $book = $ghsOrder['book'] ?? 0;
  167. $staffId = $ghsOrder['shopAdminId'] ?? 0;
  168. $staffName = $ghsOrder['shopAdminName'] ?? '';
  169. if ($staffId == 0) {
  170. $staffName = '系统';
  171. }
  172. if ($status == 0 || $status == 5) {
  173. continue;
  174. }
  175. if ($book == 1 && $status == 2) {
  176. continue;
  177. }
  178. if (isset($staffAmountList[$staffId])) {
  179. $staffAmountList[$staffId]['num'] = bcadd($staffAmountList[$staffId]['num'], 1);
  180. $staffAmountList[$staffId]['amount'] = bcadd($staffAmountList[$staffId]['amount'], $actPrice, 2);
  181. } else {
  182. $staffAmountList[$staffId] = ['num' => 1, 'amount' => $actPrice, 'staffName' => $staffName, 'staffId' => $staffId];
  183. }
  184. if ($debtPrice > 0) {
  185. if ($remainDebtPrice > 0) {
  186. $incomeList['debt']['amount'] = bcadd($incomeList['debt']['amount'], $actPrice, 2);
  187. $incomeList['debt']['num'] = bcadd($incomeList['debt']['num'], 1);
  188. $incomeList['debt']['category']['pf']['amount'] = bcadd($incomeList['debt']['category']['pf']['amount'], $actPrice, 2);
  189. $incomeList['debt']['category']['pf']['num'] = bcadd($incomeList['debt']['category']['pf']['num'], 1);
  190. } else {
  191. $incomeList['debtClear']['amount'] = bcadd($incomeList['debtClear']['amount'], $actPrice, 2);
  192. $incomeList['debtClear']['num'] = bcadd($incomeList['debtClear']['num'], 1);
  193. $incomeList['debtClear']['category']['pf']['amount'] = bcadd($incomeList['debtClear']['category']['pf']['amount'], $actPrice, 2);
  194. $incomeList['debtClear']['category']['pf']['num'] = bcadd($incomeList['debtClear']['category']['pf']['num'], 1);
  195. }
  196. } else {
  197. if ($onlinePay == dict::getDict('onlinePay', 'yes')) {
  198. //使用在线支付
  199. $incomeList['system']['amount'] = bcadd($incomeList['system']['amount'], $actPrice, 2);
  200. $incomeList['system']['num'] = bcadd($incomeList['system']['num'], 1);
  201. $incomeList['system']['category']['pf']['amount'] = bcadd($incomeList['system']['category']['pf']['amount'], $actPrice, 2);
  202. $incomeList['system']['category']['pf']['num'] = bcadd($incomeList['system']['category']['pf']['num'], 1);
  203. } else {
  204. //使用非在线支付
  205. switch ($payWay) {
  206. case dict::getDict('payWay', 'wxPay'):
  207. //员工微信总共收了多少笔多少钱
  208. $incomeList['kfWx']['amount'] = bcadd($incomeList['kfWx']['amount'], $actPrice, 2);
  209. $incomeList['kfWx']['num'] = bcadd($incomeList['kfWx']['num'], 1);
  210. //批发开单、零售开单、批发结账、零售尾款各收了多少现金
  211. $incomeList['kfWx']['category']['pf']['amount'] = bcadd($incomeList['kfWx']['category']['pf']['amount'], $actPrice, 2);
  212. $incomeList['kfWx']['category']['pf']['num'] = bcadd($incomeList['kfWx']['category']['pf']['num'], 1);
  213. //每个客服总共收了多少笔多少钱
  214. if (isset($incomeList['kfWx']['class'][$getStaffId])) {
  215. $incomeList['kfWx']['class'][$getStaffId]['amount'] = floatval(bcadd($incomeList['kfWx']['class'][$getStaffId]['amount'], $actPrice, 2));
  216. $incomeList['kfWx']['class'][$getStaffId]['num'] = bcadd($incomeList['kfWx']['class'][$getStaffId]['num'], 1);
  217. }
  218. //每个客服收的明细
  219. if (isset($incomeList['kfWx']['class'][$getStaffId]['itemList']['pf'])) {
  220. $incomeList['kfWx']['class'][$getStaffId]['itemList']['pf']['amount'] = floatval(bcadd($incomeList['kfWx']['class'][$getStaffId]['itemList']['pf']['amount'], $actPrice, 2));
  221. $incomeList['kfWx']['class'][$getStaffId]['itemList']['pf']['num'] = bcadd($incomeList['kfWx']['class'][$getStaffId]['itemList']['pf']['num'], 1);
  222. }
  223. break;
  224. case dict::getDict('payWay', 'alipay'):
  225. $incomeList['aliPay']['amount'] = bcadd($incomeList['aliPay']['amount'], $actPrice, 2);
  226. $incomeList['aliPay']['num'] = bcadd($incomeList['aliPay']['num'], 1);
  227. $incomeList['aliPay']['category']['pf']['amount'] = bcadd($incomeList['aliPay']['category']['pf']['amount'], $actPrice, 2);
  228. $incomeList['aliPay']['category']['pf']['num'] = bcadd($incomeList['aliPay']['category']['pf']['num'], 1);
  229. break;
  230. case dict::getDict('payWay', 'cash'):
  231. $incomeList['cash']['amount'] = bcadd($incomeList['cash']['amount'], $actPrice, 2);
  232. $incomeList['cash']['num'] = bcadd($incomeList['cash']['num'], 1);
  233. $incomeList['cash']['category']['pf']['amount'] = bcadd($incomeList['cash']['category']['pf']['amount'], $actPrice, 2);
  234. $incomeList['cash']['category']['pf']['num'] = bcadd($incomeList['cash']['category']['pf']['num'], 1);
  235. break;
  236. case dict::getDict('payWay', 'balancePay'):
  237. $incomeList['balancePay']['amount'] = bcadd($incomeList['balancePay']['amount'], $actPrice, 2);
  238. $incomeList['balancePay']['num'] = bcadd($incomeList['balancePay']['num'], 1);
  239. $incomeList['balancePay']['category']['pf']['amount'] = bcadd($incomeList['balancePay']['category']['pf']['amount'], $actPrice, 2);
  240. $incomeList['balancePay']['category']['pf']['num'] = bcadd($incomeList['balancePay']['category']['pf']['num'], 1);
  241. break;
  242. case dict::getDict('payWay', 'bankCard'):
  243. $incomeList['bankCard']['amount'] = bcadd($incomeList['bankCard']['amount'], $actPrice, 2);
  244. $incomeList['bankCard']['num'] = bcadd($incomeList['bankCard']['num'], 1);
  245. $incomeList['bankCard']['category']['pf']['amount'] = bcadd($incomeList['bankCard']['category']['pf']['amount'], $actPrice, 2);
  246. $incomeList['bankCard']['category']['pf']['num'] = bcadd($incomeList['bankCard']['category']['pf']['num'], 1);
  247. break;
  248. case dict::getDict('payWay', 'unknown'):
  249. $incomeList['other']['amount'] = bcadd($incomeList['other']['amount'], $actPrice, 2);
  250. $incomeList['other']['num'] = bcadd($incomeList['other']['num'], 1);
  251. $incomeList['other']['category']['pf']['amount'] = bcadd($incomeList['other']['category']['pf']['amount'], $actPrice, 2);
  252. $incomeList['other']['category']['pf']['num'] = bcadd($incomeList['other']['category']['pf']['num'], 1);
  253. break;
  254. default:
  255. }
  256. }
  257. }
  258. }
  259. }
  260. //零售开单
  261. $payCodeIncome = 0;
  262. $lsAfterSale = 0;
  263. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  264. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  265. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  266. foreach ($hdOrderList as $hdKey => $hdOrder) {
  267. $payWay = $hdOrder['payWay'] ?? 0;
  268. $mainPay = $hdOrder['mainPay'] ?? 0;
  269. $cash = $hdOrder['cash'] ?? 0;
  270. $actPrice = $hdOrder['mainPay'] ?? 0;
  271. $getStaffId = $hdOrder['getStaffId'] ?? 0;
  272. $debtPrice = $hdOrder['debtPrice'] ?? 0;
  273. $remainDebtPrice = $hdOrder['remainDebtPrice'] ?? 0;
  274. $onlinePay = $hdOrder['onlinePay'] ?? 0;
  275. $fromType = $hdOrder['fromType'] ?? 0;
  276. $staffId = $hdOrder['shopAdminId'] ?? 0;
  277. $staffName = $hdOrder['shopAdminName'] ?? '';
  278. $repeat = $hdOrder['repeat'] ?? 0;
  279. $forward = $hdOrder['forward'] ?? 0;
  280. $tkPrice = $hdOrder['tkPrice'] ?? 0;
  281. $mainPay = bcsub($mainPay, $tkPrice, 2);
  282. if ($forward == 1) {
  283. //售后付款属于支出
  284. $lsAfterSale = bcadd($lsAfterSale, $mainPay, 2);
  285. continue;
  286. }
  287. $currentAmount = 0;
  288. if ($onlinePay == dict::getDict('onlinePay', 'yes')) {
  289. $incomeList['system']['amount'] = bcadd($incomeList['system']['amount'], $mainPay, 2);
  290. $incomeList['system']['num'] = bcadd($incomeList['system']['num'], 1);
  291. $incomeList['system']['category']['ls']['amount'] = bcadd($incomeList['system']['category']['ls']['amount'], $mainPay, 2);
  292. $incomeList['system']['category']['ls']['num'] = bcadd($incomeList['system']['category']['ls']['num'], 1);
  293. if ($repeat == 1) {
  294. $payCodeIncome = bcadd($payCodeIncome, $mainPay, 2);
  295. }
  296. if ($cash > 0) {
  297. $incomeList['cash']['amount'] = bcadd($incomeList['cash']['amount'], $cash, 2);
  298. $incomeList['cash']['num'] = bcadd($incomeList['cash']['num'], 1);
  299. $incomeList['cash']['category']['ls']['amount'] = bcadd($incomeList['cash']['category']['ls']['amount'], $cash, 2);
  300. $incomeList['cash']['category']['ls']['num'] = bcadd($incomeList['cash']['category']['ls']['num'], 1);
  301. }
  302. $currentAmount = bcadd($mainPay, $cash, 2);
  303. } else {
  304. if ($fromType == dict::getDict('fromType', 'mt')) {
  305. //美团!!!
  306. $incomeList['mt']['amount'] = bcadd($incomeList['mt']['amount'], $actPrice, 2);
  307. $incomeList['mt']['num'] = bcadd($incomeList['mt']['num'], 1);
  308. } else {
  309. if ($debtPrice > 0) {
  310. if ($remainDebtPrice > 0) {
  311. $incomeList['debt']['amount'] = bcadd($incomeList['debt']['amount'], $remainDebtPrice, 2);
  312. $incomeList['debt']['num'] = bcadd($incomeList['debt']['num'], 1);
  313. $incomeList['debt']['category']['ls']['amount'] = bcadd($incomeList['debt']['category']['ls']['amount'], $remainDebtPrice, 2);
  314. $incomeList['debt']['category']['ls']['num'] = bcadd($incomeList['debt']['category']['ls']['num'], 1);
  315. }
  316. $clearDebt = bcsub($debtPrice, $remainDebtPrice, 2);
  317. if ($clearDebt > 0) {
  318. $incomeList['debtClear']['amount'] = bcadd($incomeList['debtClear']['amount'], $clearDebt, 2);
  319. $incomeList['debtClear']['num'] = bcadd($incomeList['debtClear']['num'], 1);
  320. $incomeList['debtClear']['category']['ls']['amount'] = bcadd($incomeList['debtClear']['category']['ls']['amount'], $clearDebt, 2);
  321. $incomeList['debtClear']['category']['ls']['num'] = bcadd($incomeList['debtClear']['category']['ls']['num'], 1);
  322. }
  323. $currentAmount = bcadd($remainDebtPrice, $clearDebt, 2);
  324. } else {
  325. $currentAmount = $mainPay;
  326. }
  327. if ($currentAmount > 0) {
  328. //使用非在线支付
  329. switch ($payWay) {
  330. case dict::getDict('payWay', 'wxPay'):
  331. //员工微信总共收了多少笔多少钱
  332. $incomeList['kfWx']['amount'] = bcadd($incomeList['kfWx']['amount'], $currentAmount, 2);
  333. $incomeList['kfWx']['num'] = bcadd($incomeList['kfWx']['num'], 1);
  334. //批发开单、零售开单、批发结账、零售尾款各收了多少现金
  335. $incomeList['kfWx']['category']['ls']['amount'] = bcadd($incomeList['kfWx']['category']['ls']['amount'], $currentAmount, 2);
  336. $incomeList['kfWx']['category']['ls']['num'] = bcadd($incomeList['kfWx']['category']['ls']['num'], 1);
  337. //客1 客2 客3各收了多少钱
  338. if (isset($incomeList['kfWx']['class'][$getStaffId])) {
  339. $incomeList['kfWx']['class'][$getStaffId]['amount'] = bcadd($incomeList['kfWx']['class'][$getStaffId]['amount'], $currentAmount, 2);
  340. $incomeList['kfWx']['class'][$getStaffId]['num'] = bcadd($incomeList['kfWx']['class'][$getStaffId]['num'], 1);
  341. }
  342. //每个客服收的明细
  343. if (isset($incomeList['kfWx']['class'][$getStaffId]['itemList']['ls'])) {
  344. $incomeList['kfWx']['class'][$getStaffId]['itemList']['ls']['amount'] = floatval(bcadd($incomeList['kfWx']['class'][$getStaffId]['itemList']['ls']['amount'], $actPrice, 2));
  345. $incomeList['kfWx']['class'][$getStaffId]['itemList']['ls']['num'] = bcadd($incomeList['kfWx']['class'][$getStaffId]['itemList']['ls']['num'], 1);
  346. }
  347. break;
  348. case dict::getDict('payWay', 'alipay'):
  349. $incomeList['aliPay']['amount'] = bcadd($incomeList['aliPay']['amount'], $currentAmount, 2);
  350. $incomeList['aliPay']['num'] = bcadd($incomeList['aliPay']['num'], 1);
  351. $incomeList['aliPay']['category']['ls']['amount'] = bcadd($incomeList['aliPay']['category']['ls']['amount'], $currentAmount, 2);
  352. $incomeList['aliPay']['category']['ls']['num'] = bcadd($incomeList['aliPay']['category']['ls']['num'], 1);
  353. break;
  354. case dict::getDict('payWay', 'cash'):
  355. $incomeList['cash']['amount'] = bcadd($incomeList['cash']['amount'], $currentAmount, 2);
  356. $incomeList['cash']['num'] = bcadd($incomeList['cash']['num'], 1);
  357. $incomeList['cash']['category']['ls']['amount'] = bcadd($incomeList['cash']['category']['ls']['amount'], $currentAmount, 2);
  358. $incomeList['cash']['category']['ls']['num'] = bcadd($incomeList['cash']['category']['ls']['num'], 1);
  359. break;
  360. case dict::getDict('payWay', 'balancePay'):
  361. $incomeList['balancePay']['amount'] = bcadd($incomeList['balancePay']['amount'], $currentAmount, 2);
  362. $incomeList['balancePay']['num'] = bcadd($incomeList['balancePay']['num'], 1);
  363. $incomeList['balancePay']['category']['ls']['amount'] = bcadd($incomeList['balancePay']['category']['ls']['amount'], $currentAmount, 2);
  364. $incomeList['balancePay']['category']['ls']['num'] = bcadd($incomeList['balancePay']['category']['ls']['num'], 1);
  365. break;
  366. case dict::getDict('payWay', 'bankCard'):
  367. $incomeList['bankCard']['amount'] = bcadd($incomeList['bankCard']['amount'], $currentAmount, 2);
  368. $incomeList['bankCard']['num'] = bcadd($incomeList['bankCard']['num'], 1);
  369. $incomeList['bankCard']['category']['ls']['amount'] = bcadd($incomeList['bankCard']['category']['ls']['amount'], $currentAmount, 2);
  370. $incomeList['bankCard']['category']['ls']['num'] = bcadd($incomeList['bankCard']['category']['ls']['num'], 1);
  371. break;
  372. case dict::getDict('payWay', 'unknown'):
  373. $incomeList['other']['amount'] = bcadd($incomeList['other']['amount'], $currentAmount, 2);
  374. $incomeList['other']['num'] = bcadd($incomeList['other']['num'], 1);
  375. $incomeList['other']['category']['ls']['amount'] = bcadd($incomeList['other']['category']['ls']['amount'], $currentAmount, 2);
  376. $incomeList['other']['category']['ls']['num'] = bcadd($incomeList['other']['category']['ls']['num'], 1);
  377. break;
  378. default:
  379. }
  380. }
  381. }
  382. }
  383. //员工业绩
  384. if (isset($staffAmountList[$staffId])) {
  385. $staffAmountList[$staffId]['num'] = bcadd($staffAmountList[$staffId]['num'], 1);
  386. $staffAmountList[$staffId]['amount'] = bcadd($staffAmountList[$staffId]['amount'], $currentAmount, 2);
  387. } else {
  388. $staffAmountList[$staffId] = ['num' => 1, 'amount' => $currentAmount, 'staffName' => $staffName];
  389. }
  390. }
  391. // 隔天冲销:按原单入账渠道扣减金额(只减金额不减笔数)
  392. self::deductNextDayFromChannelIncome($incomeList, $mainId, $currentStartTime, $currentEndTime);
  393. return ['incomeList' => $incomeList, 'staffAmountList' => $staffAmountList, 'payCodeIncome' => $payCodeIncome, 'lsAfterSale' => $lsAfterSale];
  394. }
  395. /**
  396. * 渠道收入对冲隔天冲销金额(挂账已结扣 debtClear 等)
  397. */
  398. protected static function deductNextDayFromChannelIncome(&$incomeList, $mainId, $startTime, $endTime)
  399. {
  400. $rows = NextDayRefundService::listForChannelIncomeDeduct($mainId, $startTime, $endTime);
  401. if (empty($rows)) {
  402. return;
  403. }
  404. foreach ($rows as $row) {
  405. $amount = bcadd((string)($row['amount'] ?? '0'), '0', 2);
  406. if (bccomp($amount, '0', 2) <= 0) {
  407. continue;
  408. }
  409. $channel = self::resolveNextDayIncomeChannel($row);
  410. if (empty($channel) || !isset($incomeList[$channel])) {
  411. $channel = 'other';
  412. }
  413. $incomeList[$channel]['amount'] = bcsub((string)$incomeList[$channel]['amount'], $amount, 2);
  414. if (isset($incomeList[$channel]['category']['pf'])) {
  415. $incomeList[$channel]['category']['pf']['amount'] = bcsub(
  416. (string)$incomeList[$channel]['category']['pf']['amount'],
  417. $amount,
  418. 2
  419. );
  420. }
  421. }
  422. }
  423. /** 与批发开单入账分支一致,映射渠道 key;无原单退款按 fundType 归余额/其它 */
  424. protected static function resolveNextDayIncomeChannel($row)
  425. {
  426. $debtPrice = bcadd((string)($row['debtPrice'] ?? '0'), '0', 2);
  427. $remainDebt = bcadd((string)($row['remainDebtPrice'] ?? '0'), '0', 2);
  428. $orderId = intval($row['orderId'] ?? 0);
  429. $payWay = intval($row['orderPayWay'] ?? ($row['payWay'] ?? 0));
  430. $onlinePay = intval($row['onlinePay'] ?? dict::getDict('onlinePay', 'not'));
  431. // 无关联销售单:返余额记 balancePay,仅记账进 other
  432. if ($orderId <= 0) {
  433. $fundType = intval($row['fundType'] ?? 0);
  434. return $fundType === 2 ? 'balancePay' : 'other';
  435. }
  436. if ($orderId > 0 && bccomp($debtPrice, '0', 2) > 0) {
  437. return bccomp($remainDebt, '0', 2) > 0 ? 'debt' : 'debtClear';
  438. }
  439. if ($onlinePay == dict::getDict('onlinePay', 'yes')) {
  440. return 'system';
  441. }
  442. switch ($payWay) {
  443. case dict::getDict('payWay', 'wxPay'):
  444. return 'kfWx';
  445. case dict::getDict('payWay', 'alipay'):
  446. return 'aliPay';
  447. case dict::getDict('payWay', 'cash'):
  448. return 'cash';
  449. case dict::getDict('payWay', 'balancePay'):
  450. return 'balancePay';
  451. case dict::getDict('payWay', 'bankCard'):
  452. return 'bankCard';
  453. case dict::getDict('payWay', 'debtPay'):
  454. return 'debtClear';
  455. default:
  456. return 'other';
  457. }
  458. }
  459. }