push
This commit is contained in:
99
tests/test_nfz_production.py
Normal file
99
tests/test_nfz_production.py
Normal file
@@ -0,0 +1,99 @@
|
||||
from decimal import Decimal
|
||||
|
||||
from app.extensions import db
|
||||
from app.models.catalog import Customer, Product, InvoiceLine
|
||||
from app.models.company import Company
|
||||
from app.models.invoice import Invoice, InvoiceStatus, InvoiceType
|
||||
from app.models.setting import AppSetting
|
||||
from app.services.invoice_service import InvoiceService
|
||||
|
||||
|
||||
def _enable_nfz(app):
|
||||
with app.app_context():
|
||||
company = Company.query.first()
|
||||
AppSetting.set(f'company.{company.id}.modules.nfz_enabled', 'true')
|
||||
customer = Customer.query.filter_by(company_id=company.id, tax_id='1070001057').first()
|
||||
if not customer:
|
||||
customer = Customer(company_id=company.id, name='NFZ Client', tax_id='1070001057', is_active=True)
|
||||
db.session.add(customer)
|
||||
product = Product.query.filter_by(company_id=company.id, name='Swiadczenie NFZ').first()
|
||||
if not product:
|
||||
product = Product(company_id=company.id, name='Swiadczenie NFZ', unit='usl', net_price=Decimal('100.00'), vat_rate=Decimal('8.00'), is_active=True)
|
||||
db.session.add(product)
|
||||
db.session.commit()
|
||||
return company.id, customer.id, product.id
|
||||
|
||||
|
||||
def test_duplicate_nfz_redirects_to_nfz_form(auth_client, app):
|
||||
company_id, customer_id, product_id = _enable_nfz(app)
|
||||
with app.app_context():
|
||||
invoice = Invoice(
|
||||
company_id=company_id,
|
||||
customer_id=customer_id,
|
||||
ksef_number='NFZ-PENDING/1',
|
||||
invoice_number='FV/NFZ/1',
|
||||
contractor_name='Narodowy Fundusz Zdrowia - Slaski OW NFZ',
|
||||
contractor_nip='1070001057',
|
||||
issue_date=InvoiceService.today_date(),
|
||||
received_date=InvoiceService.today_date(),
|
||||
net_amount=Decimal('100.00'),
|
||||
vat_amount=Decimal('8.00'),
|
||||
gross_amount=Decimal('108.00'),
|
||||
invoice_type=InvoiceType.SALE,
|
||||
status=InvoiceStatus.NEW,
|
||||
source='nfz',
|
||||
issued_status='draft',
|
||||
external_metadata={'nfz': {'recipient_branch_id': '1070001057-00122', 'settlement_from': '2026-02-01', 'settlement_to': '2026-02-28', 'template_identifier': 'TMP', 'provider_identifier': 'SWD1', 'service_code': '01.02', 'contract_number': 'C/1', 'nfz_schema': 'FA(3)'}},
|
||||
)
|
||||
db.session.add(invoice)
|
||||
db.session.flush()
|
||||
db.session.add(InvoiceLine(invoice_id=invoice.id, product_id=product_id, description='Swiadczenie NFZ', quantity=1, unit='usl', unit_net=Decimal('100.00'), vat_rate=Decimal('8.00'), net_amount=Decimal('100.00'), vat_amount=Decimal('8.00'), gross_amount=Decimal('108.00')))
|
||||
db.session.commit()
|
||||
invoice_id = invoice.id
|
||||
|
||||
response = auth_client.get(f'/invoices/{invoice_id}/duplicate', follow_redirects=False)
|
||||
assert response.status_code == 302
|
||||
assert f'/nfz/?duplicate_id={invoice_id}' in response.headers['Location']
|
||||
|
||||
form_response = auth_client.get(response.headers['Location'])
|
||||
body = form_response.get_data(as_text=True)
|
||||
assert form_response.status_code == 200
|
||||
assert 'FV/NFZ/1/COPY' in body
|
||||
assert 'SWD1' in body
|
||||
assert 'C/1' in body
|
||||
|
||||
|
||||
def test_build_ksef_payload_contains_nfz_xml(app):
|
||||
company_id, customer_id, product_id = _enable_nfz(app)
|
||||
with app.app_context():
|
||||
company = Company.query.first()
|
||||
invoice = Invoice(
|
||||
company_id=company_id,
|
||||
company=company,
|
||||
customer_id=customer_id,
|
||||
ksef_number='NFZ-PENDING/2',
|
||||
invoice_number='FV/NFZ/2',
|
||||
contractor_name='Narodowy Fundusz Zdrowia - Slaski OW NFZ',
|
||||
contractor_nip='1070001057',
|
||||
issue_date=InvoiceService.today_date(),
|
||||
received_date=InvoiceService.today_date(),
|
||||
net_amount=Decimal('100.00'),
|
||||
vat_amount=Decimal('8.00'),
|
||||
gross_amount=Decimal('108.00'),
|
||||
invoice_type=InvoiceType.SALE,
|
||||
status=InvoiceStatus.NEW,
|
||||
source='nfz',
|
||||
issued_status='draft',
|
||||
external_metadata={'nfz': {'recipient_branch_id': '1070001057-00122', 'settlement_from': '2026-02-01', 'settlement_to': '2026-02-28', 'template_identifier': 'TMP', 'provider_identifier': 'SWD1', 'service_code': '01.02', 'contract_number': 'C/1', 'nfz_schema': 'FA(3)'}},
|
||||
)
|
||||
db.session.add(invoice)
|
||||
db.session.flush()
|
||||
db.session.add(InvoiceLine(invoice_id=invoice.id, product_id=product_id, description='Swiadczenie NFZ', quantity=1, unit='usl', unit_net=Decimal('100.00'), vat_rate=Decimal('8.00'), net_amount=Decimal('100.00'), vat_amount=Decimal('8.00'), gross_amount=Decimal('108.00')))
|
||||
db.session.commit()
|
||||
|
||||
payload = InvoiceService().build_ksef_payload(invoice)
|
||||
assert payload['schemaVersion'] == 'FA(3)'
|
||||
assert '<IDWew>1070001057-00122</IDWew>' in payload['xml_content']
|
||||
assert '<P_6_Od>2026-02-01</P_6_Od>' in payload['xml_content']
|
||||
assert '<P_6_Do>2026-02-28</P_6_Do>' in payload['xml_content']
|
||||
assert 'NrUmowy' in payload['xml_content']
|
||||
Reference in New Issue
Block a user