def bill_checkout(): result = 0 f = request.get_json() if f is None: f = request.form frm = f['bill'] frm_items = frm['bill_items'] result = bill_prepare_save(frm, frm_items) checkforms = f['checkout'] if result and checkforms: # delete old receipts Receipt.query.filter_by(bill_id=result).delete() # create new receipts receipt = Receipt(site_id=g.sid, branch_id=g.bid, bill_id=result) receipt.line_no = 1 receipt.receipt_no = 'R' + frm['bill_no'] receipt.method = 'cash' receipt.total_amount = checkforms.get('total_amount') or 0 receipt.total_charge = checkforms.get('total_charge') or 0 receipt.total_return = checkforms.get('total_return') or 0 db.session.add(receipt) db.session.commit() # update bill status status = Status.query.filter_by(name='paid', module='bill').first() bill = Bill.query.get(result) if bill: bill.status_id = status.id db.session.merge(bill) db.session.commit() return jsonify({ 'result': result })
def bill_form_save(): f = request.get_json() if f is None: f = request.form forms = { 'branch_id': g.bid, 'site_id': g.sid, 'user_id': g.uid, 'bill_no': f.get('bill_no') or '', 'currency': f.get('currency'), 'cust_id': f.get('cust_id') or None, 'no': f.get('no'), 'receipt_no': f.get('receipt_no') or None, 'remark': f.get('remark') or '', 'status_id': f.get('status_id') or None, 'table_id': f.get('table_id') or None, 'tax_rate': f.get('tax_rate') or 0, 'discount_rate': get_num(f.get('discount_rate')), 'total_amount': get_num(f.get('total_amount')), 'total_discount': get_num(f.get('total_discount')), 'total_price': get_num(f.get('total_price')), 'total_tax': get_num(f.get('total_tax')), 'total_qty': get_num(f.get('total_qty')), } # update bill bill = None id = f.get('id') or None if id: bill = Bill.query.get(id) for k, v in forms.iteritems(): setattr(bill, k, v) db.session.merge(bill) else: bill = Bill() for k, v in forms.iteritems(): setattr(bill, k, v) db.session.add(bill) db.session.commit() # update bill items bill_items = json.loads(f.get('bill_items')) if bill.id: result = bill.id BillItem.query.filter_by(bill_id=bill.id).delete() for elem in bill_items: bill_item = BillItem() for k, v in elem.iteritems(): try: if k == 'id': continue setattr(bill_item, k, v) except: pass bill_item.bill_id = bill.id db.session.add(bill_item) db.session.commit() # update receipt info rcv = f.get('receipt_no') or None if bill.id and rcv: Receipt.query.filter_by(site_id=g.sid, branch_id=g.bid, bill_id=result).delete() receipt = Receipt(site_id=g.sid, branch_id=g.bid, bill_id=result) receipt.line_no = 1 receipt.receipt_no = f.get('receipt_no') or '' receipt.method = f.get('method') or '' receipt.total_amount = get_num(f.get('total_amount')) receipt.total_charge = get_num(f.get('total_rcv_charge')) receipt.total_return = get_num(f.get('total_rcv_return')) db.session.add(receipt) db.session.commit() return jsonify({'result': bill.id})