def edit_tax_(request, c): # permission if not has_permission(request.user, c, 'tax', 'edit'): return JsonError(_("You have no permission to edit taxes")) # the data data = JsonParse(request.POST.get('data')) if not data: return JsonError(_("No data sent")) # validate valid = validate_tax(request.user, c, data) if not valid['success']: return JsonError(valid['message']) data = valid['data'] if data['id'] != -1: # it's an existing tax, fetch it and update # get the tax and save it try: tax = Tax.objects.get(company=c, id=data['id']) except Tax.DoesNotExist: return JsonError(_("Tax does not exist")) tax.amount = data['amount'] tax.name = data['name'] else: # it's a new tax, create a new one tax = Tax( created_by=request.user, company=c, amount=data['amount'], name=data['name'], default=False, # false by default, the tax must be selected to become default ) try: tax.save() except IntegrityError as e: return JsonError(_("Could not save tax; ") + e.message) return JsonOk(extra=tax_to_dict(request.user, c, tax))
def create_company_defaults(user, company): """ creates all initial data for a new company; """ # permissions # add 'admin' permissions for the user that registered this company default_permission = Permission( created_by=user, user=user, company=company, permission='admin', ) default_permission.save() ## assign pin number #default_permission.create_pin() # taxes # create a set of taxes depending on company's country if company.country == 'SI': tax = Tax(created_by=user, company=company, amount=22.0, name='Splošno', default=True) tax.save() tax = Tax(created_by=user, company=company, amount=9.5, name='Znižana', default=False) tax.save() tax = Tax(created_by=user, company=company, amount=0, name='Brez', default=False) tax.save() # categories # create a fixed set of categories: {name, description, color, [children]} default_categories = [ [ _("Foods"), _("Everything edible"), 10, [ # [_("Bread"), _("Stuff from the oven"), 1, []], [_("Vegetables"), _("Oh so healthy"), 6, []], [_("Fruit"), _("Oh so healthy"), 3, []], ] ], [_("Drinks"), _("For the thirsty"), 9, []] ] def add_category(data, parent=None): cat = Category( created_by=user, company=company, parent=parent, name=data[0], description=data[1], color=g.CATEGORY_COLORS[data[2]] ) cat.save() for child in data[3]: add_category(child, cat) for c in default_categories: add_category(c) # cash register # create a default, A4 cash register with data from company register = Register( created_by=user, company=company, name=_("Default"), receipt_format=g.RECEIPT_FORMATS[0][0], receipt_type=g.RECEIPT_TYPES[0][0], location="", print_location=False, printer_driver=g.PRINTER_DRIVERS[0][0]) register.save()