|
|
@@ -7,6 +7,7 @@ use biz\shop\classes\ShopClass;
|
|
|
use biz\stat\classes\StatOutClass;
|
|
|
use bizGhs\base\services\BaseService;
|
|
|
use bizGhs\custom\classes\CustomClass;
|
|
|
+use bizGhs\order\classes\OrderClass;
|
|
|
use bizGhs\order\classes\RefundOrderClass;
|
|
|
use bizGhs\order\classes\RefundOrderItemClass;
|
|
|
use bizGhs\stock\classes\StockRecordClass;
|
|
|
@@ -24,6 +25,9 @@ class RefundOrderService extends BaseService
|
|
|
//新增退款
|
|
|
public static function addRefund($post, $order)
|
|
|
{
|
|
|
+ $order->refund = OrderClass::REFUND_YES;
|
|
|
+ $order->save();
|
|
|
+
|
|
|
$data = [];
|
|
|
$data['status'] = RefundOrderClass::STATUS_COMPLETE;
|
|
|
$orderSn = orderSn::getGhsRefundSn();
|
|
|
@@ -51,101 +55,119 @@ class RefundOrderService extends BaseService
|
|
|
$data['refundPrice'] = $refundPrice;
|
|
|
$data['refundType'] = $post['refundType'] ?? 0;
|
|
|
|
|
|
- try {
|
|
|
-
|
|
|
- //写入订单表
|
|
|
- $refund = RefundOrderClass::add($data, true);
|
|
|
- if ($data['refundType'] == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
|
|
|
- //退货,加库存
|
|
|
- foreach ($ghsItemInfo as $k => $v) {
|
|
|
- $stockInfo = ProductClass::addStock($v['productId'], $v['bigNum'], $v['smallNum']);
|
|
|
- $ghsItemInfo[$k]['oldStock'] = $stockInfo['oldStock'];
|
|
|
- $ghsItemInfo[$k]['newStock'] = $stockInfo['newStock'];
|
|
|
- }
|
|
|
- }
|
|
|
- //写入详情表
|
|
|
- if ($ghsItemInfo) {
|
|
|
- $batchData = RefundOrderClass::groupOrderItem($ghsItemInfo, $orderSn);
|
|
|
- RefundOrderItemClass::batchAddOrderItem($batchData);
|
|
|
+ //写入订单表
|
|
|
+ $refund = RefundOrderClass::add($data, true);
|
|
|
+ if ($data['refundType'] == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
|
|
|
+ //退货,加库存
|
|
|
+ foreach ($ghsItemInfo as $k => $v) {
|
|
|
+ $stockInfo = ProductClass::addStock($v['productId'], $v['bigNum'], $v['smallNum']);
|
|
|
+ $ghsItemInfo[$k]['oldStock'] = $stockInfo['oldStock'];
|
|
|
+ $ghsItemInfo[$k]['newStock'] = $stockInfo['newStock'];
|
|
|
}
|
|
|
- $orderData = RefundOrderClass::getOrderDetail($orderSn, $refund->shopId);
|
|
|
- $orderData['shopId'] = $data['shopId'];
|
|
|
- if ($data['refundType'] == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
|
|
|
- //写流水
|
|
|
- StockRecordClass::addRecordByOrder($orderData, StockRecordClass::STOCK_RECORD_TYPE_REFUND);
|
|
|
+ }
|
|
|
+ //写入详情表
|
|
|
+ if ($ghsItemInfo) {
|
|
|
+ $batchData = RefundOrderClass::groupOrderItem($ghsItemInfo, $orderSn);
|
|
|
+ RefundOrderItemClass::batchAddOrderItem($batchData);
|
|
|
+ }
|
|
|
+ $orderData = RefundOrderClass::getOrderDetail($orderSn, $refund->shopId);
|
|
|
+ $orderData['shopId'] = $data['shopId'];
|
|
|
+ if ($data['refundType'] == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
|
|
|
+ //写流水
|
|
|
+ StockRecordClass::addRecordByOrder($orderData, StockRecordClass::STOCK_RECORD_TYPE_REFUND);
|
|
|
+ }
|
|
|
+
|
|
|
+ $payWay = $order->payWay;
|
|
|
+ $shopId = $order->shopId;
|
|
|
+ $shop = ShopClass::getLockById($shopId);
|
|
|
+ if (empty($shop)) {
|
|
|
+ util::fail('没有找到门店');
|
|
|
+ }
|
|
|
+ //总支出增加
|
|
|
+ $currentTotalExpend = bcadd($shop->totalExpend, $refundPrice, 2);
|
|
|
+ $shop->totalExpend = $currentTotalExpend;
|
|
|
+ $shop->save();
|
|
|
+
|
|
|
+ //支出流水
|
|
|
+ $thisType = dict::getDict('capitalType', 'saleRefund', 'id');
|
|
|
+ $sjId = $order->sjId ?? 0;
|
|
|
+ $event = '销售退款';
|
|
|
+ $capitalData = [
|
|
|
+ 'capitalType' => $thisType,
|
|
|
+ 'io' => 0,
|
|
|
+ 'totalExpend' => $currentTotalExpend,
|
|
|
+ 'payWay' => 0,
|
|
|
+ 'amount' => $refundPrice,
|
|
|
+ 'sjId' => $sjId,
|
|
|
+ 'shopId' => $shopId,
|
|
|
+ 'event' => $event,
|
|
|
+ ];
|
|
|
+ ShopCapitalClass::addCapital($capitalData);
|
|
|
+ //当天和当月支出统计
|
|
|
+ StatOutClass::updateOrInsert($shop, $refundPrice);
|
|
|
+
|
|
|
+ if ($payWay == dict::getDict('payWay', 'wxPay')) {
|
|
|
+ //门店余额减少
|
|
|
+ ShopClass::saleRefundReduceBalance($shop, $refund, $refundPrice);
|
|
|
+ } elseif ($payWay == dict::getDict('payWay', 'debtPay')) {
|
|
|
+
|
|
|
+ $clearId = $order->clearId;
|
|
|
+ if (empty($clearId)) {
|
|
|
+ util::fail('订单已月结,无法再发起退款');
|
|
|
}
|
|
|
|
|
|
- $payWay = $order->payWay;
|
|
|
- $shopId = $order->shopId;
|
|
|
- $shop = ShopClass::getLockById($shopId);
|
|
|
- if (empty($shop)) {
|
|
|
- util::fail('没有找到门店');
|
|
|
+ //门店应收款减少
|
|
|
+ $currentMayGathering = bcsub($shop->mayGathering, $refundPrice, 2);
|
|
|
+ if ($currentMayGathering < 0) {
|
|
|
+ util::fail('应付款有问题');
|
|
|
}
|
|
|
- //总支出增加
|
|
|
- $currentTotalExpend = bcadd($shop->totalExpend, $refundPrice, 2);
|
|
|
- $shop->totalExpend = $currentTotalExpend;
|
|
|
+ $shop->mayGathering = $currentMayGathering;
|
|
|
$shop->save();
|
|
|
|
|
|
- //支出流水
|
|
|
- $thisType = dict::getDict('capitalType', 'saleRefund', 'id');
|
|
|
- $sjId = $order->sjId ?? 0;
|
|
|
- $event = '销售退款';
|
|
|
- $capitalData = [
|
|
|
- 'capitalType' => $thisType,
|
|
|
- 'io' => 0,
|
|
|
- 'totalExpend' => $currentTotalExpend,
|
|
|
- 'payWay' => 0,
|
|
|
- 'amount' => $refundPrice,
|
|
|
- 'sjId' => $sjId,
|
|
|
- 'shopId' => $shopId,
|
|
|
- 'event' => $event,
|
|
|
- ];
|
|
|
- ShopCapitalClass::addCapital($capitalData);
|
|
|
- //当天和当月支出统计
|
|
|
- StatOutClass::updateOrInsert($shop, $refundPrice);
|
|
|
-
|
|
|
- if ($payWay == dict::getDict('payWay', 'wxPay')) {
|
|
|
- //门店余额减少
|
|
|
- ShopClass::saleRefundReduceBalance($shop, $refund, $refundPrice);
|
|
|
- } elseif ($payWay == dict::getDict('payWay', 'debtPay')) {
|
|
|
-
|
|
|
- //门店应收款减少
|
|
|
- $currentMayGathering = bcsub($shop->mayGathering, $refundPrice, 2);
|
|
|
- if ($currentMayGathering < 0) {
|
|
|
- util::fail('应付款有问题');
|
|
|
- }
|
|
|
- $shop->mayGathering = $currentMayGathering;
|
|
|
- $shop->save();
|
|
|
-
|
|
|
- //门店客户欠款减少
|
|
|
- $customId = $order->customId;
|
|
|
- $custom = CustomClass::getLockById($customId);
|
|
|
- if (empty($custom)) {
|
|
|
- util::fail('没有找到客户');
|
|
|
- }
|
|
|
- $currentDebtAmount = bcsub($custom->debtAmount, $refundPrice, 2);
|
|
|
- if ($currentDebtAmount < 0) {
|
|
|
- util::fail('客户欠款有问题');
|
|
|
- }
|
|
|
- $custom->debtAmount = $currentDebtAmount;
|
|
|
- $custom->save();
|
|
|
- if ($currentDebtAmount == 0) {
|
|
|
- $shop->mayGatheringNum -= 1;
|
|
|
- $shop->save();
|
|
|
- }
|
|
|
-
|
|
|
- } elseif ($payWay == dict::getDict('payWay', 'balancePay')) {
|
|
|
- //门店余额减少
|
|
|
- ShopClass::saleRefundReduceBalance($shop, $refund, $refundPrice);
|
|
|
- } else {
|
|
|
- util::fail('支付方式没有找到');
|
|
|
+ //减掉退款金额
|
|
|
+ $currentRefund = bcadd($order->refundPrice, $refundPrice, 2);
|
|
|
+ $order->refundPrice = $currentRefund;
|
|
|
+ $currentActPrice = bcsub($order->actPrice, $refundPrice, 2);
|
|
|
+ if ($currentActPrice < 0) {
|
|
|
+ util::fail('退款金额有问题!');
|
|
|
+ }
|
|
|
+ $order->actPrice = $currentActPrice;
|
|
|
+ $currentRealPrice = bcsub($order->realPrice, $refundPrice, 2);
|
|
|
+ if ($currentRealPrice < 0) {
|
|
|
+ util::fail('退款金额有问题,请排查');
|
|
|
+ }
|
|
|
+ $order->realPrice = $currentRealPrice;
|
|
|
+ if ($currentRealPrice == 0) {
|
|
|
+ $order->debt = OrderClass::DEBT_NO;
|
|
|
}
|
|
|
+ $order->save();
|
|
|
|
|
|
- return $refund;
|
|
|
+ //门店客户欠款减少
|
|
|
+ $customId = $order->customId;
|
|
|
+ $custom = CustomClass::getLockById($customId);
|
|
|
+ if (empty($custom)) {
|
|
|
+ util::fail('没有找到客户');
|
|
|
+ }
|
|
|
+ $currentDebtAmount = bcsub($custom->debtAmount, $refundPrice, 2);
|
|
|
+ if ($currentDebtAmount < 0) {
|
|
|
+ util::fail('客户欠款有问题');
|
|
|
+ }
|
|
|
+ $custom->debtAmount = $currentDebtAmount;
|
|
|
+ $custom->save();
|
|
|
+ if ($currentDebtAmount == 0) {
|
|
|
+ $shop->mayGatheringNum -= 1;
|
|
|
+ $shop->save();
|
|
|
+ }
|
|
|
|
|
|
- } catch (\Exception $exception) {
|
|
|
- util::fail("操作失败" . $exception->getMessage());
|
|
|
+ } elseif ($payWay == dict::getDict('payWay', 'balancePay')) {
|
|
|
+ //门店余额减少
|
|
|
+ ShopClass::saleRefundReduceBalance($shop, $refund, $refundPrice);
|
|
|
+ } else {
|
|
|
+ util::fail('支付方式没有找到');
|
|
|
}
|
|
|
+
|
|
|
+ return $refund;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
}
|