CgRefundController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\cg\classes\CgRefundClass;
  4. use bizHd\cg\classes\CgRefundItemClass;
  5. use bizHd\cg\services\CgRefundService;
  6. use bizHd\purchase\classes\PurchaseClass;
  7. use common\components\imgUtil;
  8. use Yii;
  9. use common\components\util;
  10. class CgRefundController extends BaseController
  11. {
  12. public $guestAccess = ['list', 'get-refund-data'];
  13. //取消退款申请 ssh 20230808
  14. public function actionCancelRefund()
  15. {
  16. $get = Yii::$app->request->get();
  17. $id = isset($get['id']) ? $get['id'] : 0;
  18. $refund = CgRefundClass::getById($id, true);
  19. if (empty($refund)) {
  20. util::fail('没有找到退款单');
  21. }
  22. if ($refund->mainId != $this->mainId) {
  23. util::fail('没有权限操作');
  24. }
  25. $connection = Yii::$app->db;
  26. $transaction = $connection->beginTransaction();
  27. try {
  28. CgRefundService::cancelRefundApply($refund);
  29. $transaction->commit();
  30. util::complete('取消成功');
  31. } catch (\Exception $exception) {
  32. $transaction->rollBack();
  33. Yii::info("取消出错了,报错信息:" . $exception->getMessage());
  34. util::fail('取消失败');
  35. }
  36. }
  37. //申请退款 ssh 20230807
  38. public function actionCreateRefund()
  39. {
  40. $post = Yii::$app->request->post();
  41. $id = isset($post['id']) ? $post['id'] : 0;
  42. $cg = PurchaseClass::getById($id, true);
  43. if (empty($cg)) {
  44. util::fail('没有找到采购单');
  45. }
  46. if ($cg->mainId != $this->mainId) {
  47. util::fail('没有权限操作');
  48. }
  49. if ($cg->status != PurchaseClass::STATUS_COMPLETE) {
  50. util::fail("完成的订单才能申请退货退款");
  51. }
  52. $connection = Yii::$app->db;
  53. $transaction = $connection->beginTransaction();
  54. try {
  55. //退款类型 1退货并退款 2 仅退款
  56. $refundType = $post['refundType'] ?? 1;
  57. if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
  58. //花材列表结构
  59. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
  60. $productJson = $post['product'] ?? '';
  61. if (empty($productJson)) {
  62. util::fail('请选择要退款的花材');
  63. }
  64. $productList = json_decode($productJson, true);
  65. if (!is_array($productList)) {
  66. util::fail('请选择要退款的花材哦!');
  67. }
  68. $post['product'] = $productList;
  69. } else {
  70. //仅退款花材直接设置为空
  71. $post['product'] = [];
  72. }
  73. $post['price'] = $post['price'] ?? 0;
  74. if ($post['price'] <= 0) {
  75. util::fail("退款金额不能小于0");
  76. }
  77. if (bccomp($post['price'], $cg->realPrice, 2) == 1) {
  78. util::fail("退款金额超过订单金额");
  79. }
  80. $post['shopId'] = $this->shopId;
  81. $post['sjId'] = $this->sjId;
  82. $post['shopAdminId'] = $this->shopAdminId;
  83. $post['mainId'] = $this->mainId;
  84. $shopAdmin = $this->shopAdmin;
  85. $adminName = $shopAdmin['name'] ?? '';
  86. $post['shopAdminName'] = $adminName;
  87. $respond = CgRefundService::createOrder($post, $cg);
  88. $transaction->commit();
  89. util::success($respond);
  90. } catch (\Exception $exception) {
  91. $transaction->rollBack();
  92. Yii::info("申请出错了,报错信息:" . $exception->getMessage());
  93. util::fail('申请失败');
  94. }
  95. }
  96. //列表
  97. public function actionList()
  98. {
  99. $get = Yii::$app->request->get();
  100. $where = [];
  101. $cgId = $get['cgId'] ?? 0;
  102. if (!empty($cgId)) {
  103. $where['cgId'] = $cgId;
  104. }
  105. $list = CgRefundClass::getRefundList($where);
  106. util::success($list);
  107. }
  108. //根据采购单查退款信息 ssh 20210908
  109. public function actionGetRefundData()
  110. {
  111. $id = Yii::$app->request->get('id', 0);
  112. $refund = CgRefundClass::getById($id);
  113. if (empty($refund)) {
  114. util::fail('没有找到退款信息');
  115. }
  116. $imgString = $refund['imgList'] ?? '';
  117. if (!empty($imgString)) {
  118. $imgArr = json_decode($imgString, true);
  119. $smallImgList = [];
  120. $bigImgList = [];
  121. if (!empty($imgArr)) {
  122. foreach ($imgArr as $image) {
  123. $smallImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  124. $bigImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  125. $smallImgList[] = $smallImg;
  126. $bigImgList[] = $bigImg;
  127. }
  128. }
  129. $refund['imgList'] = $imgArr;
  130. $refund['smallImgList'] = $smallImgList;
  131. $refund['bigImgList'] = $bigImgList;
  132. }
  133. //未登录用户也需要查看,暂时先注释
  134. //CgRefundClass::valid($refund, $this->mainId);
  135. $cgId = $refund['cgId'] ?? 0;
  136. $cg = PurchaseClass::getById($cgId);
  137. if (empty($refund)) {
  138. util::fail('没有退款信息');
  139. }
  140. $orderSn = $refund['orderSn'] ?? '';
  141. $cgItemList = CgRefundItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  142. if (!empty($cgItemList)) {
  143. foreach ($cgItemList as $key => $item) {
  144. $cover = $item['cover'] ?? '';
  145. $shortCover = $cover;
  146. $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  147. $cover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  148. $cgItemList[$key]['cover'] = $cover;
  149. $cgItemList[$key]['bigCover'] = $bigCover;
  150. $cgItemList[$key]['shortCover'] = $shortCover;
  151. $cgItemList[$key]['itemNum'] = isset($item['itemNum']) ? floatval($item['itemNum']) : 0;
  152. }
  153. }
  154. util::success(['info' => $refund, 'itemList' => $cgItemList, 'cgInfo' => $cg]);
  155. }
  156. //根据退款id查退款信息 ssh 20210809
  157. public function actionGetInfo()
  158. {
  159. $id = Yii::$app->request->get('id', 0);
  160. }
  161. }