SettleController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\custom\classes\CustomClass;
  4. use bizHd\custom\classes\HdClass;
  5. use bizHd\order\classes\OrderClass;
  6. use bizHd\order\classes\SettleClass;
  7. use bizHd\recharge\classes\RechargeClass;
  8. use common\components\dict;
  9. use common\components\orderSn;
  10. use common\components\util;
  11. use Yii;
  12. class SettleController extends BaseController
  13. {
  14. public $guestAccess = [];
  15. //结清尾款 ssh 20220708
  16. public function actionConfirmSettle()
  17. {
  18. $post = Yii::$app->request->post();
  19. $connection = Yii::$app->db;
  20. $transaction = $connection->beginTransaction();
  21. try {
  22. $staff = $this->shopAdmin;
  23. $payWay = $post['payWay'] ?? dict::getDict('payWay', 'wxPay');
  24. $idData = $post['id'] ?? '';
  25. if (is_numeric($idData)) {
  26. util::fail('请求参数错误哦');
  27. }
  28. $customId = $post['customId'] ?? 0;
  29. $custom = CustomClass::getLockById($customId);
  30. if (empty($custom)) {
  31. util::fail('没有找到客户');
  32. }
  33. $hdId = $custom->hdId ?? 0;
  34. $hd = HdClass::getlockById($hdId);
  35. if (empty($hd)) {
  36. util::fail('客户信息缺失');
  37. }
  38. //先充值
  39. $totalClear = 0;
  40. $idList = json_decode($idData, true);
  41. $ids = array_column($idList, 'id');
  42. if (empty($ids)) {
  43. util::fail('请选择订单');
  44. }
  45. $orderList = OrderClass::getAllByCondition(['id' => ['in', $ids]], null, 'id,mainId,remainDebtPrice', null, true);
  46. $clearData = [];
  47. if (!empty($orderList)) {
  48. foreach ($orderList as $order) {
  49. if ($order->mainId != $this->mainId) {
  50. util::fail('不是你的订单哦');
  51. }
  52. $orderId = $order->id;
  53. $remainDebtPrice = $order->remainDebtPrice ?? 0;
  54. $clearData[] = [['orderId' => $orderId, 'clearAmount' => $remainDebtPrice]];
  55. $totalClear = bcadd($totalClear, $remainDebtPrice, 2);
  56. }
  57. }
  58. if (empty($totalClear)) {
  59. util::fail('不需要销账');
  60. }
  61. $staffId = $this->shopAdminId;
  62. $staffName = $this->shopAdminName;
  63. $customName = $custom->name ?? '';
  64. $userId = $custom->userId;
  65. $hdName = $hd->name;
  66. $shopId = $this->shopId;
  67. $mainId = $this->mainId;
  68. $orderSn = orderSn::getRechargeSn();
  69. $shop = $this->shop;
  70. $hasSettle = SettleClass::getByCondition(['customId' => $customId, 'status' => 1], true);
  71. if (!empty($hasSettle)) {
  72. util::fail('这个客户名下有待结账单,请先取消');
  73. }
  74. //创建待账单
  75. $settleParams = ['staffId' => $staffId, 'staffName' => $staffName];
  76. $settle = SettleClass::addSettle($clearData, $shop, $custom, $hd, $settleParams);
  77. $settleId = $settle->id;
  78. $settleAmount = $settle->actPrice;
  79. $settleNo = $settle->orderSn ?? '';
  80. $addData = [
  81. 'orderSn' => $orderSn,
  82. 'modPrice' => 1,
  83. 'payWay' => $payWay,
  84. 'style' => 1,
  85. 'shopId' => $shopId,
  86. 'mainId' => $mainId,
  87. 'hdId' => $hdId,
  88. 'hdName' => $hdName,
  89. 'onlinePay' => 1,
  90. 'amount' => $totalClear,
  91. 'staffId' => $staffId,
  92. 'staffName' => $staffName,
  93. 'payStatus' => 0,
  94. 'payTime' => '0000-00-00 00:00:00',
  95. 'status' => 0,
  96. 'settleId' => $settleId,
  97. 'settleAmount' => $settleAmount,
  98. 'settleNo' => $settleNo,
  99. 'remark' => '',
  100. 'customId' => $customId,
  101. 'customName' => $customName,
  102. 'userId' => $userId,
  103. ];
  104. $respond = RechargeClass::addRecharge($addData);
  105. $retId = $respond->id;
  106. //充值成功并且销账成功
  107. $recharge = RechargeClass::getLockById($retId);
  108. $params = ['onlinePay' => 1, 'side' => 0];
  109. RechargeClass::complete($recharge, $payWay, $params);
  110. $transaction->commit();
  111. util::complete();
  112. } catch (\Exception $exception) {
  113. $transaction->rollBack();
  114. Yii::info("失败:" . $exception->getMessage());
  115. util::fail('操作失败');
  116. }
  117. }
  118. }