dryRun === 1; $force = (int)$this->force === 1; $ghsId = intval($this->ghsId); echo $dry ? "【预览:ghsApp 采购供货商按订单重算余额】\n" : "【执行:ghsApp 采购供货商按订单重算余额】\n"; echo 'force=' . ($force ? '1(含已合并)' : '0(仅未合并)') . PHP_EOL; if ($ghsId > 0) { $count = $this->mergeOneGhs($ghsId, $dry, $force) ? 1 : 0; echo "完成:处理 {$count} 条,dryRun=" . ($dry ? '1' : '0') . PHP_EOL; return; } $count = 0; $purchasePtStyles = $this->getGhsAppPurchasePtStyles(); $where = [ 'ownShopId>' => 0, 'ownPtStyle' => ['in', $purchasePtStyles], ]; if (!$force && $this->columnExists('xhGhs', 'balanceMerged')) { $where['balanceMerged'] = 0; } elseif (!$force) { $where['debtAmount>'] = 0; } $list = BizGhsClass::getAllByCondition($where, null, 'id,ownShopId,ownPtStyle,balance,debtAmount,debtNum,balanceMerged', null, true); foreach ($list as $ghs) { if ($this->mergeOneGhs(intval($ghs->id ?? 0), $dry, $force)) { $count++; } } // ownPtStyle 未回填的历史行 $legacyWhere = [ 'ownShopId>' => 0, 'ownPtStyle' => 0, ]; if (!$force && $this->columnExists('xhGhs', 'balanceMerged')) { $legacyWhere['balanceMerged'] = 0; } $legacyList = BizGhsClass::getAllByCondition($legacyWhere, null, 'id,ownShopId,ownPtStyle,balance,debtAmount,debtNum,balanceMerged', null, true); foreach ($legacyList as $ghs) { if ($this->mergeOneGhs(intval($ghs->id ?? 0), $dry, $force)) { $count++; } } echo "完成:处理 {$count} 条,dryRun=" . ($dry ? '1' : '0') . PHP_EOL; } /** * ghsApp 采购买方平台类型:二级批发(2)、基地端(4);不含 hd 花店(1)。 */ protected function getGhsAppPurchasePtStyles() { return [ (int)dict::getDict('ptStyle', 'ghs'), (int)dict::getDict('ptStyle', 'kmGhs'), ]; } /** * 合并单行;预览模式只打印将写入的金额。 */ protected function mergeOneGhs($ghsId, $dry, $force) { if ($ghsId <= 0) { return false; } $ghs = BizGhsClass::getById($ghsId, true); if (empty($ghs)) { echo "[跳过] id={$ghsId} 不存在" . PHP_EOL; return false; } if (!AccountMoneyClass::isGhsAppPurchaseSupplierRow($ghs)) { $ownPtStyle = intval($ghs->ownPtStyle ?? 0); echo "[跳过] id={$ghsId} 非 ghsApp 采购供货商行(ownPtStyle={$ownPtStyle},hd 行请用 balance-merge)" . PHP_EOL; return false; } $ownShopId = intval($ghs->ownShopId ?? 0); $oldBalance = bcadd((string)($ghs->balance ?? '0'), '0', 2); $oldDebtNum = intval($ghs->debtNum ?? 0); $fixedOrders = AccountMoneyClass::countInvalidPurchaseDebtOrders($ghsId); $debtStats = AccountMoneyClass::getValidPurchaseDebtStats($ghsId); $orderDebt = $debtStats['amount']; $orderDebtNum = $debtStats['num']; $positiveCredit = bccomp($oldBalance, '0', 2) > 0 ? $oldBalance : '0.00'; $newBalance = bcsub($positiveCredit, $orderDebt, 2); if ($dry) { echo "[ghsApp采购供货商] id={$ghsId} ownShopId={$ownShopId} ownPtStyle={$ghs->ownPtStyle}" . " 原balance={$oldBalance} 原debtNum={$oldDebtNum}" . " 清理幽灵单={$fixedOrders}" . " 待结{$orderDebtNum}笔合计={$orderDebt} => 新balance={$newBalance} 新debtNum={$orderDebtNum}" . PHP_EOL; return true; } $locked = BizGhsClass::getLockById($ghsId); $fixedOrders = AccountMoneyClass::syncInvalidPurchaseDebtOrders($ghsId); AccountMoneyClass::mergeOwnShopGhsBalanceFromPurchaseOrdersIfNeeded($locked, false, $force); echo "[已重算] id={$ghsId} 清理幽灵单={$fixedOrders} 待结{$orderDebtNum}笔合计={$orderDebt}" . " balance {$oldBalance}=>{$newBalance} debtNum {$oldDebtNum}=>{$orderDebtNum}" . PHP_EOL; return true; } /** * 判断 xhGhs 是否已有 balanceMerged 字段。 */ protected function columnExists($table, $column) { $db = \Yii::$app->db; $schema = $db->getTableSchema($table, true); return $schema && isset($schema->columns[$column]); } }