예제 #1
0
def create_document():
    json = request.get_json(force=True)
    ## update or create person
    new_person = json['person']
    products = json['products']

    person = Person.new_or_update(new_person)
    person.save()

    ## get new invoice number
    parameter = Parameter.get_first()
    parameter.last_invoice = parameter.last_invoice + 1
    parameter.save()

    total = 0
    discount = 0
    total_tax = 0
    sub_total = 0
    person_id = person.id
    exchange = parameter.exchange
    for product in products:
        base = product.price
        tax = base * product.tax
        subtotal = subtotal + (base - tax)
        total = total + base
    total = sub_total + tax
    document = Document.new(person_id=person_id,
                            number=parameter.last_invoice,
                            date=datetime.now(),
                            document_type='FACTURA',
                            sub_total=sub_total,
                            discount=discount,
                            tax=total_tax,
                            total=total,
                            exchange=exchange)
    if document.save():
        return response(document_schema.dump(document))

    return bad_request()
예제 #2
0
def create_document():
    json = request.get_json(force=True)
    ## update or create person
    new_person = json['provider']
    new_person.person_type = 'supplier'
    error = params_person_schema.validate(new_person)
    if error:
        print(error)
        return bad_request()

    details = json['products']

    person = Person.new_or_update(new_person)
    db.session.add(person)

    ## get new invoice number
    parameter = Parameter.get_first()
    parameter.last_purchase = parameter.last_purchase + 1
    db.session.add(parameter)

    total = 0
    total_tax = 0
    sub_total = 0
    person_id = person.id
    for product in details:
        base = product.get('cost')  # * product.quantity
        tax = base * product.get('tax')
        sub_total = sub_total + (base - tax)
        total = total + base
        total_tax = total_tax + tax

    total = sub_total + total_tax

    document = Document.new(person_id=person_id,
                            number=parameter.last_purchase,
                            date=datetime.today(),
                            document_type='COMPRA',
                            sub_total=sub_total,
                            discount=0,
                            tax=total_tax,
                            total=total,
                            exchange=parameter.exchange)

    for detail in details:
        document_detail = DocumentDetail.new(
            cost=detail.get('cost'),
            price=detail.get('price'),
            quantity=detail.get('quantity'),
            sku=detail.get('sku'),
            tax=detail.get('tax'),
            departament=detail.get('departament'),
            description=detail.get('description'),
            product_id=detail.get('id'),
            document_id=document.id)
        document.details.append(document_detail)
        product = Product.query.filter_by(id=detail.get('id')).first()
        if not product is None:
            product.stock = (product.stock if product.stock else
                             0) + detail.get('quantity', 0)
            db.session.add(product)

    db.session.add(document)

    try:
        db.session.commit()
    except exc.SQLAlchemyError as e:
        db.session.rollback()
        print(e)
        return bad_request()

    return response(document_schema.dump(document))