OrderClearController.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\custom\classes\CustomClass;
  4. use bizGhs\order\classes\OrderClearClass;
  5. use bizHd\purchase\classes\PurchaseClearClass;
  6. use common\components\dateUtil;
  7. use Yii;
  8. use common\components\util;
  9. class OrderClearController extends BaseController
  10. {
  11. //收款 ssh 2021.2.4
  12. public function actionClear()
  13. {
  14. $post = Yii::$app->request->post();
  15. $shopAdmin = $this->shopAdmin;
  16. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  17. util::fail('超管才能结帐');
  18. }
  19. $id = Yii::$app->request->post('id');
  20. $customId = Yii::$app->request->post('customId', 0);
  21. if (empty($id)) {
  22. util::fail('请选择交易记录');
  23. }
  24. //抹零之后的价格
  25. $modifyPrice = Yii::$app->request->post('modifyPrice', 0.00);
  26. if (is_numeric($modifyPrice) == false || $modifyPrice <= 0) {
  27. util::fail('金额填错了哦');
  28. }
  29. $custom = CustomClass::getById($customId, true);
  30. CustomClass::valid($custom, $this->shopId);
  31. $current = time();
  32. $where = ['customId' => $customId, 'status' => PurchaseClearClass::STATUS_AWAIT_PAY];
  33. $searchTime = $post['searchTime'] ?? '';
  34. //结账记录太多数据库会报错,所以必须选月份才能结账!!!!!
  35. if (empty($searchTime)) {
  36. util::fail('请选择月份');
  37. }
  38. $startTime = $post['startTime'] ?? '';
  39. $endTime = $post['endTime'] ?? '';
  40. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  41. $startDate = $period['startTime'];
  42. $endDate = $period['endTime'];
  43. $where['addTime'] = ['between', [$startDate, $endDate]];
  44. if (bcdiv(bcsub(strtotime($endDate), strtotime($startDate)), 86400) > 50) {
  45. util::fail('超过30天,请按月结账');
  46. }
  47. //查找有没结帐订单还没有过期
  48. $list = OrderClearClass::getAllByCondition($where, null, '*', null, true);
  49. if (!empty($list)) {
  50. foreach ($list as $item) {
  51. $deadline = $item->deadline;
  52. $deadTime = strtotime($deadline);
  53. if ($current > $deadTime) {
  54. $item->status = PurchaseClearClass::STATUS_EXPIRE;
  55. $item->save();
  56. } else {
  57. util::fail('客户是否也在结账?请几分钟后再试');
  58. }
  59. }
  60. }
  61. $payWay = Yii::$app->request->post('payWay', 0);
  62. $connection = Yii::$app->db;
  63. $transaction = $connection->beginTransaction();
  64. $name = $this->shopAdmin['name'] ?? '';
  65. $data = [
  66. 'customId' => $customId,
  67. 'ghsShopAdminId' => $this->shopAdminId,
  68. 'ghsShopAdminName' => $name,
  69. 'modifyPrice' => $modifyPrice,
  70. 'payWay' => $payWay,
  71. ];
  72. try {
  73. OrderClearClass::clear($data, $id, $this->sjId, $this->shopId);
  74. $transaction->commit();
  75. } catch (\Exception $exception) {
  76. $transaction->rollBack();
  77. Yii::info("批量收款失败:" . $exception->getMessage());
  78. util::fail('操作失败');
  79. }
  80. util::complete();
  81. }
  82. }