60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
from decimal import Decimal, InvalidOperation
|
|
|
|
from summer.models import session_scope
|
|
from summer.models.validation_exception import ValidationError
|
|
|
|
|
|
__author__ = 'tanshu'
|
|
|
|
from summer.models.master import Tax
|
|
|
|
|
|
def save_tax(json):
|
|
name = json['Name'].strip()
|
|
try:
|
|
rate = Decimal(json['Rate'])
|
|
if rate < 0:
|
|
raise ValidationError("Rate must be a decimal >= 0")
|
|
except (ValueError, InvalidOperation):
|
|
raise ValidationError("Rate must be a decimal >= 0")
|
|
item = Tax(name, rate)
|
|
with session_scope() as DBSession:
|
|
DBSession.add(item)
|
|
return tax_info(item.id)
|
|
|
|
|
|
def update_tax(json):
|
|
with session_scope() as DBSession:
|
|
item = Tax.by_id(json['TaxID'], session=DBSession)
|
|
if item.is_fixture:
|
|
raise ValidationError("{0} is a fixture and cannot be edited or deleted.".format(item.name))
|
|
item.name = json['Name'].strip()
|
|
try:
|
|
rate = Decimal(json['Rate'])
|
|
if rate < 0:
|
|
raise ValidationError("Rate must be a decimal >= 0")
|
|
except (ValueError, InvalidOperation):
|
|
raise ValidationError("Rate must be a decimal >= 0")
|
|
item.rate = rate
|
|
return tax_info(item.id)
|
|
|
|
|
|
def show_list():
|
|
with session_scope() as DBSession:
|
|
list = DBSession.query(Tax).order_by(Tax.name)
|
|
taxes = []
|
|
for item in list:
|
|
taxes.append({'TaxID': item.id, 'Name': item.name, 'Rate': item.rate, 'IsFixture': item.is_fixture})
|
|
return taxes
|
|
|
|
|
|
def tax_info(id):
|
|
with session_scope() as DBSession:
|
|
if id is None:
|
|
item = {'Name': '', 'Rate': 0, 'IsFixture': False}
|
|
else:
|
|
tax = Tax.by_id(id, session=DBSession)
|
|
return {'TaxID': tax.id, 'Name': tax.name, 'Rate': tax.rate, 'IsFixture': tax.is_fixture}
|
|
return item
|
|
|