OrderClearClass.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. namespace bizGhs\order\classes;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\shop\classes\ShopClass;
  5. use bizGhs\custom\classes\CustomClass;
  6. use bizGhs\shop\classes\MainClass;
  7. use bizGhs\shop\classes\ShopMoneyChangeClass;
  8. use common\components\dict;
  9. use common\components\noticeUtil;
  10. use common\components\orderSn;
  11. use common\components\util;
  12. use bizGhs\base\classes\BaseClass;
  13. use bizHd\purchase\classes\PurchaseClass;
  14. use bizHd\purchase\classes\PurchaseClearClass;
  15. class OrderClearClass extends BaseClass
  16. {
  17. public static $baseFile = '\bizGhs\order\models\OrderClear';
  18. //使用余额自助结欠款 ssh 20240310
  19. public static function useBalanceClear($custom, $ghs, $shop, $staff)
  20. {
  21. $staffId = $staff->id ?? 0;
  22. $staffName = $staff->name ?? '';
  23. $shopId = $shop->id ?? 0;
  24. $sjId = $shop->sjId ?? 0;
  25. $customId = $custom->id ?? 0;
  26. $ghsId = $ghs->id ?? 0;
  27. $customBalance = $custom->balance ?? 0;
  28. $ghsBalance = $ghs->balance ?? 0;
  29. if (floatval($customBalance) != floatval($ghsBalance)) {
  30. noticeUtil::push("客户{$customId} {$customBalance} 供货商{$ghsId} {$ghsBalance} 的余额有问题", '15280215347');
  31. util::fail('系统故障');
  32. }
  33. if ($customBalance < 0) {
  34. noticeUtil::push("客户{$customId} 供货商{$ghsId} 的余额小于0", '15280215347');
  35. util::fail('系统故障哦');
  36. }
  37. if (floatval($customBalance) == 0) {
  38. return true;
  39. }
  40. $where = ['customId' => $customId, 'debt' => 1];
  41. $orderList = OrderClass::getAllByCondition($where, 'id asc', '*', null, true);
  42. if (empty($orderList)) {
  43. return true;
  44. }
  45. $totalAmount = 0;
  46. $need = [];
  47. foreach ($orderList as $order) {
  48. $orderId = $order->id ?? 0;
  49. $remainDebtPrice = $order->remainDebtPrice ?? 0;
  50. $currentTotal = bcadd($totalAmount, $remainDebtPrice, 2);
  51. if ($currentTotal > $customBalance) {
  52. if (getenv('YII_ENV') == 'production') {
  53. //小向需要先结旧账
  54. if (in_array($shop->mainId, [23390])) {
  55. break;
  56. } else {
  57. continue;
  58. }
  59. } else {
  60. if (in_array($shop->mainId, [])) {
  61. break;
  62. } else {
  63. continue;
  64. }
  65. }
  66. }
  67. $totalAmount = bcadd($totalAmount, $remainDebtPrice, 2);
  68. $need[] = ['id' => $orderId];
  69. }
  70. if (empty($need)) {
  71. return true;
  72. }
  73. $where = ['customId' => $customId, 'status' => PurchaseClearClass::STATUS_AWAIT_PAY];
  74. $list = OrderClearClass::getAllByCondition($where, null, '*', null, true);
  75. if (!empty($list)) {
  76. foreach ($list as $item) {
  77. //只要新建账单,老账单就取消掉
  78. $item->status = PurchaseClearClass::STATUS_EXPIRE;
  79. $item->save();
  80. }
  81. }
  82. $idText = json_encode($need);
  83. $clearData = [
  84. 'customId' => $customId,
  85. 'ghsShopAdminId' => $staffId,
  86. 'ghsShopId' => $shopId,
  87. 'ghsShopAdminName' => $staffName,
  88. 'modifyPrice' => $totalAmount,
  89. 'payWay' => 0,
  90. 'complete' => 1,
  91. 'remark' => '充值余额结账',
  92. ];
  93. $clearInfo = self::clear($clearData, $idText, $sjId, $shopId);
  94. //结账消费余额
  95. CustomClass::clearConsumeBalance($totalAmount, $custom, $ghs, $shop, $staff, $clearInfo);
  96. }
  97. //订单结算 ssh 2021.3.14
  98. public static function clear($post, $idText, $sjId, $shopId)
  99. {
  100. $customId = $post['customId'] ?? 0;
  101. $data = json_decode($idText, true);
  102. $ids = array_column($data, 'id');
  103. if (empty($ids)) {
  104. util::fail('请选择交易记录');
  105. }
  106. $list = OrderClass::getByIds($ids);
  107. if (empty($list)) {
  108. util::fail('请选择订单');
  109. }
  110. $amount = 0;
  111. $modifyPrice = $post['modifyPrice'] ?? 0.00;
  112. $purchaseArr = [];
  113. foreach ($list as $key => $val) {
  114. if ($val['sjId'] != $sjId) {
  115. util::fail('只能结算自己的订单哦');
  116. }
  117. if ($val['debt'] == 0) {
  118. util::fail('有订单已经结算过了');
  119. }
  120. if ($val['customId'] != $customId) {
  121. util::fail('每次只能结算一个客户的订单');
  122. }
  123. $amount = bcadd($amount, $val['remainDebtPrice'], 2);
  124. $purchaseId = $val['purchaseId'] ?? 0;
  125. if (!empty($purchaseId)) {
  126. $purchaseArr[] = $purchaseId;
  127. }
  128. }
  129. $purchaseString = '';
  130. if (!empty($purchaseArr)) {
  131. $purchaseString = implode(',', $purchaseArr);
  132. }
  133. $custom = CustomClass::getLockById($customId);
  134. if (empty($custom)) {
  135. util::fail('没有找到客户');
  136. }
  137. $ghsId = $custom->ghsId;
  138. $ghs = GhsClass::getLockById($ghsId);
  139. if (empty($ghs)) {
  140. util::fail('没有找到供货商');
  141. }
  142. $ghsShopId = $ghs['shopId'] ?? 0;
  143. $ghsShop = ShopClass::getById($ghsShopId, true);
  144. $ghsShopName = $ghsShop->shopName ?? '';
  145. $ghsShopName = $ghsShopName == '首店' ? '总店' : $ghsShopName;
  146. $orderSn = orderSn::getPurchaseClearSn();
  147. //默认24小时过期
  148. $deadline = date("Y-m-d H:i:s", time() + 24 * 60 * 60);
  149. //天天鲜花12小时之后
  150. if ($ghsShopId == 763 || $ghsShopId == 795 || $ghsShopId == 1405) {
  151. $deadline = date("Y-m-d H:i:s", time() + 10 * 60 * 60);
  152. }
  153. //开发环境10个小时过期
  154. if (getenv('YII_ENV') != 'production') {
  155. $deadline = date("Y-m-d H:i:s", time() + 10 * 60 * 60);
  156. }
  157. //惠雅鲜花 结账单过期时间设置为30天
  158. if ($ghsShopId == 520 || $ghsShopId == 1489 || $ghsShopId == 2164 || $ghsShopId == 2167) {
  159. $deadline = date("Y-m-d H:i:s", time() + 30 * 24 * 60 * 60);
  160. }
  161. //昱成花卉 结账单过期时间设置为30天
  162. if ($ghsShopId == 8596) {
  163. $deadline = date("Y-m-d H:i:s", time() + 30 * 24 * 60 * 60);
  164. }
  165. //花大苪 结账单过期时间设置为30天
  166. if ($ghsShopId == 8249) {
  167. $deadline = date("Y-m-d H:i:s", time() + 30 * 24 * 60 * 60);
  168. }
  169. //丰行鲜花 结账单过期时间设置为30天
  170. if ($ghsShopId == 7855) {
  171. $deadline = date("Y-m-d H:i:s", time() + 30 * 24 * 60 * 60);
  172. }
  173. $payWay = isset($post['payWay']) && is_numeric($post['payWay']) ? $post['payWay'] : dict::getDict('payWay', 'unPay');
  174. $remark = $post['remark'] ?? '';
  175. $clearData = [
  176. 'prePrice' => $amount,
  177. 'actPrice' => $modifyPrice,
  178. 'realPrice' => $modifyPrice,
  179. 'ghsId' => $ghsId,
  180. 'ghsName' => $ghs['name'] ?? '',
  181. 'ghsMobile' => $ghs['mobile'] ?? '',
  182. 'ghsAvatar' => $ghs['avatar'] ?? '',
  183. 'ghsAddress' => $ghs['address'] ?? '',
  184. 'ghsShopAdminId' => $post['ghsShopAdminId'],
  185. 'ghsShopId' => $post['ghsShopId'] ?? 0,
  186. 'ghsShopAdminName' => $post['ghsShopAdminName'],
  187. 'sjId' => $sjId,
  188. 'shopId' => $shopId,
  189. 'orderSn' => $orderSn,
  190. 'saleIds' => implode(',', $ids),
  191. 'status' => 1,
  192. 'clearStyle' => dict::getDict('clearStyle', 'gys2Hd'),
  193. 'customId' => $customId,
  194. 'customName' => $custom['name'] ?? '',
  195. 'customShopId' => $custom['shopId'] ?? 0,
  196. 'customAvatar' => $custom['avatar'] ?? '',
  197. 'customMobile' => $custom['mobile'] ?? '',
  198. 'customAddress' => $custom['address'] ?? '',
  199. 'deadline' => $deadline,
  200. 'purchaseIds' => $purchaseString,
  201. 'payWay' => $payWay,
  202. 'num' => count($ids),
  203. 'ghsShopName' => $ghsShopName,
  204. 'hasNoticeCustom' => 0,
  205. 'remark' => $remark,
  206. ];
  207. if ($modifyPrice < $amount) {
  208. $clearData['discountAmount'] = bcsub($amount, $modifyPrice, 2);
  209. $clearData['discountType'] = dict::getDict('discountType', 'discount');
  210. }
  211. if ($modifyPrice > $amount) {
  212. //noticeUtil::push("结账金额大于总欠款金额 {$modifyPrice} {$amount}", '15280215347');
  213. util::fail('结账金额大于总欠款金额');
  214. }
  215. $return = PurchaseClearClass::add($clearData, true);
  216. $complete = $post['complete'] ?? 0;
  217. if ($complete == 1) {
  218. self::confirmClear($return, $payWay);
  219. }
  220. return $return;
  221. }
  222. //确认结账 ssh 20220510
  223. public static function confirmClear($clear, $payWay)
  224. {
  225. if ($clear->status == 2) {
  226. util::fail('已结过账了');
  227. }
  228. if ($clear->status == 3) {
  229. util::fail('已取消了');
  230. }
  231. $current = time();
  232. if ($current >= strtotime($clear->deadline)) {
  233. util::fail('已过期,请手动取消');
  234. }
  235. $clear->payWay = $payWay;
  236. $clear->payTime = date("Y-m-d H:i:s");
  237. $clear->status = 2;
  238. $clear->save();
  239. $clearId = $clear->id ?? 0;
  240. $customId = $clear->customId ?? 0;
  241. $orderSn = $clear->orderSn ?? '';
  242. $customName = $clear->customName ?? '';
  243. $custom = CustomClass::getLockById($customId);
  244. if (empty($custom)) {
  245. util::fail('没有找到客户');
  246. }
  247. $ghsId = $custom->ghsId;
  248. $ghs = GhsClass::getLockById($ghsId);
  249. if (empty($ghs)) {
  250. util::fail('没有找到供货商');
  251. }
  252. $amount = $clear->prePrice ?? 0;
  253. $saleIds = $clear->saleIds ?? '';
  254. $ids = explode(',', trim($saleIds));
  255. if (empty($ids)) {
  256. util::fail('数据错误');
  257. }
  258. $purchaseIds = $clear->purchaseIds ?? '';
  259. $purchaseArr = explode(',', $purchaseIds);
  260. if (empty($purchaseArr)) {
  261. util::fail('数据错误');
  262. }
  263. //客户欠款减少
  264. CustomClass::clearDebtAmountReduce($custom, $ghs, $amount, $clear);
  265. $clearTime = date("Y-m-d H:i:s");
  266. // 销售单状态变更
  267. OrderClass::updateByIds($ids, ['debt' => 0, 'clearId' => $clearId, 'remainDebtPrice' => 0, 'clearTime' => $clearTime]);
  268. // 采购单状态变更
  269. PurchaseClass::updateByIds($purchaseArr, ['debt' => 2, 'clearId' => $clearId, 'remainDebtPrice' => 0, 'clearTime' => $clearTime]);
  270. //供货商结账欠款减少
  271. \bizHd\ghs\classes\GhsClass::clearDebtAmountReduce($ghs, $custom, $amount, $clear);
  272. //现金余额增加
  273. if ($payWay == dict::getDict('payWay', 'cash')) {
  274. $mainId = $ghs->mainId ?? 0;
  275. $main = MainClass::getLockById($mainId);
  276. if (empty($main)) {
  277. util::fail('没有商家信息');
  278. }
  279. $moneyBalance = bcadd($main->money, $amount, 2);
  280. $main->money = $moneyBalance;
  281. $main->save();
  282. $moneyEvent = $customName . "现金结账" . floatval($amount) . "元,单号 {$orderSn})";
  283. $moneyRemark = '';
  284. $capitalType = dict::getDict('capitalType', 'ghsXsClear', 'id');
  285. $change = [
  286. 'relateId' => $clearId,
  287. 'amount' => $amount,
  288. 'balance' => $moneyBalance,
  289. 'io' => 1,
  290. 'mainId' => $mainId,
  291. 'capitalType' => $capitalType,
  292. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  293. 'event' => $moneyEvent,
  294. 'remark' => $moneyRemark,
  295. ];
  296. ShopMoneyChangeClass::addData($change);
  297. }
  298. }
  299. }