LsCustomController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. print_r($customList);
  38. die;
  39. foreach ($customList as $custom) {
  40. $customId = $custom['id'] ?? 0;
  41. $debtAmount = $custom['debtAmount'] ?? 0;
  42. $hdId = $custom['hdId'] ?? 0;
  43. $customName = $custom['name'] ?? '';
  44. if (empty($hdId) || empty($customId)) {
  45. echo $customName . " customId:" . $customId . " hdId:" . $hdId . " 的ID是空的 \n";
  46. util::stop();
  47. }
  48. if ($debtAmount > 0) {
  49. $c = CustomClass::getById($customId, true);
  50. $h = HdClass::getById($hdId, true);
  51. if (empty($c) || empty($h)) {
  52. echo $customName . " customId:" . $customId . " hdId:" . $hdId . " 的对象是空的哦!! \n";
  53. util::stop();
  54. }
  55. $balance = $c->balance ?? 0;
  56. $hdBalance = $h->balance ?? 0;
  57. if (floatval($balance) != $hdBalance) {
  58. echo $customName . " customId:" . $customId . " hdId:" . $hdId . " 二边余额不一致 \n";
  59. util::stop();
  60. }
  61. $newBalance = bcsub($balance, $debtAmount, 2);
  62. $c->balance = $newBalance;
  63. $c->save();
  64. $h->balance = $newBalance;
  65. $h->save();
  66. $hdName = $h->name ?? '';
  67. $mainId = $shop['mainId'] ?? 0;
  68. $event = '合并欠款,系统操作';
  69. $capitalType = dict::getDict('capitalType', 'system', 'id');
  70. $change = [
  71. 'customId' => $customId,
  72. 'customName' => $customName,
  73. 'hdId' => $hdId,
  74. 'hdName' => $hdName,
  75. 'relateId' => 0,
  76. 'onlinePay' => 1,
  77. 'capitalType' => $capitalType,
  78. 'amount' => $debtAmount,
  79. 'balance' => $newBalance,
  80. 'io' => 0,
  81. 'side' => 0,
  82. 'payWay' => 0,
  83. 'event' => $event,
  84. 'staffId' => 0,
  85. 'staffName' => '',
  86. 'shopId' => $shopId,
  87. 'mainId' => $mainId,
  88. 'remark' => '',
  89. ];
  90. BalanceChangeClass::add($change, true);
  91. }
  92. }
  93. }
  94. $transaction->commit();
  95. } catch (\Exception $e) {
  96. $transaction->rollBack();
  97. $msg = $e->getMessage();
  98. echo "报错了:" . $msg . "\n";
  99. }
  100. }
  101. }
  102. }
  103. }