LsCustomController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace console\controllers;
  3. use bizHd\balance\classes\BalanceChangeClass;
  4. use bizHd\custom\classes\CustomClass;
  5. use bizHd\custom\classes\HdClass;
  6. use bizHd\custom\models\Custom;
  7. use bizHd\shop\models\Shop;
  8. use common\components\dict;
  9. use common\components\util;
  10. use yii\console\Controller;
  11. use Yii;
  12. class LsCustomController extends Controller
  13. {
  14. public function actionIndex()
  15. {
  16. //先确认所有客户的欠款总数跟账单总和能不能对应上 ssh 20250620
  17. //批发不需要这一步
  18. //零售要去数据库确认这一步
  19. }
  20. public function actionMigrate()
  21. {
  22. $query = new \yii\db\Query();
  23. $query->from(Shop::tableName());
  24. $query->where(['ptStyle' => 1]);
  25. $query->orderBy('addTime DESC');
  26. foreach ($query->batch(20) as $shopList) {
  27. foreach ($shopList as $shop) {
  28. $connection = Yii::$app->db;
  29. $transaction = $connection->beginTransaction();
  30. try {
  31. $shopId = $shop['id'] ?? 0;
  32. $query = new \yii\db\Query();
  33. $query->from(Custom::tableName());
  34. $query->where(['shopId' => $shopId]);
  35. $query->orderBy('addTime ASC');
  36. foreach ($query->batch(500) as $customList) {
  37. foreach ($customList as $custom) {
  38. $customId = $custom['id'] ?? 0;
  39. $debtAmount = $custom['debtAmount'] ?? 0;
  40. $hdId = $custom['hdId'] ?? 0;
  41. $customName = $custom['name'] ?? '';
  42. if (empty($hdId) || empty($customId)) {
  43. echo $customName . " customId:" . $customId . " hdId:" . $hdId . " 的ID是空的 \n";
  44. util::stop();
  45. }
  46. if ($debtAmount > 0) {
  47. echo "shopId:" . $shopId . " | " . $customName . " customId:" . $customId . " hdId:" . $hdId . " 有欠款 ==== \n\n";
  48. $custom = CustomClass::getById($customId, true);
  49. $hd = HdClass::getById($hdId, true);
  50. if (empty($custom) || empty($hd)) {
  51. echo $customName . " customId:" . $customId . " hdId:" . $hdId . " 的对象是空的哦!! \n";
  52. util::stop();
  53. }
  54. $balance = $custom->balance ?? 0;
  55. $hdBalance = $hd->balance ?? 0;
  56. if (floatval($balance) != $hdBalance) {
  57. echo $customName . " customId:" . $customId . " hdId:" . $hdId . " 二边余额不一致 \n";
  58. util::stop();
  59. }
  60. $newBalance = bcsub($balance, $debtAmount, 2);
  61. $custom->balance = $newBalance;
  62. $custom->debtAmount = 0;
  63. $custom->isDebt = 0;
  64. $custom->debtNum = 0;
  65. $custom->save();
  66. $hd->balance = $newBalance;
  67. $hd->debtAmount = 0;
  68. $hd->debt = 1;
  69. $hd->debtNum = 0;
  70. $hd->save();
  71. $hdName = $hd->name ?? '';
  72. $mainId = $shop['mainId'] ?? 0;
  73. $event = '系统操作,合并欠款到余额';
  74. $capitalType = dict::getDict('capitalType', 'system', 'id');
  75. $change = [
  76. 'customId' => $customId,
  77. 'customName' => $customName,
  78. 'hdId' => $hdId,
  79. 'hdName' => $hdName,
  80. 'relateId' => 0,
  81. 'onlinePay' => 1,
  82. 'capitalType' => $capitalType,
  83. 'amount' => $debtAmount,
  84. 'balance' => $newBalance,
  85. 'io' => 0,
  86. 'side' => 0,
  87. 'payWay' => 0,
  88. 'event' => $event,
  89. 'staffId' => 0,
  90. 'staffName' => '',
  91. 'shopId' => $shopId,
  92. 'mainId' => $mainId,
  93. 'remark' => '',
  94. ];
  95. BalanceChangeClass::add($change, true);
  96. } else {
  97. //echo "shopId:".$shopId." | ".$customName . " customId:" . $customId . " hdId:" . $hdId . " 没有欠款 ok ok \n";
  98. }
  99. }
  100. }
  101. $transaction->commit();
  102. } catch (\Exception $e) {
  103. $transaction->rollBack();
  104. $msg = $e->getMessage();
  105. echo "报错了:" . $msg . "\n";
  106. }
  107. }
  108. }
  109. }
  110. }