CgRefundController.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace hd\controllers;
  3. use biz\wx\classes\WxMessageClass;
  4. use bizGhs\notify\classes\NotifyClass;
  5. use bizGhs\order\classes\RefundOrderClass;
  6. use bizHd\cg\classes\CgRefundClass;
  7. use bizHd\cg\classes\CgRefundItemClass;
  8. use bizHd\cg\services\CgRefundService;
  9. use bizHd\ghs\classes\GhsClass;
  10. use bizHd\purchase\classes\PurchaseClass;
  11. use bizHd\shop\classes\ShopClass;
  12. use common\components\imgUtil;
  13. use common\components\noticeUtil;
  14. use Yii;
  15. use common\components\util;
  16. class CgRefundController extends BaseController
  17. {
  18. public $guestAccess = ['list', 'get-refund-data'];
  19. //取消退款申请 ssh 20230808
  20. public function actionCancelRefund()
  21. {
  22. $get = Yii::$app->request->get();
  23. $id = isset($get['id']) ? $get['id'] : 0;
  24. $refund = CgRefundClass::getById($id, true);
  25. if (empty($refund)) {
  26. util::fail('没有找到退款单');
  27. }
  28. if ($refund->mainId != $this->mainId) {
  29. util::fail('无法操作');
  30. }
  31. $connection = Yii::$app->db;
  32. $transaction = $connection->beginTransaction();
  33. try {
  34. CgRefundService::cancelRefundApply($refund);
  35. $transaction->commit();
  36. util::complete('取消成功');
  37. } catch (\Exception $exception) {
  38. $transaction->rollBack();
  39. Yii::info("取消出错了,报错信息:" . $exception->getMessage());
  40. util::fail('取消失败');
  41. }
  42. }
  43. //申请退款 ssh 20230807
  44. public function actionCreateRefund()
  45. {
  46. $post = Yii::$app->request->post();
  47. $id = $post['id'] ?? 0;
  48. $cg = PurchaseClass::getById($id, true);
  49. if (empty($cg)) {
  50. util::fail('没有找到采购单');
  51. }
  52. if ($cg->mainId != $this->mainId) {
  53. util::fail('无法操作');
  54. }
  55. if (in_array($cg->debt, [0, 2]) && $cg->addTime < '2023-10-17 00:00:00') {
  56. util::fail('10月17日前订单无法申请退款');
  57. }
  58. $ghsId = $cg->ghsId ?? 0;
  59. $ghs = GhsClass::getById($ghsId, true);
  60. if (empty($ghs)) {
  61. util::fail('没有供货商信息');
  62. }
  63. $ghsShopId = $ghs->shopId ?? 0;
  64. $ghsShop = ShopClass::getById($ghsShopId, true);
  65. if (isset($ghsShop->afterSale) && $ghsShop->afterSale == 0) {
  66. util::fail('售后功能未开通');
  67. }
  68. if ($cg->status == PurchaseClass::STATUS_UN_PAY) {
  69. util::fail("待付款订单,不能申请售后");
  70. }
  71. if ($cg->status == PurchaseClass::STATUS_UN_SEND) {
  72. util::fail("待发货订单,不能申请售后");
  73. }
  74. if ($cg->status == PurchaseClass::STATUS_SENDING) {
  75. util::fail("订单配送中,不能申请售后");
  76. }
  77. if ($cg->status == PurchaseClass::STATUS_CANCEL) {
  78. util::fail("订单已取消,不能申请售后");
  79. }
  80. if (isset($cg->clearId) && !empty($cg->clearId)) {
  81. util::fail('订单已结,不能再申请售后');
  82. }
  83. $payTime = $cg->payTime;
  84. $currentTime = strtotime($payTime);
  85. $now = time();
  86. $newTime = bcadd($currentTime, 120000);
  87. noticeUtil::push("payTime:{$payTime} currentTime:{$currentTime} now:{$now} newTime:{$newTime}", '15280215347');
  88. if ($now > $newTime) {
  89. util::fail('超时订单,不能自行申请售后');
  90. }
  91. $connection = Yii::$app->db;
  92. $transaction = $connection->beginTransaction();
  93. try {
  94. //退款类型 1退货并退款 2 仅退款
  95. $refundType = $post['refundType'] ?? 1;
  96. if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
  97. //花材列表结构
  98. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
  99. $productJson = $post['product'] ?? '';
  100. if (empty($productJson)) {
  101. util::fail('请选择要退款的花材');
  102. }
  103. $productList = json_decode($productJson, true);
  104. if (!is_array($productList)) {
  105. util::fail('请选择要退款的花材哦!');
  106. }
  107. $post['product'] = $productList;
  108. } else {
  109. //仅退款花材直接设置为空
  110. $post['product'] = [];
  111. }
  112. $post['price'] = $post['price'] ?? 0;
  113. if ($post['price'] <= 0) {
  114. util::fail("退款金额不能小于0");
  115. }
  116. if (bccomp($post['price'], $cg->realPrice, 2) == 1) {
  117. util::fail("退款金额超过订单金额");
  118. }
  119. $post['shopId'] = $this->shopId;
  120. $post['sjId'] = $this->sjId;
  121. $post['shopAdminId'] = $this->shopAdminId;
  122. $post['mainId'] = $this->mainId;
  123. $shopAdmin = $this->shopAdmin;
  124. $adminName = $shopAdmin['name'] ?? '';
  125. $post['shopAdminName'] = $adminName;
  126. $respond = CgRefundService::createOrder($post, $cg);
  127. $transaction->commit();
  128. $saleRefundId = $respond->saleRefundId ?? 0;
  129. $saleRefund = RefundOrderClass::getById($saleRefundId, true);
  130. if (!empty($saleRefund)) {
  131. NotifyClass::addRefundApplyNotify($saleRefund);
  132. $saleShopId = $saleRefund->shopId ?? 0;
  133. $saleShop = Shopclass::getById($saleShopId, true);
  134. if (!empty($saleShop)) {
  135. WxMessageClass::ghsHasNewRefundApplyInform($saleShop, $saleRefund);
  136. }
  137. }
  138. util::success($respond);
  139. } catch (\Exception $exception) {
  140. $transaction->rollBack();
  141. Yii::info("申请出错了,报错信息:" . $exception->getMessage());
  142. util::fail('申请失败');
  143. }
  144. }
  145. //列表
  146. public function actionList()
  147. {
  148. $get = Yii::$app->request->get();
  149. $where = [];
  150. $cgId = $get['cgId'] ?? 0;
  151. if (!empty($cgId)) {
  152. $where['cgId'] = $cgId;
  153. }
  154. $list = CgRefundClass::getRefundList($where);
  155. util::success($list);
  156. }
  157. //根据采购单查退款信息 ssh 20210908
  158. public function actionGetRefundData()
  159. {
  160. $id = Yii::$app->request->get('id', 0);
  161. $refund = CgRefundClass::getById($id);
  162. if (empty($refund)) {
  163. util::fail('没有找到退款信息');
  164. }
  165. $imgString = $refund['imgList'] ?? '';
  166. if (!empty($imgString)) {
  167. $imgArr = json_decode($imgString, true);
  168. $smallImgList = [];
  169. $bigImgList = [];
  170. if (!empty($imgArr)) {
  171. foreach ($imgArr as $image) {
  172. $smallImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  173. $bigImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  174. $smallImgList[] = $smallImg;
  175. $bigImgList[] = $bigImg;
  176. }
  177. }
  178. $refund['imgList'] = $imgArr;
  179. $refund['smallImgList'] = $smallImgList;
  180. $refund['bigImgList'] = $bigImgList;
  181. }
  182. //未登录用户也需要查看,暂时先注释
  183. //CgRefundClass::valid($refund, $this->mainId);
  184. $cgId = $refund['cgId'] ?? 0;
  185. $cg = PurchaseClass::getById($cgId);
  186. if (empty($refund)) {
  187. util::fail('没有退款信息');
  188. }
  189. $orderSn = $refund['orderSn'] ?? '';
  190. $cgItemList = CgRefundItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  191. if (!empty($cgItemList)) {
  192. foreach ($cgItemList as $key => $item) {
  193. $cover = $item['cover'] ?? '';
  194. $shortCover = $cover;
  195. $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  196. $cover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  197. $cgItemList[$key]['cover'] = $cover;
  198. $cgItemList[$key]['bigCover'] = $bigCover;
  199. $cgItemList[$key]['shortCover'] = $shortCover;
  200. $cgItemList[$key]['itemNum'] = isset($item['itemNum']) ? floatval($item['itemNum']) : 0;
  201. }
  202. }
  203. util::success(['info' => $refund, 'itemList' => $cgItemList, 'cgInfo' => $cg]);
  204. }
  205. //根据退款id查退款信息 ssh 20210809
  206. public function actionGetInfo()
  207. {
  208. $id = Yii::$app->request->get('id', 0);
  209. }
  210. }