| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- <?php
- namespace bizHd\refund\services;
- use biz\product\classes\ProductClass;
- use biz\shop\classes\MainClass;
- use biz\shop\classes\ShopCapitalClass;
- use biz\stat\classes\StatOutClass;
- use biz\stat\classes\StatRefundClass;
- use biz\stock\classes\GoodsStockRecordClass;
- use bizHd\base\services\BaseService;
- use bizHd\custom\classes\CustomClass;
- use bizHd\goods\classes\GoodsClass;
- use bizHd\merchant\classes\ShopClass;
- use bizHd\order\classes\OrderClass;
- use bizHd\order\classes\OrderGoodsClass;
- use bizHd\order\classes\OrderItemClass;
- use bizHd\refund\classes\HdRefundClass;
- use bizHd\refund\classes\HdRefundGoodsClass;
- use bizHd\refund\classes\HdRefundItemClass;
- use bizGhs\shop\classes\ShopMoneyClass;
- use bizHd\wx\classes\WxOpenClass;
- use common\components\dict;
- use common\components\noticeUtil;
- use common\components\orderSn;
- use common\components\util;
- use Yii;
- use common\components\lakala\Lakala;
- class HdRefundService extends BaseService
- {
- public static $baseFile = '\bizHd\refund\classes\HdRefundClass';
- //新增退款
- public static function addRefund($post, $order)
- {
- $refundPrice = $post['price'] ?? 0;
- if ($refundPrice <= 0) {
- util::fail('请填写退款金额');
- }
- //记录总的退款金额
- $order->refundPrice = $refundPrice;
- $currentTkPrice = bcadd($order->tkPrice, $refundPrice, 2);
- if ($currentTkPrice > $order->mainPay) {
- util::fail('可退款金额不足');
- }
- $order->tkPrice = $currentTkPrice;
- $order->refund = OrderClass::REFUND_YES;
- $currentActPrice = bcsub($order->actPrice, $refundPrice, 2);
- if ($currentActPrice < 0) {
- util::fail('可退款金额不足!');
- }
- $order->actPrice = $currentActPrice;
- $order->realPrice = $currentActPrice;
- $order->save();
- $refundType = $post['refundType'] ?? 1;
- $post['orderId'] = $order->id ?? 0;
- $data = [];
- $data['status'] = HdRefundClass::STATUS_COMPLETE;
- $refundSn = orderSn::getHdRefundSn();
- $data['refundSn'] = $refundSn;
- $data['orderSn'] = $order->orderSn ?? '';
- $data['orderId'] = $order->id ?? 0;
- $sjId = $post['sjId'] ?? 0;
- $shopId = $post['shopId'] ?? 0;
- $mainId = $post['mainId'] ?? 0;
- $myOrderSn = $order->orderSn ?? '';
- $thirdNo = $order->thirdNo ?? '';
- $data['sjId'] = $sjId;
- $data['shopId'] = $shopId;
- $data['mainId'] = $mainId;
- $data['shopAdminId'] = $post['shopAdminId'] ?? 0;
- $data['shopAdminName'] = $post['shopAdminName'] ?? '';
- $postProduct = $post['product'] ?? '';
- if ($refundType == 1) {
- //商品花材结构 property 0商品 1花材
- //[{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎',property:1}]
- if (empty($postProduct)) {
- util::fail('没有退款商品');
- }
- $goodsInfoList = [];
- $itemInfoList = [];
- $calcAmount = 0;
- foreach ($postProduct as $current) {
- $property = $current['property'] ?? 0;
- if ($property == 0) {
- $goodsInfoList[] = $current;
- } else {
- $itemInfoList[] = $current;
- }
- $calcNum = $current['num'] ?? 0;
- $calcUnitPrice = $current['unitPrice'] ?? 0;
- $calcPrice = bcmul($calcNum, $calcUnitPrice, 2);
- $calcAmount = bcadd($calcAmount, $calcPrice, 2);
- }
- if (floatval($refundPrice) < floatval($calcAmount)) {
- //util::fail('退款金额要大于商品金额总和');
- }
- if (!empty($itemInfoList)) {
- $ids = array_column($itemInfoList, 'productId');
- $productInfo = ProductClass::getByids($ids, null, 'id');
- $orderItemData = OrderItemClass::getAllByCondition(['orderSn' => $myOrderSn], null, '*', 'itemId', true);
- if (empty($orderItemData)) {
- util::fail('没有找到商品');
- }
- foreach ($itemInfoList as $currentProduct) {
- $id = $currentProduct['productId'] ?? 0;
- $unitPrice = $currentProduct['unitPrice'] ?? 0;
- $unitType = $currentProduct['unitType'] ?? 0;
- $unitName = $currentProduct['unitName'] ?? '';
- $currentRefundNum = $currentProduct['num'] ?? 0;
- $price = bcmul($currentRefundNum, $unitPrice, 2);
- $name = $productInfo[$id]['name'] ?? '';
- $ptItemId = $productInfo[$id]['itemId'] ?? 0;
- $cover = $productInfo[$id]['cover'] ?? '';
- $ratio = $productInfo[$id]['ratio'] ?? '';
- $bigUnit = $productInfo[$id]['bigUnit'] ?? '';
- $smallUnit = $productInfo[$id]['smallUnit'] ?? '';
- $currentOrderItem = $orderItemData[$id] ?? null;
- if (empty($currentOrderItem)) {
- util::fail('没有找到退款的花材');
- }
- $orderItemNum = $currentOrderItem->num ?? 0;
- //已退货数
- $hasRefundNum = $currentOrderItem->refundNum ?? 0;
- $remainNum = bcsub($orderItemNum, $hasRefundNum);
- if ($currentRefundNum > $remainNum) {
- util::fail("{$name}超过可退数量");
- }
- $currentOrderItem->refundNum = bcadd($hasRefundNum, $currentRefundNum);
- $currentOrderItem->save();
- $thisItemData = [
- 'refundSn' => $refundSn,
- 'mainId' => $mainId,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'name' => $name,
- 'itemId' => $id,
- 'ptItemId' => $ptItemId,
- 'num' => $currentRefundNum,
- 'unitName' => $unitName,
- 'unitType' => $unitType,
- 'cover' => $cover,
- 'ratio' => $ratio,
- 'bigUnit' => $bigUnit,
- 'smallUnit' => $smallUnit,
- 'unitPrice' => $unitPrice,
- 'price' => $price,
- ];
- HdRefundItemClass::addData($thisItemData, $refundSn, $post);
- }
- }
- if (!empty($goodsInfoList)) {
- $ids = array_column($goodsInfoList, 'productId');
- $goodsData = GoodsClass::getByIds($ids, null, 'id');
- $orderGoodsData = OrderGoodsClass::getAllByCondition(['orderSn' => $myOrderSn], null, '*', 'goodsId', true);
- foreach ($goodsInfoList as $currentGoods) {
- $goodsId = $currentGoods['productId'] ?? 0;
- $unitPrice = $currentGoods['unitPrice'] ?? 0;
- $name = $goodsData[$goodsId]['name'] ?? '';
- $currentRefundNum = $currentGoods['num'] ?? 0;
- $price = bcmul($currentRefundNum, $unitPrice, 2);
- $currentOrderGoods = $orderGoodsData[$goodsId] ?? [];
- if (empty($currentOrderGoods)) {
- util::fail('没有找到商品呢');
- }
- $totalNum = $currentOrderGoods->num ?? 0;
- $hasRefundNum = $currentOrderGoods->refundNum ?? 0;
- $remainNum = bcsub($totalNum, $hasRefundNum);
- if ($currentRefundNum > $remainNum) {
- util::fail("{$name}超过可退数量");
- }
- $currentOrderGoods->refundNum = bcadd($hasRefundNum, $currentRefundNum);
- $currentOrderGoods->save();
- $cover = $goodsData[$goodsId]['cover'] ?? '';
- $num = $currentGoods['num'] ?? 0;
- $saveGoods = [
- 'refundSn' => $refundSn,
- 'mainId' => $mainId,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'name' => $name,
- 'cover' => $cover,
- 'goodsId' => $goodsId,
- 'num' => $num,
- 'unitPrice' => $unitPrice,
- 'price' => $price,
- ];
- HdRefundGoodsClass::addData($saveGoods, $refundSn, $post);
- }
- }
- }
- $data['customId'] = $order->customId ?? 0;
- $data['remark'] = $post['remark'] ?? '';
- $data['refundPrice'] = $refundPrice;
- $data['refundType'] = $post['refundType'] ?? 0;
- //写入订单表
- $refund = HdRefundClass::add($data, true);
- $payWay = $order->payWay;
- $shopId = $order->shopId;
- $onlinePay = $order->onlinePay ?? 1;
- $main = MainClass::getLockById($mainId);
- $shop = ShopClass::getLockById($shopId);
- if (empty($main)) {
- util::fail('main信息缺失呢');
- }
- if (empty($shop)) {
- util::fail('没有找到门店,编号15860');
- }
- //总支出增加
- $currentTotalExpend = bcadd($main->totalExpend, $refundPrice, 2);
- $main->totalExpend = $currentTotalExpend;
- $totalRefund = bcadd($main->totalRefund, $refundPrice, 2);
- $main->totalRefund = $totalRefund;
- $main->save();
- //支出流水
- $thisType = dict::getDict('capitalType', 'hdOrderRefund', 'id');
- $sjId = $order->sjId ?? 0;
- $event = '销售退款';
- $capitalData = [
- 'capitalType' => $thisType,
- 'io' => 0,
- 'totalExpend' => $currentTotalExpend,
- 'payWay' => 0,
- 'amount' => $refundPrice,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'event' => $event,
- 'mainId' => $mainId,
- ];
- ShopCapitalClass::addCapital($capitalData);
- //当天和当月支出统计
- //StatOutClass::updateOrInsert($main, $shop, $refundPrice);
- //退款统计
- //StatRefundClass::replace($main, $shop, $payWay, $refundPrice);
- if ($onlinePay == dict::getDict('onlinePay', 'yes')) {
- //退款流程
- if ($payWay == dict::getDict('payWay', 'wxPay') || $payWay == dict::getDict('payWay', 'alipay')) {
- //门店余额减少
- \biz\shop\classes\ShopClass::hdSaleRefundReduceBalance($main, $shop, $refund, $refundPrice);
- $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' => $shop->lklSjNo,
- 'term_no' => $shop->lklScanTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $refundReason = '';
- $refundParams = [
- 'refundSn' => $refundSn,
- 'orderSn' => $myOrderSn,
- 'refundAmount' => $refundFee,
- 'refundReason' => $refundReason,
- 'thirdNo' => $thirdNo,//这个参数必传,不然聚合收银合支付宝付款的退款不会成功
- ];
- $response = $laResource->refund($refundParams);
- if (!isset($response['code']) || $response['code'] != 'BBS00000') {
- $errMsg = $response['msg'] ?? '';
- noticeUtil::push('散客订单,支付宝,退款失败,原因:' . $errMsg . ' refundSn:' . $refundSn . ' orderSn:' . $myOrderSn, '15280215347');
- util::fail('退款失败');
- }
- $wxRefundId = isset($response['resp_data']['trade_no']) ? $response['resp_data']['trade_no'] : '';
- $refund->thirdRefundNo = $wxRefundId;
- $refund->save();
- }
- } else {
- $customId = $order->customId ?? 0;
- $custom = CustomClass::getLockById($customId);
- if (empty($custom)) {
- util::fail('没有找到客户');
- }
- if ($payWay == dict::getDict('payWay', 'debtPay')) {
- //如果欠款支付,减掉欠款,其实就是增加余额
- $currentRemain = bcsub($order->remainDebtPrice, $refundPrice, 2);
- if ($currentRemain < 0) {
- util::fail('剩余欠款不足售后金额');
- }
- $order->remainDebtPrice = $currentRemain;
- if ($currentRemain <= 0) {
- $order->debt = 0;
- }
- $order->save();
- CustomClass::skRefundDebtAmountReduce($custom, $refundPrice, $refund, $payWay);
- }
- if ($payWay == dict::getDict('payWay', 'cash')) {
- //如果现金支付
- $moneyData = [
- 'shopId' => $shopId,
- 'amount' => $refundPrice,
- 'orderSn' => $order->orderSn,
- 'mainId' => $mainId,
- ];
- ShopMoneyClass::hdRefundSkOutAmount($moneyData, $main);
- }
- if ($payWay == dict::getDict('payWay', 'balancePay')) {
- //如果是余额支付,客户的余额要增加
- CustomClass::skRefundDebtAmountReduce($custom, $refundPrice, $refund, $payWay);
- }
- }
- return $refund;
- }
- }
|