GhsController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace console\controllers;
  3. use biz\shop\models\Shop;
  4. use bizGhs\ghs\classes\GhsClass;
  5. use bizGhs\ghs\classes\GhsDebtRecordClass;
  6. use bizGhs\order\classes\PurchaseOrderClass;
  7. use common\components\dict;
  8. use common\components\noticeUtil;
  9. use common\components\util;
  10. use yii\console\Controller;
  11. use Yii;
  12. class GhsController extends Controller
  13. {
  14. //供货商欠款跟踪 ssh 20240629 重要脚本
  15. public function actionAdjustDebt()
  16. {
  17. ini_set('memory_limit', '2045M');
  18. set_time_limit(0);
  19. $query = new \yii\db\Query();
  20. $query->from(Shop::tableName());
  21. $query->where(['ptStyle' => 2]);
  22. foreach ($query->batch(10) as $batch) {
  23. foreach ($batch as $shop) {
  24. $sjName = $shop['merchantName'] ?? '';
  25. $shopName = $shop['shopName'] ?? '';
  26. $name = $sjName . ' ' . $shopName;
  27. $where = ['ownShopId' => $shop['id']];
  28. $ghsList = GhsClass::getAllByCondition($where, null, '*', null, true);
  29. if (!empty($ghsList)) {
  30. foreach ($ghsList as $ghs) {
  31. $ghsName = $ghs->name ?? '';
  32. $ghsId = $ghs->id ?? 0;
  33. $totalDebtAmount = PurchaseOrderClass::sum(['ghsId' => $ghsId, 'status' => 4, 'debt' => PurchaseOrderClass::DEBT_YES], 'actPrice');
  34. $totalDebtAmount = empty($totalDebtAmount) ? 0 : $totalDebtAmount;
  35. $ghsDebtAmount = $ghs->debtAmount ?? 0;
  36. if (floatval($totalDebtAmount) != floatval($ghsDebtAmount)) {
  37. noticeUtil::push("{$name}的供货商{$ghsName}({$ghsId})应付金额不一致。登记金额:{$ghsDebtAmount} 汇总金额:{$totalDebtAmount}", '15280215347');
  38. }
  39. }
  40. }
  41. }
  42. }
  43. }
  44. public function actionDebt()
  45. {
  46. ini_set('memory_limit', '2045M');
  47. set_time_limit(0);
  48. $sql = 'TRUNCATE `xhGhsDebtRecord`;';
  49. Yii::$app->db->createCommand($sql)->execute();
  50. $query = new \yii\db\Query();
  51. $query->from(Shop::tableName());
  52. $query->where(['ptStyle' => 2]);
  53. foreach ($query->batch(10) as $batch) {
  54. foreach ($batch as $shop) {
  55. $shopId = $shop['id'] ?? 0;
  56. $sjId = $shop['sjId'] ?? 0;
  57. $where = ['ownShopId' => $shop['id']];
  58. $ghsList = GhsClass::getAllByCondition($where, null, '*', null, true);
  59. if (!empty($ghsList)) {
  60. foreach ($ghsList as $ghs) {
  61. $ghsId = $ghs->id ?? 0;
  62. $totalDebtAmount = PurchaseOrderClass::sum(['ghsId' => $ghsId, 'status' => 4, 'debt' => PurchaseOrderClass::DEBT_YES], 'actPrice');
  63. $totalDebtAmount = empty($totalDebtAmount) ? 0 : $totalDebtAmount;
  64. $ghsAddDebtKey = 'ghs_change_debt_' . $ghsId;
  65. $lock = util::lock($ghsAddDebtKey);
  66. if (!$lock) {
  67. echo "供货商 {$ghsId} 数据没有初始化\n";
  68. continue;
  69. }
  70. if ($totalDebtAmount > 0) {
  71. $ghs->debt = 2;
  72. } else {
  73. $ghs->debt = 1;
  74. }
  75. $ghs->debtAmount = $totalDebtAmount;
  76. $ghs->save();
  77. $capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
  78. $debtData = [
  79. 'ghsId' => $ghsId,
  80. 'relateId' => 0,
  81. 'ptStyle' => 2,
  82. 'capitalType' => $capitalType,
  83. 'amount' => $totalDebtAmount,
  84. 'balance' => $totalDebtAmount,
  85. 'io' => 1,
  86. 'event' => '欠款数据初始化',
  87. 'sjId' => $sjId,
  88. 'shopId' => $shopId,
  89. ];
  90. GhsDebtRecordClass::add($debtData);
  91. util::unlock($ghsAddDebtKey);
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }