db; $transaction = $connection->beginTransaction(); try { $customBalanceList = CustomBalanceChangeClass::getAllByCondition(['capitalType' => 81], null, '*', null, true); if (!empty($customBalanceList)) { foreach ($customBalanceList as $customBalance) { $customBalanceId = $customBalance->id ?? 0; $customId = $customBalance->customId ?? 0; $custom = CustomClass::getById($customId, true); if (empty($custom)) { echo $customId . " 没有客户信息"; exit(); } $customName = $custom->name ?? ''; $before = CustomBalanceChangeClass::getByCondition(['customId' => $customId, 'id<' => $customBalanceId],true,'id desc' ); $beforeBalance = $before->balance ?? 0; if($customId == 25695){ //小吉要补3000 $beforeBalance = bcadd($beforeBalance, 3000, 2); } $ghsId = $custom->ghsId ?? 0; $ghs = GhsClass::getById($ghsId, true); if (empty($ghs)) { echo $customId . " 客户供货商信息没有"; exit(); } $ghsName = $ghs->name ?? ''; $ownPtStyle = $ghs->ownPtStyle ?? 1; if ($ownPtStyle == 1) { $cgList = PurchaseClass::getAllByCondition(['ghsId' => $ghsId, 'debt' => 1], null, '*', null, true); $debtAmount = 0; if (!empty($cgList)) { foreach ($cgList as $cg) { $currentDebt = $cg->remainDebtPrice; $debtAmount = bcadd($debtAmount, $currentDebt, 2); } } $has = CustomBalanceChangeClass::getAllByCondition(['customId' => $customId, 'id>' => $customBalanceId], null, '*', null, true); $hasCount = count($has); if ($hasCount > 0) { echo $customName . "({$ghsName}){$customId} 要恢复欠款:" . $debtAmount . " 余额:" . $beforeBalance . " 合并之后还有余额变动{$hasCount}条" . " ------ \n"; } else { echo $customName . "({$ghsName}){$customId} 要恢复欠款:" . $debtAmount . " 余额:" . $beforeBalance . " ------ \n"; } } elseif ($ownPtStyle == 2) { $cgList = CgClass::getAllByCondition(['ghsId' => $ghsId, 'debt' => 1], null, '*', null, true); $debtAmount = 0; if (!empty($cgList)) { foreach ($cgList as $cg) { $currentDebt = $cg->actPrice; $debtAmount = bcadd($debtAmount, $currentDebt, 2); } } $has = CustomBalanceChangeClass::getAllByCondition(['customId' => $customId, 'id>' => $customBalanceId], null, '*', null, true); $hasCount = count($has); if ($hasCount > 0) { echo $customName . "({$ghsName}){$customId}【供货商端】要恢复欠款:" . $debtAmount . " 余额:" . $beforeBalance . " 合并之后还有余额变动{$hasCount}条" . " $$$$$$$ \n"; } else { echo $customName . "({$ghsName}){$customId}【供货商端】要恢复欠款:" . $debtAmount . " 余额:" . $beforeBalance . " $$$$$$$ \n"; } } else { echo $customId . " 客户ptStyle有问题" . $ownPtStyle; exit(); } //continue; $custom->debtAmount = $debtAmount; $custom->balanceMerged = 0; $custom->balance = $beforeBalance; $custom->save(); $ghs->debtAmount = $debtAmount; $ghs->balanceMerged = 0; $ghs->balance = $beforeBalance; $ghs->save(); // 1. 写入客户余额变动明细(CustomBalanceChange) $cbData = [ 'customId' => $custom->id ?? 0, 'customName' => $custom->name ?? '', 'relateId' => 0, 'onlinePay' => 0, 'ptStyle' => dict::getDict('ptStyle', 'ghs'), 'capitalType' => 10, 'amount' => bcsub($beforeBalance, $customBalance->balance ?? 0, 2), // 变化金额 = 恢复后的余额 - 之前的余额 'balance' => $beforeBalance, 'staffId' => 0, 'staffName' => '', 'io' => 1, 'side' => 0, 'payWay' => 0, 'fromType' => dict::getDict('fromType', 'shop'), 'event' => '系统升级恢复', 'mainId' => $ghs->mainId ?? 0, 'shopId' => $ghs->shopId ?? 0, 'sjId' => $ghs->sjId ?? 0, 'remark' => '', ]; CustomBalanceChangeClass::add($cbData, true); // 2. 写入供货商余额变动明细(GhsBalanceChange) $gbData = [ 'ghsId' => $ghs->id ?? 0, 'relateId' => 0, 'ptStyle' => 1, 'capitalType' => 11, 'amount' => bcsub($beforeBalance, $customBalance->balance ?? 0, 2), // 变化金额 = 恢复后的余额 - 之前的余额 'balance' => $beforeBalance, 'io' => 1, 'side' => 0, 'onlinePay' => 0, 'payWay' => 0, 'fromType' => dict::getDict('fromType', 'shop'), 'event' => '系统升级恢复', 'mainId' => $custom->mainId ?? 0, 'shopId' => $custom->shopId ?? 0, 'sjId' => $custom->sjId ?? 0, 'remark' => '', ]; GhsBalanceChangeClass::add($gbData, true); // 3. 写入客户欠款挂账变动明细(CustomDebtChangeClass) $cdcData = [ 'customId' => $custom->id ?? 0, 'customName' => $custom->name ?? '', 'relateId' => 0, 'ptStyle' => dict::getDict('ptStyle', 'ghs'), 'capitalType' => 20, // 20 对应 结账/变动 'amount' => $debtAmount, 'balance' => $debtAmount, 'io' => 0, // 0 对应增加欠款 'event' => '系统升级恢复', 'mainId' => $ghs->mainId ?? 0, 'shopId' => $ghs->shopId ?? 0, 'sjId' => $ghs->sjId ?? 0, 'remark' => '', ]; CustomDebtChangeClass::addChange($cdcData); // 4. 写入供货商欠款挂账变动明细(GhsDebtChangeClass) $gdcData = [ 'ghsId' => $ghs->id ?? 0, 'ptStyle' => 1, 'capitalType' => 20, // 20 对应 结账/变动 'amount' => $debtAmount, 'balance' => $debtAmount, 'io' => 0, // 0 对应增加欠款 'event' => '系统升级恢复', 'mainId' => $custom->mainId ?? 0, 'shopId' => $custom->shopId ?? 0, 'sjId' => $custom->sjId ?? 0, 'remark' => '', ]; GhsDebtChangeClass::addChange($gdcData); } } $transaction->commit(); } catch (\Exception $e) { $transaction->rollBack(); $msg = $e->getMessage(); echo $msg; } } }