CgRefundController.php 8.4 KB

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