CgRefundController.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\cg\classes\CgPurchaseNextDayClass;
  4. use bizGhs\cg\classes\CgRefundClass;
  5. use bizGhs\cg\classes\CgRefundItemClass;
  6. use bizGhs\cg\services\CgRefundService;
  7. use bizGhs\order\classes\PurchaseOrderClass;
  8. use common\components\dateUtil;
  9. use common\components\imgUtil;
  10. use common\components\util;
  11. use Yii;
  12. class CgRefundController extends BaseController
  13. {
  14. /**
  15. * 采购原单是否可走隔天售后(售后页预检)
  16. * GET: orderId
  17. */
  18. public function actionCheckNextDayEligible()
  19. {
  20. $orderId = Yii::$app->request->get('orderId', 0);
  21. $order = PurchaseOrderClass::getById($orderId, true);
  22. if (empty($order) || intval($order->mainId) !== intval($this->mainId)) {
  23. util::fail('没有找到采购单');
  24. }
  25. $check = CgPurchaseNextDayClass::checkEligible($order);
  26. $check['hasEntryTime'] = CgPurchaseNextDayClass::hasValidEntryTime($order) ? 1 : 0;
  27. $check['hasNextDay'] = bccomp((string)($order->nextDayTkPrice ?? '0'), '0', 2) > 0 ? 1 : 0;
  28. $check['nextDayTkPrice'] = $order->nextDayTkPrice ?? '0.00';
  29. $check['tkPrice'] = $order->tkPrice ?? '0.00';
  30. $check['actPrice'] = $order->actPrice ?? '0.00';
  31. $check['couldRefundPrice'] = CgPurchaseNextDayClass::mustUseNextDay($order)
  32. ? CgPurchaseNextDayClass::getCouldRefundPrice($order)
  33. : bcadd((string)($order->realPrice ?? '0'), '0', 2);
  34. $check['payWay'] = $order->payWay ?? 0;
  35. $check['debt'] = intval($order->debt ?? 0);
  36. $check['clearId'] = intval($order->clearId ?? 0);
  37. $check['entryTime'] = $order->entryTime ?? '';
  38. util::success($check);
  39. }
  40. //详情 ssh 20220910
  41. public function actionGetRefundFullInfo()
  42. {
  43. $get = Yii::$app->request->get();
  44. $id = $get['id'] ?? 0;
  45. $refund = CgRefundClass::getById($id, true);
  46. CgRefundClass::valid($refund, $this->mainId);
  47. $refundSn = $refund->refundSn ?? '';
  48. $itemList = CgRefundItemClass::getAllByCondition(['refundSn' => $refundSn], null, '*');
  49. if (!empty($itemList)) {
  50. foreach ($itemList as $key => $item) {
  51. $shortCover = $item['cover'] ?? '';
  52. $itemList[$key]['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  53. }
  54. }
  55. util::success(['info' => $refund, 'itemList' => $itemList]);
  56. }
  57. //记录,有重复方法,需要同步修改cg_refund_list ssh 20240817
  58. public function actionGetRefundList()
  59. {
  60. $get = Yii::$app->request->get();
  61. $where = [];
  62. $where['mainId'] = $this->mainId;
  63. $relateOrderSn = $get['relateOrderSn'] ?? '';
  64. if (!empty($relateOrderSn)) {
  65. $where['relateOrderSn'] = $relateOrderSn;
  66. }
  67. $list = CgRefundClass::getRefundList($where);
  68. util::success($list);
  69. }
  70. //记录,有重复方法,需要同步修改cg_refund_list ssh 20240817
  71. public function actionList()
  72. {
  73. $get = Yii::$app->request->get();
  74. $where = [];
  75. $where['mainId'] = $this->mainId;
  76. $relateOrderSn = $get['relateOrderSn'] ?? '';
  77. if (!empty($relateOrderSn)) {
  78. $where['relateOrderSn'] = $relateOrderSn;
  79. }
  80. $searchTime = $get['searchTime'] ?? '';
  81. if (!empty($searchTime)) {
  82. $startTime = $get['startTime'] ?? '';
  83. $endTime = $get['endTime'] ?? '';
  84. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  85. $where['orderTime'] = ['between', [$period['startTime'], $period['endTime']]];
  86. }
  87. $refundSearchTime = $get['refundSearchTime'] ?? '';
  88. if (!empty($refundSearchTime)) {
  89. $startTime = $get['refundStartTime'] ?? '';
  90. $endTime = $get['refundEndTime'] ?? '';
  91. $period = dateUtil::formatTime($refundSearchTime, $startTime, $endTime);
  92. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  93. }
  94. $ghsId = $get['ghsId'] ?? '';
  95. if (!empty($ghsId)) {
  96. $where['ghsId'] = $ghsId;
  97. }
  98. $shopAdminId = $get['shopAdminId'] ?? 0;
  99. if (!empty($shopAdminId)) {
  100. $where['shopAdminId'] = $shopAdminId;
  101. }
  102. $status = $get['status'] ?? -1;
  103. if ($status > -1) {
  104. $where['status'] = $status;
  105. }
  106. $export = $get['export'] ?? 0;
  107. if ($export == 1) {
  108. if (isset($where['orderTime']) == false && isset($where['addTime']) == false) {
  109. util::fail('请选时间');
  110. }
  111. ini_set('memory_limit', '2045M');
  112. set_time_limit(0);
  113. if (isset($where['orderTime'])) {
  114. $start = $where['orderTime'][1][0];
  115. $end = $where['orderTime'][1][1];
  116. $monthTime = bcmul(31, 86400);
  117. $startArea = strtotime($start);
  118. $endArea = strtotime($end);
  119. $diff = bcsub($endArea, $startArea);
  120. if ($diff > $monthTime) {
  121. util::fail('最长可导出一个月');
  122. }
  123. }
  124. if (isset($where['addTime'])) {
  125. $start = $where['addTime'][1][0];
  126. $end = $where['addTime'][1][1];
  127. $monthTime = bcmul(31, 86400);
  128. $startArea = strtotime($start);
  129. $endArea = strtotime($end);
  130. $diff = bcsub($endArea, $startArea);
  131. if ($diff > $monthTime) {
  132. util::fail('最长可导出一个月');
  133. }
  134. }
  135. }
  136. $respond = CgRefundClass::getRefundList($where);
  137. if ($export == 1) {
  138. CgRefundClass::exportRefundData($respond, $this->mainId);
  139. }
  140. util::success($respond);
  141. }
  142. //退款 ssh 20220909;支持隔天售后(sameDay=0,不减 actPrice)
  143. public function actionCreateOrder()
  144. {
  145. $shopAdmin = $this->shopAdmin;
  146. if (!isset($shopAdmin['super']) || $shopAdmin['super'] != 1) {
  147. util::fail('超管才能操作退款');
  148. }
  149. $connection = Yii::$app->db;
  150. $transaction = $connection->beginTransaction();
  151. try {
  152. $post = Yii::$app->request->post();
  153. $id = $post['id'] ?? 0;
  154. $order = PurchaseOrderClass::getLockById($id);
  155. if ($order->mainId != $this->mainId) {
  156. util::fail('没有权限');
  157. }
  158. // 资格、当天/隔天金额上限在 CgRefundService::addRefund 内校验
  159. CgPurchaseNextDayClass::assertCanRefund($order);
  160. if (getenv('YII_ENV') == 'production') {
  161. if ($this->mainId == 23390) {
  162. //小向花卉售后必须备注
  163. $remark = $post['remark'] ?? '';
  164. if (empty($remark)) {
  165. util::fail('请填写备注哈');
  166. }
  167. }
  168. }
  169. //退款类型 1退货并退款 2 仅退款
  170. $refundType = $post['refundType'] ?? 1;
  171. if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
  172. //花材列表结构
  173. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
  174. $productJson = $post['product'] ?? '';
  175. if (empty($productJson)) {
  176. util::fail('请选择花材2');
  177. }
  178. $productList = json_decode($productJson, true);
  179. if (!is_array($productList)) {
  180. util::fail('请选择花材哦9');
  181. }
  182. $post['product'] = $productList;
  183. } else {
  184. //仅退款花材直接设置为空
  185. $post['product'] = [];
  186. }
  187. $post['price'] = $post['price'] ?? 0;
  188. if ($post['price'] <= 0) {
  189. util::fail("退款金额不能小于0");
  190. }
  191. $post['shopId'] = $this->shopId;
  192. $post['shop'] = $this->shop;
  193. $post['sjId'] = $this->sjId;
  194. $post['shopAdminId'] = $this->shopAdminId;
  195. $post['mainId'] = $this->mainId;
  196. $shopAdmin = $this->shopAdmin;
  197. $adminName = $shopAdmin['name'] ?? '';
  198. $post['shopAdminName'] = $adminName;
  199. $refund = CgRefundService::addRefund($order, $post);
  200. $transaction->commit();
  201. util::success([
  202. 'id' => intval($refund->id ?? 0),
  203. 'refundSn' => $refund->refundSn ?? '',
  204. 'sameDay' => intval($refund->sameDay ?? 1),
  205. 'hasReturn' => intval($refund->hasReturn ?? 0),
  206. 'couldReturn' => intval($refund->couldReturn ?? 0),
  207. 'returnBalance' => intval($refund->returnBalance ?? 0),
  208. 'refundPrice' => $refund->refundPrice ?? 0,
  209. ]);
  210. } catch (\Exception $exception) {
  211. if ($transaction->isActive) {
  212. $transaction->rollBack();
  213. }
  214. Yii::info("退款申请出错了,报错信息:" . $exception->getMessage());
  215. // 透出业务失败原因,便于隔天售后排错
  216. util::fail($exception->getMessage() ?: '操作失败');
  217. }
  218. }
  219. }