maniaquerie
[photoprint.git] / price.py
1 # -*- coding: utf-8 -*-
2 #######################################################################################
3 # Copyright © 2009 Benoît Pin <pin@cri.ensmp.fr> #
4 # Plinn - http://plinn.org #
5 # #
6 # #
7 # This program is free software; you can redistribute it and/or #
8 # modify it under the terms of the GNU General Public License #
9 # as published by the Free Software Foundation; either version 2 #
10 # of the License, or (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program; if not, write to the Free Software #
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #
20 #######################################################################################
21 """
22 Pricing types
23
24
25
26 """
27
28 from Globals import Persistent
29 from AccessControl import ModuleSecurityInfo
30 from utils import Message as _
31 from utils import translate
32 from zope.globalrequest import getRequest
33
34 msecurity = ModuleSecurityInfo('Products.photoprint.price')
35 msecurity.declarePublic('Price')
36
37 class Price(object, Persistent) :
38 """
39 Price of an object which have VAT tax.
40 """
41 __allow_access_to_unprotected_subobjects__ = 1
42
43 def __init__(self, taxedPrice, rate=0) :
44 """price is initialized with taxed value"""
45 self._rate = float(rate)
46 self._setTaxed(taxedPrice)
47
48 @property
49 def rate(self):
50 return self._localeStrNum(self._rate)
51
52 def _setTaxed(self, value) :
53 self._taxed = value
54 self._price = value / (1 + self._rate)
55
56 @property
57 def taxed(self) :
58 return self._localeStrNum(self._taxed)
59
60 @property
61 def value(self) :
62 return self._localeStrNum(self._price)
63
64 @property
65 def tax(self) :
66 tax = self._rate * self._price
67 return self._localeStrNum(tax)
68
69 @property
70 def vat(self) :
71 "returns vat rate in percent"
72 vat = self._rate * 100
73 return self._localeStrNum(vat)
74
75 def _localeStrNum(self, n) :
76 i = int(n)
77 if i == n :
78 return str(i)
79 else :
80 n = str(round(n, 2))
81 i, d = n.split('.')
82 ds = _(u'${i}.${d}', mapping={'i':i, 'd':d}, default=n)
83 return translate(ds).encode('utf-8')
84
85 def getValues(self) :
86 values = {'value':self._price,
87 'taxed': self._taxed,
88 'rate':self._rate}
89 return values
90
91
92 def __add__(self, other) :
93 taxed = self._taxed + other._taxed
94 value = self._price + other._price
95 rate = (taxed - value ) / float(value)
96 return Price(taxed, rate)
97
98 def __div__(self, other) :
99 return Price(self._taxed / other, self._rate)
100
101 def __mul__(self, other) :
102 return Price(self._taxed * other, self._rate)
103
104 def __repr__(self):
105 return '%s with VAT' % self.taxed