GhsPurchaseBalanceMergeController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace console\controllers;
  3. use biz\ghs\classes\GhsClass as BizGhsClass;
  4. use bizGhs\custom\classes\AccountMoneyClass;
  5. use common\components\dict;
  6. use yii\console\Controller;
  7. /**
  8. * ghsApp 采购供货商 xhGhs 余额合并(按采购待结订单重算)
  9. *
  10. * 【用途】仅处理 ghsApp 采购供货商行(ownPtStyle=ghs/kmGhs),不处理 hd 花店侧 xhGhs 行(ownPtStyle=hd)。
  11. * 【公式】净 balance = max(原正余额,0) - 待结 ghs 采购单 actPrice 合计,debtAmount 清 0。
  12. *
  13. * hd 花店侧供货商请用:php yii balance-merge/run
  14. *
  15. * 用法:php yii ghs-purchase-balance-merge/run
  16. * 预览:php yii ghs-purchase-balance-merge/run --dryRun=1
  17. * 单户:php yii ghs-purchase-balance-merge/run --ghsId=123
  18. */
  19. class GhsPurchaseBalanceMergeController extends Controller
  20. {
  21. /** 1=只预览不写库 */
  22. public $dryRun = 0;
  23. /** 指定 xhGhs.id,0=扫全表符合条件的 ghsApp 采购供货商行 */
  24. public $ghsId = 0;
  25. public function options($actionID)
  26. {
  27. return array_merge(parent::options($actionID), ['dryRun', 'ghsId']);
  28. }
  29. /**
  30. * 执行或预览:仅 ghsApp 采购供货商(ownPtStyle=ghs/kmGhs)按采购待结订单重算净余额。
  31. */
  32. public function actionRun()
  33. {
  34. $dry = (int)$this->dryRun === 1;
  35. $ghsId = intval($this->ghsId);
  36. echo $dry ? "【预览:ghsApp 采购供货商按订单重算余额】\n" : "【执行:ghsApp 采购供货商按订单重算余额】\n";
  37. if ($ghsId > 0) {
  38. $count = $this->mergeOneGhs($ghsId, $dry) ? 1 : 0;
  39. echo "完成:处理 {$count} 条,dryRun=" . ($dry ? '1' : '0') . PHP_EOL;
  40. return;
  41. }
  42. $count = 0;
  43. $purchasePtStyles = $this->getGhsAppPurchasePtStyles();
  44. // 本项目 conditionQuery 只支持哈希条件,不支持 Yii 的 ['and', ...] 写法
  45. $where = [
  46. 'ownShopId>' => 0,
  47. 'ownPtStyle' => ['in', $purchasePtStyles],
  48. ];
  49. if ($this->columnExists('xhGhs', 'balanceMerged')) {
  50. $where['balanceMerged'] = 0;
  51. } else {
  52. $where['debtAmount>'] = 0;
  53. }
  54. $list = BizGhsClass::getAllByCondition($where, null, 'id,ownShopId,ownPtStyle,balance,debtAmount,balanceMerged', null, true);
  55. foreach ($list as $ghs) {
  56. if ($this->mergeOneGhs(intval($ghs->id ?? 0), $dry)) {
  57. $count++;
  58. }
  59. }
  60. // ownPtStyle 未回填的历史行:再扫 ownPtStyle=0,由 isGhsAppPurchaseSupplierRow 按门店 ptStyle 判定
  61. $legacyWhere = [
  62. 'ownShopId>' => 0,
  63. 'ownPtStyle' => 0,
  64. ];
  65. if ($this->columnExists('xhGhs', 'balanceMerged')) {
  66. $legacyWhere['balanceMerged'] = 0;
  67. }
  68. $legacyList = BizGhsClass::getAllByCondition($legacyWhere, null, 'id,ownShopId,ownPtStyle,balance,debtAmount,balanceMerged', null, true);
  69. foreach ($legacyList as $ghs) {
  70. if ($this->mergeOneGhs(intval($ghs->id ?? 0), $dry)) {
  71. $count++;
  72. }
  73. }
  74. echo "完成:处理 {$count} 条,dryRun=" . ($dry ? '1' : '0') . PHP_EOL;
  75. }
  76. /**
  77. * ghsApp 采购买方平台类型:二级批发(2)、基地端(4);不含 hd 花店(1)。
  78. */
  79. protected function getGhsAppPurchasePtStyles()
  80. {
  81. return [
  82. (int)dict::getDict('ptStyle', 'ghs'),
  83. (int)dict::getDict('ptStyle', 'kmGhs'),
  84. ];
  85. }
  86. /**
  87. * 合并单行;预览模式只打印将写入的金额。
  88. */
  89. protected function mergeOneGhs($ghsId, $dry)
  90. {
  91. if ($ghsId <= 0) {
  92. return false;
  93. }
  94. $ghs = BizGhsClass::getById($ghsId, true);
  95. if (empty($ghs)) {
  96. echo "[跳过] id={$ghsId} 不存在" . PHP_EOL;
  97. return false;
  98. }
  99. if (!AccountMoneyClass::isGhsAppPurchaseSupplierRow($ghs)) {
  100. $ownPtStyle = intval($ghs->ownPtStyle ?? 0);
  101. echo "[跳过] id={$ghsId} 非 ghsApp 采购供货商行(ownPtStyle={$ownPtStyle},hd 行请用 balance-merge)" . PHP_EOL;
  102. return false;
  103. }
  104. $ownShopId = intval($ghs->ownShopId ?? 0);
  105. $orderDebt = AccountMoneyClass::sumPurchaseOrderDebtForGhs($ghsId);
  106. $rawBalance = bcadd((string)($ghs->balance ?? '0'), '0', 2);
  107. $positiveCredit = bccomp($rawBalance, '0', 2) > 0 ? $rawBalance : '0.00';
  108. $newBalance = bcsub($positiveCredit, $orderDebt, 2);
  109. if ($dry) {
  110. echo "[ghsApp采购供货商] id={$ghsId} ownShopId={$ownShopId} ownPtStyle={$ghs->ownPtStyle}"
  111. . " balance={$ghs->balance} debtAmount={$ghs->debtAmount}"
  112. . " 采购待结合计={$orderDebt} => 新balance={$newBalance}" . PHP_EOL;
  113. return true;
  114. }
  115. $locked = BizGhsClass::getLockById($ghsId);
  116. AccountMoneyClass::mergeOwnShopGhsBalanceFromPurchaseOrdersIfNeeded($locked, true);
  117. echo "[已合并] id={$ghsId} 采购待结={$orderDebt} 新balance={$newBalance}" . PHP_EOL;
  118. return true;
  119. }
  120. /**
  121. * 判断 xhGhs 是否已有 balanceMerged 字段。
  122. */
  123. protected function columnExists($table, $column)
  124. {
  125. $db = \Yii::$app->db;
  126. $schema = $db->getTableSchema($table, true);
  127. return $schema && isset($schema->columns[$column]);
  128. }
  129. }