forked from OCA/pos
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpost_install.py
43 lines (37 loc) · 1.46 KB
/
post_install.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Copyright (C) 2022 - Today: GRAP (http://www.grap.coop)
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import api, SUPERUSER_ID
import logging
logger = logging.getLogger(__name__)
def set_margin_on_pos_invoices(cr, registry):
logger.info(
"Define purchase prices on invoice lines"
" created from Point of Sale. This could take a while ..."
)
with api.Environment.manage():
env = api.Environment(cr, SUPERUSER_ID, {})
orders = env["pos.order"].search([
("margin_percent", "!=", 100),
("state", "=", "invoiced")]
)
count = 1
total_count = len(orders)
for order in orders:
logger.info(
"{count}/{total_count}: Compute margin for"
" invoice related to {order_name}".format(
count=count,
total_count=total_count,
order_name=order.name
)
)
invoice = order.invoice_id
for invoice_line in invoice.invoice_line_ids:
# Try to get the related order_line
order_lines = order.lines.filtered(
lambda x: x.product_id == invoice_line.product_id
)
if order_lines:
invoice_line.purchase_price = order_lines[0].purchase_price
count += 1