push
This commit is contained in:
160
tests/test_invoices.py
Normal file
160
tests/test_invoices.py
Normal file
@@ -0,0 +1,160 @@
|
||||
from app.models.company import Company
|
||||
from app.models.invoice import Invoice
|
||||
from app.services.sync_service import SyncService
|
||||
|
||||
|
||||
def test_invoice_list(auth_client, app):
|
||||
with app.app_context():
|
||||
company = Company.query.first()
|
||||
SyncService(company).run_manual_sync()
|
||||
response = auth_client.get('/invoices/')
|
||||
assert response.status_code == 200
|
||||
assert b'Faktury' in response.data
|
||||
|
||||
|
||||
def test_invoice_pdf(auth_client, app):
|
||||
with app.app_context():
|
||||
company = Company.query.first()
|
||||
SyncService(company).run_manual_sync()
|
||||
invoice = Invoice.query.first()
|
||||
response = auth_client.get(f'/invoices/{invoice.id}/pdf')
|
||||
assert response.status_code == 200
|
||||
assert response.mimetype == 'application/pdf'
|
||||
|
||||
|
||||
def test_issued_form_uses_quick_add_modals(auth_client):
|
||||
response = auth_client.get('/invoices/issued/new')
|
||||
assert response.status_code == 200
|
||||
assert b'customerQuickAddModal' in response.data
|
||||
assert b'productQuickAddModal' in response.data
|
||||
assert b'Szybkie dodanie klienta' not in response.data
|
||||
|
||||
|
||||
def test_invoice_list_shows_only_incoming(auth_client, app):
|
||||
from datetime import date
|
||||
from app.extensions import db
|
||||
from app.models.company import Company
|
||||
from app.models.invoice import Invoice, InvoiceStatus, InvoiceType
|
||||
|
||||
with app.app_context():
|
||||
company = Company.query.first()
|
||||
sale = Invoice(
|
||||
company_id=company.id,
|
||||
ksef_number='SALE-HIDDEN',
|
||||
invoice_number='FV/SALE/1',
|
||||
contractor_name='Sprzedaz Sp z o.o.',
|
||||
issue_date=date(2026, 3, 1),
|
||||
received_date=date(2026, 3, 1),
|
||||
net_amount=100,
|
||||
vat_amount=23,
|
||||
gross_amount=123,
|
||||
invoice_type=InvoiceType.SALE,
|
||||
status=InvoiceStatus.NEW,
|
||||
source='issued',
|
||||
issued_status='draft',
|
||||
)
|
||||
purchase = Invoice(
|
||||
company_id=company.id,
|
||||
ksef_number='PURCHASE-VISIBLE',
|
||||
invoice_number='FV/PUR/1',
|
||||
contractor_name='Dostawca Sp z o.o.',
|
||||
issue_date=date(2026, 3, 1),
|
||||
received_date=date(2026, 3, 1),
|
||||
net_amount=200,
|
||||
vat_amount=46,
|
||||
gross_amount=246,
|
||||
invoice_type=InvoiceType.PURCHASE,
|
||||
status=InvoiceStatus.NEW,
|
||||
source='ksef',
|
||||
)
|
||||
db.session.add_all([sale, purchase])
|
||||
db.session.commit()
|
||||
|
||||
response = auth_client.get('/invoices/')
|
||||
assert response.status_code == 200
|
||||
assert b'FV/PUR/1' in response.data
|
||||
assert b'FV/SALE/1' not in response.data
|
||||
assert b'Faktury otrzymane' in response.data
|
||||
|
||||
|
||||
|
||||
def test_resolve_payment_details_reads_metadata_and_xml(app, tmp_path):
|
||||
from datetime import date
|
||||
from app.extensions import db
|
||||
from app.models.company import Company
|
||||
from app.models.invoice import Invoice, InvoiceStatus, InvoiceType
|
||||
from app.services.invoice_service import InvoiceService
|
||||
|
||||
xml_path = tmp_path / 'invoice.xml'
|
||||
xml_path.write_text('''<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Faktura xmlns="http://example.com/fa">
|
||||
<Platnosc>
|
||||
<FormaPlatnosci>6</FormaPlatnosci>
|
||||
<RachunekBankowy>
|
||||
<NrRB>12 3456 7890 1234 5678 9012 3456</NrRB>
|
||||
<NazwaBanku>Bank Testowy</NazwaBanku>
|
||||
</RachunekBankowy>
|
||||
<TerminPlatnosci>
|
||||
<Termin>2026-03-31</Termin>
|
||||
</TerminPlatnosci>
|
||||
</Platnosc>
|
||||
</Faktura>''', encoding='utf-8')
|
||||
|
||||
with app.app_context():
|
||||
company = Company.query.first()
|
||||
invoice = Invoice(
|
||||
company_id=company.id,
|
||||
ksef_number='KSEF/TEST/1',
|
||||
invoice_number='FV/TEST/1',
|
||||
contractor_name='Dostawca',
|
||||
issue_date=date(2026, 3, 12),
|
||||
received_date=date(2026, 3, 12),
|
||||
net_amount=100,
|
||||
vat_amount=23,
|
||||
gross_amount=123,
|
||||
invoice_type=InvoiceType.PURCHASE,
|
||||
status=InvoiceStatus.NEW,
|
||||
source='ksef',
|
||||
xml_path=str(xml_path),
|
||||
external_metadata={'payment_form_label': 'przelew'},
|
||||
)
|
||||
db.session.add(invoice)
|
||||
db.session.commit()
|
||||
|
||||
details = InvoiceService().resolve_payment_details(invoice)
|
||||
|
||||
assert details['payment_form_code'] == '6'
|
||||
assert details['payment_form_label'] == 'przelew'
|
||||
assert details['bank_account'] == '12345678901234567890123456'
|
||||
assert details['bank_name'] == 'Bank Testowy'
|
||||
assert details['payment_due_date'] == '2026-03-31'
|
||||
|
||||
|
||||
def test_purchase_invoice_does_not_fallback_to_company_bank_account(app):
|
||||
from datetime import date
|
||||
from app.extensions import db
|
||||
from app.models.company import Company
|
||||
from app.models.invoice import Invoice, InvoiceStatus, InvoiceType
|
||||
from app.services.invoice_service import InvoiceService
|
||||
|
||||
with app.app_context():
|
||||
company = Company.query.first()
|
||||
company.bank_account = '11 1111 1111 1111 1111 1111 1111'
|
||||
invoice = Invoice(
|
||||
company_id=company.id,
|
||||
ksef_number='KSEF/TEST/2',
|
||||
invoice_number='FV/TEST/2',
|
||||
contractor_name='Dostawca',
|
||||
issue_date=date(2026, 3, 12),
|
||||
received_date=date(2026, 3, 12),
|
||||
net_amount=100,
|
||||
vat_amount=23,
|
||||
gross_amount=123,
|
||||
invoice_type=InvoiceType.PURCHASE,
|
||||
status=InvoiceStatus.NEW,
|
||||
source='ksef',
|
||||
)
|
||||
db.session.add(invoice)
|
||||
db.session.commit()
|
||||
|
||||
assert InvoiceService()._resolve_seller_bank_account(invoice) == ''
|
||||
Reference in New Issue
Block a user