def customer_get(id): if id: customer = Customer.query.filter_by(site_id=g.sid, branch_id=g.bid,is_active='Y', id=id).first() else: no = db.session.query(db.func.max(Customer.no)).filter_by( site_id=g.sid, branch_id=g.bid).scalar() or 0 no = no + 1 customer = Customer(site_id=g.sid, branch_id=g.bid) customer.no = no customer.code = 'C' + str(no).zfill(4) schema = CustomerSchema() return jsonify({ 'customer': schema.dump(customer).data })
def customer_add(): result = None f = request.get_json() if f['customer']: f = f['customer'] forms = { 'site_id': g.sid, 'branch_id': g.bid, 'firstname': f.get('firstname') or '', 'lastname': f.get('lastname') or '', 'gender': f.get('gender') or None, 'email': f.get('email') or None, 'phone': f.get('phone') or '', 'img': f.get('img') or None, 'img_path': f.get('img_path') or None } email = f.get('email') or '' cust = Customer.query.filter_by(site_id=g.sid, branch_id=g.bid, email=email).first() if cust: # customer already existed result = cust else: # save new customer customer = Customer(site_id=g.sid, branch_id=g.bid) for k, v in forms.iteritems(): setattr(customer, k, v) if customer.code is None: no = db.session.query(db.func.max(Customer.no)).filter_by( site_id=g.sid, branch_id=g.bid).scalar() or 0 no = no + 1 customer.no = no customer.code = 'C' + str(no).zfill(4) db.session.add(customer) db.session.commit() result = customer # serializer for customer data = {} if result: schema = CustomerSchema() data = schema.dump(result).data return jsonify({ 'result': data })