1 # -*- coding: utf-8 -*-
2 #######################################################################################
3 # Copyright © 2009 Benoît Pin <pin@cri.ensmp.fr> #
4 # Plinn - http://plinn.org #
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. #
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. #
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 #######################################################################################
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
34 msecurity
= ModuleSecurityInfo('Products.photoprint.price')
35 msecurity
.declarePublic('Price')
37 class Price(object, Persistent
) :
39 Price of an object which have VAT tax.
41 __allow_access_to_unprotected_subobjects__
= 1
43 def __init__(self
, taxedPrice
, rate
=0) :
44 """price is initialized with taxed value"""
45 self
._rate
= float(rate
)
46 self
._setTaxed
(taxedPrice
)
50 return self
._localeStrNum
(self
._rate
)
52 def _setTaxed(self
, value
) :
54 self
._price
= value
/ (1 + self
._rate
)
58 return self
._localeStrNum
(self
._taxed
)
62 return self
._localeStrNum
(self
._price
)
66 tax
= self
._rate
* self
._price
67 return self
._localeStrNum
(tax
)
71 "returns vat rate in percent"
72 vat
= self
._rate
* 100
73 return self
._localeStrNum
(vat
)
75 def _localeStrNum(self
, n
) :
82 ds
= _(u
'${i}.${d}', mapping
={'i':i
, 'd':d
}, default
=n
)
83 return translate(ds
).encode('utf-8')
86 values
= {'value':self
._price
,
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
)
98 def __div__(self
, other
) :
99 return Price(self
._taxed
/ other
, self
._rate
)
101 def __mul__(self
, other
) :
102 return Price(self
._taxed
* other
, self
._rate
)
105 return '%s with VAT' % self
.taxed