def mobile_add_discount(request, company_id): # edit an existing contact try: c = Company.objects.get(id=company_id) except Company.DoesNotExist: return JsonError(_("Company does not exist")) # check permissions: needs to be at least manager if not has_permission(request.user, c, 'discount', 'edit'): return JsonError(_("add discounts")) data = JsonParse(request.POST['data']) valid = validate_discount(data, request.user, c, android=True) if not valid.get('status'): return JsonError(valid['message']) form = valid['form'] d = Discount( description=form.cleaned_data.get('description'), code=form.cleaned_data.get('code'), type=form.cleaned_data.get('type'), amount=form.cleaned_data.get('amount'), start_date=form.cleaned_data.get('start_date'), end_date=form.cleaned_data.get('end_date'), enabled=form.cleaned_data.get('enabled'), created_by=request.user, company=c ) d.save() return JsonOk(extra=discount_to_dict(request.user, c, d, android=True))
def add_discount(request, company): c = get_object_or_404(Company, url_name=company) # check for permission for adding discounts if not has_permission(request.user, c, 'discount', 'edit'): return no_permission_view(request, c, _("You have no permission to add discounts.")) context = { 'title': _("Add discount"), 'site_title': g.MISC['site_title'], 'company': c, 'date_format_js': get_date_format(request.user, c, 'js'), } if request.method == 'POST': # submit data form = DiscountForm(data=request.POST, user=request.user, company=c) if form.is_valid(): # save the new discount and redirect back to discounts d = Discount( description=form.cleaned_data.get('description'), code=form.cleaned_data.get('code'), type=form.cleaned_data.get('type'), amount=form.cleaned_data.get('amount'), start_date=form.cleaned_data.get('start_date'), end_date=form.cleaned_data.get('end_date'), enabled=form.cleaned_data.get('enabled'), created_by=request.user, company=c ) d.save() return redirect('pos:list_discounts', company=c.url_name) else: form = DiscountForm(user=request.user, company=c) context['form'] = form context['company'] = c context['add'] = True return render(request, 'pos/manage/discount.html', context)