Sfoglia il codice sorgente

Merge branch 'master' into dev

shish 1 mese fa
parent
commit
c7b45c9e13

+ 7 - 8
biz-ghs/custom/classes/AccountMoneyClass.php

@@ -95,21 +95,20 @@ class AccountMoneyClass
 
     /**
      * 【用途】把接口返回给前端的金额字段,按新版形态格式化。
-     * 【说明】由于已全部升级至新版,直接返回净 balance,并将废弃的 debtAmount 设为 '0.00'。
+     * 【说明】由于已全部升级至新版,直接返回净 balance。为了前端各页面和打印机安全兼容,
+     *        我们将真实的待结欠款(正数)同时赋予 debtAmount 和 remainDebtAmount。
      */
     public static function formatMoneyForClient(array $row, $appVersion = null)
     {
         $net = self::getNetBalanceFromRow($row);
         $row['displayBalance'] = $net;
         $row['balance'] = $net;
-        $row['debtAmount'] = '0.00';
         
-        // remainDebtAmount 辅助展示:负数余额(欠款)时显示为正数,正数余额时显示为负数
-        $remain = bccomp($net, '0', 2) < 0 ? bcmul($net, '-1', 2) : '0.00';
-        if (bccomp($net, '0', 2) > 0) {
-            $remain = bcsub('0', $net, 2);
-        }
-        $row['remainDebtAmount'] = $remain;
+        // 计算出真实的待结欠款(正数)
+        $outstanding = bccomp($net, '0', 2) < 0 ? bcmul($net, '-1', 2) : '0.00';
+        
+        $row['debtAmount'] = $outstanding;
+        $row['remainDebtAmount'] = $outstanding;
         return $row;
     }
 

+ 0 - 95
console/controllers/BalanceMergeController.php

@@ -1,95 +0,0 @@
-<?php
-
-namespace console\controllers;
-
-use biz\ghs\classes\GhsClass as BizGhsClass;
-use bizGhs\custom\classes\AccountMoneyClass;
-use bizGhs\custom\classes\CustomClass;
-use yii\console\Controller;
-
-/**
- * 挂账并入余额 — 控制台补漏(休眠账户批量处理)
- *
- * 【用途】处理长期未触发「开单/充值/结账」等入口、仍未 merge 的历史账户。
- * 内部调用 AccountMoneyClass::mergeCustomMoneyForScript / mergeGhsMoneyForScript(线上业务不再自动合并)。
- *
- * 用法:php yii balance-merge/run  或  --dryRun=1 仅预览
- */
-class BalanceMergeController extends Controller
-{
-    /** 1=只打印将处理的 id 与金额,不写库 */
-    public $dryRun = 0;
-
-    public function options($actionID)
-    {
-        return array_merge(parent::options($actionID), ['dryRun']);
-    }
-
-    /**
-     * 【用途】执行或预览补漏:先客户表 xhGhsCustom,再关系表 xhGhs。
-     */
-    public function actionRun()
-    {
-        $dry = (int)$this->dryRun === 1;
-        echo $dry ? "【预览模式】\n" : "【执行合并】\n";
-        $customCount = $this->mergeCustomRows($dry);
-        $ghsCount = $this->mergeGhsRows($dry);
-        echo "完成:客户 {$customCount} 条,供货商关系 {$ghsCount} 条,dryRun=" . ($dry ? '1' : '0') . PHP_EOL;
-    }
-
-    /**
-     * 【用途】扫 xhGhsCustom:debtAmount>0 且(有 balanceMerged 列时)balanceMerged=0,逐条脚本合并。
-     */
-    protected function mergeCustomRows($dry)
-    {
-        $count = 0;
-        $where = ['debtAmount>' => 0];
-        if ($this->columnExists('xhGhsCustom', 'balanceMerged')) {
-            $where['balanceMerged'] = 0;
-        }
-        $list = CustomClass::getAllByCondition($where, null, '*', null, true);
-        foreach ($list as $custom) {
-            if ($dry) {
-                echo "[客户] id={$custom->id} debtAmount={$custom->debtAmount} balance={$custom->balance}" . PHP_EOL;
-                $count++;
-                continue;
-            }
-            AccountMoneyClass::mergeCustomMoneyForScript($custom, true);
-            $count++;
-        }
-        return $count;
-    }
-
-    /**
-     * 【用途】扫 xhGhs:条件同上,逐条 mergeGhsMoneyForScript(不自动带 custom,休眠 ghs 单独处理)。
-     */
-    protected function mergeGhsRows($dry)
-    {
-        $count = 0;
-        $where = ['debtAmount>' => 0];
-        if ($this->columnExists('xhGhs', 'balanceMerged')) {
-            $where['balanceMerged'] = 0;
-        }
-        $list = BizGhsClass::getAllByCondition($where, null, '*', null, true);
-        foreach ($list as $ghs) {
-            if ($dry) {
-                echo "[关系] id={$ghs->id} debtAmount={$ghs->debtAmount} balance={$ghs->balance}" . PHP_EOL;
-                $count++;
-                continue;
-            }
-            AccountMoneyClass::mergeGhsMoneyForScript($ghs, true);
-            $count++;
-        }
-        return $count;
-    }
-
-    /**
-     * 【用途】判断迁移 SQL 是否已执行,避免 where balanceMerged 报错。
-     */
-    protected function columnExists($table, $column)
-    {
-        $db = \Yii::$app->db;
-        $schema = $db->getTableSchema($table, true);
-        return $schema && isset($schema->columns[$column]);
-    }
-}

