| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php
- namespace bizGhs\cg\services;
- use bizGhs\base\services\BaseService;
- use bizGhs\book\classes\BookCustomClass;
- use bizGhs\book\classes\BookItemClass;
- use bizGhs\book\classes\BookItemCustomClass;
- use bizGhs\book\classes\BookItemGhsClass;
- use bizGhs\cg\classes\CgOrderItemSendClass;
- use bizGhs\cg\classes\CgRefundClass;
- use bizGhs\cg\classes\CgRefundItemClass;
- use bizGhs\ghs\classes\GhsClass;
- use bizGhs\order\classes\PurchaseOrderClass;
- use bizGhs\order\classes\PurchaseOrderItemClass;
- use bizGhs\product\classes\ProductClass;
- use common\components\dict;
- use common\components\orderSn;
- use common\components\util;
- use Yii;
- class CgRefundService extends BaseService
- {
- public static $baseFile = '\bizGhs\cg\classes\CgRefundClass';
- //退款
- public static function addRefund($order, $post)
- {
- $refundPrice = $post['price'] ?? 0;
- if ($refundPrice <= 0) {
- util::fail('请填写退款金额');
- }
- $order->refundPrice = $refundPrice;
- $currentTkPrice = bcadd($order->tkPrice, $refundPrice, 2);
- $order->tkPrice = $currentTkPrice;
- $actPrice = $order->actPrice ?? 0;
- $realPrice = $order->realPrice ?? 0;
- $bookSn = $order->bookSn ?? 0;
- $currentActPrice = bcsub($actPrice, $refundPrice, 2);
- $currentRealPrice = bcsub($realPrice, $refundPrice, 2);
- $order->actPrice = $currentActPrice;
- $order->realPrice = $currentRealPrice;
- $order->refund = 2;
- // 待结采购单售后退尽:actPrice 已为 0 须同步改为已结清,否则会留下 debt=待结 的幽灵单
- if (intval($order->debt ?? 0) === PurchaseOrderClass::DEBT_YES
- && bccomp($currentActPrice, '0', 2) <= 0) {
- $order->debt = PurchaseOrderClass::DEBT_NO;
- }
- $order->save();
- $data = [];
- $data['status'] = 1;
- $orderSn = orderSn::getGhsCgRefundSn();
- $data['refundSn'] = $orderSn;
- $data['relateOrderSn'] = $order->orderSn ?? '';
- $sjId = $post['sjId'] ?? 0;
- $refundType = $post['refundType'] ?? 0;
- $shopId = $post['shopId'] ?? 0;
- $mainId = $post['mainId'] ?? 0;
- $data['sjId'] = $sjId;
- $data['shopId'] = $shopId;
- $data['mainId'] = $mainId;
- $data['shopAdminId'] = $post['shopAdminId'] ?? 0;
- $data['shopAdminName'] = $post['shopAdminName'] ?? '';
- $productData = $post['product'] ?? '';
- if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
- //前端传过来的结构 [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}]
- if (empty($productData)) {
- util::fail('没有退款花材');
- }
- $shop = $post['shop'] ?? [];
- $originalPrice = 0;
- $cgItemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $order->orderSn], null, '*', 'productId', true);
- $productIds = [];
- foreach ($cgItemList as $cgItem) {
- $productIds[] = $cgItem->productId ?? 0;
- }
- $productInfoList = ProductClass::getAllByCondition(['id' => ['in', $productIds]], null, '*', 'id', true);
- foreach ($productData as $currentProduct) {
- $num = $currentProduct['num'] ?? 0;
- $productId = $currentProduct['productId'] ?? 0;
- if (empty($productId)) {
- util::fail('发现无效花材');
- }
- $currentCgItem = $cgItemList[$productId] ?? null;
- $productInfo = $productInfoList[$productId] ?? null;
- if (empty($productInfo)) {
- util::fail('花材信息没有找到呢。');
- }
- $hasRefundNum = $currentCgItem->refundNum ?? 0;
- $totalNum = $currentCgItem->itemNum ?? 0;
- $unitPrice = $currentCgItem->bigPrice ?? 0;
- $unitName = $currentCgItem->bigUnit ?? '';
- $unitCost = $currentCgItem->cost ?? 0;
- //已退数量增加
- $currentRefundNum = bcadd($currentCgItem->refundNum, $num);
- $currentCgItem->refundNum = $currentRefundNum;
- $currentCgItem->save();
- $totalPrice = bcmul($unitPrice, $num, 2);
- $couldRefundNum = bcsub($totalNum, $hasRefundNum);
- if ($couldRefundNum < $num) {
- util::fail('超过可退数量');
- }
- $originalPrice = bcadd($originalPrice, $totalPrice, 2);
- $name = $currentCgItem->name ?? '';
- $cover = $currentCgItem->cover ?? '';
- $ptItemId = $currentCgItem->itemId ?? 0;
- $refundItemData = [
- 'refundSn' => $orderSn,
- 'name' => $name,
- 'cover' => $cover,
- 'itemId' => $productId,
- 'ptItemId' => $ptItemId,
- 'num' => $num,
- 'unitName' => $unitName,
- 'unitType' => 0,
- 'unitPrice' => $unitPrice,
- 'price' => $totalPrice,
- 'unitCost' => $unitCost
- ];
- CgRefundItemClass::addData($refundItemData, $order);
- $sendRefundData = $currentProduct['sendRefund'] ?? [];
- if ($shop->bookSn == $bookSn && !empty($bookSn)) {
- $assignSeat = $currentCgItem->assignSeat;
- if ($assignSeat == 0) {
- $params['currentChangeType'] = 'refund';
- $params['customName'] = '';
- $params['customId'] = '';
- $params['customNamePy'] = '';
- $currentItemInfo = ProductClass::getById($productId, true);
- $bookItemRes = BookItemClass::getByCondition(['bookSn' => $bookSn, 'itemId' => $productId], true);
- $bookItemRes = BookItemClass::addNeedCgNum($bookItemRes, $num, 0, $order, $shop, $currentItemInfo, $params);
- } else {
- if (!empty($sendRefundData)) {
- $sendIds = array_column($sendRefundData, 'id');
- $sendRes = CgOrderItemSendClass::getAllByCondition(['id' => ['in', $sendIds]], null, '*', 'id', true);
- $totalSendRefundNum = 0;
- foreach ($sendRefundData as $sendKey => $send) {
- $sendRefundNum = $send['refundNum'] ?? 0;
- $totalSendRefundNum = bcadd($totalSendRefundNum, $sendRefundNum);
- $sendId = $send['id'] ?? 0;
- $currentSend = $sendRes[$sendId] ?? null;
- if (empty($currentSend)) {
- util::fail('没有找到采购单花材的配送信息呢');
- }
- $totalOnNum = $currentSend->num ?? 0;
- $hasOutNum = $currentSend->outNum ?? 0;
- $remainNum = bcsub($totalOnNum, $hasOutNum);
- $customName = $currentSend->customName ?? '';
- if ($sendRefundNum > $remainNum) {
- util::fail($customName . '货位不够退了');
- }
- $currentSend->outNum = bcadd($currentSend->outNum, $sendRefundNum);
- $currentSend->save();
- $bookGhsGiveId = $currentSend->bookGhsGiveId ?? 0;
- $give = BookItemGhsClass::getById($bookGhsGiveId, true);
- if (empty($give)) {
- util::fail('售后时,货位配送相关信息缺失哦');
- }
- $give->outNum = bcadd($give->outNum, $sendRefundNum);
- $give->save();
- $bookItemCustomId = $currentSend->bookItemCustomId ?? 0;
- $bookItemCustomRes = BookItemCustomClass::getById($bookItemCustomId, true);
- if (empty($bookItemCustomRes)) {
- util::fail('售后时,bookItemCustom信息缺失');
- }
- $onNum = $bookItemCustomRes->onNum ?? 0;
- if ($sendRefundNum > $onNum) {
- util::fail('售后时,bookItemCustom上架数量不够扣');
- }
- $staffId = $post['shopAdminId'] ?? 0;
- $staffName = $post['shopAdminName'] ?? '';
- $params = ['staffId' => $staffId, 'staffName' => $staffName, 'currentChangeType' => 'ghsCgRefund'];
- $bookItemCustomRes = BookItemCustomClass::delOnNum($bookItemCustomRes, $sendRefundNum, $num, $order, $shop, $productInfo, $params);
- $customId = $bookItemCustomRes->customId ?? 0;
- $bookCustomRes = BookCustomClass::getByCondition(['bookSn' => $bookSn, 'customId' => $customId], true);
- if (empty($bookCustomRes)) {
- util::fail('售后时,bookCustom信息缺失');
- }
- $onNum2 = $bookCustomRes->onNum ?? 0;
- if ($sendRefundNum > $onNum2) {
- util::fail('售后时,bookCustom上架数量不够扣');
- }
- $bookCustomRes = BookCustomClass::delOnNum($bookCustomRes, $sendRefundNum, $num, $order, $shop, $productInfo, $params);
- $itemId = $bookItemCustomRes->itemId ?? 0;
- $bookItemRes = BookItemClass::getByCondition(['bookSn' => $bookSn, 'itemId' => $itemId], true);
- if (empty($bookItemRes)) {
- util::fail('售后时,bookItem信息缺失');
- }
- $onNum3 = $bookItemRes->onNum ?? 0;
- if ($sendRefundNum > $onNum3) {
- util::fail('售后时,bookItem上架数量不够扣');
- }
- $bookItemRes = BookItemClass::delOnNum($bookItemRes, $sendRefundNum, $num, $order, $shop, $productInfo, $params);
- }
- if (floatval($num) != floatval($totalSendRefundNum)) {
- util::fail("{$name} 退货数量必须和货位退货数和一致");
- }
- } else {
- $sendList = CgOrderItemSendClass::getAllByCondition(['parentId' => $currentCgItem->id], null, '*', null, true);
- if (!empty($sendList)) {
- foreach ($sendList as $sendInfo) {
- if ($sendInfo->status != 2) {
- util::fail('请填写货位要退的数量');
- }
- }
- }
- }
- }
- }
- }
- if ($refundPrice < $originalPrice) {
- util::fail('退款金额需要等于或大于花材总和');
- }
- }
- $data['customId'] = $order->customId ?? 0;
- $data['remark'] = $post['remark'] ?? '';
- $data['refundPrice'] = $refundPrice;
- $data['refundType'] = $post['refundType'] ?? 0;
- $data['passTime'] = date("Y-m-d H:i:s");
- $data['orderTime'] = $order->entryTime;
- $data['ghsId'] = $order->ghsId ?? 0;
- $data['ghsName'] = $order->ghsName ?? '';
- $refund = CgRefundClass::add($data, true);
- //锁定当前增加资产的运作,有多处,请搜索关键词 ghsAddDebtAction
- $ghsId = $order->ghsId ?? 0;
- $ghs = GhsClass::getLockById($ghsId);
- if (empty($ghs)) {
- util::fail('没有找到供货商');
- }
- PurchaseOrderClass::applyPurchaseRefundOnAccounts($ghs, $refundPrice, $refund, $order, $sjId, $shopId);
- return $refund;
- }
- }
|