BalanceUnmergeController.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace console\controllers;
  3. use bizGhs\cg\classes\CgClass;
  4. use bizGhs\custom\classes\CustomBalanceChangeClass;
  5. use bizGhs\custom\classes\CustomClass;
  6. use bizGhs\custom\classes\CustomDebtChangeClass;
  7. use bizHd\ghs\classes\GhsClass;
  8. use bizHd\purchase\classes\PurchaseClass;
  9. use common\components\dict;
  10. use bizGhs\ghs\classes\GhsBalanceChangeClass;
  11. use bizHd\ghs\classes\GhsDebtChangeClass;
  12. use yii\console\Controller;
  13. use Yii;
  14. class BalanceUnmergeController extends Controller
  15. {
  16. public function actionRun()
  17. {
  18. $connection = Yii::$app->db;
  19. $transaction = $connection->beginTransaction();
  20. try {
  21. $customBalanceList = CustomBalanceChangeClass::getAllByCondition(['capitalType' => 81], null, '*', null, true);
  22. if (!empty($customBalanceList)) {
  23. foreach ($customBalanceList as $customBalance) {
  24. $customBalanceId = $customBalance->id ?? 0;
  25. $customId = $customBalance->customId ?? 0;
  26. $custom = CustomClass::getById($customId, true);
  27. if (empty($custom)) {
  28. echo $customId . " 没有客户信息";
  29. exit();
  30. }
  31. $customName = $custom->name ?? '';
  32. $before = CustomBalanceChangeClass::getByCondition(['customId' => $customId, 'id<' => $customBalanceId],true,'id desc' );
  33. $beforeBalance = $before->balance ?? 0;
  34. if($customId == 25695){
  35. //小吉要补3000
  36. $beforeBalance = bcadd($beforeBalance, 3000, 2);
  37. }
  38. $ghsId = $custom->ghsId ?? 0;
  39. $ghs = GhsClass::getById($ghsId, true);
  40. if (empty($ghs)) {
  41. echo $customId . " 客户供货商信息没有";
  42. exit();
  43. }
  44. $ghsName = $ghs->name ?? '';
  45. $ownPtStyle = $ghs->ownPtStyle ?? 1;
  46. if ($ownPtStyle == 1) {
  47. $cgList = PurchaseClass::getAllByCondition(['ghsId' => $ghsId, 'debt' => 1], null, '*', null, true);
  48. $debtAmount = 0;
  49. if (!empty($cgList)) {
  50. foreach ($cgList as $cg) {
  51. $currentDebt = $cg->remainDebtPrice;
  52. $debtAmount = bcadd($debtAmount, $currentDebt, 2);
  53. }
  54. }
  55. $has = CustomBalanceChangeClass::getAllByCondition(['customId' => $customId, 'id>' => $customBalanceId], null, '*', null, true);
  56. $hasCount = count($has);
  57. if ($hasCount > 0) {
  58. echo $customName . "({$ghsName}){$customId} 要恢复欠款:" . $debtAmount . " 余额:" . $beforeBalance . " 合并之后还有余额变动{$hasCount}条" . " ------ \n";
  59. } else {
  60. echo $customName . "({$ghsName}){$customId} 要恢复欠款:" . $debtAmount . " 余额:" . $beforeBalance . " ------ \n";
  61. }
  62. } elseif ($ownPtStyle == 2) {
  63. $cgList = CgClass::getAllByCondition(['ghsId' => $ghsId, 'debt' => 1], null, '*', null, true);
  64. $debtAmount = 0;
  65. if (!empty($cgList)) {
  66. foreach ($cgList as $cg) {
  67. $currentDebt = $cg->actPrice;
  68. $debtAmount = bcadd($debtAmount, $currentDebt, 2);
  69. }
  70. }
  71. $has = CustomBalanceChangeClass::getAllByCondition(['customId' => $customId, 'id>' => $customBalanceId], null, '*', null, true);
  72. $hasCount = count($has);
  73. if ($hasCount > 0) {
  74. echo $customName . "({$ghsName}){$customId}【供货商端】要恢复欠款:" . $debtAmount . " 余额:" . $beforeBalance . " 合并之后还有余额变动{$hasCount}条" . " $$$$$$$ \n";
  75. } else {
  76. echo $customName . "({$ghsName}){$customId}【供货商端】要恢复欠款:" . $debtAmount . " 余额:" . $beforeBalance . " $$$$$$$ \n";
  77. }
  78. } else {
  79. echo $customId . " 客户ptStyle有问题" . $ownPtStyle;
  80. exit();
  81. }
  82. //continue;
  83. $custom->debtAmount = $debtAmount;
  84. $custom->balanceMerged = 0;
  85. $custom->balance = $beforeBalance;
  86. $custom->save();
  87. $ghs->debtAmount = $debtAmount;
  88. $ghs->balanceMerged = 0;
  89. $ghs->balance = $beforeBalance;
  90. $ghs->save();
  91. // 1. 写入客户余额变动明细(CustomBalanceChange)
  92. $cbData = [
  93. 'customId' => $custom->id ?? 0,
  94. 'customName' => $custom->name ?? '',
  95. 'relateId' => 0,
  96. 'onlinePay' => 0,
  97. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  98. 'capitalType' => 10,
  99. 'amount' => bcsub($beforeBalance, $customBalance->balance ?? 0, 2), // 变化金额 = 恢复后的余额 - 之前的余额
  100. 'balance' => $beforeBalance,
  101. 'staffId' => 0,
  102. 'staffName' => '',
  103. 'io' => 1,
  104. 'side' => 0,
  105. 'payWay' => 0,
  106. 'fromType' => dict::getDict('fromType', 'shop'),
  107. 'event' => '系统升级恢复',
  108. 'mainId' => $ghs->mainId ?? 0,
  109. 'shopId' => $ghs->shopId ?? 0,
  110. 'sjId' => $ghs->sjId ?? 0,
  111. 'remark' => '',
  112. ];
  113. CustomBalanceChangeClass::add($cbData, true);
  114. // 2. 写入供货商余额变动明细(GhsBalanceChange)
  115. $gbData = [
  116. 'ghsId' => $ghs->id ?? 0,
  117. 'relateId' => 0,
  118. 'ptStyle' => 1,
  119. 'capitalType' => 11,
  120. 'amount' => bcsub($beforeBalance, $customBalance->balance ?? 0, 2), // 变化金额 = 恢复后的余额 - 之前的余额
  121. 'balance' => $beforeBalance,
  122. 'io' => 1,
  123. 'side' => 0,
  124. 'onlinePay' => 0,
  125. 'payWay' => 0,
  126. 'fromType' => dict::getDict('fromType', 'shop'),
  127. 'event' => '系统升级恢复',
  128. 'mainId' => $custom->mainId ?? 0,
  129. 'shopId' => $custom->shopId ?? 0,
  130. 'sjId' => $custom->sjId ?? 0,
  131. 'remark' => '',
  132. ];
  133. GhsBalanceChangeClass::add($gbData, true);
  134. // 3. 写入客户欠款挂账变动明细(CustomDebtChangeClass)
  135. $cdcData = [
  136. 'customId' => $custom->id ?? 0,
  137. 'customName' => $custom->name ?? '',
  138. 'relateId' => 0,
  139. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  140. 'capitalType' => 20, // 20 对应 结账/变动
  141. 'amount' => $debtAmount,
  142. 'balance' => $debtAmount,
  143. 'io' => 0, // 0 对应增加欠款
  144. 'event' => '系统升级恢复',
  145. 'mainId' => $ghs->mainId ?? 0,
  146. 'shopId' => $ghs->shopId ?? 0,
  147. 'sjId' => $ghs->sjId ?? 0,
  148. 'remark' => '',
  149. ];
  150. CustomDebtChangeClass::addChange($cdcData);
  151. // 4. 写入供货商欠款挂账变动明细(GhsDebtChangeClass)
  152. $gdcData = [
  153. 'ghsId' => $ghs->id ?? 0,
  154. 'ptStyle' => 1,
  155. 'capitalType' => 20, // 20 对应 结账/变动
  156. 'amount' => $debtAmount,
  157. 'balance' => $debtAmount,
  158. 'io' => 0, // 0 对应增加欠款
  159. 'event' => '系统升级恢复',
  160. 'mainId' => $custom->mainId ?? 0,
  161. 'shopId' => $custom->shopId ?? 0,
  162. 'sjId' => $custom->sjId ?? 0,
  163. 'remark' => '',
  164. ];
  165. GhsDebtChangeClass::addChange($gdcData);
  166. }
  167. }
  168. $transaction->commit();
  169. } catch (\Exception $e) {
  170. $transaction->rollBack();
  171. $msg = $e->getMessage();
  172. echo $msg;
  173. }
  174. }
  175. }