OrderClearController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\ghs\classes\GhsClass;
  4. use bizGhs\custom\classes\CustomClass;
  5. use bizGhs\order\classes\OrderClearClass;
  6. use bizHd\purchase\classes\PurchaseClearClass;
  7. use common\components\dateUtil;
  8. use common\components\dict;
  9. use Yii;
  10. use common\components\util;
  11. class OrderClearController extends BaseController
  12. {
  13. //收款 ssh 2021.2.4
  14. public function actionClear()
  15. {
  16. $post = Yii::$app->request->post();
  17. $shopAdmin = $this->shopAdmin;
  18. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  19. util::fail('超管才能结帐');
  20. }
  21. $id = Yii::$app->request->post('id');
  22. $customId = Yii::$app->request->post('customId', 0);
  23. if (empty($id)) {
  24. util::fail('请选择交易记录');
  25. }
  26. //抹零之后的价格
  27. $modifyPrice = Yii::$app->request->post('modifyPrice', 0.00);
  28. if (is_numeric($modifyPrice) == false || $modifyPrice <= 0) {
  29. util::fail('金额填错了哦');
  30. }
  31. $custom = CustomClass::getById($customId, true);
  32. CustomClass::valid($custom, $this->shopId);
  33. $current = time();
  34. $where = ['customId' => $customId, 'status' => PurchaseClearClass::STATUS_AWAIT_PAY];
  35. $searchTime = $post['searchTime'] ?? '';
  36. //结账记录太多数据库会报错,所以必须选月份才能结账!!!!!
  37. //订单详情里可以单个结账
  38. if (count((array)$id) > 120) {
  39. util::fail('提交订单数超过120单');
  40. }
  41. $startTime = $post['startTime'] ?? '';
  42. $endTime = $post['endTime'] ?? '';
  43. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  44. $startDate = $period['startTime'];
  45. $endDate = $period['endTime'];
  46. if (bcdiv(bcsub(strtotime($endDate), strtotime($startDate)), 86400) > 50) {
  47. util::fail('超过30天,请按月结账');
  48. }
  49. //查找有没结帐订单还没有过期
  50. $list = OrderClearClass::getAllByCondition($where, null, '*', null, true);
  51. if (!empty($list)) {
  52. foreach ($list as $item) {
  53. $deadline = $item->deadline;
  54. $deadTime = strtotime($deadline);
  55. if ($current > $deadTime) {
  56. $item->status = PurchaseClearClass::STATUS_EXPIRE;
  57. $item->save();
  58. } else {
  59. util::success(['hasUnComplete' => 1], '此客户还有待完成结账单');
  60. }
  61. }
  62. }
  63. $payWay = Yii::$app->request->post('payWay', 0);
  64. $connection = Yii::$app->db;
  65. $transaction = $connection->beginTransaction();
  66. $name = $this->shopAdmin['name'] ?? '';
  67. $complete = $post['complete'] ?? 0;
  68. $data = [
  69. 'customId' => $customId,
  70. 'ghsShopAdminId' => $this->shopAdminId ?? 0,
  71. 'ghsShopId' => $this->shopId ?? 0,
  72. 'ghsShopAdminName' => $name,
  73. 'modifyPrice' => $modifyPrice,
  74. 'payWay' => $payWay,
  75. 'complete' => $complete,
  76. ];
  77. try {
  78. $respond = OrderClearClass::clear($data, $id, $this->sjId, $this->shopId);
  79. $transaction->commit();
  80. util::success($respond);
  81. } catch (\Exception $exception) {
  82. $transaction->rollBack();
  83. Yii::info("批量收款失败:" . $exception->getMessage());
  84. util::fail('操作失败');
  85. }
  86. util::complete();
  87. }
  88. //确认结账 ssh 20220510
  89. public function actionConfirmClear()
  90. {
  91. $get = Yii::$app->request->get();
  92. $id = $get['id'] ?? 0;
  93. $payWay = $get['payWay'] ?? dict::getDict('payWay', 'wxPay');
  94. $clear = OrderClearClass::getById($id, true);
  95. if (empty($clear)) {
  96. util::fail('没有找到结账单');
  97. }
  98. $ghsId = $clear->ghsId ?? 0;
  99. $ghs = GhsClass::getById($ghsId, true);
  100. if ($ghs->shopId != $this->shopId) {
  101. util::fail('没有权限');
  102. }
  103. $connection = Yii::$app->db;
  104. $transaction = $connection->beginTransaction();
  105. try {
  106. OrderClearClass::confirmClear($clear, $payWay);
  107. $transaction->commit();
  108. util::complete();
  109. } catch (\Exception $exception) {
  110. $transaction->rollBack();
  111. Yii::info("失败:" . $exception->getMessage());
  112. util::fail('操作失败');
  113. }
  114. }
  115. }