def bill_get(id): receipt = None if id: bill = Bill.query.filter_by(site_id=g.sid, branch_id=g.bid, id=id).first() receipt = Receipt.query.filter_by(bill_id=bill.id).first() else: no = db.session.query(db.func.max(Bill.no)).filter_by(site_id=g.sid, branch_id=g.bid).scalar() or 0 ref_no = no + 1 bill_no = strftime('%Y%m') + '-' + str(ref_no).zfill(g.bigit) bill = Bill(site_id=g.sid, branch_id=g.bid) bill.no = ref_no bill.bill_no = bill_no receipt_data = {} if receipt: receiptSchema = ReceiptSchema() receipt_data = receiptSchema.dump(receipt).data schema = BillSchema() return jsonify({ 'bill': schema.dump(bill).data, 'receipt': receipt_data })
def bill_search(): f = request.get_json() if f is None: f = request.form page = f.get('page') or 1 rp = f.get('rp') or 10 term = f.get('term') or '' sort = f.get('sort') or None desc = f.get('desc') or False from_date = f.get('from_date') or None to_date = f.get('to_date') or None status = f.get('status') or None query = Bill.query.filter_by(site_id=g.sid, branch_id=g.bid) # filter by bill no if term: query = query.filter(or_(Bill.bill_no.ilike('%' + term + '%'))) # filter by from and to date if from_date: query = query.filter(Bill.bill_date >= datetime.strptime(from_date, '%d/%m/%Y')) if to_date: query = query.filter(Bill.bill_date <= datetime.strptime(to_date + ' 23:59:59', '%d/%m/%Y %H:%M:%S')) # filter by status id if status: query = query.filter_by(status_id=int(status)) # sort and order result items if sort: query = query.order_by(text(sort + ' ' + ('desc' if desc else 'asc'))) total = query.count() total_pages = int(ceil(float(total) / rp)) results = page * rp page_count = results if results <= total else total bills = query.limit(rp).offset((page - 1) * rp) schema = BillSchema(many=True) return jsonify({ 'total': total, 'total_pages': total_pages, 'page_count': page_count, 'bills': schema.dump(bills).data })