OrderClearClass.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 common\components\dict;
  7. use common\components\noticeUtil;
  8. use common\components\orderSn;
  9. use common\components\util;
  10. use bizGhs\base\classes\BaseClass;
  11. use bizHd\purchase\classes\PurchaseClass;
  12. use bizHd\purchase\classes\PurchaseClearClass;
  13. class OrderClearClass extends BaseClass
  14. {
  15. public static $baseFile = '\bizGhs\order\models\OrderClear';
  16. //订单结算 ssh 2021.3.14
  17. public static function clear($post, $idText, $sjId, $shopId)
  18. {
  19. $customId = $post['customId'] ?? 0;
  20. $data = json_decode($idText, true);
  21. $ids = array_column($data, 'id');
  22. if (empty($ids)) {
  23. util::fail('请选择交易记录');
  24. }
  25. $list = OrderClass::getByIds($ids);
  26. if (empty($list)) {
  27. util::fail('请选择订单');
  28. }
  29. $amount = 0;
  30. $modifyPrice = $post['modifyPrice'] ?? 0.00;
  31. $purchaseArr = [];
  32. foreach ($list as $key => $val) {
  33. if ($val['sjId'] != $sjId) {
  34. util::fail('只能结算自己的订单哦');
  35. }
  36. if ($val['debt'] == 0) {
  37. util::fail('有订单已经结算过了');
  38. }
  39. if ($val['customId'] != $customId) {
  40. util::fail('每次只能结算一个客户的订单');
  41. }
  42. $amount = bcadd($amount, $val['remainDebtPrice'], 2);
  43. $purchaseId = $val['purchaseId'] ?? 0;
  44. if (!empty($purchaseId)) {
  45. $purchaseArr[] = $purchaseId;
  46. }
  47. }
  48. $purchaseString = '';
  49. if (!empty($purchaseArr)) {
  50. $purchaseString = implode(',', $purchaseArr);
  51. }
  52. $custom = CustomClass::getLockById($customId);
  53. if (empty($custom)) {
  54. util::fail('没有找到客户');
  55. }
  56. $ghsId = $custom->ghsId;
  57. $ghs = GhsClass::getLockById($ghsId);
  58. if (empty($ghs)) {
  59. util::fail('没有找到供货商');
  60. }
  61. $ghsShopId = $ghs['shopId'] ?? 0;
  62. $ghsShop = ShopClass::getById($ghsShopId, true);
  63. $ghsShopName = $ghsShop->shopName ?? '';
  64. $ghsShopName = $ghsShopName == '首店' ? '总店' : $ghsShopName;
  65. $orderSn = orderSn::getPurchaseClearSn();
  66. $payWay = isset($post['payWay']) && is_numeric($post['payWay']) ? $post['payWay'] : dict::getDict('payWay', 'unPay');
  67. $clearData = [
  68. 'prePrice' => $amount,
  69. 'actPrice' => $modifyPrice,
  70. 'realPrice' => $modifyPrice,
  71. 'ghsId' => $ghsId,
  72. 'ghsName' => $ghs['name'] ?? '',
  73. 'ghsMobile' => $ghs['mobile'] ?? '',
  74. 'ghsAvatar' => $ghs['avatar'] ?? '',
  75. 'ghsAddress' => $ghs['address'] ?? '',
  76. 'ghsShopAdminId' => $post['ghsShopAdminId'],
  77. 'ghsShopId' => $post['ghsShopId'] ?? 0,
  78. 'ghsShopAdminName' => $post['ghsShopAdminName'],
  79. 'sjId' => $sjId,
  80. 'shopId' => $shopId,
  81. 'orderSn' => $orderSn,
  82. 'saleIds' => implode(',', $ids),
  83. 'status' => 1,
  84. 'clearStyle' => dict::getDict('clearStyle', 'gys2Hd'),
  85. 'customId' => $customId,
  86. 'customName' => $custom['name'] ?? '',
  87. 'customShopId' => $custom['shopId'] ?? 0,
  88. 'customAvatar' => $custom['avatar'] ?? '',
  89. 'customMobile' => $custom['mobile'] ?? '',
  90. 'customAddress' => $custom['address'] ?? '',
  91. 'deadline' => date("Y-m-d H:i:s", time() + 10 * 60 * 60),
  92. 'purchaseIds' => $purchaseString,
  93. 'payWay' => $payWay,
  94. 'num' => count($ids),
  95. 'ghsShopName' => $ghsShopName,
  96. ];
  97. if ($modifyPrice < $amount) {
  98. $clearData['discountAmount'] = bcsub($amount, $modifyPrice, 2);
  99. $clearData['discountType'] = dict::getDict('discountType', 'discount');
  100. }
  101. if ($modifyPrice > $amount) {
  102. util::fail('结账金额大于总欠款金额');
  103. }
  104. $return = PurchaseClearClass::add($clearData, true);
  105. $complete = $post['complete'] ?? 0;
  106. if ($complete == 1) {
  107. self::confirmClear($return, $payWay);
  108. }
  109. return $return;
  110. }
  111. //确认结账 ssh 20220510
  112. public static function confirmClear($clear, $payWay)
  113. {
  114. if ($clear->status == 2) {
  115. util::fail('已结过账了');
  116. }
  117. if ($clear->status == 3) {
  118. util::fail('已取消了');
  119. }
  120. $current = time();
  121. if ($current >= strtotime($clear->deadline)) {
  122. util::fail('已过期,请手动取消');
  123. }
  124. $clear->payWay = $payWay;
  125. $clear->payTime = date("Y-m-d H:i:s");
  126. $clear->status = 2;
  127. $clear->save();
  128. $clearId = $clear->id ?? 0;
  129. $customId = $clear->customId ?? 0;
  130. $custom = CustomClass::getLockById($customId);
  131. if (empty($custom)) {
  132. util::fail('没有找到客户');
  133. }
  134. $ghsId = $custom->ghsId;
  135. $ghs = GhsClass::getLockById($ghsId);
  136. if (empty($ghs)) {
  137. util::fail('没有找到供货商');
  138. }
  139. $amount = $clear->prePrice ?? 0;
  140. $saleIds = $clear->saleIds ?? '';
  141. $ids = explode(',', trim($saleIds));
  142. if (empty($ids)) {
  143. util::fail('数据错误');
  144. }
  145. $purchaseIds = $clear->purchaseIds ?? '';
  146. $purchaseArr = explode(',', $purchaseIds);
  147. if (empty($purchaseArr)) {
  148. util::fail('数据错误');
  149. }
  150. //客户欠款减少
  151. CustomClass::clearDebtAmountReduce($custom, $ghs, $amount, $clear);
  152. // 销售单状态变更
  153. OrderClass::updateByIds($ids, ['debt' => 0, 'clearId' => $clearId, 'remainDebtPrice' => 0]);
  154. // 采购单状态变更
  155. PurchaseClass::updateByIds($purchaseArr, ['debt' => 2, 'clearId' => $clearId, 'remainDebtPrice' => 0]);
  156. //供货商结账欠款减少
  157. \bizHd\ghs\classes\GhsClass::clearDebtAmountReduce($ghs, $custom, $amount, $clear);
  158. }
  159. }