SettleController.php 4.7 KB

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