Ménage
[photoprint.git] / price.py
1 # -*- coding: utf-8 -*-
2 ############################################################
3 # Copyright © 2009 Benoît PIN <pinbe@luxia.fr> #
4 # Cliché - http://luxia.fr #
5 # #
6 # This program is free software; you can redistribute it #
7 # and/or modify it under the terms of the Creative Commons #
8 # "Attribution-Noncommercial 2.0 Generic" #
9 # http://creativecommons.org/licenses/by-nc/2.0/ #
10 ############################################################
11 """
12 Pricing types
13
14
15
16 """
17
18 from Globals import Persistent
19 from AccessControl import ModuleSecurityInfo
20 from utils import Message as _
21 from utils import translate
22 from zope.globalrequest import getRequest
23
24 msecurity = ModuleSecurityInfo('Products.photoprint.price')
25 msecurity.declarePublic('Price')
26
27 class Price(object, Persistent) :
28 """
29 Price of an object which have VAT tax.
30 """
31 __allow_access_to_unprotected_subobjects__ = 1
32
33 def __init__(self, taxedPrice, rate=0) :
34 """price is initialized with taxed value"""
35 self._rate = float(rate)
36 self._setTaxed(taxedPrice)
37
38 @property
39 def rate(self):
40 return self._localeStrNum(self._rate)
41
42 def _setTaxed(self, value) :
43 self._taxed = value
44 self._price = value / (1 + self._rate)
45
46 @property
47 def taxed(self) :
48 return self._localeStrNum(self._taxed)
49
50 @property
51 def value(self) :
52 return self._localeStrNum(self._price)
53
54 @property
55 def tax(self) :
56 tax = self._rate * self._price
57 return self._localeStrNum(tax)
58
59 @property
60 def vat(self) :
61 "returns vat rate in percent"
62 vat = self._rate * 100
63 return self._localeStrNum(vat)
64
65 def _localeStrNum(self, n) :
66 i = int(n)
67 if i == n :
68 return str(i)
69 else :
70 n = str(round(n, 2))
71 i, d = n.split('.')
72 ds = _(u'${i}.${d}', mapping={'i':i, 'd':d}, default=n)
73 return translate(ds).encode('utf-8')
74
75 def getValues(self) :
76 values = {'value':self._price,
77 'taxed': self._taxed,
78 'rate':self._rate}
79 return values
80
81
82 def __add__(self, other) :
83 taxed = self._taxed + other._taxed
84 value = self._price + other._price
85 rate = (taxed - value ) / float(value)
86 return Price(taxed, rate)
87
88 def __div__(self, other) :
89 return Price(self._taxed / other, self._rate)
90
91 def __mul__(self, other) :
92 return Price(self._taxed * other, self._rate)
93
94 def __repr__(self):
95 return '%s with VAT' % self.taxed