PurchaseClearClass.php 8.1 KB

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