PurchaseClearClass.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace bizHd\purchase\classes;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\shop\classes\ShopClass;
  5. use biz\shop\classes\ShopYeChangeClass;
  6. use biz\wx\classes\WxMessageClass;
  7. use bizGhs\custom\classes\CustomClass;
  8. use bizGhs\order\classes\OrderClass;
  9. use bizHd\base\classes\BaseClass;
  10. use bizHd\shop\classes\MainClass;
  11. use common\components\dict;
  12. use common\components\orderSn;
  13. use common\components\util;
  14. use Yii;
  15. class PurchaseClearClass extends BaseClass
  16. {
  17. public static $baseFile = '\bizHd\purchase\models\PurchaseClear';
  18. const STATUS_AWAIT_PAY = 1;
  19. const STATUS_HAS_PAY = 2;
  20. const STATUS_EXPIRE = 3;
  21. public static function addOrder($data, $ghs)
  22. {
  23. $ghsId = $ghs->id;
  24. $customId = $ghs->customId;
  25. $custom = CustomClass::getById($customId, true);
  26. if (empty($custom)) {
  27. util::fail('没有找到客户');
  28. }
  29. $ghsShopId = $ghs->shopId ?? 0;
  30. $ghsShop = ShopClass::getById($ghsShopId, true);
  31. $ghsShopName = $ghsShop->shopName ?? '';
  32. $ghsShopName = $ghsShopName == '首店' ? '总店' : $ghsShopName;
  33. $orderSn = orderSn::getPurchaseClearSn();
  34. $purchaseIds = $data['purchaseIds'];
  35. $arr = explode(',', $purchaseIds);
  36. if (empty($arr)) {
  37. util::fail('请选择采购单');
  38. }
  39. //订单太多数据库会报错
  40. if (count($arr) > 100) {
  41. util::fail('订单太多,请选月份再结');
  42. }
  43. $purchaseList = PurchaseClass::getAllByCondition(['id' => ['in', $arr]], null, '*', null, true);
  44. if (empty($purchaseList)) {
  45. util::fail('请选择采购单');
  46. }
  47. $totalPrice = 0;
  48. $currentGhsId = $data['ghsId'] ?? 0;
  49. $saleArr = [];
  50. foreach ($purchaseList as $purchase) {
  51. if ($purchase->debt != PurchaseClass::DEBT_YES) {
  52. util::fail('请选择有欠款的采购单');
  53. }
  54. if ($purchase->ghsId != $currentGhsId) {
  55. util::fail('请选择同个供货商的订单');
  56. }
  57. $totalPrice = bcadd($totalPrice, $purchase->remainDebtPrice, 2);
  58. $saleId = $purchase->saleId;
  59. $saleArr[] = $saleId;
  60. }
  61. $saleString = implode(',', $saleArr);
  62. $data['saleIds'] = $saleString;
  63. $data['prePrice'] = $totalPrice;
  64. $data['actPrice'] = $totalPrice;
  65. $data['realPrice'] = $totalPrice;
  66. $data['orderSn'] = $orderSn;
  67. $validTime = dict::getDict('order_pay_has_time');
  68. $deadTime = time() + $validTime;
  69. $data['deadline'] = date("Y-m-d H:i:s", $deadTime);
  70. $params = [
  71. 'ghsId' => $ghsId,
  72. 'ghsName' => $ghs['name'] ?? '',
  73. 'ghsMobile' => $ghs['mobile'] ?? '',
  74. 'ghsAvatar' => $ghs['avatar'] ?? '',
  75. 'ghsAddress' => $ghs['address'] ?? '',
  76. 'customId' => $customId,
  77. 'customName' => $custom['name'] ?? '',
  78. 'customAvatar' => $custom['avatar'] ?? '',
  79. 'customMobile' => $custom['mobile'] ?? '',
  80. 'customAddress' => $custom['address'] ?? '',
  81. 'num' => count($arr),
  82. 'ghsShopName' => $ghsShopName,
  83. ];
  84. $data = array_merge($data, $params);
  85. return self::add($data);
  86. }
  87. //第三方支付回调 ssh 2021.4.28
  88. public static function thirdPay($payWay, $orderSn, $totalFee, $attach, $transactionId)
  89. {
  90. $info = self::getByCondition(['orderSn' => $orderSn], true);
  91. if (empty($info)) {
  92. $msg = "没有找到清算订单 orderSn:{$orderSn}";
  93. Yii::info($msg);
  94. util::fail($msg);
  95. }
  96. if ($info->actPrice != $totalFee) {
  97. $msg = "清算订单金额与回调通知金额不一致 orderSn:{$orderSn} {$info->actPrice} {$totalFee}";
  98. Yii::info($msg);
  99. util::fail($msg);
  100. }
  101. //临时解决微信一秒内通知二次问题
  102. $id = $info->id;
  103. $info = self::getLockById($id);
  104. $info->onlinePay = dict::getDict('onlinePay', 'yes');
  105. $info->thirdNo = $transactionId;
  106. $info->save();
  107. self::complete($info, $payWay);
  108. }
  109. //支付成功后的流程 ssh 2021.4.28
  110. public static function complete($order, $payWay)
  111. {
  112. if (empty($order)) {
  113. util::fail('没有找到订单');
  114. }
  115. if (isset($order->status) == false || $order->status != self::STATUS_AWAIT_PAY) {
  116. util::fail('订单不是待付款状态');
  117. }
  118. $order->status = self::STATUS_HAS_PAY;
  119. $order->payTime = date("Y-m-d H:i:s");
  120. $order->save();
  121. $clearId = $order->id;
  122. $orderSn = $order->orderSn;
  123. $purchaseIds = $order->purchaseIds;
  124. $arr = explode(',', $purchaseIds);
  125. //供货商欠款变更
  126. $ghsId = $order->ghsId;
  127. $ghs = GhsClass::getLockById($ghsId);
  128. if (empty($ghs)) {
  129. util::fail('没有找到供货商信息');
  130. }
  131. $customId = $ghs->customId;
  132. $custom = CustomClass::getLockById($customId);
  133. if (empty($custom)) {
  134. util::fail('没有找到客户信息');
  135. }
  136. \bizHd\ghs\classes\GhsClass::clearDebtAmountReduce($ghs, $custom, $order->actPrice, $order);
  137. $shopId = $ghs->shopId;
  138. $shop = ShopClass::getLockById($shopId);
  139. if (empty($shop)) {
  140. util::fail('没有找到供货商门店');
  141. }
  142. $mainId = $shop->mainId ?? 0;
  143. $main = MainClass::getLockById($mainId);
  144. if (empty($main)) {
  145. util::fail('没有main信息');
  146. }
  147. if ($payWay == dict::getDict('payWay', 'wxPay')) {
  148. //供货商门店余额增加
  149. ShopClass::customClearAddBalance($main, $shop, $order);
  150. }
  151. //供货商的门店应收客户款减少
  152. $currentGathering = bcsub($main->mayGathering, $order->actPrice, 2);
  153. $main->mayGathering = $currentGathering;
  154. $main->save();
  155. if (empty($arr)) {
  156. $msg = "结算订单 orderSn:{$orderSn} 支付成功,回调处理,没有找到采购订单";
  157. util::fail($msg);
  158. }
  159. $list = PurchaseClass::getAllByCondition(['id' => ['in', $arr]], null, '*', null, true);
  160. if (empty($list)) {
  161. $msg = "结算订单 orderSn:{$orderSn} 支付成功,回调处理,没有找到采购订单!";
  162. util::fail($msg);
  163. }
  164. //采购单状态变更
  165. $saleIds = [];
  166. foreach ($list as $purchase) {
  167. $purchase->debt = PurchaseClass::DEBT_NO;
  168. $purchase->payWay = $payWay;
  169. $purchase->clearId = $clearId;
  170. $purchase->remainDebtPrice = 0;
  171. $purchase->save();
  172. $saleIds[] = $purchase->saleId;
  173. }
  174. //销售单状态变化
  175. OrderClass::updateByIds($saleIds, ['debt' => OrderClass::DEBT_NO, 'clearId' => $clearId, 'remainDebtPrice' => 0]);
  176. //客户付供货商款减少
  177. CustomClass::clearDebtAmountReduce($custom, $ghs, $order->actPrice, $order);
  178. WxMessageClass::clearInform($shop, $order, $custom);
  179. }
  180. }