CgRefundController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. //限制12小时内可以主动申请售后
  87. $changeTime = 43200;
  88. if ($ghsShop->pfLevel && $ghsShop->pfLevel == 1) {
  89. //昆明批发商售后时间调整为3天
  90. $changeTime = bcmul(3, 86400);
  91. }
  92. if($ghsShopId == 26578){
  93. //九江云朵48小时内可以自主售后
  94. $changeTime = bcmul(2, 86400);
  95. }
  96. $newTime = bcadd($currentTime, $changeTime);
  97. if ($now > $newTime) {
  98. util::fail('超时订单,请联系门店售后');
  99. }
  100. $connection = Yii::$app->db;
  101. $transaction = $connection->beginTransaction();
  102. try {
  103. //退款类型 1退货并退款 2 仅退款
  104. $refundType = $post['refundType'] ?? 1;
  105. if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
  106. //花材列表结构
  107. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
  108. $productJson = $post['product'] ?? '';
  109. if (empty($productJson)) {
  110. util::fail('请选择要退款的花材');
  111. }
  112. $productList = json_decode($productJson, true);
  113. if (!is_array($productList)) {
  114. util::fail('请选择要退款的花材哦!');
  115. }
  116. $post['product'] = $productList;
  117. } else {
  118. //仅退款花材直接设置为空
  119. $post['product'] = [];
  120. }
  121. $post['price'] = $post['price'] ?? 0;
  122. if ($post['price'] <= 0) {
  123. util::fail("退款金额不能小于0");
  124. }
  125. if (bccomp($post['price'], $cg->realPrice, 2) == 1) {
  126. util::fail("退款金额超过订单金额");
  127. }
  128. $post['shopId'] = $this->shopId;
  129. $post['sjId'] = $this->sjId;
  130. $post['shopAdminId'] = $this->shopAdminId;
  131. $post['mainId'] = $this->mainId;
  132. $shopAdmin = $this->shopAdmin;
  133. $adminName = $shopAdmin['name'] ?? '';
  134. $post['shopAdminName'] = $adminName;
  135. $respond = CgRefundService::createOrder($post, $cg);
  136. $transaction->commit();
  137. RefundOrderClass::printRefund($respond);
  138. $saleRefundId = $respond->saleRefundId ?? 0;
  139. $saleRefund = RefundOrderClass::getById($saleRefundId, true);
  140. if (!empty($saleRefund)) {
  141. NotifyClass::addRefundApplyNotify($saleRefund);
  142. $saleShopId = $saleRefund->shopId ?? 0;
  143. $saleShop = Shopclass::getById($saleShopId, true);
  144. if (!empty($saleShop)) {
  145. WxMessageClass::ghsHasNewRefundApplyInform($saleShop, $saleRefund);
  146. }
  147. }
  148. util::success($respond);
  149. } catch (\Exception $exception) {
  150. $transaction->rollBack();
  151. Yii::info("申请出错了,报错信息:" . $exception->getMessage());
  152. util::fail('申请失败');
  153. }
  154. }
  155. //列表
  156. public function actionList()
  157. {
  158. $get = Yii::$app->request->get();
  159. $where = [];
  160. $cgId = $get['cgId'] ?? 0;
  161. if (!empty($cgId)) {
  162. $where['cgId'] = $cgId;
  163. }
  164. $list = CgRefundClass::getRefundList($where);
  165. util::success($list);
  166. }
  167. //根据采购单查退款信息 ssh 20210908
  168. public function actionGetRefundData()
  169. {
  170. $id = Yii::$app->request->get('id', 0);
  171. $refund = CgRefundClass::getById($id);
  172. if (empty($refund)) {
  173. util::fail('没有找到退款信息');
  174. }
  175. $imgString = $refund['imgList'] ?? '';
  176. if (!empty($imgString)) {
  177. $imgArr = json_decode($imgString, true);
  178. $smallImgList = [];
  179. $bigImgList = [];
  180. if (!empty($imgArr)) {
  181. foreach ($imgArr as $image) {
  182. $smallImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  183. $bigImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  184. $smallImgList[] = $smallImg;
  185. $bigImgList[] = $bigImg;
  186. }
  187. }
  188. $refund['imgList'] = $imgArr;
  189. $refund['smallImgList'] = $smallImgList;
  190. $refund['bigImgList'] = $bigImgList;
  191. }
  192. //未登录用户也需要查看,暂时先注释
  193. //CgRefundClass::valid($refund, $this->mainId);
  194. $cgId = $refund['cgId'] ?? 0;
  195. $cg = PurchaseClass::getById($cgId);
  196. if (empty($refund)) {
  197. util::fail('没有退款信息');
  198. }
  199. $orderSn = $refund['orderSn'] ?? '';
  200. $cgItemList = CgRefundItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  201. if (!empty($cgItemList)) {
  202. foreach ($cgItemList as $key => $item) {
  203. $cover = $item['cover'] ?? '';
  204. $shortCover = $cover;
  205. $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  206. $cover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  207. $cgItemList[$key]['cover'] = $cover;
  208. $cgItemList[$key]['bigCover'] = $bigCover;
  209. $cgItemList[$key]['shortCover'] = $shortCover;
  210. $cgItemList[$key]['itemNum'] = isset($item['itemNum']) ? floatval($item['itemNum']) : 0;
  211. }
  212. }
  213. util::success(['info' => $refund, 'itemList' => $cgItemList, 'cgInfo' => $cg]);
  214. }
  215. //根据退款id查退款信息 ssh 20210809
  216. public function actionGetInfo()
  217. {
  218. $id = Yii::$app->request->get('id', 0);
  219. }
  220. }