id ?? 0; $staffName = $staff->name ?? ''; $shopId = $shop->id ?? 0; $sjId = $shop->sjId ?? 0; $customId = $custom->id ?? 0; $ghsId = $ghs->id ?? 0; $customBalance = $custom->balance ?? 0; $ghsBalance = $ghs->balance ?? 0; if (floatval($customBalance) != floatval($ghsBalance)) { noticeUtil::push("客户{$customId} {$customBalance} 供货商{$ghsId} {$ghsBalance} 的余额有问题", '15280215347'); util::fail('系统故障'); } if ($customBalance < 0) { noticeUtil::push("客户{$customId} 供货商{$ghsId} 的余额小于0", '15280215347'); util::fail('系统故障哦'); } if (floatval($customBalance) == 0) { return true; } $where = ['customId' => $customId, 'debt' => 1]; $orderList = OrderClass::getAllByCondition($where, 'id asc', '*', null, true); if (empty($orderList)) { return true; } $totalAmount = 0; $need = []; foreach ($orderList as $order) { $orderId = $order->id ?? 0; $remainDebtPrice = $order->remainDebtPrice ?? 0; $currentTotal = bcadd($totalAmount, $remainDebtPrice, 2); if ($currentTotal > $customBalance) { if (getenv('YII_ENV') == 'production') { //小向需要先结旧账 if (in_array($shop->mainId, [23390])) { break; } else { continue; } } else { if (in_array($shop->mainId, [])) { break; } else { continue; } } } $totalAmount = bcadd($totalAmount, $remainDebtPrice, 2); $need[] = ['id' => $orderId]; } if (empty($need)) { return true; } $where = ['customId' => $customId, 'status' => PurchaseClearClass::STATUS_AWAIT_PAY]; $list = OrderClearClass::getAllByCondition($where, null, '*', null, true); if (!empty($list)) { foreach ($list as $item) { //只要新建账单,老账单就取消掉 $item->status = PurchaseClearClass::STATUS_EXPIRE; $item->save(); } } $idText = json_encode($need); $clearData = [ 'customId' => $customId, 'ghsShopAdminId' => $staffId, 'ghsShopId' => $shopId, 'ghsShopAdminName' => $staffName, 'modifyPrice' => $totalAmount, 'payWay' => 0, 'complete' => 1, ]; $clearInfo = self::clear($clearData, $idText, $sjId, $shopId); //结账消费余额 CustomClass::clearConsumeBalance($totalAmount, $custom, $ghs, $shop, $staff, $clearInfo); } //订单结算 ssh 2021.3.14 public static function clear($post, $idText, $sjId, $shopId) { $customId = $post['customId'] ?? 0; $data = json_decode($idText, true); $ids = array_column($data, 'id'); if (empty($ids)) { util::fail('请选择交易记录'); } $list = OrderClass::getByIds($ids); if (empty($list)) { util::fail('请选择订单'); } $amount = 0; $modifyPrice = $post['modifyPrice'] ?? 0.00; $purchaseArr = []; foreach ($list as $key => $val) { if ($val['sjId'] != $sjId) { util::fail('只能结算自己的订单哦'); } if ($val['debt'] == 0) { util::fail('有订单已经结算过了'); } if ($val['customId'] != $customId) { util::fail('每次只能结算一个客户的订单'); } $amount = bcadd($amount, $val['remainDebtPrice'], 2); $purchaseId = $val['purchaseId'] ?? 0; if (!empty($purchaseId)) { $purchaseArr[] = $purchaseId; } } $purchaseString = ''; if (!empty($purchaseArr)) { $purchaseString = implode(',', $purchaseArr); } $custom = CustomClass::getLockById($customId); if (empty($custom)) { util::fail('没有找到客户'); } $ghsId = $custom->ghsId; $ghs = GhsClass::getLockById($ghsId); if (empty($ghs)) { util::fail('没有找到供货商'); } $ghsShopId = $ghs['shopId'] ?? 0; $ghsShop = ShopClass::getById($ghsShopId, true); $ghsShopName = $ghsShop->shopName ?? ''; $ghsShopName = $ghsShopName == '首店' ? '总店' : $ghsShopName; $orderSn = orderSn::getPurchaseClearSn(); //默认24小时过期 $deadline = date("Y-m-d H:i:s", time() + 24 * 60 * 60); //天天鲜花12小时之后 if ($ghsShopId == 763 || $ghsShopId == 795 || $ghsShopId == 1405) { $deadline = date("Y-m-d H:i:s", time() + 10 * 60 * 60); } //开发环境10个小时过期 if (getenv('YII_ENV') != 'production') { $deadline = date("Y-m-d H:i:s", time() + 10 * 60 * 60); } //惠雅鲜花 结账单过期时间设置为30天 if ($ghsShopId == 520 || $ghsShopId == 1489 || $ghsShopId == 2164 || $ghsShopId == 2167) { $deadline = date("Y-m-d H:i:s", time() + 30 * 24 * 60 * 60); } //昱成花卉 结账单过期时间设置为30天 if ($ghsShopId == 8596) { $deadline = date("Y-m-d H:i:s", time() + 30 * 24 * 60 * 60); } //花大苪 结账单过期时间设置为30天 if ($ghsShopId == 8249) { $deadline = date("Y-m-d H:i:s", time() + 30 * 24 * 60 * 60); } //丰行鲜花 结账单过期时间设置为30天 if ($ghsShopId == 7855) { $deadline = date("Y-m-d H:i:s", time() + 30 * 24 * 60 * 60); } $payWay = isset($post['payWay']) && is_numeric($post['payWay']) ? $post['payWay'] : dict::getDict('payWay', 'unPay'); $clearData = [ 'prePrice' => $amount, 'actPrice' => $modifyPrice, 'realPrice' => $modifyPrice, 'ghsId' => $ghsId, 'ghsName' => $ghs['name'] ?? '', 'ghsMobile' => $ghs['mobile'] ?? '', 'ghsAvatar' => $ghs['avatar'] ?? '', 'ghsAddress' => $ghs['address'] ?? '', 'ghsShopAdminId' => $post['ghsShopAdminId'], 'ghsShopId' => $post['ghsShopId'] ?? 0, 'ghsShopAdminName' => $post['ghsShopAdminName'], 'sjId' => $sjId, 'shopId' => $shopId, 'orderSn' => $orderSn, 'saleIds' => implode(',', $ids), 'status' => 1, 'clearStyle' => dict::getDict('clearStyle', 'gys2Hd'), 'customId' => $customId, 'customName' => $custom['name'] ?? '', 'customShopId' => $custom['shopId'] ?? 0, 'customAvatar' => $custom['avatar'] ?? '', 'customMobile' => $custom['mobile'] ?? '', 'customAddress' => $custom['address'] ?? '', 'deadline' => $deadline, 'purchaseIds' => $purchaseString, 'payWay' => $payWay, 'num' => count($ids), 'ghsShopName' => $ghsShopName, 'hasNoticeCustom' => 0, ]; if ($modifyPrice < $amount) { $clearData['discountAmount'] = bcsub($amount, $modifyPrice, 2); $clearData['discountType'] = dict::getDict('discountType', 'discount'); } if ($modifyPrice > $amount) { //noticeUtil::push("结账金额大于总欠款金额 {$modifyPrice} {$amount}", '15280215347'); util::fail('结账金额大于总欠款金额'); } $return = PurchaseClearClass::add($clearData, true); $complete = $post['complete'] ?? 0; if ($complete == 1) { self::confirmClear($return, $payWay); } return $return; } //确认结账 ssh 20220510 public static function confirmClear($clear, $payWay) { if ($clear->status == 2) { util::fail('已结过账了'); } if ($clear->status == 3) { util::fail('已取消了'); } $current = time(); if ($current >= strtotime($clear->deadline)) { util::fail('已过期,请手动取消'); } $clear->payWay = $payWay; $clear->payTime = date("Y-m-d H:i:s"); $clear->status = 2; $clear->save(); $clearId = $clear->id ?? 0; $customId = $clear->customId ?? 0; $orderSn = $clear->orderSn ?? ''; $customName = $clear->customName ?? ''; $custom = CustomClass::getLockById($customId); if (empty($custom)) { util::fail('没有找到客户'); } $ghsId = $custom->ghsId; $ghs = GhsClass::getLockById($ghsId); if (empty($ghs)) { util::fail('没有找到供货商'); } $amount = $clear->prePrice ?? 0; $saleIds = $clear->saleIds ?? ''; $ids = explode(',', trim($saleIds)); if (empty($ids)) { util::fail('数据错误'); } $purchaseIds = $clear->purchaseIds ?? ''; $purchaseArr = explode(',', $purchaseIds); if (empty($purchaseArr)) { util::fail('数据错误'); } //客户欠款减少 CustomClass::clearDebtAmountReduce($custom, $ghs, $amount, $clear); $clearTime = date("Y-m-d H:i:s"); // 销售单状态变更 OrderClass::updateByIds($ids, ['debt' => 0, 'clearId' => $clearId, 'remainDebtPrice' => 0, 'clearTime' => $clearTime]); // 采购单状态变更 PurchaseClass::updateByIds($purchaseArr, ['debt' => 2, 'clearId' => $clearId, 'remainDebtPrice' => 0, 'clearTime' => $clearTime]); //供货商结账欠款减少 \bizHd\ghs\classes\GhsClass::clearDebtAmountReduce($ghs, $custom, $amount, $clear); //现金余额增加 if ($payWay == dict::getDict('payWay', 'cash')) { $mainId = $ghs->mainId ?? 0; $main = MainClass::getLockById($mainId); if (empty($main)) { util::fail('没有商家信息'); } $moneyBalance = bcadd($main->money, $amount, 2); $main->money = $moneyBalance; $main->save(); $moneyEvent = $customName . "现金结账" . floatval($amount) . "元,单号 {$orderSn})"; $moneyRemark = ''; $capitalType = dict::getDict('capitalType', 'ghsXsClear', 'id'); $change = [ 'relateId' => $clearId, 'amount' => $amount, 'balance' => $moneyBalance, 'io' => 1, 'mainId' => $mainId, 'capitalType' => $capitalType, 'ptStyle' => dict::getDict('ptStyle', 'ghs'), 'event' => $moneyEvent, 'remark' => $moneyRemark, ]; ShopMoneyChangeClass::addData($change); } } }