예제 #1
0
def deactivate_invoice(invoice_id):
    try:
        conn, cur = connect_db()

        actual_date = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")


        if request.get_json() and 'force_delete' in request.get_json() and request.get_json()['force_delete']:
            delete_sql = ("DELETE FROM invoices " +
                "WHERE id = '" + invoice_id + "';")
            
            deleted_info = "Invoice Removed Succesfully"

        
        else:
            delete_sql = ("UPDATE invoices " +
                    " SET isactive = False, deactiveAt = '" + actual_date +
                    "' WHERE id = '" + invoice_id + "';")
        
            deleted_info = "Invoice Deactivated Succesfully"

        print(delete_sql)
        cur.execute(delete_sql)
        conn.commit()

    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
        return jsonify({'error': 'Invoice to remove was not found'}), 503
    finally:
        close_db(conn, cur)
    
    return jsonify({"message": deleted_info}), 200
예제 #2
0
def update_invoice(invoice_id):
    
    if request.get_json():

        try:
            conn, cur = connect_db()
                
            set_string = ""
            for k in request.get_json().keys():
                set_string += str(k) + " = '" + str(request.get_json()[k]) + "', "

            set_string = set_string[:-2]
            
            
            update_sql = ("UPDATE invoices " +
                    " SET " + set_string +
                    " WHERE id = '" + invoice_id + "';")

            cur.execute(update_sql)
            conn.commit()

        except (Exception, psycopg2.DatabaseError) as error:
            print(error)
            return jsonify({'error': 'Invoice to update was not found'}), 503

        finally:
            close_db(conn, cur)

        
        return jsonify({"message":"Updated Succesfully"}), 200
    else:
        return jsonify({'error': 'No requested body found'}), 400
예제 #3
0
def get_invoices_by_id(invoice_id):
    try:
        conn, cur = connect_db()

        cur.execute("SELECT id, document, description, amount, referenceMonth, referenceYear, createdAt, isActive, deactiveAt "
        + "FROM invoices WHERE id = '" + invoice_id + "';")
        row = cur.fetchone()

        if row:
            invoice = Invoice.generate_from_row(row)  
        else:
            return jsonify({'error': 'Invoice not found'}), 503
          
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
        return jsonify({"error":"Could not connect to Database"}), 503
    finally:
        close_db(conn, cur)

        
    return jsonify(invoice.__dict__), 200
예제 #4
0
def create_invoice():

    if request.get_json():
        
        try:
            conn, cur = connect_db()

            if 'id' in request.get_json():
                invoice = Invoice(request.get_json(), request.get_json()['id'])
            else:
                invoice = Invoice(request.get_json())


            insert_string = ("INSERT INTO invoices VALUES('" 
                    + str(invoice.id) + "', '"
                    + invoice.document + "', '"
                    + invoice.description + "', "
                    + str(invoice.amount) + ", '"
                    + str(invoice.referenceMonth) + "', '"
                    + str(invoice.referenceYear) + "', '"
                    + str(invoice.createdAt) + "', "
                    + str(invoice.isActive) + ");")

            cur.execute(insert_string)

            conn.commit()

        except (Exception, psycopg2.DatabaseError) as error:
            print(error)
            return jsonify({'error': 'Some error inserting data'}), 503
        finally:
            close_db(conn, cur)


        return jsonify(invoice.__dict__), 201

    else:
        return jsonify({'error': 'No requested body found'}), 400
예제 #5
0
def list_invoices():

    try:
        
        # Obtendo o valor da query para ordenacao
        order_query = request.args.get('order')
        
        order_string = order_invoices(order_query)

        
    except:
        print("Error generating order query")
        return jsonify({"error":"Something went wrong. Please, Contact the admin"}), 503

    try:
       
        # Obtendo valores de filtro
        filter_string = filter_invoices(request.args)


    except:
        print("Error generating filter query")
        return jsonify({"error":"Something went wrong. Please, Contact the admin"}), 400
    try:
        (conn, cur) = connect_db()

        psql_query_string = """
            SELECT id, document, description, amount, referenceMonth, referenceYear, createdAt, isActive, deactiveAt
            FROM invoices
        """

        psql_query_string += filter_string + order_string

        cur.execute(psql_query_string)
        rows = cur.fetchall()

        print("The number of parts: ", cur.rowcount)
        
        list_invoices = []
        for row in rows:
            invoice = Invoice.generate_from_row(row)
            list_invoices.append(invoice.__dict__)
        
        

    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
        return jsonify({"error":"Could not connect to Database"}), 503

    finally:
        close_db(conn, cur)

    try:
        # Tratando o valor das paginas
        page = int(request.args.get('page')) if request.args.get('page') else 1
        page = page if page > 1 else 1
    
        limit = 5 # Itens por pagina
        
        links_json = generate_links_json(page, limit, list_invoices)    


        invoices = separate_result_per_page(list_invoices, limit, page)

        result = create_list_result(links_json, limit, cur.rowcount, invoices)

        return jsonify(result), 200
    except:
        print("Error while separeting itens per page")
        return jsonify({"error":"Something went wrong. Please, Contact the admin"}), 400