StatKdClass.php 31 KB

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