StatKdClass.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. <?php
  2. namespace bizGhs\stat\classes;
  3. use biz\shop\classes\ShopAdminClass;
  4. use bizGhs\forward\classes\ForwardClass;
  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. //ssh 冲销单功能:金额(actPrice为负数)正常混入各渠道抵扣;但冲销单本质是售后调整,
  179. //不算一笔正常收款,所有"笔数(num)"统计都要排除冲销单,用 $numDelta 代替固定的 1
  180. $forward = $ghsOrder['forward'] ?? 0;
  181. $numDelta = ($forward == 1) ? 0 : 1;
  182. if (isset($staffAmountList[$staffId])) {
  183. $staffAmountList[$staffId]['num'] = bcadd($staffAmountList[$staffId]['num'], $numDelta);
  184. $staffAmountList[$staffId]['amount'] = bcadd($staffAmountList[$staffId]['amount'], $actPrice, 2);
  185. } else {
  186. $staffAmountList[$staffId] = ['num' => $numDelta, 'amount' => $actPrice, 'staffName' => $staffName, 'staffId' => $staffId];
  187. }
  188. if ($debtPrice > 0) {
  189. if ($remainDebtPrice > 0) {
  190. $incomeList['debt']['amount'] = bcadd($incomeList['debt']['amount'], $actPrice, 2);
  191. $incomeList['debt']['num'] = bcadd($incomeList['debt']['num'], $numDelta);
  192. $incomeList['debt']['category']['pf']['amount'] = bcadd($incomeList['debt']['category']['pf']['amount'], $actPrice, 2);
  193. $incomeList['debt']['category']['pf']['num'] = bcadd($incomeList['debt']['category']['pf']['num'], $numDelta);
  194. } else {
  195. $incomeList['debtClear']['amount'] = bcadd($incomeList['debtClear']['amount'], $actPrice, 2);
  196. $incomeList['debtClear']['num'] = bcadd($incomeList['debtClear']['num'], $numDelta);
  197. $incomeList['debtClear']['category']['pf']['amount'] = bcadd($incomeList['debtClear']['category']['pf']['amount'], $actPrice, 2);
  198. $incomeList['debtClear']['category']['pf']['num'] = bcadd($incomeList['debtClear']['category']['pf']['num'], $numDelta);
  199. }
  200. } else {
  201. if ($onlinePay == dict::getDict('onlinePay', 'yes')) {
  202. //使用在线支付
  203. $incomeList['system']['amount'] = bcadd($incomeList['system']['amount'], $actPrice, 2);
  204. $incomeList['system']['num'] = bcadd($incomeList['system']['num'], $numDelta);
  205. $incomeList['system']['category']['pf']['amount'] = bcadd($incomeList['system']['category']['pf']['amount'], $actPrice, 2);
  206. $incomeList['system']['category']['pf']['num'] = bcadd($incomeList['system']['category']['pf']['num'], $numDelta);
  207. } else {
  208. //使用非在线支付
  209. switch ($payWay) {
  210. case dict::getDict('payWay', 'wxPay'):
  211. //员工微信总共收了多少笔多少钱
  212. $incomeList['kfWx']['amount'] = bcadd($incomeList['kfWx']['amount'], $actPrice, 2);
  213. $incomeList['kfWx']['num'] = bcadd($incomeList['kfWx']['num'], $numDelta);
  214. //批发开单、零售开单、批发结账、零售尾款各收了多少现金
  215. $incomeList['kfWx']['category']['pf']['amount'] = bcadd($incomeList['kfWx']['category']['pf']['amount'], $actPrice, 2);
  216. $incomeList['kfWx']['category']['pf']['num'] = bcadd($incomeList['kfWx']['category']['pf']['num'], $numDelta);
  217. //每个客服总共收了多少笔多少钱
  218. if (isset($incomeList['kfWx']['class'][$getStaffId])) {
  219. $incomeList['kfWx']['class'][$getStaffId]['amount'] = floatval(bcadd($incomeList['kfWx']['class'][$getStaffId]['amount'], $actPrice, 2));
  220. $incomeList['kfWx']['class'][$getStaffId]['num'] = bcadd($incomeList['kfWx']['class'][$getStaffId]['num'], $numDelta);
  221. }
  222. //每个客服收的明细
  223. if (isset($incomeList['kfWx']['class'][$getStaffId]['itemList']['pf'])) {
  224. $incomeList['kfWx']['class'][$getStaffId]['itemList']['pf']['amount'] = floatval(bcadd($incomeList['kfWx']['class'][$getStaffId]['itemList']['pf']['amount'], $actPrice, 2));
  225. $incomeList['kfWx']['class'][$getStaffId]['itemList']['pf']['num'] = bcadd($incomeList['kfWx']['class'][$getStaffId]['itemList']['pf']['num'], $numDelta);
  226. }
  227. break;
  228. case dict::getDict('payWay', 'alipay'):
  229. $incomeList['aliPay']['amount'] = bcadd($incomeList['aliPay']['amount'], $actPrice, 2);
  230. $incomeList['aliPay']['num'] = bcadd($incomeList['aliPay']['num'], $numDelta);
  231. $incomeList['aliPay']['category']['pf']['amount'] = bcadd($incomeList['aliPay']['category']['pf']['amount'], $actPrice, 2);
  232. $incomeList['aliPay']['category']['pf']['num'] = bcadd($incomeList['aliPay']['category']['pf']['num'], $numDelta);
  233. break;
  234. case dict::getDict('payWay', 'cash'):
  235. $incomeList['cash']['amount'] = bcadd($incomeList['cash']['amount'], $actPrice, 2);
  236. $incomeList['cash']['num'] = bcadd($incomeList['cash']['num'], $numDelta);
  237. $incomeList['cash']['category']['pf']['amount'] = bcadd($incomeList['cash']['category']['pf']['amount'], $actPrice, 2);
  238. $incomeList['cash']['category']['pf']['num'] = bcadd($incomeList['cash']['category']['pf']['num'], $numDelta);
  239. break;
  240. case dict::getDict('payWay', 'balancePay'):
  241. $incomeList['balancePay']['amount'] = bcadd($incomeList['balancePay']['amount'], $actPrice, 2);
  242. $incomeList['balancePay']['num'] = bcadd($incomeList['balancePay']['num'], $numDelta);
  243. $incomeList['balancePay']['category']['pf']['amount'] = bcadd($incomeList['balancePay']['category']['pf']['amount'], $actPrice, 2);
  244. $incomeList['balancePay']['category']['pf']['num'] = bcadd($incomeList['balancePay']['category']['pf']['num'], $numDelta);
  245. break;
  246. case dict::getDict('payWay', 'bankCard'):
  247. $incomeList['bankCard']['amount'] = bcadd($incomeList['bankCard']['amount'], $actPrice, 2);
  248. $incomeList['bankCard']['num'] = bcadd($incomeList['bankCard']['num'], $numDelta);
  249. $incomeList['bankCard']['category']['pf']['amount'] = bcadd($incomeList['bankCard']['category']['pf']['amount'], $actPrice, 2);
  250. $incomeList['bankCard']['category']['pf']['num'] = bcadd($incomeList['bankCard']['category']['pf']['num'], $numDelta);
  251. break;
  252. case dict::getDict('payWay', 'unknown'):
  253. $incomeList['other']['amount'] = bcadd($incomeList['other']['amount'], $actPrice, 2);
  254. $incomeList['other']['num'] = bcadd($incomeList['other']['num'], $numDelta);
  255. $incomeList['other']['category']['pf']['amount'] = bcadd($incomeList['other']['category']['pf']['amount'], $actPrice, 2);
  256. $incomeList['other']['category']['pf']['num'] = bcadd($incomeList['other']['category']['pf']['num'], $numDelta);
  257. break;
  258. default:
  259. }
  260. }
  261. }
  262. }
  263. }
  264. //零售开单
  265. $payCodeIncome = 0;
  266. $lsAfterSale = 0;
  267. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  268. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  269. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  270. foreach ($hdOrderList as $hdKey => $hdOrder) {
  271. $payWay = $hdOrder['payWay'] ?? 0;
  272. $mainPay = $hdOrder['mainPay'] ?? 0;
  273. $cash = $hdOrder['cash'] ?? 0;
  274. $actPrice = $hdOrder['mainPay'] ?? 0;
  275. $getStaffId = $hdOrder['getStaffId'] ?? 0;
  276. $debtPrice = $hdOrder['debtPrice'] ?? 0;
  277. $remainDebtPrice = $hdOrder['remainDebtPrice'] ?? 0;
  278. $onlinePay = $hdOrder['onlinePay'] ?? 0;
  279. $fromType = $hdOrder['fromType'] ?? 0;
  280. $staffId = $hdOrder['shopAdminId'] ?? 0;
  281. $staffName = $hdOrder['shopAdminName'] ?? '';
  282. $repeat = $hdOrder['repeat'] ?? 0;
  283. $forward = $hdOrder['forward'] ?? 0;
  284. $tkPrice = $hdOrder['tkPrice'] ?? 0;
  285. $mainPay = bcsub($mainPay, $tkPrice, 2);
  286. if ($forward == 1) {
  287. //售后付款属于支出
  288. $lsAfterSale = bcadd($lsAfterSale, $mainPay, 2);
  289. continue;
  290. }
  291. $currentAmount = 0;
  292. if ($onlinePay == dict::getDict('onlinePay', 'yes')) {
  293. $incomeList['system']['amount'] = bcadd($incomeList['system']['amount'], $mainPay, 2);
  294. $incomeList['system']['num'] = bcadd($incomeList['system']['num'], 1);
  295. $incomeList['system']['category']['ls']['amount'] = bcadd($incomeList['system']['category']['ls']['amount'], $mainPay, 2);
  296. $incomeList['system']['category']['ls']['num'] = bcadd($incomeList['system']['category']['ls']['num'], 1);
  297. if ($repeat == 1) {
  298. $payCodeIncome = bcadd($payCodeIncome, $mainPay, 2);
  299. }
  300. if ($cash > 0) {
  301. $incomeList['cash']['amount'] = bcadd($incomeList['cash']['amount'], $cash, 2);
  302. $incomeList['cash']['num'] = bcadd($incomeList['cash']['num'], 1);
  303. $incomeList['cash']['category']['ls']['amount'] = bcadd($incomeList['cash']['category']['ls']['amount'], $cash, 2);
  304. $incomeList['cash']['category']['ls']['num'] = bcadd($incomeList['cash']['category']['ls']['num'], 1);
  305. }
  306. $currentAmount = bcadd($mainPay, $cash, 2);
  307. } else {
  308. if ($fromType == dict::getDict('fromType', 'mt')) {
  309. //美团!!!
  310. $incomeList['mt']['amount'] = bcadd($incomeList['mt']['amount'], $actPrice, 2);
  311. $incomeList['mt']['num'] = bcadd($incomeList['mt']['num'], 1);
  312. } else {
  313. if ($debtPrice > 0) {
  314. if ($remainDebtPrice > 0) {
  315. $incomeList['debt']['amount'] = bcadd($incomeList['debt']['amount'], $remainDebtPrice, 2);
  316. $incomeList['debt']['num'] = bcadd($incomeList['debt']['num'], 1);
  317. $incomeList['debt']['category']['ls']['amount'] = bcadd($incomeList['debt']['category']['ls']['amount'], $remainDebtPrice, 2);
  318. $incomeList['debt']['category']['ls']['num'] = bcadd($incomeList['debt']['category']['ls']['num'], 1);
  319. }
  320. $clearDebt = bcsub($debtPrice, $remainDebtPrice, 2);
  321. if ($clearDebt > 0) {
  322. $incomeList['debtClear']['amount'] = bcadd($incomeList['debtClear']['amount'], $clearDebt, 2);
  323. $incomeList['debtClear']['num'] = bcadd($incomeList['debtClear']['num'], 1);
  324. $incomeList['debtClear']['category']['ls']['amount'] = bcadd($incomeList['debtClear']['category']['ls']['amount'], $clearDebt, 2);
  325. $incomeList['debtClear']['category']['ls']['num'] = bcadd($incomeList['debtClear']['category']['ls']['num'], 1);
  326. }
  327. $currentAmount = bcadd($remainDebtPrice, $clearDebt, 2);
  328. } else {
  329. $currentAmount = $mainPay;
  330. }
  331. if ($currentAmount > 0) {
  332. //使用非在线支付
  333. switch ($payWay) {
  334. case dict::getDict('payWay', 'wxPay'):
  335. //员工微信总共收了多少笔多少钱
  336. $incomeList['kfWx']['amount'] = bcadd($incomeList['kfWx']['amount'], $currentAmount, 2);
  337. $incomeList['kfWx']['num'] = bcadd($incomeList['kfWx']['num'], 1);
  338. //批发开单、零售开单、批发结账、零售尾款各收了多少现金
  339. $incomeList['kfWx']['category']['ls']['amount'] = bcadd($incomeList['kfWx']['category']['ls']['amount'], $currentAmount, 2);
  340. $incomeList['kfWx']['category']['ls']['num'] = bcadd($incomeList['kfWx']['category']['ls']['num'], 1);
  341. //客1 客2 客3各收了多少钱
  342. if (isset($incomeList['kfWx']['class'][$getStaffId])) {
  343. $incomeList['kfWx']['class'][$getStaffId]['amount'] = bcadd($incomeList['kfWx']['class'][$getStaffId]['amount'], $currentAmount, 2);
  344. $incomeList['kfWx']['class'][$getStaffId]['num'] = bcadd($incomeList['kfWx']['class'][$getStaffId]['num'], 1);
  345. }
  346. //每个客服收的明细
  347. if (isset($incomeList['kfWx']['class'][$getStaffId]['itemList']['ls'])) {
  348. $incomeList['kfWx']['class'][$getStaffId]['itemList']['ls']['amount'] = floatval(bcadd($incomeList['kfWx']['class'][$getStaffId]['itemList']['ls']['amount'], $actPrice, 2));
  349. $incomeList['kfWx']['class'][$getStaffId]['itemList']['ls']['num'] = bcadd($incomeList['kfWx']['class'][$getStaffId]['itemList']['ls']['num'], 1);
  350. }
  351. break;
  352. case dict::getDict('payWay', 'alipay'):
  353. $incomeList['aliPay']['amount'] = bcadd($incomeList['aliPay']['amount'], $currentAmount, 2);
  354. $incomeList['aliPay']['num'] = bcadd($incomeList['aliPay']['num'], 1);
  355. $incomeList['aliPay']['category']['ls']['amount'] = bcadd($incomeList['aliPay']['category']['ls']['amount'], $currentAmount, 2);
  356. $incomeList['aliPay']['category']['ls']['num'] = bcadd($incomeList['aliPay']['category']['ls']['num'], 1);
  357. break;
  358. case dict::getDict('payWay', 'cash'):
  359. $incomeList['cash']['amount'] = bcadd($incomeList['cash']['amount'], $currentAmount, 2);
  360. $incomeList['cash']['num'] = bcadd($incomeList['cash']['num'], 1);
  361. $incomeList['cash']['category']['ls']['amount'] = bcadd($incomeList['cash']['category']['ls']['amount'], $currentAmount, 2);
  362. $incomeList['cash']['category']['ls']['num'] = bcadd($incomeList['cash']['category']['ls']['num'], 1);
  363. break;
  364. case dict::getDict('payWay', 'balancePay'):
  365. $incomeList['balancePay']['amount'] = bcadd($incomeList['balancePay']['amount'], $currentAmount, 2);
  366. $incomeList['balancePay']['num'] = bcadd($incomeList['balancePay']['num'], 1);
  367. $incomeList['balancePay']['category']['ls']['amount'] = bcadd($incomeList['balancePay']['category']['ls']['amount'], $currentAmount, 2);
  368. $incomeList['balancePay']['category']['ls']['num'] = bcadd($incomeList['balancePay']['category']['ls']['num'], 1);
  369. break;
  370. case dict::getDict('payWay', 'bankCard'):
  371. $incomeList['bankCard']['amount'] = bcadd($incomeList['bankCard']['amount'], $currentAmount, 2);
  372. $incomeList['bankCard']['num'] = bcadd($incomeList['bankCard']['num'], 1);
  373. $incomeList['bankCard']['category']['ls']['amount'] = bcadd($incomeList['bankCard']['category']['ls']['amount'], $currentAmount, 2);
  374. $incomeList['bankCard']['category']['ls']['num'] = bcadd($incomeList['bankCard']['category']['ls']['num'], 1);
  375. break;
  376. case dict::getDict('payWay', 'unknown'):
  377. $incomeList['other']['amount'] = bcadd($incomeList['other']['amount'], $currentAmount, 2);
  378. $incomeList['other']['num'] = bcadd($incomeList['other']['num'], 1);
  379. $incomeList['other']['category']['ls']['amount'] = bcadd($incomeList['other']['category']['ls']['amount'], $currentAmount, 2);
  380. $incomeList['other']['category']['ls']['num'] = bcadd($incomeList['other']['category']['ls']['num'], 1);
  381. break;
  382. default:
  383. }
  384. }
  385. }
  386. }
  387. //员工业绩
  388. if (isset($staffAmountList[$staffId])) {
  389. $staffAmountList[$staffId]['num'] = bcadd($staffAmountList[$staffId]['num'], 1);
  390. $staffAmountList[$staffId]['amount'] = bcadd($staffAmountList[$staffId]['amount'], $currentAmount, 2);
  391. } else {
  392. $staffAmountList[$staffId] = ['num' => 1, 'amount' => $currentAmount, 'staffName' => $staffName];
  393. }
  394. }
  395. // 方案A:按原单入账渠道扣减成功冲销金额(只减金额不减笔数;挂账已结扣 debtClear)
  396. self::deductForwardFromChannelIncome($incomeList, $mainId, $currentStartTime, $currentEndTime);
  397. return ['incomeList' => $incomeList, 'staffAmountList' => $staffAmountList, 'payCodeIncome' => $payCodeIncome, 'lsAfterSale' => $lsAfterSale];
  398. }
  399. /**
  400. * 渠道收入对冲冲销:按原销售单当时计入的渠道扣金额。
  401. * 挂账已结 → debtClear;挂账未结 → debt;在线付 → system;线下按 payWay。
  402. * 自由冲销无原单时按凭证 payWay(多为余额)扣,避免误进系统收款。
  403. */
  404. protected static function deductForwardFromChannelIncome(&$incomeList, $mainId, $startTime, $endTime)
  405. {
  406. $rows = ForwardClass::listForChannelIncomeDeduct($mainId, $startTime, $endTime);
  407. if (empty($rows)) {
  408. return;
  409. }
  410. foreach ($rows as $row) {
  411. $amount = bcadd((string)($row['amount'] ?? '0'), '0', 2);
  412. if (bccomp($amount, '0', 2) <= 0) {
  413. continue;
  414. }
  415. $channel = self::resolveForwardIncomeChannel($row);
  416. if (empty($channel) || !isset($incomeList[$channel])) {
  417. $channel = 'other';
  418. }
  419. $incomeList[$channel]['amount'] = bcsub((string)$incomeList[$channel]['amount'], $amount, 2);
  420. // 批发冲销明细落在 pf;无 category 的渠道(如美团)只扣总额
  421. if (isset($incomeList[$channel]['category']['pf'])) {
  422. $incomeList[$channel]['category']['pf']['amount'] = bcsub(
  423. (string)$incomeList[$channel]['category']['pf']['amount'],
  424. $amount,
  425. 2
  426. );
  427. }
  428. }
  429. }
  430. /**
  431. * 根据冲销凭证 + 关联原单字段,映射到 kdIncome 渠道 key(与批发开单入账分支一致)
  432. */
  433. protected static function resolveForwardIncomeChannel($row)
  434. {
  435. $debtPrice = bcadd((string)($row['debtPrice'] ?? '0'), '0', 2);
  436. $remainDebt = bcadd((string)($row['remainDebtPrice'] ?? '0'), '0', 2);
  437. $orderId = intval($row['orderId'] ?? 0);
  438. $payWay = intval($row['orderPayWay'] ?? ($row['payWay'] ?? 0));
  439. if ($orderId <= 0) {
  440. $payWay = intval($row['payWay'] ?? 0);
  441. }
  442. $onlinePay = intval($row['onlinePay'] ?? dict::getDict('onlinePay', 'not'));
  443. // 有挂账标记的原单:已结清进「挂账已结」,未结进「挂账未结」
  444. if ($orderId > 0 && bccomp($debtPrice, '0', 2) > 0) {
  445. if (bccomp($remainDebt, '0', 2) > 0) {
  446. return 'debt';
  447. }
  448. return 'debtClear';
  449. }
  450. if ($onlinePay == dict::getDict('onlinePay', 'yes')) {
  451. return 'system';
  452. }
  453. switch ($payWay) {
  454. case dict::getDict('payWay', 'wxPay'):
  455. return 'kfWx';
  456. case dict::getDict('payWay', 'alipay'):
  457. return 'aliPay';
  458. case dict::getDict('payWay', 'cash'):
  459. return 'cash';
  460. case dict::getDict('payWay', 'balancePay'):
  461. return 'balancePay';
  462. case dict::getDict('payWay', 'bankCard'):
  463. return 'bankCard';
  464. case dict::getDict('payWay', 'debtPay'):
  465. // 凭证记欠款但原单无 debtPrice 时,按已结挂账对冲(冲销前提多为已结清)
  466. return 'debtClear';
  467. case dict::getDict('payWay', 'unknown'):
  468. return 'other';
  469. default:
  470. return 'other';
  471. }
  472. }
  473. }