relateOrderSn ?? ''; $order = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true); if (empty($order)) { util::fail('没有找到订单信息'); } if ($order->status == OrderClass::ORDER_STATUS_UN_PAY) { util::fail("待付款订单,不能申请售后"); } if ($order->status == OrderClass::ORDER_STATUS_UN_SEND) { util::fail("待配送订单,不能申请售后"); } if ($order->status == OrderClass::ORDER_STATUS_SENDING) { util::fail("配送中订单,不能申请售后"); } if ($order->status == OrderClass::ORDER_STATUS_CANCEL) { util::fail("已取消订单,不能申请售后"); } if (isset($order->clearId) && !empty($order->clearId)) { util::fail('订单已结账,请驳回,并在线下完成售后'); } $currentOrderId = $order->id; $addTime = $order->addTime ?? ''; $current = time(); if ($order->debt != 1 && (strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) { util::fail('历史订单,不能再发起退款'); } if (strtotime($addTime) <= strtotime('2022-03-30 00:00:00')) { util::fail('历史订单,不能发起退款哦'); } if (bccomp($refund->refundPrice, $order->realPrice, 2) == 1) { util::fail("退款金额超过订单金额"); } $refund->passTime = date("Y-m-d H:i:s"); $refund->save(); $customId = $order->customId ?? 0; $clearList = PurchaseClearClass::getAllByCondition(['customId' => $customId, 'status' => 1], null, '*', null, true); if (!empty($clearList)) { $now = date("Y-m-d H:i:s"); foreach ($clearList as $clear) { $deadline = $clear->deadline ?? ''; if (!empty($deadline) && $deadline < $now) { $clear->status = 3; $clear->remark = "账单过期,已自动取消!!参数:{$deadline} {$now}"; $clear->save(); OrderCgClearClass::markExpireByClearId($clear->id); } } $currentClearId = OrderCgClearClass::getPendingClearIdByOrderId($currentOrderId, $customId); if ($currentClearId > 0) { //有结账单要取消才能售后 util::success(['clearId' => $currentClearId, 'error' => 'hasUnClearOrder']); } } $relateOrderSn = $refund->relateOrderSn ?? ''; if (empty($relateOrderSn)) { util::fail('没有订单信息'); } $order = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true); if (empty($order)) { util::fail('没有订单信息哦'); } RefundOrderClass::passRefund($refund, $order); $cgRefundId = $refund->cgRefundId ?? 0; $cgRefund = CgRefundClass::getById($cgRefundId, true); if (empty($cgRefund)) { util::fail('信息缺失哦'); } $cgId = $cgRefund->cgId ?? 0; $cg = PurchaseClass::getById($cgId, true); if (empty($cg)) { util::fail('采购信息缺失'); } CgRefundClass::passRefund($cgRefund, $cg); } //新增退款 public static function addRefund($post, $order) { $refundPrice = $post['price'] ?? 0; if ($refundPrice <= 0) { util::fail('请填写退款金额'); } $cause = $post['cause'] ?? 0; $data = []; $data['status'] = RefundOrderClass::STATUS_UN_COMPLETE; $orderSn = orderSn::getGhsRefundSn(); $data['orderSn'] = $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'] ?? ''; $data['cause'] = $cause; $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; $order->refundSendCost = bcadd($refundSendCost, $order->refundSendCost, 2); $order->refundPackCost = bcadd($refundPackCost, $order->refundPackCost, 2); $order->refundLog = 1; $order->save(); $postProduct = $post['product'] ?? ''; if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) { //花材列表结构 // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] if (empty($postProduct)) { util::fail('没有退款花材'); } $saleOrderSn = $order->orderSn ?? ''; $orderItemData = OrderItemClass::getAllByCondition(['orderSn' => $saleOrderSn], null, '*', 'productId', true); if (empty($orderItemData)) { util::fail('没有找到花材'); } $originPrice = 0; foreach ($postProduct as $currentProduct) { $id = $currentProduct['productId'] ?? 0; if (isset($orderItemData[$id]) == false) { util::fail('没有找到退款的花材'); } $currentInfo = $orderItemData[$id]; $name = $currentInfo->name; $ptItemId = $currentInfo->itemId; $ratio = $currentInfo->ratio; $itemCost = $currentInfo->cost; $orderItemId = $currentInfo->id; $variety = $currentInfo->variety ?? 0; $num = $currentProduct['num'] ?? 0; $xhUnitName = $currentProduct['unitName'] ?? ''; $xhUnitPrice = $currentProduct['unitPrice'] ?? ''; $unitType = $currentProduct['unitType'] ?? 0; $xhPrice = bcmul($num, $xhUnitPrice, 2); $originPrice = bcadd($originPrice, $xhPrice, 2); $itemNum = $num; if ($unitType == dict::getDict('unitType', 'small')) { $itemNum = bcdiv($num, $ratio, 2); } $thisItemData = [ 'sjId' => $sjId, 'shopId' => $shopId, 'orderSn' => $orderSn, 'productId' => $id, 'name' => $name, 'itemId' => $ptItemId, 'itemCost' => $itemCost, 'itemNum' => $itemNum, 'xhNum' => $num, 'xhUnitName' => $xhUnitName, 'xhUnitType' => $unitType, 'xhUnitPrice' => $xhUnitPrice, 'xhPrice' => $xhPrice, 'mainId' => $mainId, 'orderItemId' => $orderItemId, 'cause' => $cause, 'variety' => $variety, ]; RefundOrderItemClass::addData($thisItemData, $order); } if ($refundPrice < $originPrice) { //util::fail('退款金额要等于或大于花材金额总合'); } } $data['customId'] = $order->customId ?? 0; $data['imgList'] = $post['imgList'] ?? ''; $data['remark'] = $post['remark'] ?? ''; $data['refundPrice'] = $refundPrice; $data['refundType'] = $post['refundType'] ?? 0; $data['status'] = 0; $data['customName'] = $order->customName ?? ''; $data['customNamePy'] = $order->customNamePy ?? ''; $data['customMobile'] = $order->customMobile ?? ''; $data['orderTime'] = $order->payTime ?? ''; $refund = RefundOrderClass::add($data, true); return $refund; } }