GhsPurchaseBalanceMergeController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace console\controllers;
  3. use biz\ghs\classes\GhsClass as BizGhsClass;
  4. use bizGhs\custom\classes\AccountMoneyClass;
  5. use yii\console\Controller;
  6. /**
  7. * 采购供货商 xhGhs 余额合并(按采购待结订单重算)
  8. *
  9. * 【用途】批量处理 ghsApp 采购供货商(ownShopId>0)未 merge 的 xhGhs 行:
  10. * 净 balance = max(原正余额,0) - 待结采购单 actPrice 合计,debtAmount 清 0。
  11. * 【与线上一致】调用 AccountMoneyClass::mergeOwnShopGhsBalanceFromPurchaseOrdersIfNeeded。
  12. *
  13. * 用法:php yii ghs-purchase-balance-merge/run
  14. * 预览:php yii ghs-purchase-balance-merge/run --dryRun=1
  15. * 单户:php yii ghs-purchase-balance-merge/run --ghsId=123
  16. */
  17. class GhsPurchaseBalanceMergeController extends Controller
  18. {
  19. /** 1=只预览不写库 */
  20. public $dryRun = 0;
  21. /** 指定 xhGhs.id,0=扫全表符合条件的 ownShop 行 */
  22. public $ghsId = 0;
  23. public function options($actionID)
  24. {
  25. return array_merge(parent::options($actionID), ['dryRun', 'ghsId']);
  26. }
  27. /**
  28. * 执行或预览:按采购待结订单重算 ownShop 供货商净余额。
  29. */
  30. public function actionRun()
  31. {
  32. $dry = (int)$this->dryRun === 1;
  33. $ghsId = intval($this->ghsId);
  34. echo $dry ? "【预览:采购供货商按订单重算余额】\n" : "【执行:采购供货商按订单重算余额】\n";
  35. if ($ghsId > 0) {
  36. $count = $this->mergeOneGhs($ghsId, $dry) ? 1 : 0;
  37. echo "完成:处理 {$count} 条,dryRun=" . ($dry ? '1' : '0') . PHP_EOL;
  38. return;
  39. }
  40. $count = 0;
  41. $where = ['and', ['>', 'ownShopId', 0]];
  42. if ($this->columnExists('xhGhs', 'balanceMerged')) {
  43. $where = ['and', ['>', 'ownShopId', 0], ['balanceMerged' => 0]];
  44. } else {
  45. $where = ['and', ['>', 'ownShopId', 0], ['>', 'debtAmount', 0]];
  46. }
  47. $list = BizGhsClass::getAllByCondition($where, null, 'id,ownShopId,balance,debtAmount,balanceMerged', null, true);
  48. foreach ($list as $ghs) {
  49. if ($this->mergeOneGhs(intval($ghs->id ?? 0), $dry)) {
  50. $count++;
  51. }
  52. }
  53. echo "完成:处理 {$count} 条,dryRun=" . ($dry ? '1' : '0') . PHP_EOL;
  54. }
  55. /**
  56. * 合并单行;预览模式只打印将写入的金额。
  57. */
  58. protected function mergeOneGhs($ghsId, $dry)
  59. {
  60. if ($ghsId <= 0) {
  61. return false;
  62. }
  63. $ghs = BizGhsClass::getById($ghsId, true);
  64. if (empty($ghs)) {
  65. echo "[跳过] id={$ghsId} 不存在" . PHP_EOL;
  66. return false;
  67. }
  68. $ownShopId = intval($ghs->ownShopId ?? 0);
  69. if ($ownShopId <= 0) {
  70. echo "[跳过] id={$ghsId} 非采购供货商行(无 ownShopId)" . PHP_EOL;
  71. return false;
  72. }
  73. $orderDebt = AccountMoneyClass::sumPurchaseOrderDebtForGhs($ghsId);
  74. $rawBalance = bcadd((string)($ghs->balance ?? '0'), '0', 2);
  75. $positiveCredit = bccomp($rawBalance, '0', 2) > 0 ? $rawBalance : '0.00';
  76. $newBalance = bcsub($positiveCredit, $orderDebt, 2);
  77. if ($dry) {
  78. echo "[供货商] id={$ghsId} ownShopId={$ownShopId} balance={$ghs->balance} debtAmount={$ghs->debtAmount}"
  79. . " 采购待结合计={$orderDebt} => 新balance={$newBalance}" . PHP_EOL;
  80. return true;
  81. }
  82. $locked = BizGhsClass::getLockById($ghsId);
  83. AccountMoneyClass::mergeOwnShopGhsBalanceFromPurchaseOrdersIfNeeded($locked, true);
  84. echo "[已合并] id={$ghsId} 采购待结={$orderDebt} 新balance={$newBalance}" . PHP_EOL;
  85. return true;
  86. }
  87. /**
  88. * 判断 xhGhs 是否已有 balanceMerged 字段。
  89. */
  90. protected function columnExists($table, $column)
  91. {
  92. $db = \Yii::$app->db;
  93. $schema = $db->getTableSchema($table, true);
  94. return $schema && isset($schema->columns[$column]);
  95. }
  96. }