SettleController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 20250714
  16. public function actionCancelSettle()
  17. {
  18. util::fail('开发中');
  19. }
  20. public function actionGetDetail()
  21. {
  22. $id = Yii::$app->request->get('id', '');
  23. $settle = SettleClass::getById($id);
  24. if ($settle['mainId'] != $this->mainId) {
  25. util::fail('不是你的账单');
  26. }
  27. $respond = SettleClass::getMyDetail($settle);
  28. util::success($respond);
  29. }
  30. public function actionGetList()
  31. {
  32. $get = Yii::$app->request->get();
  33. $where = [];
  34. if (isset($get['status']) && is_numeric($get['status'])) {
  35. $where['status'] = $get['status'];
  36. }
  37. $customId = $get['customId'] ?? 0;
  38. if (!empty($customId)) {
  39. $custom = CustomClass::getById($customId, true);
  40. if ($custom->shopId != $this->shopId) {
  41. util::fail('不是你的客户');
  42. }
  43. $where['customId'] = $customId;
  44. }
  45. $where['shopId'] = $this->shopId;
  46. $list = SettleClass::getMySettleList($where);
  47. $totalAmount = SettleClass::sum(['shopId' => $this->shopId, 'status' => 2, 'customId' => $customId], 'actPrice');
  48. $list['totalAmount'] = floatval($totalAmount);
  49. util::success($list);
  50. }
  51. //结清尾款 ssh 20220708
  52. public function actionConfirmSettle()
  53. {
  54. $post = Yii::$app->request->post();
  55. $connection = Yii::$app->db;
  56. $transaction = $connection->beginTransaction();
  57. try {
  58. $payWay = $post['payWay'] ?? dict::getDict('payWay', 'wxPay');
  59. $idData = $post['id'] ?? '';
  60. if (is_numeric($idData)) {
  61. util::fail('请求参数错误哦');
  62. }
  63. $customId = $post['customId'] ?? 0;
  64. $custom = CustomClass::getLockById($customId);
  65. if (empty($custom)) {
  66. util::fail('没有找到客户');
  67. }
  68. $hdId = $custom->hdId ?? 0;
  69. $hd = HdClass::getlockById($hdId);
  70. if (empty($hd)) {
  71. util::fail('客户信息缺失');
  72. }
  73. //先充值
  74. $totalClear = 0;
  75. $idList = json_decode($idData, true);
  76. $ids = array_column($idList, 'id');
  77. if (empty($ids)) {
  78. util::fail('请选择订单');
  79. }
  80. $orderList = OrderClass::getAllByCondition(['id' => ['in', $ids]], null, 'id,mainId,remainDebtPrice,orderSn', null, true);
  81. $clearData = [];
  82. if (!empty($orderList)) {
  83. foreach ($orderList as $order) {
  84. if ($order->mainId != $this->mainId) {
  85. util::fail('不是你的订单哦');
  86. }
  87. $orderId = $order->id;
  88. $orderSn = $order->orderSn ?? '';
  89. $remainDebtPrice = $order->remainDebtPrice ?? 0;
  90. $clearData[] = ['orderId' => $orderId, 'clearAmount' => $remainDebtPrice, 'orderSn' => $orderSn];
  91. $totalClear = bcadd($totalClear, $remainDebtPrice, 2);
  92. }
  93. }
  94. if ($totalClear <= 0) {
  95. util::fail('不需要销账');
  96. }
  97. $staffId = $this->shopAdminId;
  98. $staffName = $this->shopAdminName;
  99. $customName = $custom->name ?? '';
  100. $userId = $custom->userId;
  101. $hdName = $hd->name;
  102. $shopId = $this->shopId;
  103. $mainId = $this->mainId;
  104. $orderSn = orderSn::getRechargeSn();
  105. $shop = $this->shop;
  106. //避免重复提交
  107. util::checkRepeatCommit($staffId, 6);
  108. $addData = [
  109. 'orderSn' => $orderSn,
  110. 'modPrice' => 1,
  111. 'payWay' => $payWay,
  112. 'style' => 1,
  113. 'shopId' => $shopId,
  114. 'mainId' => $mainId,
  115. 'hdId' => $hdId,
  116. 'hdName' => $hdName,
  117. 'onlinePay' => 1,
  118. 'amount' => $totalClear,
  119. 'staffId' => $staffId,
  120. 'staffName' => $staffName,
  121. 'payStatus' => 0,
  122. 'payTime' => '0000-00-00 00:00:00',
  123. 'status' => 0,
  124. 'remark' => '',
  125. 'customId' => $customId,
  126. 'customName' => $customName,
  127. 'userId' => $userId,
  128. ];
  129. $respond = RechargeClass::addRecharge($addData);
  130. $retId = $respond->id;
  131. //创建待结账单
  132. $settleParams = ['staffId' => $staffId, 'staffName' => $staffName];
  133. $settle = SettleClass::addSettle($clearData, $shop, $custom, $hd, $settleParams);
  134. $settleId = $settle->id;
  135. $settleAmount = $settle->actPrice;
  136. $settleNo = $settle->orderSn ?? '';
  137. $respond->settleId = $settleId;
  138. $respond->settleAmount = $settleAmount;
  139. $respond->settleNo = $settleNo;
  140. $respond->save();
  141. //充值成功并且销账成功
  142. $recharge = RechargeClass::getLockById($retId);
  143. $params = ['onlinePay' => 1, 'side' => 0];
  144. RechargeClass::complete($recharge, $payWay, $params);
  145. $transaction->commit();
  146. util::complete();
  147. } catch (\Exception $exception) {
  148. $transaction->rollBack();
  149. Yii::info("失败:" . $exception->getMessage());
  150. util::fail('操作失败');
  151. }
  152. }
  153. }