ClearController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\ghs\classes\GhsClass;
  4. use bizGhs\clear\classes\ClearClass;
  5. use bizGhs\clear\services\ClearService;
  6. use bizGhs\custom\classes\CustomClass;
  7. use common\components\lakala\Lakala;
  8. use bizHd\wx\classes\WxOpenClass;
  9. use common\components\dict;
  10. use common\components\noticeUtil;
  11. use common\components\util;
  12. use Yii;
  13. class ClearController extends BaseController
  14. {
  15. //结账单取消了,钱给原路退回 ssh 20250501
  16. public function actionRefundMoney()
  17. {
  18. $id = Yii::$app->request->get('id', '');
  19. $info = ClearClass::getById($id, true);
  20. if ($info->ghsShopId != $this->shopId) {
  21. util::fail('不是你的结账单');
  22. }
  23. if ($info->status != 3) {
  24. util::fail('结账单没有取消,不能退钱');
  25. }
  26. $addTime = $info->addTime;
  27. $addTime = strtotime($addTime);
  28. $earTime = strtotime('2025-05-01 00:00:00');
  29. if ($addTime < $earTime) {
  30. util::fail('2025.5.1号前的订单,不能退款');
  31. }
  32. $shop = $this->shop;
  33. $respond = ClearClass::refundMoney($shop, $info);
  34. $status = $respond['status'];
  35. $msg = $respond['msg'];
  36. util::success(['status' => $status], $msg);
  37. }
  38. //获取客户待结订单明细 ssh 20220926
  39. public function actionGetUnClear()
  40. {
  41. $get = Yii::$app->request->get();
  42. $customId = $get['customId'] ?? 0;
  43. $custom = CustomClass::getById($customId, true);
  44. CustomClass::valid($custom, $this->shopId);
  45. $info = ClearClass::getByCondition(['customId' => $customId, 'status' => 1], true);
  46. if (isset($info->deadline) && $info->deadline < date("Y-m-d H:i:s")) {
  47. $n = date("Y-m-d H:i:s");
  48. $info->status = 3;
  49. $info->remark = "账单过期,已自动取消,参数:{$info->deadline} {$n}";
  50. $info->save();
  51. util::success(['info' => []]);
  52. }
  53. util::success(['info' => $info]);
  54. }
  55. public function actionCancel()
  56. {
  57. $id = Yii::$app->request->get('id');
  58. $clear = ClearClass::getLockById($id);
  59. if (empty($clear)) {
  60. util::fail('没有结账信息呢');
  61. }
  62. $ghsId = $clear->ghsId ?? 0;
  63. $orderSn = $clear->orderSn ?? '';
  64. $ghs = GhsClass::getById($ghsId, true);
  65. if (empty($ghs)) {
  66. util::fail('没有找到供货商关系');
  67. }
  68. if ($ghs->shopId != $this->shopId) {
  69. util::fail('不是你的账单哦');
  70. }
  71. $staff = $this->shopAdmin;
  72. $staffName = $staff->name ?? '';
  73. if ($clear->status == ClearClass::STATUS_HAS_PAY) {
  74. util::fail('账单已完成');
  75. }
  76. if ($clear->status == ClearClass::STATUS_EXPIRE) {
  77. util::fail('账单已取消');
  78. }
  79. $ghsShop = $this->shop;
  80. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  81. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  82. $params = [
  83. 'appid' => 'OP00002119',
  84. 'serial_no' => '018b08cfddbd',
  85. 'merchant_no' => $ghsShop->lklSjNo,
  86. 'term_no' => $ghsShop->lklScanTermNo,
  87. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  88. 'lklCertificatePath' => $lklCertificatePath,
  89. ];
  90. $laResource = new Lakala($params);
  91. // 1. 先查询拉卡拉订单状态,确保未付款成功,防止时序竞争问题
  92. $queryResponse = $laResource->query(['orderSn' => $orderSn]);
  93. if (isset($queryResponse['code']) && $queryResponse['code'] == 'BBS00000') {
  94. $tradeState = $queryResponse['resp_data']['trade_state'] ?? '';
  95. if ($tradeState == 'SUCCESS') {
  96. util::fail('账单已付款成功,不可取消!');
  97. }
  98. }
  99. // 2. 调用拉卡拉关单,防止后续再次支付成功
  100. $closeParams = [
  101. 'orderSn' => $orderSn,
  102. ];
  103. $response = $laResource->close($closeParams);
  104. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  105. $msg = $response['msg'] ?? '';
  106. $code = $response['code'] ?? 0;
  107. // 如果拉卡拉明确返回已经支付,则阻断本地状态更新为取消
  108. if (in_array($code, ['BBS11112', 'BBS11113'])) {
  109. util::fail('账单在第三方支付渠道已被支付或处理中,无法取消!');
  110. }
  111. if (!in_array($code, ['BBS11114'])) {
  112. noticeUtil::push("\n花店结账单:" . $orderSn . "\n关单失败了\n{$msg} {$code}", '15280215347');
  113. }
  114. }
  115. // 收银台关单
  116. $closeParams = ['orderSn' => $orderSn];
  117. $response = $laResource->cashClose($closeParams);
  118. if (isset($response['code']) == false || $response['code'] != '000000') {
  119. //$msg = $response['msg'] ?? '';
  120. //noticeUtil::push('单号:' . $orderSn . ',关单没有成功(收银台-支付宝),' . $msg, '15280215347');
  121. }
  122. // 3. 第三方关单/状态校验通过后,最后才修改并保存本地数据库状态为 STATUS_EXPIRE
  123. $clear->status = ClearClass::STATUS_EXPIRE;
  124. $clear->remark = "商家取消 " . date("m-d H:i") . ' 操作人 ' . $staffName;
  125. $clear->save();
  126. util::complete();
  127. }
  128. //城市供货商向基地批发商的结账单 ssh 20220523
  129. public function actionList()
  130. {
  131. $get = Yii::$app->request->get();
  132. $where = [];
  133. if (isset($get['status']) && is_numeric($get['status'])) {
  134. $where['status'] = $get['status'];
  135. }
  136. $ghsId = $get['ghsId'] ?? 0;
  137. if (!empty($ghsId)) {
  138. $ghs = GhsClass::getById($ghsId, true);
  139. GhsClass::valid($ghs, $this->shopId);
  140. $where['ghsId'] = $ghsId;
  141. }
  142. $where['customShopId'] = $this->shopId ?? 0;
  143. $list = ClearService::getClearList($where);
  144. util::success($list);
  145. }
  146. //获取基本信息 ssh 20210625
  147. public function actionGetFullInfo()
  148. {
  149. $id = Yii::$app->request->get('id', '');
  150. $info = clearClass::getById($id);
  151. if (isset($info['customShopId']) == false || $info['customShopId'] != $this->shopId) {
  152. util::fail('没有权限');
  153. }
  154. $respond = ClearClass::getGhsCgInfo($info);
  155. util::success($respond);
  156. }
  157. //客户的结帐单 ssh 20210625
  158. public function actionCustomList()
  159. {
  160. $get = Yii::$app->request->get();
  161. $where = [];
  162. if (isset($get['status']) && is_numeric($get['status'])) {
  163. $where['status'] = $get['status'];
  164. }
  165. $customId = $get['customId'] ?? 0;
  166. if (!empty($customId)) {
  167. $custom = CustomClass::getById($customId, true);
  168. CustomClass::valid($custom, $this->shopId);
  169. $where['customId'] = $customId;
  170. }
  171. $where['ghsShopId'] = $this->shopId ?? 0;
  172. $list = ClearService::getClearList($where);
  173. $totalAmount = ClearClass::sum(['ghsShopId' => $this->shopId, 'status' => 2, 'customId' => $customId], 'realPrice');
  174. $list['totalAmount'] = floatval($totalAmount);
  175. util::success($list);
  176. }
  177. //获取基本信息 ssh 20210625
  178. public function actionGetCustomFullInfo()
  179. {
  180. $id = Yii::$app->request->get('id', '');
  181. $info = ClearClass::getById($id);
  182. if (isset($info['ghsShopId']) == false || $info['ghsShopId'] != $this->shopId) {
  183. util::fail('没有权限');
  184. }
  185. $respond = ClearClass::getSaleInfo($info);
  186. //分享结账单时,显示的标题里面,如果是首店或总店,不显示店名
  187. $respond['ghsShopName'] = isset($respond['ghsShopName']) && $respond['ghsShopName'] == '总店' ? '' : $respond['ghsShopName'];
  188. util::success($respond);
  189. }
  190. }