Example #1
0
def sale(sale_id):
    sale = Sale.query.get(sale_id)
    
    sale_status_histories = SaleStatusHistory.query.filter_by(sale_id=sale_id).order_by(SaleStatusHistory.created.desc()).all()
    form = SaleForm(**sale.serialize())

    form.agent.query = Agent.query.order_by(Agent.first_name, Agent.last_name)

    if form.validate_on_submit():
        sale_status = form.sale_status.data
        if sale_status != sale.sale_status:
            ssh = SaleStatusHistory(sale=sale, status=sale_status)
            db_session.add(ssh)

        form.populate_obj(sale)
        if sale.annual_consumption == '':
            sale.annual_consumption = None
        if sale.commission_value == '':
            sale.commission_value = None
        if sale.clawback_value == '':
            sale.clawback_value = None

        db_session.commit()

        return redirect(url_for('sale', sale_id=sale_id))
        

    context = {'sale': sale,
               'form': form,
               'sale_status_histories': sale_status_histories}

    return render_template('sale.html', **context)
Example #2
0
def sale_add(request):
  if request.method == 'POST':
    form = SaleForm(request.POST)
    p_sn = generate_sn(prefix = 'SO')
    if form.is_valid():
      p = form.save(commit=False)
      p.sn = p_sn
      p.save()
      serializer = JSONSimpleSerializer()
      return HttpResponse(serializer.serialize([p,], use_natural_foreign_keys=True))
    else:
      print form.errors
  else:
    form = SaleForm()
    return render_to_response('modal/main_form.html',{'action':'Add', 'module':module, 'form': form},context_instance=RequestContext(request))
Example #3
0
def sale_update(request):
  if request.method == 'POST':
    postdata = request.POST.copy()
    try:
      p = SaleOrder.objects.get(sn=postdata.pop('sn')[0])
    except SaleOrder.DoesNotExist:
      raise Http404(module + ' not found')
    form = SaleForm(postdata, instance=p)
    print form.errors
    if form.is_valid():
      p_saved = form.save()
      # updateSalePrice(p_saved.sn)
      p.updatePrice()
      serializer = JSONSimpleSerializer()
      return HttpResponse(serializer.serialize([p,],use_natural_foreign_keys=True))
  else:
    return HttpResponse('Invalid request.')