Example #1
0
 def wrapped(*args, **kwargs):
     try:
         return f(*args, **kwargs)
     except Exception as err:
         traceback.print_exc(file=sys.stdout)
         if isinstance(err, NotImplementedError):
             return jsonify(errors=["Not implemented error"]), 501
         # elif isinstance(err, basestring):
         #     return jsonify(errors=[err]), 501
         elif hasattr(err[0], "errors"):
             return jsonify(errors=err[0].errors), 500
         return jsonify(errors=err[0]), 500
Example #2
0
def update_client():
    data = json.loads(request.data.decode('utf-8'))
    client = Client.query.filter_by(id=data['id']).first()
    check_cl = Client.query.filter_by(vat=data['vat']).first()
    if check_cl:
        #Check if is not the same client
        if check_cl.id != client.id:
            return jsonify({'code_exists': True})
    client.name = data['name']
    client.vat_type = data['vat_type']
    client.vat = data['vat']
    client.credit_limit = data['credit_limit']
    db.session.commit()
    clients = [c.get_dict() for c in Client.query.all()]
    return jsonify({'clients': clients})
Example #3
0
def add_client():
    data = json.loads(request.data.decode('utf-8'))
    client = Client.query.filter_by(vat=data['vat']).first()
    if client:
        return jsonify({'code_exists': True})
    c = Client(
        name=data['name'],
        vat_type=data['vat_type'],
        vat=data['vat'],
        credit_limit=data.get('credit_limit', False) and data['credit_limit']
        or 0,
    )
    db.session.add(c)
    db.session.commit()
    clients = [c.get_dict() for c in Client.query.all()]
    return jsonify({'clients': clients})
Example #4
0
def delete_sale_line(ticket, line_id):
    line = SaleLine.query.filter_by(id=line_id).first()
    prod = Product.query.filter_by(id=line.product_id).first()
    #Actualizo el stock automaticamente
    prod.quantity = prod.quantity + line.quantity
    db.session.delete(line)
    db.session.commit()
    return jsonify(get_lines())
Example #5
0
def delete_product(prod_id):
    # prod = [prod for prod in product_list if prod['id'] == prod_id]
    # if prod:
        # product_list.remove(prod[0])
    prod = Product.query.filter_by(id=prod_id).first()
    db.session.delete(prod)
    db.session.commit()
    return jsonify({'result': True})
Example #6
0
def delete_ticket(ticket):
    ticket_lines = SaleLine.query.filter_by(ticket=ticket,sale_id=0).all()
    if ticket_lines:
        for tl in ticket_lines:
            prod = Product.query.filter_by(id=tl.product_id).first()
            #Actualizo el stock automaticamente
            prod.quantity = prod.quantity + tl.quantity
            db.session.delete(tl)
    db.session.commit()
    return jsonify(get_lines())
Example #7
0
def add_sale_line():
    data = json.loads(request.data.decode('utf-8'))
    try:
        code,sep,name = data['product'].split(' ', 2)
    except:
        response = get_lines()
        response.update({'incorrect_format':True})
        return jsonify(response)
    prod = Product.query.filter_by(code=code).first()
    #Busca si ya hay una linea registrada con el mismo producto (por codigo)
    line = SaleLine.query.filter_by(ticket=data['active_ticket'], code=code, sale_id =0).first()
    if line:
        response = get_lines()
        response.update({'exists':True})
        return jsonify(response)
    #Si lleva iva
    if prod.quantity < data['quantity']:
        response = get_lines()
        response.update({'no_stock':True})
        return jsonify(response)
    price = prod.sell_price
    if prod.iva:
        price += (prod.sell_price) * 12 / 100
        price = round(price,2)
    sl = SaleLine(
            code = prod.code,
            description = prod.description,
            unit_price = price,
            quantity = data['quantity'],
            amount = price * data['quantity'],
            stock = prod.quantity - data['quantity'],
            product_id = prod.id,
            ticket = data['active_ticket'],
            sale_id = 0
            )
    #El stock se decrementa en el momento, pues aunque queda pendiente
    #se toma como vendido
    prod.quantity = prod.quantity - data['quantity']
    db.session.add(sl)
    db.session.commit()
    #Guardo mi lista con las lineas creadas
    return jsonify(get_lines())
Example #8
0
def add_product():
    data = json.loads(request.data.decode('utf-8'))
    prod = Product.query.filter_by(code=data['code']).first()
    if prod:
        return jsonify({'code_exists':True})
    p = Product(
            code = data['code'],
            description = data['description'],
            sell_price = data.get('sell_price',False) and data['purchase_price'] or 0,
            purchase_price = data.get('purchase_price', False) and data['purchase_price'] or 0,
            maj_price = data.get('maj_price',False) and data['maj_price'] or 0,
            has_stock = data.get('has_stock',False),
            iva = data.get('iva',False),
            quantity = data.get('quantity',False) and data['quantity'] or 0,
            min_stock = data.get('min_stock', False) and data['min_stock'] or 0
            )
    db.session.add(p)
    db.session.commit()
    product_list = [p.get_dict() for p in Product.query.all()]
    return jsonify({'products':product_list})
Example #9
0
def update_line_quantity():
    data = json.loads(request.data.decode('utf-8'))
    line = SaleLine.query.filter_by(id=data['id']).first()
    prod = Product.query.filter_by(id=line.product_id).first()
    #Devuelvo todo lo que se ha usado en esa linea de ese producto
    prod.quantity = line.quantity + prod.quantity
    line.quantity = float(data['qty'])
    line.amount = line.unit_price * float(data['qty'])
    #Actualizo la cantidad del producto en stock restandole la nueva cantidad
    prod.quantity = prod.quantity - float(data['qty'])
    #Y actualizo stock visible en la linea
    line.stock = prod.quantity
    db.session.commit()
    return jsonify(get_lines())
Example #10
0
def get_clients():
    clients = [c.get_dict() for c in Client.query.all()]
    return jsonify({'clients': clients})
Example #11
0
def get_client(cid):
    c = Client.query.filter_by(id=cid).first()
    c = c.get_dict() if c else False
    return jsonify({'client': c})
Example #12
0
def get_product(prod_id):
    prod = Product.query.filter_by(id=prod_id).first()
    prod = prod.get_dict() if prod else False
    return jsonify({'product':prod})
Example #13
0
def get_products_description():
    product_list = [p.code+" - "+p.description for p in Product.query.all()]
    return jsonify({'products':product_list})
Example #14
0
def get_products_codes():
    product_list = [p.code for p in Product.query.all()]
    return jsonify({'products':product_list})
Example #15
0
def get_products():
    product_list = [p.get_dict() for p in Product.query.all()]
    return jsonify({'products':product_list})
Example #16
0
def delete_client(cid):
    c = Client.query.filter_by(id=cid).first()
    db.session.delete(c)
    db.session.commit()
    return jsonify({'result': True})
Example #17
0
def get_sale_line(ticket,id):
    c = SaleLine.query.filter_by(ticket=ticket,id=id,sale_id=0).first()
    c = c.get_dict() if c else False
    response = get_lines()
    response.update({'line':c})
    return jsonify(response)
Example #18
0
def get_sale_lines():
    return jsonify(get_lines())
Example #19
0
def to_json(state):
    debug(to_json.__name__, "json:", state.data)
    if len(state.errors) == 0:
        return jsonify(data=state.data)
    else:
        return jsonify(errors=state.errors), 500