StatKdClass.php 31 KB

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