|
|
@@ -0,0 +1,96 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace bizGhs\cg\services;
|
|
|
+
|
|
|
+use bizGhs\base\services\BaseService;
|
|
|
+use bizGhs\cg\classes\CgRefundClass;
|
|
|
+use bizGhs\cg\classes\CgRefundItemClass;
|
|
|
+use bizGhs\order\classes\PurchaseOrderItemClass;
|
|
|
+use bizGhs\product\classes\ProductClass;
|
|
|
+use common\components\dict;
|
|
|
+use common\components\orderSn;
|
|
|
+use common\components\util;
|
|
|
+use Yii;
|
|
|
+
|
|
|
+class CgRefundService extends BaseService
|
|
|
+{
|
|
|
+
|
|
|
+ public static $baseFile = '\bizGhs\cg\classes\CgRefundClass';
|
|
|
+
|
|
|
+ //退款
|
|
|
+ public static function addRefund($order, $post)
|
|
|
+ {
|
|
|
+ $refundPrice = $post['price'] ?? 0;
|
|
|
+ if ($refundPrice <= 0) {
|
|
|
+ util::fail('请填写退款金额');
|
|
|
+ }
|
|
|
+ $order->refundPrice = $refundPrice;
|
|
|
+ $actPrice = $order->actPrice ?? 0;
|
|
|
+ $realPrice = $order->realPrice ?? 0;
|
|
|
+ $currentActPrice = bcsub($actPrice, $refundPrice, 2);
|
|
|
+ $currentRealPrice = bcsub($realPrice, $refundPrice, 2);
|
|
|
+ $order->actPrice = $currentActPrice;
|
|
|
+ $order->realPrice = $currentRealPrice;
|
|
|
+ $order->refund = 2;
|
|
|
+ $order->save();
|
|
|
+
|
|
|
+ $data = [];
|
|
|
+ $data['status'] = 1;
|
|
|
+ $orderSn = orderSn::getGhsCgRefundSn();
|
|
|
+ $data['refundSn'] = $orderSn;
|
|
|
+ $data['relateOrderSn'] = $order->orderSn ?? '';
|
|
|
+ $sjId = $post['sjId'] ?? 0;
|
|
|
+ $refundType = $post['refundType'] ?? 0;
|
|
|
+ $shopId = $post['shopId'] ?? 0;
|
|
|
+ $mainId = $post['mainId'] ?? 0;
|
|
|
+ $data['sjId'] = $sjId;
|
|
|
+ $data['shopId'] = $shopId;
|
|
|
+ $data['mainId'] = $mainId;
|
|
|
+ $data['shopAdminId'] = $post['shopAdminId'] ?? 0;
|
|
|
+ $data['shopAdminName'] = $post['shopAdminName'] ?? '';
|
|
|
+
|
|
|
+ $productData = $post['product'] ?? '';
|
|
|
+
|
|
|
+ if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
|
|
|
+ //前端传过来的结构 [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}]
|
|
|
+ if (empty($productData)) {
|
|
|
+ util::fail('没有退款花材');
|
|
|
+ }
|
|
|
+ $cgItemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $order->orderSn], null, '*', 'productId', true);
|
|
|
+ foreach ($productData as $currentProduct) {
|
|
|
+ $num = $currentProduct['num'] ?? 0;
|
|
|
+ $productId = $currentProduct['productId'] ?? 0;
|
|
|
+ if (empty($productId)) {
|
|
|
+ util::fail('发现无效花材');
|
|
|
+ }
|
|
|
+ $currentCgItem = $cgItemList[$productId] ?? null;
|
|
|
+ $hasRefundNum = $currentCgItem->refundNum ?? 0;
|
|
|
+ $totalNum = $currentCgItem->itemNum ?? 0;
|
|
|
+ $couldRefundNum = bcsub($totalNum, $hasRefundNum);
|
|
|
+ if ($couldRefundNum < $num) {
|
|
|
+ util::fail('超过可退数量');
|
|
|
+ }
|
|
|
+ $name = $currentCgItem->name ?? '';
|
|
|
+ $cover = $currentCgItem->cover ?? '';
|
|
|
+ $ptItemId = $currentCgItem->itemId ?? 0;
|
|
|
+ $refundItemData = [
|
|
|
+ 'refundSn' => $orderSn,
|
|
|
+ 'name' => $name,
|
|
|
+ 'cover' => $cover,
|
|
|
+ 'itemId' => $productId,
|
|
|
+ 'ptItemId' => $ptItemId,
|
|
|
+ 'num' => $num,
|
|
|
+ ];
|
|
|
+ CgRefundItemClass::addData($refundItemData, $order);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ $data['customId'] = $order->customId ?? 0;
|
|
|
+ $data['remark'] = $post['remark'] ?? '';
|
|
|
+ $data['refundPrice'] = $refundPrice;
|
|
|
+ $data['refundType'] = $post['refundType'] ?? 0;
|
|
|
+ $refund = CgRefundClass::add($data, true);
|
|
|
+ return $refund;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|