SettleController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. $id = $post['id'] ?? '';
  25. $customId = $post['customId'] ?? 0;
  26. $custom = CustomClass::getLockById($customId);
  27. if (empty($custom)) {
  28. util::fail('没有找到客户');
  29. }
  30. $hdId = $custom->hdId ?? 0;
  31. $hd = HdClass::getlockById($hdId);
  32. if (empty($hd)) {
  33. util::fail('客户信息缺失');
  34. }
  35. //1、先充值
  36. $rechargeAmount = 0;
  37. if (is_numeric($id)) {
  38. $order = OrderClass::getById($id, true);
  39. if (empty($order)) {
  40. util::fail('没有找到订单');
  41. }
  42. $rechargeAmount = $order->remainDebtPrice ?? 0;
  43. } else {
  44. $idArr = json_decode($id, true);
  45. $ids = array_column($idArr, 'id');
  46. $orderList = OrderClass::getAllByCondition(['id' => ['in', $ids]], null, '*', null, true);
  47. if (!empty($orderList)) {
  48. foreach ($orderList as $order) {
  49. $remainDebtPrice = $order->remainDebtPrice ?? 0;
  50. $rechargeAmount = bcadd($rechargeAmount, $remainDebtPrice, 2);
  51. }
  52. }
  53. }
  54. if (empty($rechargeAmount)) {
  55. util::fail('不需要销账');
  56. }
  57. $staffId = $this->shopAdminId;
  58. $staffName = $this->shopAdminName;
  59. $customName = $custom->name ?? '';
  60. $hdName = $hd->name;
  61. $shopId = $this->shopId;
  62. $mainId = $this->mainId;
  63. $orderSn = orderSn::getRechargeSn();
  64. $rechargeData = [
  65. 'orderSn' => $orderSn,
  66. 'modPrice' => 1,
  67. 'payWay' => $payWay,
  68. 'shopId' => $shopId,
  69. 'mainId' => $mainId,
  70. 'hdId' => $hdId,
  71. 'hdName' => $hdName,
  72. 'onlinePay' => 1,
  73. 'amount' => $rechargeAmount,
  74. 'staffId' => $staffId,
  75. 'staffName' => $staffName,
  76. 'payStatus' => 0,
  77. 'payTime' => '0000-00-00 00:00:00',
  78. 'status' => 0,
  79. 'remark' => '',
  80. 'customId' => $customId,
  81. 'customName' => $customName,
  82. ];
  83. $respond = RechargeClass::addRecharge($rechargeData);
  84. $id = $respond->id;
  85. $recharge = RechargeClass::getLockById($id);
  86. $params = [
  87. 'onlinePay' => 1,
  88. 'side' => 0,
  89. ];
  90. RechargeClass::complete($recharge, $payWay, $params);
  91. //2、再销账
  92. if (is_numeric($id)) {
  93. $order = OrderClass::getById($id, true);
  94. if (empty($order)) {
  95. util::fail('没有找到订单');
  96. }
  97. if ($order->mainId != $this->mainId) {
  98. util::fail('不是你的订单');
  99. }
  100. SettleClass::confirmSettle($order, $payWay, $staff);
  101. } else {
  102. $idArr = json_decode($id, true);
  103. $ids = array_column($idArr, 'id');
  104. $orderList = OrderClass::getAllByCondition(['id' => ['in', $ids]], null, '*', null, true);
  105. if (!empty($orderList)) {
  106. foreach ($orderList as $order) {
  107. if (empty($order)) {
  108. util::fail('没有找到订单');
  109. }
  110. if ($order->mainId != $this->mainId) {
  111. util::fail('不是你的订单');
  112. }
  113. SettleClass::confirmSettle($order, $payWay, $staff);
  114. }
  115. }
  116. }
  117. $transaction->commit();
  118. util::complete();
  119. } catch (\Exception $exception) {
  120. $transaction->rollBack();
  121. Yii::info("失败:" . $exception->getMessage());
  122. util::fail('操作失败');
  123. }
  124. }
  125. }