+ 0 - 197
console/controllers/BalanceUnmergeController.php

@@ -1,197 +0,0 @@
-<?php
-
-namespace console\controllers;
-
-use bizGhs\cg\classes\CgClass;
-use bizGhs\custom\classes\CustomBalanceChangeClass;
-use bizGhs\custom\classes\CustomClass;
-use bizGhs\custom\classes\CustomDebtChangeClass;
-use bizHd\ghs\classes\GhsClass;
-use bizHd\purchase\classes\PurchaseClass;
-use common\components\dict;
-use bizGhs\ghs\classes\GhsBalanceChangeClass;
-use bizHd\ghs\classes\GhsDebtChangeClass;
-use yii\console\Controller;
-use Yii;
-
-class BalanceUnmergeController extends Controller
-{
-
-
-    public function actionRun()
-    {
-
-        $connection = Yii::$app->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;
-        }
-
-    }
-
-}

+ 0 - 159
console/controllers/GhsPurchaseBalanceMergeController.php

@@ -1,159 +0,0 @@
-<?php
-
-namespace console\controllers;
-
-use biz\ghs\classes\GhsClass as BizGhsClass;
-use bizGhs\custom\classes\AccountMoneyClass;
-use common\components\dict;
-use yii\console\Controller;
-
-/**
- * ghsApp 采购供货商 xhGhs 余额合并(按采购待结订单重算)
- *
- * 【用途】仅处理 ghsApp 采购供货商行(ownPtStyle=ghs/kmGhs),不处理 hd 花店侧 xhGhs 行(ownPtStyle=hd)。
- * 【公式】净 balance = max(当前正余额,0) - 待结 ghs 采购单 actPrice 合计;debtNum 与有效待结单数对齐。
- * 【可重复执行】默认 force=1,已 balanceMerged 的行也会重算,并清理 actPrice<=0/已取消仍标待结的幽灵单。
- *
- * hd 花店侧供货商请用:php yii balance-merge/run
- *
- * 用法:php yii ghs-purchase-balance-merge/run
- * 预览:php yii ghs-purchase-balance-merge/run --dryRun=1
- * 单户:php yii ghs-purchase-balance-merge/run --ghsId=123
- * 仅未合并:php yii ghs-purchase-balance-merge/run --force=0
- */
-class GhsPurchaseBalanceMergeController extends Controller
-{
-    /** 1=只预览不写库 */
-    public $dryRun = 0;
-
-    /** 指定 xhGhs.id,0=扫全表符合条件的 ghsApp 采购供货商行 */
-    public $ghsId = 0;
-
-    /** 1=已合并行也重算(默认);0=仅处理未合并行 */
-    public $force = 1;
-
-    public function options($actionID)
-    {
-        return array_merge(parent::options($actionID), ['dryRun', 'ghsId', 'force']);
-    }
-
-    /**
-     * 执行或预览:仅 ghsApp 采购供货商(ownPtStyle=ghs/kmGhs)按采购待结订单重算净余额。
-     */
-    public function actionRun()
-    {
-        $dry = (int)$this->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]);
-    }
-}