| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541 |
- <?php
- namespace bizHd\cg\classes;
- use biz\shop\classes\ShopCapitalClass;
- use biz\shop\classes\ShopExtClass;
- use bizHd\ghs\classes\GhsClass;
- use bizHd\purchase\classes\PurchaseClass;
- use bizHd\purchase\classes\PurchaseItemClass;
- use bizHd\shop\classes\MainClass;
- use bizGhs\stock\classes\StockRecordClass;
- use bizHd\shop\classes\ShopClass;
- use bizHd\wx\classes\WxOpenClass;
- use common\components\dict;
- use common\components\noticeUtil;
- use common\components\orderSn;
- use common\components\util;
- use Yii;
- use bizGhs\base\classes\BaseClass;
- use bizGhs\order\traits\OrderTrait;
- use bizGhs\product\classes\ProductClass;
- use common\components\lakala\Lakala;
- class CgRefundClass extends BaseClass
- {
- use OrderTrait;
- public static $baseFile = '\bizHd\cg\models\CgRefund';
- const STATUS_UN_COMPLETE = 0;//待审核
- const STATUS_COMPLETE = 1;//已通过
- const STATUS_REJECT = 2;//已驳回
- const STATUS_CANCEL = 3;//已取消
- const REFUND_TYPE_MONEY_GOOD = 1; //退款退货
- const REFUND_TYPE_MONEY = 2; // 仅退款
- public static function onlyRefundMoney($ghsShop, $cg)
- {
- $refundOrderSn = orderSn::getCgRefundSn();
- $refundPrice = $cg->actPrice ?? 0;
- $orderSn = $cg->orderSn ?? '';
- $refundFee = bcmul($refundPrice, 100);
- $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
- $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
- $params = [
- 'appid' => 'OP00002119',
- 'serial_no' => '018b08cfddbd',
- 'merchant_no' => $ghsShop->lklSjNo,
- 'term_no' => $ghsShop->lklScanTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $refundReason = '';
- //退款用拉卡拉原单号比较稳定,用自己内部的单号,好像会出现找不到订单号的情况
- $thirdNo = $cg->thirdNo ?? '';
- $thisOrderSn = '';
- //竟然会出现thirdNo为空的情况
- if (!empty($thirdNo)) {
- $thisOrderSn = $orderSn;
- }
- $backParams = [
- 'refundSn' => $refundOrderSn,
- 'thirdNo' => $thirdNo,
- 'orderSn' => $thisOrderSn,
- 'refundAmount' => $refundFee,
- 'refundReason' => $refundReason,
- ];
- $response = $laResource->refund($backParams);
- if (!isset($response['code']) || $response['code'] != 'BBS00000') {
- $errMsg = $response['msg'] ?? '';
- noticeUtil::push("钱退回失败 {$orderSn} {$errMsg}", '15280215347');
- return ['status' => 0, 'msg' => $errMsg];
- } else {
- $ghsShopId = $ghsShop->id;
- ShopExtClass::orderCancelBackMoneyReport($ghsShopId);
- $sjName = $ghsShop->merchantName;
- $shopName = $ghsShop->shopName;
- $name = $shopName == '首店' ? $sjName : $sjName . '-' . $shopName;
- noticeUtil::push("钱已经原路退回 success {$orderSn} {$name}", '15280215347');
- return ['status' => 1, 'msg' => '退款成功'];
- }
- }
- //审核通过 ssh 20230811
- public static function passRefund($cgRefund, $cg)
- {
- if ($cgRefund->status == 1) {
- util::fail('已通过');
- }
- if ($cgRefund->status == 2) {
- util::fail('已驳回');
- }
- if ($cgRefund->status == 3) {
- util::fail('已取消');
- }
- $refundType = $cgRefund->refundType ?? 1;
- $refundSn = $cgRefund->orderSn ?? '';
- $cgRefund->status = 1;
- $cgRefund->save();
- $orderSn = $cg->orderSn ?? '';
- $mainId = $cg->mainId ?? 0;
- $shopId = $cg->shopId ?? 0;
- $sjId = $cg->sjId ?? 0;
- $ghsName = $cg->ghsName ?? '';
- $payWay = $cg->payWay;
- $onlinePay = $cg->onlinePay ?? 1;
- $cg->refund = PurchaseClass::REFUND_YES;
- $cg->save();
- $transaction_id = $cg->thirdNo ?? '';
- $refundOrderSn = orderSn::getCgRefundSn();
- $refundPrice = $cgRefund->refundPrice ?? 0;
- if (is_numeric($refundPrice) == false || $refundPrice < 0) {
- util::fail('退款金额有问题');
- }
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店14');
- }
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息7');
- }
- // 处理退货退款前,把库存先补回。(本次哪些花材要退多少,就补回多少)
- if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
- $field = 'id, cgItemId, itemNum, xhNum';
- $refundItemList = CgRefundItemClass::getAllByCondition(['orderSn' => $refundSn], null, $field, null, true);
- if (empty($refundItemList)) {
- util::fail('没有找到售后订单的花材哦');
- }
- $refundList = [];
- foreach ($refundItemList as $val) {
- $cgItemId = $val->cgItemId;
- if (empty($cgItemId)) {
- util::fail('旧订单无法售后,请联系管理员哦');
- }
- $refundList[$cgItemId] = $val;
- }
- $field = 'id, xhNum, productId';
- $cgItemList = PurchaseItemClass::getAllByCondition(['orderSn' => $cg->orderSn], null, $field, null, true);
- if (empty($cgItemList)) {
- util::fail('采购单花材错误');
- }
- $itemDatas = [];
- foreach ($cgItemList as $cgItem) {
- if (isset($refundList[$cgItem->id])) {
- $refund = $refundList[$cgItem->id];
- $product = ProductClass::getById($cgItem->productId, true);
- $itemDatas[$cgItem->productId] = ['product' => $product, 'addNum' => $refund->itemNum]; // 使用 itemNum 合适,还是 xhNum ?
- }
- }
- $cgShop = $shop;//采购门店
- $adminId = $cgShop->adminId;
- // 补回本次退货的花材数量
- PurchaseClass::increaseProductStock($itemDatas, $cgShop, $adminId, $cg->mainId, '系统');
- }
- if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
- $refundItemList = CgRefundItemClass::getAllByCondition(['orderSn' => $refundSn], null, '*', null, true);
- if (empty($refundItemList)) {
- util::fail('没有找到售后订单的花材哦');
- }
- $refundData = [];
- foreach ($refundItemList as $key => $val) {
- $val->status = 1;
- $val->save();
- $cgItemId = $val->cgItemId ?? 0;
- if (empty($cgItemId)) {
- util::fail('旧订单无法售后,请联系管理员哦');
- }
- $refundData[$cgItemId] = $val;
- }
- $cgItemList = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (empty($cgItemList)) {
- util::fail('采购单花材错误');
- }
- foreach ($cgItemList as $oneKey => $oneItem) {
- $oneItemId = $oneItem->id;
- $productId = $oneItem->productId ?? 0;
- $ptItemId = $oneItem->itemId ?? 0;
- $itemName = $oneItem->name ?? '';
- $cgItemNum = $oneItem->xhNum ?? 0;
- $refundNum = $oneItem->refundNum ?? 0;
- $remainNum = bcsub($cgItemNum, $refundNum);
- if (isset($refundData[$oneItemId])) {
- $refundSingle = $refundData[$oneItemId];
- $num = $refundSingle->xhNum ?? 0;
- if ($num > $remainNum) {
- util::fail("{$itemName} 超过可退数量");
- }
- $oneItem->refundNum = bcadd($refundNum, $num);
- $oneItem->save();
- if ($refundSingle->xhUnitType == 1) {
- $smallNum = $num;
- $bigNum = 0;
- } else {
- $smallNum = 0;
- $bigNum = $num;
- }
- $itemNum = $refundSingle->itemNum;
- if ($bigNum > 0 || $smallNum > 0) {
- $stockInfo = ProductClass::decreaseStock($productId, $bigNum, $smallNum);
- $oldStock = $stockInfo['oldStock'] ?? 0;
- $newStock = $stockInfo['newStock'] ?? 0;
- $recordData = [];
- $recordData['itemNum'] = $itemNum;
- $recordData['sjId'] = $sjId;
- $recordData['shopId'] = $shopId;
- $recordData['mainId'] = $mainId;
- $recordData['orderSn'] = $orderSn;
- $recordData['itemId'] = $ptItemId;
- $recordData['oldStock'] = $oldStock; //当时库存
- $recordData['productId'] = $productId;
- $recordData['newStock'] = $newStock;// 最新库存
- $recordData['relateName'] = $ghsName;
- $recordData['io'] = 0;
- StockRecordClass::hdCgApplyRefundAddRecord($recordData);
- $refundSingle->itemStock = $oldStock;
- $refundSingle->newStock = $newStock;
- $refundSingle->save();
- }
- }
- }
- }
- //记录总的退款金额
- $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('没有找到供货商');
- }
- // 根据 $refundPrice 更新消费金额 expendAmount
- $expendAmount = bcsub($ghs->expendAmount, $refundPrice, 2);
- $ghs->expendAmount = $expendAmount;
- $ghs->save();
- if ($onlinePay == dict::getDict('onlinePay', 'yes')) {
- if ($payWay == dict::getDict('payWay', 'wxPay') || $payWay == dict::getDict('payWay', 'alipay')) {
- $ghsShopId = $ghs->shopId ?? 0;
- $ghsShop = ShopClass::getById($ghsShopId, true);
- if (empty($ghsShop)) {
- util::fail('没有找到供货商的门店');
- }
- $refundFee = bcmul($refundPrice, 100);
- $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
- $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
- $params = [
- 'appid' => 'OP00002119',
- 'serial_no' => '018b08cfddbd',
- 'merchant_no' => $ghsShop->lklSjNo,
- 'term_no' => $ghsShop->lklScanTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $refundReason = '';
- $aliParams = [
- 'refundSn' => $refundOrderSn,
- 'orderSn' => $orderSn,
- 'refundAmount' => $refundFee,
- 'refundReason' => $refundReason,
- 'thirdNo' => $transaction_id,//这个参数必传,不然聚合收银合支付宝付款的退款不会成功
- ];
- $response = $laResource->refund($aliParams);
- if (!isset($response['code']) || $response['code'] != 'BBS00000') {
- $errMsg = $response['msg'] ?? '';
- //$lklSjNo = $ghsShop->lklSjNo ?? '';
- //noticeUtil::push('商户号:' . $lklSjNo . '的退款失败了哦,请注意:' . $errMsg . ' 金额:' . $refundFee, '15280215347');
- util::fail('退款失败:' . $errMsg);
- }
- $refundTradeNo = isset($response['resp_data']['trade_no']) ? $response['resp_data']['trade_no'] : '';
- $cgRefund->thirdRefundNo = $refundTradeNo;
- $cgRefund->save();
- $cg->refundPrice = $refundPrice;
- $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();
- } else {
- util::fail('暂不支持退款');
- }
- } else {
- if ($payWay == dict::getDict('payWay', 'debtPay')) {
- $clearId = $cg->clearId ?? 0;
- if (!empty($clearId)) {
- util::fail('订单已经结帐,无法再发起退款');
- }
- $cg->refundPrice = $refundPrice;
- $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) {
- //noticeUtil::push("应付款有问题! {$main->mayPay} {$refundPrice}", '15280215347');
- //util::fail('应付款有问题!');
- }
- $main->mayPay = $currentMayPay;
- $main->save();
- //应付供货商款减少
- GhsClass::refundDebtAmountReduce($ghs, $cgRefund, $cg);
- } elseif ($payWay == dict::getDict('payWay', 'balancePay')) {
- util::fail('余额支付暂不支持退款');
- //采购单总金额还要减去退款
- $cg->refundPrice = $refundPrice;
- $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();
- //门店余额增加
- \biz\shop\classes\ShopClass::cgRefundAddBalance($main, $shop, $refundPrice, $cgRefund, $ghs);
- } elseif ($payWay == dict::getDict('payWay', 'cash')) {
- $cg->refundPrice = $refundPrice;
- $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();
- } else {
- //其它线下退款直接完成
- $cg->refundPrice = $refundPrice;
- $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();
- }
- }
- return $cgRefund;
- }
- //驳回 ssh 20230811
- public static function reject($cgRefund, $rejectReason)
- {
- if ($cgRefund->status == self::STATUS_COMPLETE) {
- util::fail('申请已通过');
- }
- if ($cgRefund->status == self::STATUS_REJECT) {
- util::fail('申请已驳回');
- }
- if ($cgRefund->status == self::STATUS_CANCEL) {
- util::fail('申请已取消');
- }
- $cgRefund->status = self::STATUS_REJECT;
- $cgRefund->rejectReason = $rejectReason;
- $cgRefund->save();
- }
- //取消退款 ssh 20230808
- public static function cancelRefund($refund)
- {
- if ($refund->status == self::STATUS_COMPLETE) {
- util::fail('申请已通过');
- }
- if ($refund->status == self::STATUS_REJECT) {
- util::fail('申请已驳回');
- }
- if ($refund->status == self::STATUS_CANCEL) {
- util::fail('申请已取消');
- }
- $refund->status = self::STATUS_CANCEL;
- $refund->save();
- }
- public static function valid($refund, $mainId)
- {
- if (isset($refund->mainId) == false || $refund->mainId != $mainId) {
- util::fail('无法访问,编号5');
- }
- return true;
- }
- public static function getRefundList($where)
- {
- $data = self::getList('*', $where, 'addTime DESC');
- $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
- if (empty($list)) {
- return $data;
- }
- $data['list'] = $list;
- return $data;
- }
- //2021.1.23 lqh 订单详情
- public static function getOrderDetail($orderSn, $shopId)
- {
- $orderData = self::orderExist(['orderSn' => $orderSn, 'shopId' => $shopId]);
- $orderInfo = CgRefundItemClass::getOrderItemDetail($orderSn);
- $itemData = [];
- if ($orderInfo) {
- foreach ($orderInfo as $v) {
- $info = json_decode($v['itemInfo'], true);
- $tmp = $info;
- $tmp['itemCover'] = self::groupImg($info['itemCover']);
- unset($v['itemInfo']);
- //兼容 旧数据,没有 rank (花材等级 默认给B)
- if (!isset($tmp['rank'])) {
- $tmp['rank'] = 'B';
- }
- $tmp = array_merge($tmp, $v);
- $itemData[] = $tmp;
- }
- }
- //下面注释放开前,员工id要用shopAdminId
- //$orderData = self::groupAdmin($orderData);
- $orderData['itemInfo'] = $itemData;
- return $orderData;
- }
- //2021.1.23 lqh 判断定时是否存在
- public static function orderExist($where)
- {
- $orderData = self::getByCondition($where);
- if (!$orderData) {
- util::fail('订单不存在');
- }
- return $orderData;
- }
- // 组装 orderItem lqh 2021.1.25
- public static function groupOrderItem($ghsItemInfo, $orderSn, $cgItemList)
- {
- $productData = self::getProductMapData($ghsItemInfo);
- //写入详情表
- $batchData = [];
- foreach ($ghsItemInfo as $v) {
- $tmp = [];
- $productId = $v['productId'];
- $itemId = $productData[$productId]['itemId'];
- $rank = $productData[$productId]['rank'] ?? 'B';// 花材级别
- $tmp['orderSn'] = $orderSn;
- $tmp['itemId'] = $itemId;
- $tmp['productId'] = $v['productId'];
- $unitNum = $productData[$productId]['ratio'] ?? 0;
- $tmp['itemStock'] = $v['oldStock'];//当时库存
- $tmp['newStock'] = $v['newStock'];//当时库存
- $tmp['itemNum'] = ProductClass::mergeItemNum($v['bigNum'], $v['smallNum'], $unitNum);// 数量
- $tmp['name'] = $productData[$productId]['name'] ?? '';
- $tmp['cover'] = $productData[$productId]['cover'] ?? '';
- $tmp['itemPrice'] = $cgItemList[$productId]['price'] ?? 0; //当时售卖的价格
- $tmp['totalPrice'] = bcmul($tmp['itemPrice'], $tmp['itemNum'], 2);
- $tmp['itemCost'] = $productData[$productId]['cost'] ?? 0; //当时成本价
- $stockFormat = ProductClass::formatStock($tmp['itemStock'], $unitNum); // 库存总单位数量转大小单位的数量
- $itemInfo = [
- 'itemName' => $productData[$productId]['name'] ?? '',//花材名称
- 'itemCover' => $productData[$productId]['cover'] ?? '',//花材图片
- 'bigNum' => $v['bigNum'] ?? 0,//数量(大单位)
- 'smallNum' => $v['smallNum'] ?? 0,//数量(小单位)
- 'bigNumStock' => $stockFormat['bigNum'],// 当时库存大单位数
- 'smallNumStock' => $stockFormat['smallNum'],//当时库存 小单位数
- 'itemUnit' => $unitNum ? 2 : 1,//单位数量 2表示有两个单位(e.g 扎和支) 1表是只有一个单位(e.g 扎)
- 'ratio' => $unitNum,// 比例
- 'rank' => $rank,// 花材级别
- 'bigUnit' => $productData[$productId]['bigUnit'],//大单位名称 扎
- 'smallUnit' => $productData[$productId]['smallUnit'],//小单位名称 支
- ];
- $tmp['itemInfo'] = json_encode($itemInfo);
- $batchData[] = $tmp;
- }
- return $batchData;
- }
- }
|