From 07a5489771cd6458abbc9795fee9ecae45010b91 Mon Sep 17 00:00:00 2001 From: behruz-dev Date: Sat, 1 Nov 2025 09:41:59 +0500 Subject: [PATCH] finance: add income and expence contract import commands --- core/apps/finance/admin/expence_contract.py | 2 +- core/apps/finance/data/expence_contract.json | 155 ++++++++++++ core/apps/finance/data/income_contract.json | 230 ++++++++++++++++++ .../commands/import_expence_contract.py | 42 ++++ .../commands/import_income_contract.py | 42 ++++ .../commands/import_income_expence.py | 1 - 6 files changed, 470 insertions(+), 2 deletions(-) create mode 100644 core/apps/finance/data/expence_contract.json create mode 100644 core/apps/finance/data/income_contract.json create mode 100644 core/apps/finance/management/commands/import_expence_contract.py create mode 100644 core/apps/finance/management/commands/import_income_contract.py diff --git a/core/apps/finance/admin/expence_contract.py b/core/apps/finance/admin/expence_contract.py index 87a0bdb..e8403e4 100644 --- a/core/apps/finance/admin/expence_contract.py +++ b/core/apps/finance/admin/expence_contract.py @@ -2,6 +2,6 @@ from django.contrib import admin from core.apps.finance.models import ExpenceContract - +@admin.register(ExpenceContract) class ExpenceContractAdmin(admin.ModelAdmin): list_display = ['id', 'price', 'date', 'currency'] \ No newline at end of file diff --git a/core/apps/finance/data/expence_contract.json b/core/apps/finance/data/expence_contract.json new file mode 100644 index 0000000..30bf95c --- /dev/null +++ b/core/apps/finance/data/expence_contract.json @@ -0,0 +1,155 @@ +{ + "data": { + "current_page": 1, + "total": 3, + "data": [ + { + "id": 511, + "date": "13.09.2025", + "amount": 1, + "debt_amount": 1, + "type": "expense", + "project": { + "id": 131, + "name": "Mezbon", + "is_project": false + }, + "currency": { + "id": 1, + "name": { + "ru": "Узбекский сум", + "uz": "O'zbek so'mi" + }, + "symbol": "UZS", + "icon": "https://backend.app.uyqur.uz/public/upload/image/YZfoihq4VHMSHaZziQlp.webp" + }, + "user": null, + "financial": null, + "company_person": { + "id": 938, + "type": "supplier", + "name": "1", + "description": null, + "start_date": "02.09.2025", + "status": "active", + "person": { + "id": 854, + "name": "11", + "tin": "11", + "phone": "+998111111111", + "address": null, + "longitude": null, + "latitude": null + }, + "balances": [ + { + "id": 870, + "start_amount": 0, + "amount": 0, + "start_date": "02.09.2025", + "currency": { + "id": 1, + "name": { + "ru": "Узбекский сум", + "uz": "O'zbek so'mi" + }, + "symbol": "UZS", + "icon": "https://backend.app.uyqur.uz/public/upload/image/YZfoihq4VHMSHaZziQlp.webp" + } + }, + { + "id": 909, + "start_amount": 0, + "amount": 0, + "start_date": "09.09.2025", + "currency": { + "id": 3, + "name": { + "ru": "US dollar", + "uz": "Aqsh dollari" + }, + "symbol": "USD", + "icon": "https://backend.app.uyqur.uz/public/upload/image/LIMeAcPrnbN6X9m5uYMH.webp" + } + } + ] + }, + "creator": { + "id": 111, + "full_name": "MARDONOVA DILAFRUZ", + "image": null + } + }, + { + "id": 41, + "date": "27.06.2024", + "amount": 4000000, + "debt_amount": 4000000, + "type": "expense", + "project": { + "id": 62, + "name": "Blok A", + "is_project": true + }, + "currency": { + "id": 1, + "name": { + "ru": "Узбекский сум", + "uz": "O'zbek so'mi" + }, + "symbol": "UZS", + "icon": "https://backend.app.uyqur.uz/public/upload/image/YZfoihq4VHMSHaZziQlp.webp" + }, + "user": null, + "financial": { + "id": 42, + "name": "SPECTEXNIKA QURULISHGA ARENDA" + }, + "company_person": null, + "creator": { + "id": 109, + "full_name": "Mirjonov Meronshox", + "image": null + } + }, + { + "id": 40, + "date": "27.06.2024", + "amount": 8000000, + "debt_amount": 8000000, + "type": "expense", + "project": { + "id": 62, + "name": "Blok A", + "is_project": true + }, + "currency": { + "id": 1, + "name": { + "ru": "Узбекский сум", + "uz": "O'zbek so'mi" + }, + "symbol": "UZS", + "icon": "https://backend.app.uyqur.uz/public/upload/image/YZfoihq4VHMSHaZziQlp.webp" + }, + "user": null, + "financial": { + "id": 62, + "name": "PROCHEE RASXODI" + }, + "company_person": null, + "creator": { + "id": 109, + "full_name": "Mirjonov Meronshox", + "image": null + } + } + ] + }, + "message": { + "uz": "Muvaffaqiyatli!", + "ru": "Успешно!", + "en": "Success!", + "tr": "Başarılı!" + } +} \ No newline at end of file diff --git a/core/apps/finance/data/income_contract.json b/core/apps/finance/data/income_contract.json new file mode 100644 index 0000000..2afdf74 --- /dev/null +++ b/core/apps/finance/data/income_contract.json @@ -0,0 +1,230 @@ +{ + "data": { + "current_page": 1, + "total": 3, + "data": [ + { + "id": 512, + "date": "13.09.2025", + "amount": 1, + "debt_amount": 0.9999795545, + "type": "income", + "project": { + "id": 89, + "name": "Mavrid", + "is_project": false + }, + "currency": { + "id": 3, + "name": { + "ru": "US dollar", + "uz": "Aqsh dollari" + }, + "symbol": "USD", + "icon": "https://backend.app.uyqur.uz/public/upload/image/LIMeAcPrnbN6X9m5uYMH.webp" + }, + "user": null, + "financial": null, + "company_person": { + "id": 978, + "type": "supplier", + "name": "OOO\u003C\u003CIBRAT-AFZAL TRADE\u003E\u003E", + "description": null, + "start_date": "11.09.2025", + "status": "active", + "person": { + "id": 891, + "name": "OOO\u003C\u003CIBRAT-AFZAL TRADE\u003E\u003E", + "tin": "308342724", + "phone": "+998974047441", + "address": null, + "longitude": null, + "latitude": null + }, + "balances": [ + { + "id": 919, + "start_amount": 0, + "amount": 0, + "start_date": "11.09.2025", + "currency": { + "id": 1, + "name": { + "ru": "Узбекский сум", + "uz": "O'zbek so'mi" + }, + "symbol": "UZS", + "icon": "https://backend.app.uyqur.uz/public/upload/image/YZfoihq4VHMSHaZziQlp.webp" + } + } + ] + }, + "creator": { + "id": 111, + "full_name": "MARDONOVA DILAFRUZ", + "image": null + } + }, + { + "id": 510, + "date": "12.09.2025", + "amount": 1111, + "debt_amount": 1111, + "type": "income", + "project": { + "id": 131, + "name": "Mezbon", + "is_project": false + }, + "currency": { + "id": 3, + "name": { + "ru": "US dollar", + "uz": "Aqsh dollari" + }, + "symbol": "USD", + "icon": "https://backend.app.uyqur.uz/public/upload/image/LIMeAcPrnbN6X9m5uYMH.webp" + }, + "user": null, + "financial": null, + "company_person": { + "id": 938, + "type": "supplier", + "name": "1", + "description": null, + "start_date": "02.09.2025", + "status": "active", + "person": { + "id": 854, + "name": "11", + "tin": "11", + "phone": "+998111111111", + "address": null, + "longitude": null, + "latitude": null + }, + "balances": [ + { + "id": 870, + "start_amount": 0, + "amount": 0, + "start_date": "02.09.2025", + "currency": { + "id": 1, + "name": { + "ru": "Узбекский сум", + "uz": "O'zbek so'mi" + }, + "symbol": "UZS", + "icon": "https://backend.app.uyqur.uz/public/upload/image/YZfoihq4VHMSHaZziQlp.webp" + } + }, + { + "id": 909, + "start_amount": 0, + "amount": 0, + "start_date": "09.09.2025", + "currency": { + "id": 3, + "name": { + "ru": "US dollar", + "uz": "Aqsh dollari" + }, + "symbol": "USD", + "icon": "https://backend.app.uyqur.uz/public/upload/image/LIMeAcPrnbN6X9m5uYMH.webp" + } + } + ] + }, + "creator": { + "id": 111, + "full_name": "MARDONOVA DILAFRUZ", + "image": null + } + }, + { + "id": 508, + "date": "10.09.2025", + "amount": 1, + "debt_amount": 1, + "type": "income", + "project": { + "id": 89, + "name": "Mavrid", + "is_project": false + }, + "currency": { + "id": 1, + "name": { + "ru": "Узбекский сум", + "uz": "O'zbek so'mi" + }, + "symbol": "UZS", + "icon": "https://backend.app.uyqur.uz/public/upload/image/YZfoihq4VHMSHaZziQlp.webp" + }, + "user": null, + "financial": null, + "company_person": { + "id": 938, + "type": "supplier", + "name": "1", + "description": null, + "start_date": "02.09.2025", + "status": "active", + "person": { + "id": 854, + "name": "11", + "tin": "11", + "phone": "+998111111111", + "address": null, + "longitude": null, + "latitude": null + }, + "balances": [ + { + "id": 870, + "start_amount": 0, + "amount": 0, + "start_date": "02.09.2025", + "currency": { + "id": 1, + "name": { + "ru": "Узбекский сум", + "uz": "O'zbek so'mi" + }, + "symbol": "UZS", + "icon": "https://backend.app.uyqur.uz/public/upload/image/YZfoihq4VHMSHaZziQlp.webp" + } + }, + { + "id": 909, + "start_amount": 0, + "amount": 0, + "start_date": "09.09.2025", + "currency": { + "id": 3, + "name": { + "ru": "US dollar", + "uz": "Aqsh dollari" + }, + "symbol": "USD", + "icon": "https://backend.app.uyqur.uz/public/upload/image/LIMeAcPrnbN6X9m5uYMH.webp" + } + } + ] + }, + "creator": { + "id": 111, + "full_name": "MARDONOVA DILAFRUZ", + "image": null + } + } + ] + }, + "message": { + "uz": "Muvaffaqiyatli!", + "ru": "Успешно!", + "en": "Success!", + "tr": "Başarılı!" + } +} \ No newline at end of file diff --git a/core/apps/finance/management/commands/import_expence_contract.py b/core/apps/finance/management/commands/import_expence_contract.py new file mode 100644 index 0000000..437af3f --- /dev/null +++ b/core/apps/finance/management/commands/import_expence_contract.py @@ -0,0 +1,42 @@ +import json +from datetime import datetime + +from django.core.management import BaseCommand + +from core.apps.finance.models import ExpenceContract, ExpenceType +from core.apps.accounts.models import User +from core.apps.projects.models import Project, ProjectFolder +from core.apps.counterparty.models import Counterparty + + +class Command(BaseCommand): + def add_arguments(self, parser): + parser.add_argument("file_path", type=str) + + def handle(self, *args, **options): + file_path = options['file_path'] + + with open(file_path, 'r') as f: + data = json.load(f) + + for item in data['data']['data']: + user = User.objects.filter(full_name=item['creator']['full_name']).first() + project_folder = ProjectFolder.objects.filter(name=item['project']['name']).first() + counterparty = None + if item.get('counterparty'): + counterparty = Counterparty.objects.filter(name=item['company_person']['name']).first() + expence_type = None + if item.get('financial'): + expence_type = ExpenceType.objects.filter(name=item['financial']['name']).first() + ExpenceContract.objects.get_or_create( + user=user, + project_folder=project_folder, + price=item['amount'], + currency=item['currency']['symbol'].lower(), + defaults={ + "counterparty": counterparty, + "expence_type": expence_type, + "date": datetime.strptime(item['date'], "%d.%m.%Y").date(), + } + ) + self.stdout.write(self.style.SUCCESS("Expence Contractlar qoshildi")) \ No newline at end of file diff --git a/core/apps/finance/management/commands/import_income_contract.py b/core/apps/finance/management/commands/import_income_contract.py new file mode 100644 index 0000000..7642e9d --- /dev/null +++ b/core/apps/finance/management/commands/import_income_contract.py @@ -0,0 +1,42 @@ +import json +from datetime import datetime + +from django.core.management import BaseCommand + +from core.apps.finance.models import IncomeContract, TypeIncome +from core.apps.accounts.models import User +from core.apps.projects.models import Project, ProjectFolder +from core.apps.counterparty.models import Counterparty + + +class Command(BaseCommand): + def add_arguments(self, parser): + parser.add_argument("file_path", type=str) + + def handle(self, *args, **options): + file_path = options['file_path'] + + with open(file_path, 'r') as f: + data = json.load(f) + + for item in data['data']['data']: + user = User.objects.filter(full_name=item['creator']['full_name']).first() + project_folder = ProjectFolder.objects.filter(name=item['project']['name']).first() + counterparty = None + if item.get('counterparty'): + counterparty = Counterparty.objects.filter(name=item['company_person']['name']).first() + income_type = None + if item.get('financial'): + income_type = TypeIncome.objects.filter(name=item['financial']['name']).first() + IncomeContract.objects.get_or_create( + user=user, + project_folder=project_folder, + price=item['amount'], + currency=item['currency']['symbol'].lower(), + defaults={ + "counterparty": counterparty, + "income_type": income_type, + "date": datetime.strptime(item['date'], "%d.%m.%Y").date(), + } + ) + self.stdout.write(self.style.SUCCESS("Expence Contractlar qoshildi")) \ No newline at end of file diff --git a/core/apps/finance/management/commands/import_income_expence.py b/core/apps/finance/management/commands/import_income_expence.py index 09c73c8..481f5e4 100644 --- a/core/apps/finance/management/commands/import_income_expence.py +++ b/core/apps/finance/management/commands/import_income_expence.py @@ -110,7 +110,6 @@ class Command(BaseCommand): count = 0 for page in range(round(total_data / size)): data = get_data(page) - print(page) for item in data['data']['data']: match item['type']: case "expense":