1
|
|
|
""" |
2
|
|
|
byceps.services.shop.order.invoice_service |
3
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
:Copyright: 2014-2022 Jochen Kupperschmidt |
6
|
|
|
:License: Revised BSD (see `LICENSE` file for details) |
7
|
|
|
""" |
8
|
|
|
|
9
|
1 |
|
from __future__ import annotations |
10
|
1 |
|
from typing import Optional |
11
|
|
|
|
12
|
1 |
|
from sqlalchemy import select |
13
|
|
|
|
14
|
1 |
|
from ....database import db |
15
|
|
|
|
16
|
1 |
|
from .dbmodels.invoice import Invoice as DbInvoice |
17
|
1 |
|
from . import log_service |
18
|
1 |
|
from .transfer.invoice import Invoice |
19
|
1 |
|
from .transfer.order import OrderID |
20
|
|
|
|
21
|
|
|
|
22
|
1 |
|
def add_invoice( |
23
|
|
|
order_id: OrderID, |
24
|
|
|
number: str, |
25
|
|
|
*, |
26
|
|
|
url: Optional[str] = None, |
27
|
|
|
) -> Invoice: |
28
|
|
|
"""Add an invoice to an order.""" |
29
|
|
|
db_invoice = DbInvoice(order_id, number, url=url) |
30
|
|
|
db.session.add(db_invoice) |
31
|
|
|
|
32
|
|
|
invoice = _db_entity_to_invoice(db_invoice) |
33
|
|
|
|
34
|
|
|
log_entry_data = {'invoice_number': invoice.number} |
35
|
|
|
db_log_entry = log_service.build_log_entry( |
36
|
|
|
'order-invoice-created', invoice.order_id, log_entry_data |
37
|
|
|
) |
38
|
|
|
db.session.add(db_log_entry) |
39
|
|
|
|
40
|
|
|
db.session.commit() |
41
|
|
|
|
42
|
|
|
return invoice |
43
|
|
|
|
44
|
|
|
|
45
|
1 |
|
def get_invoices_for_order(order_id: OrderID) -> list[Invoice]: |
46
|
|
|
"""Return the invoices for that order.""" |
47
|
|
|
db_invoices = db.session.scalars( |
48
|
|
|
select(DbInvoice) |
49
|
|
|
.filter_by(order_id=order_id) |
50
|
|
|
).all() |
51
|
|
|
|
52
|
|
|
return [_db_entity_to_invoice(db_invoice) for db_invoice in db_invoices] |
53
|
|
|
|
54
|
|
|
|
55
|
1 |
|
def _db_entity_to_invoice(db_invoice: DbInvoice) -> Invoice: |
56
|
|
|
return Invoice( |
57
|
|
|
id=db_invoice.id, |
58
|
|
|
order_id=db_invoice.order_id, |
59
|
|
|
number=db_invoice.number, |
60
|
|
|
url=db_invoice.url, |
61
|
|
|
) |
62
|
|
|
|