1 # -*- coding: utf-8 -*-
2 ############################################################
3 # Copyright © 2009 Benoît PIN <pinbe@luxia.fr> #
4 # Cliché - http://luxia.fr #
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 ############################################################
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
24 msecurity
= ModuleSecurityInfo('Products.photoprint.price')
25 msecurity
.declarePublic('Price')
27 class Price(object, Persistent
) :
29 Price of an object which have VAT tax.
31 __allow_access_to_unprotected_subobjects__
= 1
33 def __init__(self
, taxedPrice
, rate
=0) :
34 """price is initialized with taxed value"""
35 self
._rate
= float(rate
)
36 self
._setTaxed
(taxedPrice
)
40 return self
._localeStrNum
(self
._rate
)
42 def _setTaxed(self
, value
) :
44 self
._price
= value
/ (1 + self
._rate
)
48 return self
._localeStrNum
(self
._taxed
)
52 return self
._localeStrNum
(self
._price
)
56 tax
= self
._rate
* self
._price
57 return self
._localeStrNum
(tax
)
61 "returns vat rate in percent"
62 vat
= self
._rate
* 100
63 return self
._localeStrNum
(vat
)
65 def _localeStrNum(self
, n
) :
72 ds
= _(u
'${i}.${d}', mapping
={'i':i
, 'd':d
}, default
=n
)
73 return translate(ds
, getRequest()).encode('utf-8')
76 values
= {'value':self
._price
,
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
)
88 def __div__(self
, other
) :
89 return Price(self
._taxed
/ other
, self
._rate
)
91 def __mul__(self
, other
) :
92 return Price(self
._taxed
* other
, self
._rate
)
95 return '%s with VAT' % self
.taxed