|
|
@@ -4,6 +4,7 @@ namespace bizHd\purchase\classes;
|
|
|
|
|
|
use biz\ghs\classes\GhsClass;
|
|
|
use biz\product\classes\XjClass;
|
|
|
+use biz\shop\classes\ShopCapitalClass;
|
|
|
use biz\shop\classes\ShopClass;
|
|
|
use biz\shop\classes\ShopCouponClass;
|
|
|
use biz\sj\classes\SjClass;
|
|
|
@@ -14,7 +15,10 @@ use bizGhs\order\traits\OrderTrait;
|
|
|
use bizGhs\product\classes\ProductClass;
|
|
|
use bizGhs\stock\classes\StockRecordClass;
|
|
|
use bizGhs\stock\services\StockRecordService;
|
|
|
+use bizHd\cg\classes\CgRefundClass;
|
|
|
use bizHd\cg\classes\CgXjClass;
|
|
|
+use bizHd\stat\classes\StatIncomeClass;
|
|
|
+use bizHd\wx\classes\WxOpenClass;
|
|
|
use common\components\dict;
|
|
|
use common\components\imgUtil;
|
|
|
use common\components\orderSn;
|
|
|
@@ -102,16 +106,113 @@ class PurchaseClass extends BaseClass
|
|
|
$cg->realPrice = $currentPrice;
|
|
|
$cg->stockChange = 1;
|
|
|
$cg->save();
|
|
|
+
|
|
|
+ $mainId = $cg->mainId ?? 0;
|
|
|
+ $sjId = $cg->sjId ?? 0;
|
|
|
+ $shopId = $cg->shopId ?? 0;
|
|
|
+ $cgId = $cg->id ?? 0;
|
|
|
+ $saleRefundId = $data['saleRefundId'] ?? 0;
|
|
|
+
|
|
|
+ $shop = ShopClass::getLockById($shopId);
|
|
|
+ if (empty($shop)) {
|
|
|
+ util::fail('没有找到零售店的门店');
|
|
|
+ }
|
|
|
+
|
|
|
$bookPrice = $cg->bookPrice ?? 0;
|
|
|
if ($bookPrice == $currentPrice) {
|
|
|
return true;
|
|
|
} elseif ($bookPrice > $currentPrice) {
|
|
|
//退款
|
|
|
-// $refundPrice = bcsub($bookPrice, $currentPrice, 2);
|
|
|
-// //退款
|
|
|
-// RefundOrderClass::add($data, true);
|
|
|
-// //门店余额减少
|
|
|
-// ShopClass::saleRefundReduceBalance($shop, $refund, $refundPrice);
|
|
|
+ $refundPrice = bcsub($bookPrice, $currentPrice, 2);
|
|
|
+
|
|
|
+ $book = 1;
|
|
|
+ $orderSn = orderSn::getCgRefundSn();
|
|
|
+ $refundType = CgRefundClass::REFUND_TYPE_MONEY;
|
|
|
+ $refundData = [
|
|
|
+ 'book' => $book,
|
|
|
+ 'mainId' => $mainId,
|
|
|
+ 'orderSn' => $orderSn,
|
|
|
+ 'cgId' => $cgId,
|
|
|
+ 'thirdRefundNo' => '',
|
|
|
+ 'status' => 0,
|
|
|
+ 'shopAdminId' => '',
|
|
|
+ 'shopAdminName' => '',
|
|
|
+ 'refundType' => $refundType,
|
|
|
+ 'refundPrice' => $refundPrice,
|
|
|
+ 'saleRefundId' => $saleRefundId,
|
|
|
+ 'sjId' => $sjId,
|
|
|
+ 'shopId' => $shopId,
|
|
|
+ ];
|
|
|
+ $cgRefund = CgRefundClass::add($refundData, true);
|
|
|
+
|
|
|
+ //门店收入增加
|
|
|
+ $currentTotalIncome = bcadd($shop->totalIncome, $refundPrice, 2);
|
|
|
+ $shop->totalIncome = $currentTotalIncome;
|
|
|
+ $shop->save();
|
|
|
+
|
|
|
+ $currentType = dict::getDict('capitalType', 'hdBookRefund', 'id');
|
|
|
+ $sjId = $cg->sjId ?? 0;
|
|
|
+ $shopId = $cg->shopId ?? 0;
|
|
|
+ $ghsName = $cg->ghsName ?? '';
|
|
|
+ $event = "预订单多付退款({$ghsName})";
|
|
|
+ $capitalData = [
|
|
|
+ 'capitalType' => $currentType,
|
|
|
+ 'io' => 1,
|
|
|
+ 'totalIncome' => $currentTotalIncome,
|
|
|
+ 'payWay' => 0,
|
|
|
+ 'amount' => $refundPrice,
|
|
|
+ 'sjId' => $sjId,
|
|
|
+ 'shopId' => $shopId,
|
|
|
+ 'event' => $event,
|
|
|
+ ];
|
|
|
+ ShopCapitalClass::addCapital($capitalData);
|
|
|
+ //每天每月收入
|
|
|
+ StatIncomeClass::updateOrInsert($shop, $refundPrice);
|
|
|
+
|
|
|
+ //微信收款的原路退回
|
|
|
+ ini_set('date.timezone', 'Asia/Shanghai');
|
|
|
+ $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
|
|
|
+ require_once($wx . '/lib/WxPay.Api.php');
|
|
|
+ require_once($wx . '/example/WxPay.Config.php');
|
|
|
+
|
|
|
+ $totalFee = $cg->realPrice * 100;
|
|
|
+ $refundFee = $refundPrice * 100;
|
|
|
+ $transaction_id = $cg->thirdNo ?? '';
|
|
|
+
|
|
|
+ $merchantExt = WxOpenClass::getWxInfo();
|
|
|
+ $mid = $merchantExt['wxPayMerchantId'] ?? '';
|
|
|
+ $appId = $merchantExt['miniAppId'] ?? '';
|
|
|
+ $key = $merchantExt['wxPayKey'] ?? '';
|
|
|
+
|
|
|
+ $input = new \WxPayRefund();
|
|
|
+ $input->SetTransaction_id($transaction_id);
|
|
|
+ $input->SetTotal_fee($totalFee);
|
|
|
+ $input->SetRefund_fee($refundFee);
|
|
|
+ $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-refund-callback/');
|
|
|
+
|
|
|
+ $config = new \WxPayConfig();
|
|
|
+ $config->SetAppId($appId);
|
|
|
+ $config->SetMerchantId($mid);
|
|
|
+ $config->SetKey($key);
|
|
|
+
|
|
|
+ $input->SetOut_refund_no($orderSn);
|
|
|
+ $input->SetOp_user_id($mid);
|
|
|
+ $return = \WxPayApi::refund($config, $input);
|
|
|
+
|
|
|
+ if (isset($return['return_code']) == false || $return['return_code'] != 'SUCCESS') {
|
|
|
+ $msg = $return['return_msg'] ?? '';
|
|
|
+ Yii::info("退款失败 cgId:{$cgId} msg:{$msg}");
|
|
|
+ util::fail('退款失败');
|
|
|
+ }
|
|
|
+ $wxRefundId = $return['refund_id'] ?? '';
|
|
|
+ $cgRefund->thirdRefundNo = $wxRefundId;
|
|
|
+ $cgRefund->save();
|
|
|
+
|
|
|
+
|
|
|
+ $cg->refundPrice = $refundPrice;
|
|
|
+ $cg->tkPrice = bcadd($cg->tkPrice, $refundPrice, 2);
|
|
|
+ $cg->save();
|
|
|
+
|
|
|
} else {
|
|
|
//欠款
|
|
|
$currentDebt = bcsub($currentPrice, $bookPrice, 2);
|