|
@@ -1,4 +1,8 @@
|
|
|
<?php
|
|
<?php
|
|
|
|
|
+/**
|
|
|
|
|
+ * 上游采购售后服务(biz-ghs/cg/services)
|
|
|
|
|
+ * 用途:创建采购售后单;按 sameDay 分支当天减实付 / 隔天累加 nextDayTkPrice,并买方加余额。
|
|
|
|
|
+ */
|
|
|
|
|
|
|
|
namespace bizGhs\cg\services;
|
|
namespace bizGhs\cg\services;
|
|
|
|
|
|
|
@@ -8,13 +12,13 @@ use bizGhs\book\classes\BookItemClass;
|
|
|
use bizGhs\book\classes\BookItemCustomClass;
|
|
use bizGhs\book\classes\BookItemCustomClass;
|
|
|
use bizGhs\book\classes\BookItemGhsClass;
|
|
use bizGhs\book\classes\BookItemGhsClass;
|
|
|
use bizGhs\cg\classes\CgOrderItemSendClass;
|
|
use bizGhs\cg\classes\CgOrderItemSendClass;
|
|
|
|
|
+use bizGhs\cg\classes\CgPurchaseNextDayClass;
|
|
|
use bizGhs\cg\classes\CgRefundClass;
|
|
use bizGhs\cg\classes\CgRefundClass;
|
|
|
use bizGhs\cg\classes\CgRefundItemClass;
|
|
use bizGhs\cg\classes\CgRefundItemClass;
|
|
|
use bizGhs\ghs\classes\GhsClass;
|
|
use bizGhs\ghs\classes\GhsClass;
|
|
|
use bizGhs\order\classes\PurchaseOrderClass;
|
|
use bizGhs\order\classes\PurchaseOrderClass;
|
|
|
use bizGhs\order\classes\PurchaseOrderItemClass;
|
|
use bizGhs\order\classes\PurchaseOrderItemClass;
|
|
|
use bizGhs\product\classes\ProductClass;
|
|
use bizGhs\product\classes\ProductClass;
|
|
|
-use common\components\dict;
|
|
|
|
|
use common\components\orderSn;
|
|
use common\components\orderSn;
|
|
|
use common\components\util;
|
|
use common\components\util;
|
|
|
use Yii;
|
|
use Yii;
|
|
@@ -24,30 +28,68 @@ class CgRefundService extends BaseService
|
|
|
|
|
|
|
|
public static $baseFile = '\bizGhs\cg\classes\CgRefundClass';
|
|
public static $baseFile = '\bizGhs\cg\classes\CgRefundClass';
|
|
|
|
|
|
|
|
- //退款
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 创建采购售后:当天减 actPrice/加 tkPrice;隔天只加 nextDayTkPrice;均买方加余额。
|
|
|
|
|
+ * @param object $order 已加锁采购单
|
|
|
|
|
+ * @param array $post price/refundType/product/sameDay/...
|
|
|
|
|
+ */
|
|
|
public static function addRefund($order, $post)
|
|
public static function addRefund($order, $post)
|
|
|
{
|
|
{
|
|
|
$refundPrice = $post['price'] ?? 0;
|
|
$refundPrice = $post['price'] ?? 0;
|
|
|
if ($refundPrice <= 0) {
|
|
if ($refundPrice <= 0) {
|
|
|
util::fail('请填写退款金额');
|
|
util::fail('请填写退款金额');
|
|
|
}
|
|
}
|
|
|
- $order->refundPrice = $refundPrice;
|
|
|
|
|
- $currentTkPrice = bcadd($order->tkPrice, $refundPrice, 2);
|
|
|
|
|
- $order->tkPrice = $currentTkPrice;
|
|
|
|
|
- $actPrice = $order->actPrice ?? 0;
|
|
|
|
|
- $realPrice = $order->realPrice ?? 0;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ CgPurchaseNextDayClass::assertCanRefund($order);
|
|
|
|
|
+
|
|
|
|
|
+ // 非当天入库 / 已结清 / 已结账 → 强制隔天
|
|
|
|
|
+ $sameDay = intval($post['sameDay'] ?? CgPurchaseNextDayClass::SAME_DAY_YES);
|
|
|
|
|
+ if (CgPurchaseNextDayClass::mustUseNextDay($order)) {
|
|
|
|
|
+ $sameDay = CgPurchaseNextDayClass::SAME_DAY_NO;
|
|
|
|
|
+ }
|
|
|
|
|
+ if ($sameDay === CgPurchaseNextDayClass::SAME_DAY_NO) {
|
|
|
|
|
+ $check = CgPurchaseNextDayClass::checkEligible($order);
|
|
|
|
|
+ if (empty($check['ok'])) {
|
|
|
|
|
+ util::fail($check['reason'] ?? '当前订单不可隔天售后');
|
|
|
|
|
+ }
|
|
|
|
|
+ $couldRefund = CgPurchaseNextDayClass::getCouldRefundPrice($order);
|
|
|
|
|
+ if (bccomp((string)$refundPrice, $couldRefund, 2) === 1) {
|
|
|
|
|
+ util::fail('退款金额超过可退金额');
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 当天售后须仍待结、未进结账单
|
|
|
|
|
+ if (intval($order->debt ?? 0) !== PurchaseOrderClass::DEBT_YES) {
|
|
|
|
|
+ util::fail('订单没有被结清才能申请当天退货退款');
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!empty($order->clearId)) {
|
|
|
|
|
+ util::fail('已结账订单请走隔天售后');
|
|
|
|
|
+ }
|
|
|
|
|
+ if (bccomp((string)$refundPrice, (string)($order->realPrice ?? '0'), 2) === 1) {
|
|
|
|
|
+ util::fail('退款金额超过订单金额');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
$bookSn = $order->bookSn ?? 0;
|
|
$bookSn = $order->bookSn ?? 0;
|
|
|
- $currentActPrice = bcsub($actPrice, $refundPrice, 2);
|
|
|
|
|
- $currentRealPrice = bcsub($realPrice, $refundPrice, 2);
|
|
|
|
|
- $order->actPrice = $currentActPrice;
|
|
|
|
|
- $order->realPrice = $currentRealPrice;
|
|
|
|
|
- $order->refund = 2;
|
|
|
|
|
- // 待结采购单售后退尽:actPrice 已为 0 须同步改为已结清,否则会留下 debt=待结 的幽灵单
|
|
|
|
|
- if (intval($order->debt ?? 0) === PurchaseOrderClass::DEBT_YES
|
|
|
|
|
- && bccomp($currentActPrice, '0', 2) <= 0) {
|
|
|
|
|
- $order->debt = PurchaseOrderClass::DEBT_NO;
|
|
|
|
|
|
|
+ $isNextDay = $sameDay === CgPurchaseNextDayClass::SAME_DAY_NO;
|
|
|
|
|
+
|
|
|
|
|
+ // 当天:减实付、累加 tkPrice;隔天:实付不动,nextDayTkPrice 在 applyAmountAndFund 写入
|
|
|
|
|
+ if (!$isNextDay) {
|
|
|
|
|
+ $currentTkPrice = bcadd($order->tkPrice, $refundPrice, 2);
|
|
|
|
|
+ $order->tkPrice = $currentTkPrice;
|
|
|
|
|
+ $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;
|
|
|
|
|
+ // 待结采购单售后退尽:actPrice 已为 0 须同步改为已结清,否则会留下 debt=待结 的幽灵单
|
|
|
|
|
+ if (intval($order->debt ?? 0) === PurchaseOrderClass::DEBT_YES
|
|
|
|
|
+ && bccomp($currentActPrice, '0', 2) <= 0) {
|
|
|
|
|
+ $order->debt = PurchaseOrderClass::DEBT_NO;
|
|
|
|
|
+ }
|
|
|
|
|
+ $order->save();
|
|
|
}
|
|
}
|
|
|
- $order->save();
|
|
|
|
|
|
|
|
|
|
$data = [];
|
|
$data = [];
|
|
|
$data['status'] = 1;
|
|
$data['status'] = 1;
|
|
@@ -98,9 +140,12 @@ class CgRefundService extends BaseService
|
|
|
$unitName = $currentCgItem->bigUnit ?? '';
|
|
$unitName = $currentCgItem->bigUnit ?? '';
|
|
|
$unitCost = $currentCgItem->cost ?? 0;
|
|
$unitCost = $currentCgItem->cost ?? 0;
|
|
|
|
|
|
|
|
- //已退数量增加
|
|
|
|
|
|
|
+ // refundNum=总已退;隔天部分另记 nextRefundNum
|
|
|
$currentRefundNum = bcadd($currentCgItem->refundNum, $num);
|
|
$currentRefundNum = bcadd($currentCgItem->refundNum, $num);
|
|
|
$currentCgItem->refundNum = $currentRefundNum;
|
|
$currentCgItem->refundNum = $currentRefundNum;
|
|
|
|
|
+ if ($isNextDay) {
|
|
|
|
|
+ $currentCgItem->nextRefundNum = bcadd($currentCgItem->nextRefundNum ?? 0, $num);
|
|
|
|
|
+ }
|
|
|
$currentCgItem->save();
|
|
$currentCgItem->save();
|
|
|
|
|
|
|
|
$totalPrice = bcmul($unitPrice, $num, 2);
|
|
$totalPrice = bcmul($unitPrice, $num, 2);
|
|
@@ -234,6 +279,15 @@ class CgRefundService extends BaseService
|
|
|
$data['orderTime'] = $order->entryTime;
|
|
$data['orderTime'] = $order->entryTime;
|
|
|
$data['ghsId'] = $order->ghsId ?? 0;
|
|
$data['ghsId'] = $order->ghsId ?? 0;
|
|
|
$data['ghsName'] = $order->ghsName ?? '';
|
|
$data['ghsName'] = $order->ghsName ?? '';
|
|
|
|
|
+
|
|
|
|
|
+ $snap = CgPurchaseNextDayClass::buildRefundPaySnapshot($order);
|
|
|
|
|
+ $data['sameDay'] = $sameDay;
|
|
|
|
|
+ $data['onlinePay'] = $snap['onlinePay'];
|
|
|
|
|
+ $data['payWay'] = $snap['payWay'];
|
|
|
|
|
+ $data['hasReturn'] = $snap['hasReturn'];
|
|
|
|
|
+ $data['couldReturn'] = $snap['couldReturn'];
|
|
|
|
|
+ $data['returnBalance'] = $snap['returnBalance'];
|
|
|
|
|
+
|
|
|
$refund = CgRefundClass::add($data, true);
|
|
$refund = CgRefundClass::add($data, true);
|
|
|
|
|
|
|
|
//锁定当前增加资产的运作,有多处,请搜索关键词 ghsAddDebtAction
|
|
//锁定当前增加资产的运作,有多处,请搜索关键词 ghsAddDebtAction
|
|
@@ -242,9 +296,16 @@ class CgRefundService extends BaseService
|
|
|
if (empty($ghs)) {
|
|
if (empty($ghs)) {
|
|
|
util::fail('没有找到供货商');
|
|
util::fail('没有找到供货商');
|
|
|
}
|
|
}
|
|
|
- PurchaseOrderClass::applyPurchaseRefundOnAccounts($ghs, $refundPrice, $refund, $order, $sjId, $shopId);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if ($isNextDay) {
|
|
|
|
|
+ CgPurchaseNextDayClass::applyAmountAndFund($refund, $order, $ghs, $sjId, $shopId);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ PurchaseOrderClass::applyPurchaseRefundOnAccounts($ghs, $refundPrice, $refund, $order, $sjId, $shopId);
|
|
|
|
|
+ $refund->hasReturn = CgPurchaseNextDayClass::FLAG_YES;
|
|
|
|
|
+ $refund->save(false, ['hasReturn']);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
return $refund;
|
|
return $refund;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
+}
|