SettleController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. $mayClearAmount = 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, '*', null, true);
  46. if (empty($orderList)) {
  47. util::fail('请选择订单呢');
  48. }
  49. foreach ($orderList as $order) {
  50. if ($order->mainId != $this->mainId) {
  51. util::fail('不是你的订单哦');
  52. }
  53. $remainDebtPrice = $order->remainDebtPrice ?? 0;
  54. $mayClearAmount = bcadd($mayClearAmount, $remainDebtPrice, 2);
  55. }
  56. if (empty($mayClearAmount)) {
  57. util::fail('不需要销账');
  58. }
  59. $staffId = $this->shopAdminId;
  60. $staffName = $this->shopAdminName;
  61. $customName = $custom->name ?? '';
  62. $userId = $custom->userId;
  63. $hdName = $hd->name;
  64. $shopId = $this->shopId;
  65. $mainId = $this->mainId;
  66. $orderSn = orderSn::getRechargeSn();
  67. $shop = $this->shop;
  68. $hasSettle = SettleClass::getByCondition(['customId' => $customId, 'status' => 1], true);
  69. if (!empty($hasSettle)) {
  70. util::fail('这个客户名下有待结账单,请先取消');
  71. }
  72. //创建待账单
  73. $settleParams = ['staffId' => $staffId, 'staffName' => $staffName];
  74. $settle = SettleClass::addSettle($orderList, $shop, $custom, $hd, $settleParams);
  75. $settleId = $settle->id;
  76. $settleAmount = $settle->actPrice;
  77. $addData = [
  78. 'orderSn' => $orderSn,
  79. 'modPrice' => 1,
  80. 'payWay' => $payWay,
  81. 'style' => 1,
  82. 'shopId' => $shopId,
  83. 'mainId' => $mainId,
  84. 'hdId' => $hdId,
  85. 'hdName' => $hdName,
  86. 'onlinePay' => 1,
  87. 'amount' => $mayClearAmount,
  88. 'staffId' => $staffId,
  89. 'staffName' => $staffName,
  90. 'payStatus' => 0,
  91. 'payTime' => '0000-00-00 00:00:00',
  92. 'status' => 0,
  93. 'settleId' => $settleId,
  94. 'settleAmount' => $settleAmount,
  95. 'remark' => '',
  96. 'customId' => $customId,
  97. 'customName' => $customName,
  98. 'userId' => $userId,
  99. ];
  100. $respond = RechargeClass::addRecharge($addData);
  101. $retId = $respond->id;
  102. //充值成功并且销账成功
  103. $recharge = RechargeClass::getLockById($retId);
  104. $params = ['onlinePay' => 1, 'side' => 0];
  105. RechargeClass::complete($recharge, $payWay, $params);
  106. $transaction->commit();
  107. util::complete();
  108. } catch (\Exception $exception) {
  109. $transaction->rollBack();
  110. Yii::info("失败:" . $exception->getMessage());
  111. util::fail('操作失败');
  112. }
  113. }
  114. }