|
|
@@ -6,11 +6,12 @@ use Yii;
|
|
|
use bizGhs\order\classes\RefundOrderClass;
|
|
|
use common\components\util;
|
|
|
use bizGhs\product\classes\ProductClass;
|
|
|
+use bizGhs\order\classes\OrderClass;
|
|
|
+use bizGhs\order\services\OrderService;
|
|
|
|
|
|
class RefundController extends BaseController
|
|
|
{
|
|
|
|
|
|
-
|
|
|
//列表
|
|
|
public function actionList()
|
|
|
{
|
|
|
@@ -28,4 +29,94 @@ class RefundController extends BaseController
|
|
|
util::success($orderData);
|
|
|
}
|
|
|
|
|
|
+ //退款 linqh 2021.6.25
|
|
|
+ public function actionCreateOrder()
|
|
|
+ {
|
|
|
+ $post = Yii::$app->request->post();
|
|
|
+ $id = isset($post['id']) ? $post['id'] : 0;
|
|
|
+ $order = OrderService::getOrderInfo($id, false, false, false, false);
|
|
|
+ OrderClass::valid($order, $this->shopId);
|
|
|
+ if ($order['status'] != OrderClass::ORDER_STATUS_COMPLETE) {
|
|
|
+ util::fail("订单完成了才能发起退款");
|
|
|
+ }
|
|
|
+ if ($order['refund'] == 2) {
|
|
|
+ util::fail("已经退过了");
|
|
|
+ }
|
|
|
+
|
|
|
+ //根据退款类型:1退货并退款(需要减库存) 2:仅退款(不用减库存,不用传花材)
|
|
|
+ $refundType = $post['refundType'] ?? 0;
|
|
|
+
|
|
|
+ if ($refundType != RefundOrderClass::REFUND_TYPE_MONEY_GOOD && $refundType != RefundOrderClass::REFUND_TYPE_MONEY) {
|
|
|
+ util::fail("请选择退款类型");
|
|
|
+ }
|
|
|
+ if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
|
|
|
+ $infoProduct = $order['product'];
|
|
|
+ $productIds = array_unique(array_filter(array_column($infoProduct, 'productId')));
|
|
|
+ $productData = ProductClass::getProductByIds($productIds);
|
|
|
+ $productData = array_column($productData, NULL, 'id');
|
|
|
+ $orderProductMap = [];
|
|
|
+ foreach ($infoProduct as $v) {
|
|
|
+ $orderProductMap[$v['productId']] = $v;
|
|
|
+ }
|
|
|
+ //[{productId:0,bigNum:1,smallNum:0}]
|
|
|
+ $productJson = $post['product'] ?? '';
|
|
|
+ if (empty($productJson)) {
|
|
|
+ util::fail('请选择花材');
|
|
|
+ }
|
|
|
+ $productList = json_decode($productJson, true);
|
|
|
+ if (!is_array($productList)) {
|
|
|
+ util::fail('花材格式不正确');
|
|
|
+ }
|
|
|
+ //合并
|
|
|
+ $productList = ProductClass::mergeItemInfo($productList);
|
|
|
+ if (empty($productList)) {
|
|
|
+ util::fail('请选择花材');
|
|
|
+ }
|
|
|
+
|
|
|
+ //判定花材数不能大于原订单数
|
|
|
+ foreach ($productList as $v) {
|
|
|
+ $orderProductNum = $orderProductMap[$v['productId']]['num'] ?? 0;
|
|
|
+ $productName = $productData[$v['productId']]['name'] ?? '';
|
|
|
+ if (!$orderProductNum) {
|
|
|
+ util::fail($productName . "花材数量错误");
|
|
|
+ }
|
|
|
+ //数量
|
|
|
+ $bigNum = $v['bigNum'] ?? 0;
|
|
|
+ $smallNum = $v['smallNum'] ?? 0;
|
|
|
+ $ratio = $productData[$v['productId']]['ratio'] ?? 0;
|
|
|
+
|
|
|
+ $num = ProductClass::mergeItemNum($bigNum, $smallNum, $ratio);
|
|
|
+ if ($num > $orderProductNum) {
|
|
|
+ util::fail($productName . "花材数量错误");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $post['product'] = $productList;
|
|
|
+ }
|
|
|
+ $post['price'] = $post['price'] ?? 0;
|
|
|
+ if ($post['price'] <= 0) {
|
|
|
+ util::fail("退款金额不能小于0");
|
|
|
+ }
|
|
|
+ if (bccomp($post['price'], $order['realPrice'], 2) == 1) {
|
|
|
+ util::fail("退款金额不能大于订单金额");
|
|
|
+ }
|
|
|
+
|
|
|
+ $connection = Yii::$app->db;
|
|
|
+ $transaction = $connection->beginTransaction();
|
|
|
+ try {
|
|
|
+ $post['shopId'] = $this->shopId;
|
|
|
+ $post['sjId'] = $this->sjId;
|
|
|
+ $post['shopAdminId'] = $this->shopAdminId;
|
|
|
+ $shopAdmin = $this->shopAdmin;
|
|
|
+ $adminName = $shopAdmin['name'] ?? '';
|
|
|
+ $post['shopAdminName'] = $adminName;
|
|
|
+ OrderService::refund($id, $post);
|
|
|
+ $transaction->commit();
|
|
|
+ } catch (\Exception $exception) {
|
|
|
+ $transaction->rollBack();
|
|
|
+ Yii::info("退款操作报错:" . $exception->getMessage());
|
|
|
+ util::fail('操作失败');
|
|
|
+ }
|
|
|
+ util::complete();
|
|
|
+ }
|
|
|
+
|
|
|
}
|