GhsController.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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\util;
  9. use yii\console\Controller;
  10. use Yii;
  11. class GhsController extends Controller
  12. {
  13. public function actionDebt()
  14. {
  15. ini_set('memory_limit', '2045M');
  16. set_time_limit(0);
  17. $sql = 'TRUNCATE `xhGhsDebtRecord`;';
  18. Yii::$app->db->createCommand($sql)->execute();
  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. $shopId = $shop['id'] ?? 0;
  25. $sjId = $shop['sjId'] ?? 0;
  26. $where = ['ownShopId' => $shop['id']];
  27. $ghsList = GhsClass::getAllByCondition($where, null, '*', null, true);
  28. if (!empty($ghsList)) {
  29. foreach ($ghsList as $ghs) {
  30. $ghsId = $ghs->id ?? 0;
  31. $totalDebtAmount = PurchaseOrderClass::sum(['ghsId' => $ghsId, 'status' => 4, 'debt' => PurchaseOrderClass::DEBT_YES], 'actPrice');
  32. $ghsAddDebtKey = 'ghs_change_debt_' . $ghsId;
  33. $lock = util::lock($ghsAddDebtKey);
  34. if (!$lock) {
  35. echo "供货商 {$ghsId} 数据没有初始化\n";
  36. continue;
  37. }
  38. if ($totalDebtAmount > 0) {
  39. $ghs->debt = 2;
  40. } else {
  41. $ghs->debt = 1;
  42. }
  43. $ghs->debtAmount = $totalDebtAmount;
  44. $ghs->save();
  45. $capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
  46. $debtData = [
  47. 'ghsId' => $ghsId,
  48. 'relateId' => 0,
  49. 'ptStyle' => 2,
  50. 'capitalType' => $capitalType,
  51. 'amount' => $totalDebtAmount,
  52. 'balance' => $totalDebtAmount,
  53. 'io' => 1,
  54. 'event' => '欠款数据初始化',
  55. 'sjId' => $sjId,
  56. 'shopId' => $shopId,
  57. ];
  58. GhsDebtRecordClass::add($debtData);
  59. util::unlock($ghsAddDebtKey);
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }