|
|
@@ -4,14 +4,16 @@ namespace console\controllers;
|
|
|
|
|
|
use biz\ghs\classes\GhsClass as BizGhsClass;
|
|
|
use bizGhs\custom\classes\AccountMoneyClass;
|
|
|
+use common\components\dict;
|
|
|
use yii\console\Controller;
|
|
|
|
|
|
/**
|
|
|
- * 采购供货商 xhGhs 余额合并(按采购待结订单重算)
|
|
|
+ * ghsApp 采购供货商 xhGhs 余额合并(按采购待结订单重算)
|
|
|
*
|
|
|
- * 【用途】批量处理 ghsApp 采购供货商(ownShopId>0)未 merge 的 xhGhs 行:
|
|
|
- * 净 balance = max(原正余额,0) - 待结采购单 actPrice 合计,debtAmount 清 0。
|
|
|
- * 【与线上一致】调用 AccountMoneyClass::mergeOwnShopGhsBalanceFromPurchaseOrdersIfNeeded。
|
|
|
+ * 【用途】仅处理 ghsApp 采购供货商行(ownPtStyle=ghs/kmGhs),不处理 hd 花店侧 xhGhs 行(ownPtStyle=hd)。
|
|
|
+ * 【公式】净 balance = max(原正余额,0) - 待结 ghs 采购单 actPrice 合计,debtAmount 清 0。
|
|
|
+ *
|
|
|
+ * hd 花店侧供货商请用:php yii balance-merge/run
|
|
|
*
|
|
|
* 用法:php yii ghs-purchase-balance-merge/run
|
|
|
* 预览:php yii ghs-purchase-balance-merge/run --dryRun=1
|
|
|
@@ -22,7 +24,7 @@ class GhsPurchaseBalanceMergeController extends Controller
|
|
|
/** 1=只预览不写库 */
|
|
|
public $dryRun = 0;
|
|
|
|
|
|
- /** 指定 xhGhs.id,0=扫全表符合条件的 ownShop 行 */
|
|
|
+ /** 指定 xhGhs.id,0=扫全表符合条件的 ghsApp 采购供货商行 */
|
|
|
public $ghsId = 0;
|
|
|
|
|
|
public function options($actionID)
|
|
|
@@ -31,13 +33,13 @@ class GhsPurchaseBalanceMergeController extends Controller
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 执行或预览:按采购待结订单重算 ownShop 供货商净余额。
|
|
|
+ * 执行或预览:仅 ghsApp 采购供货商(ownPtStyle=ghs/kmGhs)按采购待结订单重算净余额。
|
|
|
*/
|
|
|
public function actionRun()
|
|
|
{
|
|
|
$dry = (int)$this->dryRun === 1;
|
|
|
$ghsId = intval($this->ghsId);
|
|
|
- echo $dry ? "【预览:采购供货商按订单重算余额】\n" : "【执行:采购供货商按订单重算余额】\n";
|
|
|
+ echo $dry ? "【预览:ghsApp 采购供货商按订单重算余额】\n" : "【执行:ghsApp 采购供货商按订单重算余额】\n";
|
|
|
|
|
|
if ($ghsId > 0) {
|
|
|
$count = $this->mergeOneGhs($ghsId, $dry) ? 1 : 0;
|
|
|
@@ -46,22 +48,47 @@ class GhsPurchaseBalanceMergeController extends Controller
|
|
|
}
|
|
|
|
|
|
$count = 0;
|
|
|
- $where = ['and', ['>', 'ownShopId', 0]];
|
|
|
+ $purchasePtStyles = $this->getGhsAppPurchasePtStyles();
|
|
|
+ $where = ['and', ['>', 'ownShopId', 0], ['in', 'ownPtStyle', $purchasePtStyles]];
|
|
|
if ($this->columnExists('xhGhs', 'balanceMerged')) {
|
|
|
- $where = ['and', ['>', 'ownShopId', 0], ['balanceMerged' => 0]];
|
|
|
+ $where = ['and', ['>', 'ownShopId', 0], ['in', 'ownPtStyle', $purchasePtStyles], ['balanceMerged' => 0]];
|
|
|
} else {
|
|
|
- $where = ['and', ['>', 'ownShopId', 0], ['>', 'debtAmount', 0]];
|
|
|
+ $where = ['and', ['>', 'ownShopId', 0], ['in', 'ownPtStyle', $purchasePtStyles], ['>', 'debtAmount', 0]];
|
|
|
}
|
|
|
|
|
|
- $list = BizGhsClass::getAllByCondition($where, null, 'id,ownShopId,balance,debtAmount,balanceMerged', null, true);
|
|
|
+ $list = BizGhsClass::getAllByCondition($where, null, 'id,ownShopId,ownPtStyle,balance,debtAmount,balanceMerged', null, true);
|
|
|
foreach ($list as $ghs) {
|
|
|
if ($this->mergeOneGhs(intval($ghs->id ?? 0), $dry)) {
|
|
|
$count++;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ // ownPtStyle 未回填的历史行:再扫 ownPtStyle=0,由 isGhsAppPurchaseSupplierRow 按门店 ptStyle 判定
|
|
|
+ $legacyWhere = ['and', ['>', 'ownShopId', 0], ['ownPtStyle' => 0]];
|
|
|
+ if ($this->columnExists('xhGhs', 'balanceMerged')) {
|
|
|
+ $legacyWhere = ['and', ['>', 'ownShopId', 0], ['ownPtStyle' => 0], ['balanceMerged' => 0]];
|
|
|
+ }
|
|
|
+ $legacyList = BizGhsClass::getAllByCondition($legacyWhere, null, 'id,ownShopId,ownPtStyle,balance,debtAmount,balanceMerged', null, true);
|
|
|
+ foreach ($legacyList as $ghs) {
|
|
|
+ if ($this->mergeOneGhs(intval($ghs->id ?? 0), $dry)) {
|
|
|
+ $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'),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 合并单行;预览模式只打印将写入的金额。
|
|
|
*/
|
|
|
@@ -75,19 +102,21 @@ class GhsPurchaseBalanceMergeController extends Controller
|
|
|
echo "[跳过] id={$ghsId} 不存在" . PHP_EOL;
|
|
|
return false;
|
|
|
}
|
|
|
- $ownShopId = intval($ghs->ownShopId ?? 0);
|
|
|
- if ($ownShopId <= 0) {
|
|
|
- echo "[跳过] id={$ghsId} 非采购供货商行(无 ownShopId)" . PHP_EOL;
|
|
|
+ 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);
|
|
|
$orderDebt = AccountMoneyClass::sumPurchaseOrderDebtForGhs($ghsId);
|
|
|
$rawBalance = bcadd((string)($ghs->balance ?? '0'), '0', 2);
|
|
|
$positiveCredit = bccomp($rawBalance, '0', 2) > 0 ? $rawBalance : '0.00';
|
|
|
$newBalance = bcsub($positiveCredit, $orderDebt, 2);
|
|
|
|
|
|
if ($dry) {
|
|
|
- echo "[供货商] id={$ghsId} ownShopId={$ownShopId} balance={$ghs->balance} debtAmount={$ghs->debtAmount}"
|
|
|
+ echo "[ghsApp采购供货商] id={$ghsId} ownShopId={$ownShopId} ownPtStyle={$ghs->ownPtStyle}"
|
|
|
+ . " balance={$ghs->balance} debtAmount={$ghs->debtAmount}"
|
|
|
. " 采购待结合计={$orderDebt} => 新balance={$newBalance}" . PHP_EOL;
|
|
|
return true;
|
|
|
}
|