From 73112a90de7999da7b1f8ddbda97648f14b15b78 Mon Sep 17 00:00:00 2001 From: behruz-dev Date: Fri, 31 Oct 2025 22:14:41 +0500 Subject: [PATCH] finance: expence and income type import command added --- .../commands/import_income_expence_type.py | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 core/apps/finance/management/commands/import_income_expence_type.py diff --git a/core/apps/finance/management/commands/import_income_expence_type.py b/core/apps/finance/management/commands/import_income_expence_type.py new file mode 100644 index 0000000..1afc160 --- /dev/null +++ b/core/apps/finance/management/commands/import_income_expence_type.py @@ -0,0 +1,56 @@ +import requests + +from django.core.management import BaseCommand + +from core.apps.finance.models import ExpenceType, TypeIncome + +headers = { + "Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2JhY2tlbmQuYXBwLnV5cXVyLnV6L21haW4vYXV0aC9sb2dpbiIsImlhdCI6MTc2MTkyMDM2MSwiZXhwIjoxNzYyMDA2NzYxLCJuYmYiOjE3NjE5MjAzNjEsImp0aSI6Inhqak81azJLc2pSaEJJOGUiLCJzdWIiOiIxMDQiLCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.ZcREfvT21qpd9eK_-zBumKBtaKKJ-l9QoudSLZ3IpP4" +} + +def get_data(): + url = "https://backend.app.uyqur.uz/main/financial/view" + + response = requests.get(url, headers=headers) + if response.status_code == 200: + return response.json() + return response.json() + + +class Command(BaseCommand): + def handle(self, *args, **options): + data = get_data() + income_categories = { + "Boshqalar": "OTHERS", + "Doimiy daromad": "CONTANT_INCOME" + } + expence_categories = { + "Boshqalar": "OTHERS", + "Doimiy xarajatlar": "FIXED_COST", + } + activities = { + "Moliyaviy": "FINANCIAL", + "Sarmoya": "CAPITAL" + } + + for income in data['data']['income']: + TypeIncome.objects.get_or_create( + name=income['name'], + defaults={ + "comment": income['description'], + "category": income_categories.get(income['financial_category']['name']['uz']), + "activity": activities.get(income['activity_type']['name']['uz']), + } + ) + + for expence in data['data']['expense']: + ExpenceType.objects.get_or_create( + name=expence['name'], + defaults={ + "comment": expence.get('description') if expence.get('description') else "Comment", + "category": expence_categories.get(expence['financial_category']['name']['uz']), + "activity": activities.get(expence['activity_type']['name']['uz']), + } + ) + + self.stdout.write("IncomeType and ExpenceType qo'shildi") \ No newline at end of file