Отдельное приложение под финансы. API
This commit is contained in:
parent
54e7da200d
commit
c074f25796
0
app/apps/__init__.py
Normal file
0
app/apps/__init__.py
Normal file
0
app/apps/finance/__init__.py
Normal file
0
app/apps/finance/__init__.py
Normal file
6
app/apps/finance/apps.py
Normal file
6
app/apps/finance/apps.py
Normal file
@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class FinanceConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'apps.finance'
|
||||
0
app/apps/finance/migrations/__init__.py
Normal file
0
app/apps/finance/migrations/__init__.py
Normal file
30
app/apps/finance/models.py
Normal file
30
app/apps/finance/models.py
Normal file
@ -0,0 +1,30 @@
|
||||
import decimal
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class DebtorDebt:
|
||||
"""
|
||||
Модель элемента дебиторской задолженности.
|
||||
counter_party: Контрагент;
|
||||
debt: Долг;
|
||||
advance: Аванс.
|
||||
"""
|
||||
|
||||
counter_party: str
|
||||
debt: decimal.Decimal
|
||||
advance: decimal.Decimal
|
||||
|
||||
|
||||
@dataclass
|
||||
class Balance:
|
||||
"""
|
||||
Модель элемента Остатков по расчетным счетам.
|
||||
money type: Вид денежных средств;
|
||||
place: Размещение;
|
||||
amount_balance: Сумма остаток.
|
||||
"""
|
||||
|
||||
money_type: str
|
||||
place: str
|
||||
amount_balance: decimal.Decimal
|
||||
78
app/apps/finance/services/integration_1c/api.py
Normal file
78
app/apps/finance/services/integration_1c/api.py
Normal file
@ -0,0 +1,78 @@
|
||||
import datetime
|
||||
import decimal
|
||||
import traceback
|
||||
|
||||
import requests
|
||||
from apps.finance.models import Balance, DebtorDebt
|
||||
from requests.auth import HTTPBasicAuth
|
||||
from sabron.util import logging
|
||||
from sabron.util.sabronmodule import get_settings
|
||||
|
||||
|
||||
class Finance1CAPI:
|
||||
def __init__(self):
|
||||
self.fin_1c_api_login = get_settings('1c_api_login')
|
||||
self.fin_1c_api_password = get_settings('1c_api_password')
|
||||
self.fin_1c_api_host = get_settings('1c_api_host')
|
||||
|
||||
def get_balance(self) -> (list[Balance], decimal.Decimal):
|
||||
try:
|
||||
|
||||
headers = {
|
||||
'Accept': 'application/json',
|
||||
}
|
||||
basic = HTTPBasicAuth(self.fin_1c_api_login, self.fin_1c_api_password)
|
||||
|
||||
url = f'{self.fin_1c_api_host}/hs/saphire-api/balance'
|
||||
|
||||
response = requests.get(url, headers=headers, auth=basic)
|
||||
|
||||
if response.status_code == 200:
|
||||
response_data = response.json()
|
||||
balance_data = []
|
||||
for balance_item in response_data['data']:
|
||||
balance_data.append(
|
||||
Balance(
|
||||
money_type=balance_item['ВидДенежныхСредств'],
|
||||
place=balance_item['Размещение'],
|
||||
amount_balance=balance_item['СуммаОстаток'],
|
||||
)
|
||||
)
|
||||
return balance_data, response_data['summary']
|
||||
return [], decimal.Decimal(0)
|
||||
except Exception:
|
||||
logging.error(traceback.format_exc())
|
||||
return [], decimal.Decimal(0)
|
||||
|
||||
def get_debts(self) -> (list[DebtorDebt], dict[str, decimal.Decimal]):
|
||||
try:
|
||||
headers = {
|
||||
'Accept': 'application/json',
|
||||
}
|
||||
basic = HTTPBasicAuth(self.fin_1c_api_login, self.fin_1c_api_password)
|
||||
date_today = datetime.datetime.now()
|
||||
formatted_date = date_today.strftime('%Y%m%d')
|
||||
url = f'{self.fin_1c_api_host}/hs/saphire-api/debts/{formatted_date}'
|
||||
|
||||
response = requests.get(url, headers=headers, auth=basic)
|
||||
|
||||
if response.status_code == 200:
|
||||
response_data = response.json()
|
||||
debts_data = []
|
||||
for balance_item in response_data['data']:
|
||||
debts_data.append(
|
||||
DebtorDebt(
|
||||
counter_party=balance_item['Контрагент'],
|
||||
debt=balance_item['Долг'],
|
||||
advance=balance_item['Аванс'],
|
||||
)
|
||||
)
|
||||
summary_dict = {
|
||||
'debt': response_data['summaryproviders']['Долг'],
|
||||
'advance': response_data['summaryproviders']['Аванс'],
|
||||
}
|
||||
return debts_data, summary_dict
|
||||
return [], {'debt': 0, 'advance': 0}
|
||||
except Exception:
|
||||
logging.error(traceback.format_exc())
|
||||
return [], {'debt': 0, 'advance': 0}
|
||||
33
app/apps/finance/services/integration_1c/service.py
Normal file
33
app/apps/finance/services/integration_1c/service.py
Normal file
@ -0,0 +1,33 @@
|
||||
from dataclasses import asdict
|
||||
|
||||
from apps.finance.services.integration_1c.api import Finance1CAPI
|
||||
from apps.main.services.base_service import DashboardBaseService
|
||||
|
||||
|
||||
class Finance1CService(DashboardBaseService):
|
||||
@staticmethod
|
||||
def get_balance() -> dict:
|
||||
|
||||
api = Finance1CAPI()
|
||||
data, summary = api.get_balance()
|
||||
result = {
|
||||
'error': '',
|
||||
'status': 0,
|
||||
'total': len(data),
|
||||
'items': [asdict(item) for item in data],
|
||||
'summary': summary
|
||||
}
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
def get_debtor_debts() -> dict:
|
||||
api = Finance1CAPI()
|
||||
data, summary = api.get_debts()
|
||||
result = {
|
||||
'error': '',
|
||||
'status': 0,
|
||||
'total': len(data),
|
||||
'items': [asdict(item) for item in data],
|
||||
'summary': summary
|
||||
}
|
||||
return result
|
||||
15
app/apps/finance/services/integration_1c/views.py
Normal file
15
app/apps/finance/services/integration_1c/views.py
Normal file
@ -0,0 +1,15 @@
|
||||
from apps.finance.services.integration_1c.service import Finance1CService
|
||||
from apps.util.base_view import base_view
|
||||
from django.http import JsonResponse
|
||||
|
||||
|
||||
@base_view
|
||||
def get_balance(request):
|
||||
data = Finance1CService.get_balance()
|
||||
return JsonResponse(data)
|
||||
|
||||
|
||||
@base_view
|
||||
def get_debtor_debts(request):
|
||||
data = Finance1CService.get_debtor_debts()
|
||||
return JsonResponse(data)
|
||||
11
app/apps/finance/urls.py
Normal file
11
app/apps/finance/urls.py
Normal file
@ -0,0 +1,11 @@
|
||||
from apps.finance.services.integration_1c.views import get_balance, get_debtor_debts
|
||||
from apps.finance.views_money import fin_index
|
||||
from django.urls import path
|
||||
|
||||
app_name = 'finance'
|
||||
|
||||
urlpatterns = [
|
||||
path('', fin_index, name='fin_index'),
|
||||
path('balance/', get_balance, name='get_balance'),
|
||||
path('debts/', get_debtor_debts, name='get_debtor_debts-'),
|
||||
]
|
||||
@ -1,9 +1,10 @@
|
||||
{% extends 'default.html' %}
|
||||
|
||||
{% block content %}
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<h1>test</h1>
|
||||
<div class="m-content" style="padding-top: 10px; padding-bottom: 10px;">
|
||||
<div class="row">
|
||||
<div class="col-xl-8">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
from apps.main import views_main
|
||||
from apps.main.services.meteo.views import get_meteo_data
|
||||
from apps.main.views_main import aspiration, scales, worktime_history
|
||||
from apps.main.views_money import fin_index
|
||||
from django.urls import include, path
|
||||
|
||||
urlpatterns = [
|
||||
@ -16,8 +15,6 @@ urlpatterns = [
|
||||
path('aspiration/', aspiration, name='aspiration'),
|
||||
path('worktime/history/', worktime_history, name='worktime_history'),
|
||||
path('meteo/', get_meteo_data, name='get_meteo_data'),
|
||||
# Деньги
|
||||
path('fin/', fin_index, name='fin_index'),
|
||||
]
|
||||
|
||||
# create_worktime_data()
|
||||
|
||||
@ -15,6 +15,7 @@ urlpatterns = [
|
||||
re_path(r'^accounts/', views.user_login, name='user_login'),
|
||||
re_path(r'^admin/', admin.site.urls),
|
||||
path('management/', include('apps.management.urls'), name='management'),
|
||||
path('finance/', include('apps.finance.urls'), name='finance'),
|
||||
]
|
||||
|
||||
urlpatterns += static(settings.DOWNLOAD_URL, document_root=settings.DOWNLOAD_ROOT)
|
||||
|
||||
@ -286,7 +286,7 @@
|
||||
{% endif %}
|
||||
{% if user_have_fin_group %}
|
||||
<li class="m-menu__item " aria-haspopup="true">
|
||||
<a href="{% url 'fin_index' %}" class="m-menu__link ">
|
||||
<a href="{% url 'finance:fin_index' %}" class="m-menu__link ">
|
||||
<i class="m-menu__link-icon flaticon-graphic-1"></i>
|
||||
<span class="m-menu__link-title">
|
||||
<span class="m-menu__link-wrap">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user