Passed
Push — master ( feb3de...56c573 )
by Jordi
04:25
created

apply_discount()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of SENAITE.CORE
4
#
5
# Copyright 2018 by it's authors.
6
# Some rights reserved. See LICENSE.rst, CONTRIBUTORS.rst.
7
8
from persistent.mapping import PersistentMapping
9
10
11
class PricelistLineItem(PersistentMapping):
12
    pass
13
14
15
def apply_discount(price=None, discount=None):
16
    return float(price) - (float(price) * float(discount)) / 100
17
18
19
def get_vat_amount(price, vat_perc):
20
    return float(price) * float(vat_perc) / 100
21
22
23
def ObjectModifiedEventHandler(instance, event):
24
    """ Various types need automation on edit.
25
    """
26
    if not hasattr(instance, 'portal_type'):
27
        return
28
29
    if instance.portal_type == 'Pricelist':
30
        """ Create price list line items
31
        """
32
        # Remove existing line items
33
        instance.pricelist_lineitems = []
34
        for p in instance.portal_catalog(portal_type=instance.getType(),
35
                                         is_active=True):
36
            obj = p.getObject()
37
            itemDescription = None
38
            itemAccredited = False
39
            if instance.getType() == "LabProduct":
40
                print_detail = ""
41
                if obj.getVolume():
42
                    print_detail = print_detail + str(obj.getVolume())
43
                if obj.getUnit():
44
                    print_detail = print_detail + str(obj.getUnit())
45
                if obj.getVolume() or obj.getUnit():
46
                    print_detail = " (" + print_detail + ")"
47
                    itemTitle = obj.Title() + print_detail
48
                else:
49
                    itemTitle = obj.Title()
50
                cat = None
51
                if obj.getPrice():
52
                    price = float(obj.getPrice())
53
                    totalprice = float(obj.getTotalPrice())
54
                    vat = totalprice - price
55
                else:
56
                    price = 0
57
                    totalprice = 0
58
                    vat = 0
59
            elif instance.getType() == "AnalysisService":
60
61
                if str(obj.getUnit()):
62
                    print_detail = " (" + str(obj.getUnit()) + ")"
63
                    itemTitle = obj.Title() + print_detail
64
                else:
65
                    itemTitle = obj.Title()
66
                itemAccredited = obj.getAccredited()
67
68
                cat = obj.getCategoryTitle()
69
                if instance.getBulkDiscount():
70
                    price = float(obj.getBulkPrice())
71
                    vat = get_vat_amount(price, obj.getVAT())
72
                    totalprice = price + vat
73
                else:
74
                    if instance.getBulkPrice():
75
                        discount = instance.getBulkPrice()
76
                        price = float(obj.getPrice())
77
                        price = apply_discount(price, discount)
78
                        vat = get_vat_amount(price, obj.getVAT())
79
                        totalprice = price + vat
80
                    elif obj.getPrice():
81
                        price = float(obj.getPrice())
82
                        vat = get_vat_amount(price, obj.getVAT())
83
                        totalprice = price + vat
84
                    else:
85
                        totalprice = 0
86
                        price = 0
87
                        vat = 0
88
89
            if instance.getDescriptions():
90
                itemDescription = obj.Description()
91
92
            li = PricelistLineItem()
93
            li["title"] = itemTitle
0 ignored issues
show
introduced by
The variable itemTitle does not seem to be defined for all execution paths.
Loading history...
94
            li["ItemDescription"] = itemDescription
95
            li["CategoryTitle"] = cat
0 ignored issues
show
introduced by
The variable cat does not seem to be defined for all execution paths.
Loading history...
96
            li["Accredited"] = itemAccredited
97
            li["Subtotal"] = "%0.2f" % price
0 ignored issues
show
introduced by
The variable price does not seem to be defined for all execution paths.
Loading history...
98
            li["VATAmount"] = "%0.2f" % vat
0 ignored issues
show
introduced by
The variable vat does not seem to be defined for all execution paths.
Loading history...
99
            li["Total"] = "%0.2f" % totalprice
0 ignored issues
show
introduced by
The variable totalprice does not seem to be defined for all execution paths.
Loading history...
100
            instance.pricelist_lineitems.append(li)
101