LsCustomController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 ASC');
  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(50) 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";
  48. $c = CustomClass::getById($customId, true);
  49. $h = HdClass::getById($hdId, true);
  50. if (empty($c) || empty($h)) {
  51. echo $customName . " customId:" . $customId . " hdId:" . $hdId . " 的对象是空的哦!! \n";
  52. util::stop();
  53. }
  54. $balance = $c->balance ?? 0;
  55. $hdBalance = $h->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. $c->balance = $newBalance;
  62. $c->save();
  63. $h->balance = $newBalance;
  64. $h->save();
  65. $hdName = $h->name ?? '';
  66. $mainId = $shop['mainId'] ?? 0;
  67. $event = '合并欠款,系统操作';
  68. $capitalType = dict::getDict('capitalType', 'system', 'id');
  69. $change = [
  70. 'customId' => $customId,
  71. 'customName' => $customName,
  72. 'hdId' => $hdId,
  73. 'hdName' => $hdName,
  74. 'relateId' => 0,
  75. 'onlinePay' => 1,
  76. 'capitalType' => $capitalType,
  77. 'amount' => $debtAmount,
  78. 'balance' => $newBalance,
  79. 'io' => 0,
  80. 'side' => 0,
  81. 'payWay' => 0,
  82. 'event' => $event,
  83. 'staffId' => 0,
  84. 'staffName' => '',
  85. 'shopId' => $shopId,
  86. 'mainId' => $mainId,
  87. 'remark' => '',
  88. ];
  89. BalanceChangeClass::add($change, true);
  90. }else{
  91. echo "shopId:".$shopId." | ".$customName . " customId:" . $customId . " hdId:" . $hdId . " 没有欠款 ok ok \n";
  92. }
  93. }
  94. }
  95. $transaction->commit();
  96. } catch (\Exception $e) {
  97. $transaction->rollBack();
  98. $msg = $e->getMessage();
  99. echo "报错了:" . $msg . "\n";
  100. }
  101. }
  102. }
  103. }
  104. }