OrderClearClass.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. ];
  92. $clearInfo = self::clear($clearData, $idText, $sjId, $shopId);
  93. //结账消费余额
  94. CustomClass::clearConsumeBalance($totalAmount, $custom, $ghs, $shop, $staff, $clearInfo);
  95. }
  96. //订单结算 ssh 2021.3.14
  97. public static function clear($post, $idText, $sjId, $shopId)
  98. {
  99. $customId = $post['customId'] ?? 0;
  100. $data = json_decode($idText, true);
  101. $ids = array_column($data, 'id');
  102. if (empty($ids)) {
  103. util::fail('请选择交易记录');
  104. }
  105. $list = OrderClass::getByIds($ids);
  106. if (empty($list)) {
  107. util::fail('请选择订单');
  108. }
  109. $amount = 0;
  110. $modifyPrice = $post['modifyPrice'] ?? 0.00;
  111. $purchaseArr = [];
  112. foreach ($list as $key => $val) {
  113. if ($val['sjId'] != $sjId) {
  114. util::fail('只能结算自己的订单哦');
  115. }
  116. if ($val['debt'] == 0) {
  117. util::fail('有订单已经结算过了');
  118. }
  119. if ($val['customId'] != $customId) {
  120. util::fail('每次只能结算一个客户的订单');
  121. }
  122. $amount = bcadd($amount, $val['remainDebtPrice'], 2);
  123. $purchaseId = $val['purchaseId'] ?? 0;
  124. if (!empty($purchaseId)) {
  125. $purchaseArr[] = $purchaseId;
  126. }
  127. }
  128. $purchaseString = '';
  129. if (!empty($purchaseArr)) {
  130. $purchaseString = implode(',', $purchaseArr);
  131. }
  132. $custom = CustomClass::getLockById($customId);
  133. if (empty($custom)) {
  134. util::fail('没有找到客户');
  135. }
  136. $ghsId = $custom->ghsId;
  137. $ghs = GhsClass::getLockById($ghsId);
  138. if (empty($ghs)) {
  139. util::fail('没有找到供货商');
  140. }
  141. $ghsShopId = $ghs['shopId'] ?? 0;
  142. $ghsShop = ShopClass::getById($ghsShopId, true);
  143. $ghsShopName = $ghsShop->shopName ?? '';
  144. $ghsShopName = $ghsShopName == '首店' ? '总店' : $ghsShopName;
  145. $orderSn = orderSn::getPurchaseClearSn();
  146. //默认24小时过期
  147. $deadline = date("Y-m-d H:i:s", time() + 24 * 60 * 60);
  148. //天天鲜花12小时之后
  149. if ($ghsShopId == 763 || $ghsShopId == 795 || $ghsShopId == 1405) {
  150. $deadline = date("Y-m-d H:i:s", time() + 10 * 60 * 60);
  151. }
  152. //开发环境10个小时过期
  153. if (getenv('YII_ENV') != 'production') {
  154. $deadline = date("Y-m-d H:i:s", time() + 10 * 60 * 60);
  155. }
  156. //惠雅鲜花 结账单过期时间设置为30天
  157. if ($ghsShopId == 520 || $ghsShopId == 1489 || $ghsShopId == 2164 || $ghsShopId == 2167) {
  158. $deadline = date("Y-m-d H:i:s", time() + 30 * 24 * 60 * 60);
  159. }
  160. //昱成花卉 结账单过期时间设置为30天
  161. if ($ghsShopId == 8596) {
  162. $deadline = date("Y-m-d H:i:s", time() + 30 * 24 * 60 * 60);
  163. }
  164. //花大苪 结账单过期时间设置为30天
  165. if ($ghsShopId == 8249) {
  166. $deadline = date("Y-m-d H:i:s", time() + 30 * 24 * 60 * 60);
  167. }
  168. //丰行鲜花 结账单过期时间设置为30天
  169. if ($ghsShopId == 7855) {
  170. $deadline = date("Y-m-d H:i:s", time() + 30 * 24 * 60 * 60);
  171. }
  172. $payWay = isset($post['payWay']) && is_numeric($post['payWay']) ? $post['payWay'] : dict::getDict('payWay', 'unPay');
  173. $clearData = [
  174. 'prePrice' => $amount,
  175. 'actPrice' => $modifyPrice,
  176. 'realPrice' => $modifyPrice,
  177. 'ghsId' => $ghsId,
  178. 'ghsName' => $ghs['name'] ?? '',
  179. 'ghsMobile' => $ghs['mobile'] ?? '',
  180. 'ghsAvatar' => $ghs['avatar'] ?? '',
  181. 'ghsAddress' => $ghs['address'] ?? '',
  182. 'ghsShopAdminId' => $post['ghsShopAdminId'],
  183. 'ghsShopId' => $post['ghsShopId'] ?? 0,
  184. 'ghsShopAdminName' => $post['ghsShopAdminName'],
  185. 'sjId' => $sjId,
  186. 'shopId' => $shopId,
  187. 'orderSn' => $orderSn,
  188. 'saleIds' => implode(',', $ids),
  189. 'status' => 1,
  190. 'clearStyle' => dict::getDict('clearStyle', 'gys2Hd'),
  191. 'customId' => $customId,
  192. 'customName' => $custom['name'] ?? '',
  193. 'customShopId' => $custom['shopId'] ?? 0,
  194. 'customAvatar' => $custom['avatar'] ?? '',
  195. 'customMobile' => $custom['mobile'] ?? '',
  196. 'customAddress' => $custom['address'] ?? '',
  197. 'deadline' => $deadline,
  198. 'purchaseIds' => $purchaseString,
  199. 'payWay' => $payWay,
  200. 'num' => count($ids),
  201. 'ghsShopName' => $ghsShopName,
  202. 'hasNoticeCustom' => 0,
  203. ];
  204. if ($modifyPrice < $amount) {
  205. $clearData['discountAmount'] = bcsub($amount, $modifyPrice, 2);
  206. $clearData['discountType'] = dict::getDict('discountType', 'discount');
  207. }
  208. if ($modifyPrice > $amount) {
  209. //noticeUtil::push("结账金额大于总欠款金额 {$modifyPrice} {$amount}", '15280215347');
  210. util::fail('结账金额大于总欠款金额');
  211. }
  212. $return = PurchaseClearClass::add($clearData, true);
  213. $complete = $post['complete'] ?? 0;
  214. if ($complete == 1) {
  215. self::confirmClear($return, $payWay);
  216. }
  217. return $return;
  218. }
  219. //确认结账 ssh 20220510
  220. public static function confirmClear($clear, $payWay)
  221. {
  222. if ($clear->status == 2) {
  223. util::fail('已结过账了');
  224. }
  225. if ($clear->status == 3) {
  226. util::fail('已取消了');
  227. }
  228. $current = time();
  229. if ($current >= strtotime($clear->deadline)) {
  230. util::fail('已过期,请手动取消');
  231. }
  232. $clear->payWay = $payWay;
  233. $clear->payTime = date("Y-m-d H:i:s");
  234. $clear->status = 2;
  235. $clear->save();
  236. $clearId = $clear->id ?? 0;
  237. $customId = $clear->customId ?? 0;
  238. $orderSn = $clear->orderSn ?? '';
  239. $customName = $clear->customName ?? '';
  240. $custom = CustomClass::getLockById($customId);
  241. if (empty($custom)) {
  242. util::fail('没有找到客户');
  243. }
  244. $ghsId = $custom->ghsId;
  245. $ghs = GhsClass::getLockById($ghsId);
  246. if (empty($ghs)) {
  247. util::fail('没有找到供货商');
  248. }
  249. $amount = $clear->prePrice ?? 0;
  250. $saleIds = $clear->saleIds ?? '';
  251. $ids = explode(',', trim($saleIds));
  252. if (empty($ids)) {
  253. util::fail('数据错误');
  254. }
  255. $purchaseIds = $clear->purchaseIds ?? '';
  256. $purchaseArr = explode(',', $purchaseIds);
  257. if (empty($purchaseArr)) {
  258. util::fail('数据错误');
  259. }
  260. //客户欠款减少
  261. CustomClass::clearDebtAmountReduce($custom, $ghs, $amount, $clear);
  262. $clearTime = date("Y-m-d H:i:s");
  263. // 销售单状态变更
  264. OrderClass::updateByIds($ids, ['debt' => 0, 'clearId' => $clearId, 'remainDebtPrice' => 0, 'clearTime' => $clearTime]);
  265. // 采购单状态变更
  266. PurchaseClass::updateByIds($purchaseArr, ['debt' => 2, 'clearId' => $clearId, 'remainDebtPrice' => 0, 'clearTime' => $clearTime]);
  267. //供货商结账欠款减少
  268. \bizHd\ghs\classes\GhsClass::clearDebtAmountReduce($ghs, $custom, $amount, $clear);
  269. //现金余额增加
  270. if ($payWay == dict::getDict('payWay', 'cash')) {
  271. $mainId = $ghs->mainId ?? 0;
  272. $main = MainClass::getLockById($mainId);
  273. if (empty($main)) {
  274. util::fail('没有商家信息');
  275. }
  276. $moneyBalance = bcadd($main->money, $amount, 2);
  277. $main->money = $moneyBalance;
  278. $main->save();
  279. $moneyEvent = $customName . "现金结账" . floatval($amount) . "元,单号 {$orderSn})";
  280. $moneyRemark = '';
  281. $capitalType = dict::getDict('capitalType', 'ghsXsClear', 'id');
  282. $change = [
  283. 'relateId' => $clearId,
  284. 'amount' => $amount,
  285. 'balance' => $moneyBalance,
  286. 'io' => 1,
  287. 'mainId' => $mainId,
  288. 'capitalType' => $capitalType,
  289. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  290. 'event' => $moneyEvent,
  291. 'remark' => $moneyRemark,
  292. ];
  293. ShopMoneyChangeClass::addData($change);
  294. }
  295. }
  296. }