202 lines
7.8 KiB
PHP
202 lines
7.8 KiB
PHP
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||
// +----------------------------------------------------------------------
|
||
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
|
||
// +----------------------------------------------------------------------
|
||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||
// +----------------------------------------------------------------------
|
||
// | Author: CRMEB Team <admin@crmeb.com>
|
||
// +----------------------------------------------------------------------
|
||
namespace app\services\out;
|
||
|
||
use app\dao\order\StoreOrderRefundDao;
|
||
use app\services\BaseServices;
|
||
use app\services\order\StoreOrderServices;
|
||
use app\services\pay\PayServices;
|
||
use crmeb\exceptions\AdminException;
|
||
use crmeb\exceptions\ApiException;
|
||
use app\services\order\StoreOrderRefundServices;
|
||
use crmeb\services\HttpService;
|
||
use think\annotation\Inject;
|
||
|
||
/**
|
||
* 售后单
|
||
* Class OutStoreOrderRefundServices
|
||
* @package app\services\order
|
||
*/
|
||
class OutStoreOrderRefundServices extends BaseServices
|
||
{
|
||
/**
|
||
* 订单services
|
||
* @var OutStoreOrderServices
|
||
*/
|
||
#[Inject]
|
||
protected OutStoreOrderServices $storeOrderServices;
|
||
|
||
/**
|
||
* @var StoreOrderRefundDao
|
||
*/
|
||
#[Inject]
|
||
protected StoreOrderRefundDao $dao;
|
||
|
||
/**
|
||
* 售后单列表
|
||
* @param array $where
|
||
* @return array
|
||
*/
|
||
public function refundList(array $where)
|
||
{
|
||
[$page, $limit] = $this->getPageValue();
|
||
$field = 'id, store_order_id, uid, order_id, refund_type, refund_num, refund_price, refunded_price, refund_phone, refund_express, refund_express_name,
|
||
refund_explain, refund_img, refund_reason, refuse_reason, remark, refunded_time, cart_info, is_cancel, is_del, add_time';
|
||
$list = $this->dao->getRefundList($where, $field, [], $page, $limit);
|
||
$count = $this->dao->count($where);
|
||
foreach ($list as $key => &$item) {
|
||
$item['pay_price'] = $item['refund_price'];
|
||
unset($item['refund_price']);
|
||
$item['items'] = $this->tidyCartList($item['cart_info']);
|
||
unset($list[$key]['cart_info']);
|
||
}
|
||
return compact('list', 'count');
|
||
}
|
||
|
||
/**
|
||
* 格式化订单商品
|
||
* @param array $carts
|
||
* @return array
|
||
*/
|
||
public function tidyCartList(array $carts): array
|
||
{
|
||
$list = [];
|
||
foreach ($carts as $cart) {
|
||
$list[] = [
|
||
'store_name' => $cart['productInfo']['store_name'] ?? '',
|
||
'suk' => $cart['productInfo']['attrInfo']['suk'] ?? '',
|
||
'image' => $cart['productInfo']['attrInfo']['image'] ?: $cart['productInfo']['image'],
|
||
'price' => sprintf("%.2f", $cart['truePrice'] ?? '0.00'),
|
||
'cart_num' => $cart['cart_num'] ?? 0
|
||
];
|
||
}
|
||
return $list;
|
||
}
|
||
|
||
/**
|
||
* 售后单生成
|
||
* @param int $id
|
||
* @param string $pushUrl
|
||
* @return bool
|
||
*/
|
||
public function refundCreatePush(int $id, string $pushUrl): bool
|
||
{
|
||
$refundInfo = $this->getInfo('', $id);
|
||
/** @var StoreOrderServices $orderServices */
|
||
$orderServices = app()->make(StoreOrderServices::class);
|
||
$orderInfo = $orderServices->get($refundInfo['store_order_id'], ['id', 'order_id']);
|
||
if (!$orderInfo) {
|
||
throw new AdminException('订单不存在');
|
||
}
|
||
$refundInfo['order'] = $orderInfo->toArray();
|
||
return $this->outPush($pushUrl, $refundInfo, '售后单');
|
||
}
|
||
|
||
/**
|
||
* 售后单取消
|
||
* @param int $id
|
||
* @param string $pushUrl
|
||
* @return bool
|
||
*/
|
||
public function cancelApplyPush(int $id, string $pushUrl): bool
|
||
{
|
||
$refundInfo = $this->getInfo('', $id);
|
||
/** @var StoreOrderServices $orderServices */
|
||
$orderServices = app()->make(StoreOrderServices::class);
|
||
$orderInfo = $orderServices->get($refundInfo['store_order_id'], ['id', 'order_id']);
|
||
if (!$orderInfo) {
|
||
throw new AdminException('订单不存在');
|
||
}
|
||
$refundInfo['order'] = $orderInfo->toArray();
|
||
return $this->outPush($pushUrl, $refundInfo, '取消售后单');
|
||
}
|
||
|
||
/**
|
||
* 默认数据推送
|
||
* @param string $pushUrl
|
||
* @param array $data
|
||
* @param string $tip
|
||
* @return bool
|
||
*/
|
||
function outPush(string $pushUrl, array $data, string $tip = ''): bool
|
||
{
|
||
$param = json_encode($data, JSON_UNESCAPED_UNICODE);
|
||
$res = HttpService::postRequest($pushUrl, $param, ['Content-Type:application/json', 'Content-Length:' . strlen($param)]);
|
||
$res = $res ? json_decode($res, true) : [];
|
||
if (!$res || !isset($res['code']) || $res['code'] != 0) {
|
||
\think\facade\Log::error(json_encode(['msg' => $tip . '推送失败', 'data' => $res], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)
|
||
);
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
|
||
/**
|
||
* 退款订单详情
|
||
* @param string $orderId 售后单号
|
||
* @param int $id 售后单ID
|
||
* @return mixed
|
||
*/
|
||
public function getInfo(string $orderId = '', int $id = 0)
|
||
{
|
||
$field = ['id', 'store_order_id', 'order_id', 'uid', 'refund_type', 'refund_num', 'refund_price',
|
||
'refunded_price', 'refund_phone', 'refund_express', 'refund_express_name', 'refund_explain',
|
||
'refund_img', 'refund_reason', 'refuse_reason', 'remark', 'refunded_time', 'cart_info', 'is_cancel', 'is_del', 'add_time'];
|
||
|
||
if ($id > 0) {
|
||
$where = $id;
|
||
} else {
|
||
$where = ['order_id' => $orderId];
|
||
}
|
||
$refund = $this->dao->get($where, $field, ['orderData']);
|
||
if (!$refund) throw new ApiException(410173);
|
||
$refund = $refund->toArray();
|
||
|
||
//核算优惠金额
|
||
$totalPrice = 0;
|
||
$vipTruePrice = 0;
|
||
foreach ($refund['cart_info'] ?? [] as $key => &$cart) {
|
||
$cart['sum_true_price'] = sprintf("%.2f", $cart['sum_true_price'] ?? bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2));
|
||
$cart['vip_sum_truePrice'] = bcmul($cart['vip_truePrice'], $cart['cart_num'] ?: 1, 2);
|
||
$vipTruePrice = bcadd((string)$vipTruePrice, $cart['vip_sum_truePrice'], 2);
|
||
if (isset($order['split']) && $order['split']) {
|
||
$refund['cart_info'][$key]['cart_num'] = $cart['surplus_num'];
|
||
if (!$cart['surplus_num']) unset($refund['cart_info'][$key]);
|
||
}
|
||
$totalPrice = bcadd($totalPrice, $cart['sum_true_price'], 2);
|
||
}
|
||
$refund['vip_true_price'] = $vipTruePrice;
|
||
|
||
/** @var StoreOrderRefundServices $refundServices */
|
||
$refundServices = app()->make(StoreOrderRefundServices::class);
|
||
$refund['use_integral'] = $refundServices->getOrderSumPrice($refund['cart_info'], 'use_integral', false);
|
||
$refund['coupon_price'] = $refundServices->getOrderSumPrice($refund['cart_info'], 'coupon_price', false);
|
||
$refund['deduction_price'] = $refundServices->getOrderSumPrice($refund['cart_info'], 'integral_price', false);
|
||
$refund['pay_postage'] = $refundServices->getOrderSumPrice($refund['cart_info'], 'postage_price', false);
|
||
$refund['total_price'] = bcadd((string)$totalPrice, bcadd((string)$refund['deduction_price'], (string)$refund['coupon_price'], 2), 2);
|
||
$refund['items'] = $this->tidyCartList($refund['cart_info']);
|
||
if (in_array($refund['refund_type'], [0, 1, 2, 4, 5])) {
|
||
$title = '申请退款中';
|
||
} elseif ($refund['refund_type'] == 3) {
|
||
$title = '拒绝退款';
|
||
} else {
|
||
$title = '已退款';
|
||
}
|
||
|
||
$refund['refund_type_name'] = $title;
|
||
$refund['pay_type_name'] = PayServices::PAY_TYPE[$refund['pay_type']] ?? '其他方式';
|
||
unset($refund['cart_info']);
|
||
return $refund;
|
||
}
|
||
|
||
}
|