| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?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\order\classes\OrderClass;
- use bizGhs\order\classes\RefundOrderClass;
- use bizGhs\order\services\RefundOrderService;
- 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 cancelRefundApply($refund)
- {
- CgRefundClass::cancelRefund($refund);
- $saleRefundId = $refund->saleRefundId ?? 0;
- $saleRefund = RefundOrderClass::getById($saleRefundId, true);
- if (empty($saleRefund)) {
- util::fail('退款信息缺失');
- }
- RefundOrderClass::cancelRefund($saleRefund);
- }
- public static function createOrder($cgData, $cg)
- {
- $respond = self::addRefund($cgData, $cg);
- $saleId = $cg->saleId ?? 0;
- $order = OrderClass::getById($saleId, true);
- if (empty($order)) {
- util::fail('没有订单信息');
- }
- $remark = $cgData['remark'] ?? '';
- $price = $cgData['price'] ?? 0;
- $refundType = $cgData['refundType'] ?? 1;
- $imgList = $cgData['imgList'] ?? '';
- $sjId = $order->sjId ?? 0;
- $shopId = $order->shopId ?? 0;
- $mainId = $order->mainId ?? 0;
- $cgProductList = $cgData['product'] ?? [];
- $newProductList = [];
- if (!empty($cgProductList)) {
- $cgIds = array_column($cgProductList, 'productId');
- $cgProductInfo = ProductClass::getAllByCondition(['id' => ['in', $cgIds]], null, 'id,itemId');
- if (empty($cgProductInfo)) {
- util::fail('花材数据有问题');
- }
- $cgMap = [];
- foreach ($cgProductInfo as $cc) {
- $ccId = $cc['id'] ?? 0;
- $ccPtItemId = $cc['itemId'] ?? 0;
- $cgMap[$ccId] = $ccPtItemId;
- }
- $ptItemList = array_column($cgProductInfo, 'itemId');
- $saleProductInfo = ProductClass::getAllByCondition(['mainId' => $mainId, 'itemId' => ['in', $ptItemList]], null, 'id,itemId');
- if (empty($saleProductInfo)) {
- util::fail('花材信息有问题');
- }
- $saleMap = [];
- foreach ($saleProductInfo as $ss) {
- $ssId = $ss['id'] ?? 0;
- $ssPtItemId = $ss['itemId'] ?? 0;
- $saleMap[$ssPtItemId] = $ssId;
- }
- foreach ($cgProductList as $cg) {
- if (isset($cg['productId']) == false) {
- util::fail('花材有问题呢');
- }
- $productId = $cg['productId'];
- if (isset($cgMap[$productId]) == false) {
- util::fail('花材有问题哈');
- }
- $ptItemId = $cgMap[$productId];
- if (isset($saleMap[$ptItemId]) == false) {
- util::fail('花材有问题哦');
- }
- $newProductId = $saleMap[$ptItemId];
- $cg['productId'] = $newProductId;
- $newProductList[] = $cg;
- }
- }
- $orderData = [
- 'remark' => $remark,
- 'price' => $price,
- 'refundType' => $refundType,
- 'imgList' => $imgList,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'mainId' => $mainId,
- 'product' => $newProductList,
- ];
- $cgRefundId = $respond->id ?? 0;
- $saleRefund = RefundOrderService::addRefund($orderData, $order);
- $saleRefund->cgRefundId = $cgRefundId;
- $saleRefund->save();
- $saleRefundId = $saleRefund->id ?? 0;
- $respond->saleRefundId = $saleRefundId;
- $respond->save();
- return $respond;
- }
- //新增退款
- public static function addRefund($post, $cg)
- {
- $mainId = $cg->mainId ?? 0;
- $shopId = $cg->shopId ?? 0;
- $sjId = $cg->sjId ?? 0;
- $cgOrderSn = $cg->orderSn ?? '';
- $refundOrderSn = orderSn::getCgRefundSn();
- $cause = $post['cause'] ?? 0;
- $refundPrice = $post['price'] ?? 0;
- if (is_numeric($refundPrice) == false || $refundPrice < 0) {
- util::fail('请填写退款金额');
- }
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店47');
- }
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息30');
- }
- $data = [];
- $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['imgList'] = $post['imgList'] ?? '';
- $data['status'] = CgRefundClass::STATUS_UN_COMPLETE;
- $refundSendCost = isset($post['refundSendCost']) && is_numeric($post['refundSendCost']) ? $post['refundSendCost'] : 0;
- $refundPackCost = isset($post['refundPackCost']) && is_numeric($post['refundPackCost']) ? $post['refundPackCost'] : 0;
- $data['refundSendCost'] = $refundSendCost;
- $data['refundPackCost'] = $refundPackCost;
- $cg->refundSendCost = bcadd($refundSendCost, $cg->refundSendCost, 2);
- $cg->refundPackCost = bcadd($refundPackCost, $cg->refundPackCost, 2);
- $cg->refundLog = 1;
- $cg->save();
- //花材列表结构
- // [{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;
- $data['cause'] = $cause;
- $cgRefund = CgRefundClass::add($data, true);
- if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
- $cgItemInfo = PurchaseItemClass::getAllByCondition(['orderSn' => $cgOrderSn], null, '*', 'productId', true);
- $originPrice = 0;
- foreach ($productList as $info) {
- $productId = $info['productId'] ?? 0;
- if (isset($cgItemInfo[$productId]) == false) {
- util::fail('没有找到花材哦!');
- }
- $currentInfo = $cgItemInfo[$productId];
- $ptItemId = $currentInfo->itemId;
- $name = $currentInfo->name;
- $cover = $currentInfo->cover;
- $ratio = $currentInfo->ratio;
- $cgItemId = $currentInfo->id;
- $unitType = $info['unitType'] ?? 0;
- $num = $info['num'] ?? 0;
- $unitPrice = $info['unitPrice'] ?? 0;
- $unitName = $info['unitName'] ?? '';
- $currentPrice = bcmul($num,$unitPrice,2);
- $originPrice = bcadd($originPrice,$currentPrice,2);
- $refundData = [];
- $refundData['orderSn'] = $refundOrderSn;
- $refundData['itemId'] = $ptItemId;
- $refundData['productId'] = $productId;
- $refundData['name'] = $name;
- $refundData['cover'] = $cover;
- $refundData['itemCost'] = $unitPrice;
- $refundData['ratio'] = $ratio;
- $refundData['mainId'] = $mainId;
- $refundData['shopId'] = $shopId;
- $refundData['sjId'] = $sjId;
- $refundData['xhNum'] = $num;
- $refundData['xhUnitName'] = $unitName;
- $refundData['xhUnitType'] = $unitType;
- $refundData['xhUnitPrice'] = $unitPrice;
- $refundData['xhPrice'] = 0;
- $refundData['cgItemId'] = $cgItemId;
- $refundData['cause'] = $cause;
- CgRefundItemClass::addData($refundData, $cg);
- }
- if($refundPrice<$originPrice){
- util::fail('退款金额要等于或大于花材总和');
- }
- }
- return $cgRefund;
- }
- }
|