Total Complexity | 2 |
Total Lines | 53 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | """ |
||
2 | byceps.services.shop.order.order_invoice_domain_service |
||
3 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
4 | |||
5 | :Copyright: 2014-2024 Jochen Kupperschmidt |
||
6 | :License: Revised BSD (see `LICENSE` file for details) |
||
7 | """ |
||
8 | |||
9 | from datetime import datetime |
||
10 | |||
11 | from byceps.services.user.models.user import User |
||
12 | from byceps.util.uuid import generate_uuid7 |
||
13 | |||
14 | from .models.invoice import Invoice |
||
15 | from .models.log import OrderLogEntry |
||
16 | from .models.order import OrderID |
||
17 | |||
18 | |||
19 | def create_invoice( |
||
20 | order_id: OrderID, |
||
21 | initiator: User, |
||
22 | number: str, |
||
23 | *, |
||
24 | url: str | None = None, |
||
25 | ) -> tuple[Invoice, OrderLogEntry]: |
||
26 | """Create an invoice for an order.""" |
||
27 | invoice = Invoice( |
||
28 | id=generate_uuid7(), |
||
29 | order_id=order_id, |
||
30 | number=number, |
||
31 | url=url, |
||
32 | ) |
||
33 | |||
34 | log_entry = _build_order_invoice_created_log_entry(invoice, initiator) |
||
35 | |||
36 | return invoice, log_entry |
||
37 | |||
38 | |||
39 | def _build_order_invoice_created_log_entry( |
||
40 | invoice: Invoice, initiator: User |
||
41 | ) -> OrderLogEntry: |
||
42 | data = { |
||
43 | 'initiator_id': str(initiator.id), |
||
44 | 'invoice_number': invoice.number, |
||
45 | } |
||
46 | |||
47 | return OrderLogEntry( |
||
48 | id=generate_uuid7(), |
||
49 | occurred_at=datetime.utcnow(), |
||
50 | event_type='order-invoice-created', |
||
51 | order_id=invoice.order_id, |
||
52 | data=data, |
||
53 | ) |
||
54 |