| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- <?php
- namespace bizHd\cg\services;
- use biz\ghs\classes\GhsClass;
- use biz\hb\classes\HbClass;
- use biz\shop\classes\ShopCapitalClass;
- use biz\shop\classes\ShopClass;
- use biz\wx\classes\WxMessageClass;
- use bizGhs\product\classes\ProductClass;
- use bizGhs\stock\classes\StockRecordClass;
- use bizHd\base\services\BaseService;
- use bizHd\cg\classes\CgRefundClass;
- use bizHd\cg\classes\CgRefundItemClass;
- use bizHd\purchase\classes\PurchaseClass;
- use bizHd\purchase\classes\PurchaseItemClass;
- use bizHd\shop\classes\MainClass;
- use bizHd\stat\classes\StatIncomeClass;
- use bizHd\wx\classes\WxOpenClass;
- use common\components\dict;
- use common\components\noticeUtil;
- use common\components\orderSn;
- use common\components\util;
- use Yii;
- class CgRefundService extends BaseService
- {
- public static $baseFile = '\bizHd\cg\classes\CgRefundClass';
- //新增退款
- public static function addRefund($post, $cg)
- {
- $cgId = $cg->id ?? 0;
- $mainId = $cg->mainId ?? 0;
- $shopId = $cg->shopId ?? 0;
- $sjId = $cg->sjId ?? 0;
- $payWay = $cg->payWay;
- $cgOrderSn = $cg->orderSn ?? '';
- $cg->refund = PurchaseClass::REFUND_YES;
- $cg->save();
- $transaction_id = $cg->thirdNo ?? '';
- if ($payWay == dict::getDict('payWay', 'wxPay')) {
- if (empty($transaction_id)) {
- util::fail('第三方支付订单号没有找到');
- }
- }
- $refundOrderSn = orderSn::getCgRefundSn();
- $refundPrice = $post['price'] ?? 0;
- if (is_numeric($refundPrice) == false || $refundPrice < 0) {
- util::fail('退款金额有问题');
- }
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店');
- }
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息');
- }
- $data = [];
- $data['status'] = CgRefundClass::STATUS_COMPLETE;
- $data['orderSn'] = $refundOrderSn;
- $data['shopId'] = $shopId;
- $data['mainId'] = $mainId;
- $data['sjId'] = $sjId;
- $data['shopAdminId'] = $cg->shopAdminId;
- $data['shopAdminName'] = $cg->shopAdminName;
- $data['cgId'] = $cg->id;
- $refundType = $post['refundType'] ?? 0;
- $data['refundType'] = $refundType;
- $data['refundPrice'] = $refundPrice;
- $data['remark'] = $post['remark'];
- $data['status'] = CgRefundClass::STATUS_UN_COMPLETE;
- //花材列表结构
- // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
- $productList = $post['product'] ?? '';
- $bigNum = 0;
- $smallNum = 0;
- foreach ($productList as $current) {
- $unitType = $current['unitType'] ?? dict::getDict('unitType', 'big');
- $num = $current['num'] ?? 0;
- if ($unitType == dict::getDict('unitType', 'big')) {
- $bigNum = bcadd($bigNum, $num);
- } else {
- $smallNum = bcadd($smallNum, $num);
- }
- }
- $data['bigNum'] = $bigNum;
- $data['smallNum'] = $smallNum;
- $cgRefund = CgRefundClass::add($data, true);
- if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
- $cgItemInfo = PurchaseItemClass::getAllByCondition(['orderSn' => $cgOrderSn], null, '*', 'productId', true);
- foreach ($productList as $info) {
- $unitType = $info['unitType'] ?? 0;
- $num = $info['num'] ?? 0;
- $unitPrice = $info['unitPrice'] ?? 0;
- $unitName = $info['unitName'] ?? '';
- $productId = $info['productId'] ?? 0;
- $ptItemId = $info['ptItemId'] ?? 0;
- $currentCgItem = $cgItemInfo[$productId] ?? null;
- if (empty($currentCgItem)) {
- util::fail('没有找到花材哦!');
- }
- $currentCgItem->refundNum = bcadd($currentCgItem->refundNum, $num);
- $currentCgItem->save();
- $refundData = [];
- $refundData['orderSn'] = $refundOrderSn;
- $refundData['itemId'] = $ptItemId;
- $refundData['productId'] = $productId;
- $refundData['name'] = $info['name'] ?? '';
- $refundData['cover'] = $info['cover'] ?? '';
- $refundData['itemCost'] = $info['cost'] ?? 0;
- $refundData['ratio'] = $info['ratio'] ?? 0;
- $refundData['mainId'] = $mainId;
- $refundData['shopId'] = $shopId;
- $refundData['sjId'] = $sjId;
- $refundData['xhNum'] = $num;
- $refundData['xhUnitName'] = $unitName;
- $refundData['xhUnitType'] = $unitType;
- $refundData['xhUnitPrice'] = $unitPrice;
- $refundData['xhPrice'] = 0;
- CgRefundItemClass::addData($refundData, $cg);
- }
- }
- //门店总收入增加
- $currentTotalIncome = bcadd($main->totalIncome, $refundPrice, 2);
- $main->totalIncome = $currentTotalIncome;
- $main->save();
- $currentType = dict::getDict('capitalType', 'cgRefund', 'id');
- //记录总的退款金额
- $currentTkPrice = bcadd($cg->tkPrice, $refundPrice, 2);
- $cg->tkPrice = $currentTkPrice;
- $cg->refund = PurchaseClass::REFUND_YES;
- $cg->save();
- $ghsId = $cg->ghsId;
- $ghs = GhsClass::getLockById($ghsId);
- if (empty($ghs)) {
- util::fail('没有找到供货商');
- }
- $ghsName = $ghs->name ?? '';
- if ($payWay == dict::getDict('payWay', 'wxPay')) {
- //微信收款的原路退回
- 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->book == 1 ? $cg->bookPrice * 100 : $cg->orderPrice * 100;
- $refundFee = $refundPrice * 100;
- $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($refundOrderSn);
- $input->SetOp_user_id($mid);
- $return = \WxPayApi::refund($config, $input);
- if (isset($return['return_code']) == false || $return['return_code'] != 'SUCCESS' || isset($return['result_code']) == false || $return['result_code'] != 'SUCCESS') {
- $msg = $return['return_msg'] ?? '';
- $err_code_des = $return['err_code_des'] ?? '';
- Yii::info("退款失败 cgId:{$cgId} msg:{$msg}");
- Yii::info($err_code_des);
- Yii::info('微信退款返回信息:' . json_encode($return));
- util::fail('退款操作失败');
- }
- $wxRefundId = $return['refund_id'] ?? '';
- $cgRefund->thirdRefundNo = $wxRefundId;
- $cgRefund->save();
- //采购单总金额还要减去退款
- $currentRefund = bcadd($cg->refundPrice, $refundPrice, 2);
- $cg->refundPrice = $currentRefund;
- $currentActPrice = bcsub($cg->actPrice, $refundPrice, 2);
- if ($currentActPrice < 0) {
- util::fail('退款金额有问题!');
- }
- $cg->actPrice = $currentActPrice;
- $currentRealPrice = bcsub($cg->realPrice, $refundPrice, 2);
- if ($currentRealPrice < 0) {
- util::fail('退款金额有问题,请排查');
- }
- $cg->realPrice = $currentRealPrice;
- $cg->save();
- //发起退款通知
- $customName = $order->customName ?? '';
- noticeUtil::push("商家正在给{$customName}退款{$refundPrice}元", '15280215347');
- } elseif ($payWay == dict::getDict('payWay', 'debtPay')) {
- $clearId = $cg->clearId ?? 0;
- if (!empty($clearId)) {
- util::fail('订单已经结帐,请走线下退款');
- }
- //采购单总金额还要减去退款
- $currentRefund = bcadd($cg->refundPrice, $refundPrice, 2);
- $cg->refundPrice = $currentRefund;
- $currentActPrice = bcsub($cg->actPrice, $refundPrice, 2);
- if ($currentActPrice < 0) {
- util::fail('退款金额有问题!');
- }
- $cg->actPrice = $currentActPrice;
- $currentRealPrice = bcsub($cg->realPrice, $refundPrice, 2);
- if ($currentRealPrice < 0) {
- util::fail('退款金额有问题,请排查');
- }
- $cg->realPrice = $currentRealPrice;
- $currentRemainDebt = bcsub($cg->remainDebtPrice, $refundPrice, 2);
- if ($currentRemainDebt < 0) {
- util::fail('退款金额有问题哦');
- }
- if ($currentRemainDebt == 0) {
- $cg->debt = PurchaseClass::DEBT_NO;
- }
- $cg->remainDebtPrice = $currentRemainDebt;
- $cg->save();
- //门店应付总款减少
- $currentMayPay = bcsub($main->mayPay, $refundPrice, 2);
- if ($currentMayPay < 0) {
- util::fail('应付款有问题');
- }
- $main->mayPay = $currentMayPay;
- $main->save();
- //应付供货商款减少
- \bizHd\ghs\classes\GhsClass::refundDebtAmountReduce($ghs, $cgRefund, $cg);
- } elseif ($payWay == dict::getDict('payWay', 'balancePay')) {
- //采购单总金额还要减去退款
- $currentRefund = bcadd($cg->refundPrice, $refundPrice, 2);
- $cg->refundPrice = $currentRefund;
- $currentActPrice = bcsub($cg->actPrice, $refundPrice, 2);
- if ($currentActPrice < 0) {
- util::fail('退款金额有问题!');
- }
- $cg->actPrice = $currentActPrice;
- $currentRealPrice = bcsub($cg->realPrice, $refundPrice, 2);
- if ($currentRealPrice < 0) {
- util::fail('退款金额有问题,请排查');
- }
- $cg->realPrice = $currentRealPrice;
- $cg->save();
- //门店余额增加
- ShopClass::cgRefundAddBalance($main, $shop, $refundPrice, $cgRefund, $ghs);
- } else {
- util::fail('支付方式没有找到哦!');
- }
- $sjId = $cg->sjId ?? 0;
- $shopId = $cg->shopId ?? 0;
- $event = "采购退款({$ghsName})";
- $capitalData = [
- 'capitalType' => $currentType,
- 'io' => 1,
- 'totalIncome' => $currentTotalIncome,
- 'payWay' => 0,
- 'amount' => $refundPrice,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'event' => $event,
- 'mainId' => $mainId,
- ];
- ShopCapitalClass::addCapital($capitalData);
- //每天每月收入
- StatIncomeClass::updateOrInsert($main, $shop, $refundPrice);
- //微信付款的不需要通知,微信官方会通知
- if ($payWay != dict::getDict('payWay', 'wxPay')) {
- //微信通知退款成功
- WxMessageClass::cgRefundInform($shop, $cg, $refundPrice);
- }
- return $cgRefund;
- }
- //供货商productId 转 hdProductId [{productId:1,...}]
- public static function toggleProductList($ghsItemInfo, $hdShopId, $hdSjId, $mainId)
- {
- $productIds = array_column($ghsItemInfo, "productId");
- $productData = ProductClass::getProductByIds($productIds);
- $itemIds = [];
- $productMap = [];
- foreach ($productData as $v) {
- $itemIds[] = $v['itemId'];
- $productMap[$v['id']] = $v['itemId'];
- }
- $hdProductData = ProductClass::getProductInfoByItemIds($itemIds, $mainId);
- $hdProductData = array_column($hdProductData, null, "itemId");
- foreach ($ghsItemInfo as $k => $v) {
- $itemId = $productMap[$v['productId']];
- $hdProductId = $hdProductData[$itemId]['id'];
- $ghsItemInfo[$k]['hdProductId'] = $hdProductId;
- }
- return $ghsItemInfo;
- }
- }
|