CgRefundController.php 8.7 KB

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