示例#1
0
def delete_account():
    logged_user = User.get_by_username(get_jwt_identity())
    logged_customer = Customer.get_by_user_id(logged_user.id)
    customers_dwellings_contracts = Customer_Dwelling_Contract.get_by_nif(
        logged_customer.nif)
    for customer_dwelling_contract in customers_dwellings_contracts:
        contract_number = customer_dwelling_contract.contract_number
        Contract.get_by_contract_number(contract_number).delete()
    logged_user.delete()
    return "", 200
示例#2
0
def get_invoices_data():
    logged_user = User.get_by_username(get_jwt_identity())
    logged_customer = Customer.get_by_user_id(logged_user.id)
    customers_dwellings_contracts = Customer_Dwelling_Contract.get_by_nif(
        logged_customer.nif)
    data = []
    for customer_dwelling_contract in customers_dwellings_contracts:
        contract_number = customer_dwelling_contract.contract_number
        contract = Contract.get_by_contract_number(contract_number).to_dict()
        dwelling = Dwelling.get_by_cups(customer_dwelling_contract.cups)
        contract["address"] = dwelling.address
        invoices = __get_invoices(contract_number)
        contract = {"contract_data": contract, "invoices": invoices}
        data.append(contract)
    return jsonify(data)
示例#3
0
def delete_invoice(invoice_number):
    invoice = Invoice.get_by_invoice_number(invoice_number)
    invoice.delete()
    contract = Contract.get_by_contract_number(invoice.contract_number)
    invoices = __get_invoices(contract.contract_number)
    if len(invoices) == 0:
        logged_user = User.get_by_username(get_jwt_identity())
        logged_customer = Customer.get_by_user_id(logged_user.id)
        nif = logged_customer.nif
        cus_dwe_con = Customer_Dwelling_Contract \
         .get_by_nif_and_contract_number(
          nif,
          invoice.contract_number
         )
        cus_dwe_con.delete()
        contract.delete()
    return "", 200
示例#4
0
def my_bills():
    customer = None
    contracts = None
    contract_invoices = None
    if current_user.user_type == 1:
        customer = Customer.get_by_user_id(current_user.id)
        customers_dwellings_contracts = Customer_Dwelling_Contract.get_by_nif(
            customer.nif)
        contracts = []
        for customer_dwelling_contract in customers_dwellings_contracts:
            contracts.append(
                Contract.get_by_contract_number(
                    customer_dwelling_contract.contract_number))
        contract_invoices = {}
        for contract in contracts:
            contract_invoices[contract] = Invoice.get_by_contract_number(
                contract.contract_number)
    return render_template("bills/my_bills.html",
                           contracts=contracts,
                           contract_invoices=contract_invoices)
示例#5
0
def get_stats():
    customer = None
    contracts = None
    contract_invoices = None
    if current_user.user_type == 1:
        customer = Customer.get_by_user_id(current_user.id)
        customers_dwellings_contracts = Customer_Dwelling_Contract.get_by_nif(
            customer.nif)
        contracts = []
        for customer_dwelling_contract in customers_dwellings_contracts:
            contracts.append(
                Contract.get_by_contract_number(
                    customer_dwelling_contract.contract_number))
        contract_invoices = {}
        for contract in contracts:
            year = int(contract.init_date.strftime("%Y"))
            year_invoices = Invoice.get_by_contract_number(
                contract.contract_number)
            total_amounts = []
            for invoice in year_invoices:
                total_amounts.append(invoice.total_amount)
            contract_invoices[year] = total_amounts
    return contract_invoices
示例#6
0
def process_bill():
    if request.method == "POST":

        file = request.files["inputFile"]

        if not file.filename:
            flash("No se ha seleccionado ningún archivo")
            return redirect(url_for("customer.upload_bill"))

        if file and __allowed_file(file.filename):

            # save file to upload directory with a hash code
            file_extension = file.filename.rsplit(".", 1)[1].lower()
            filename = str(uuid.uuid4()) + "." + file_extension

            bill_path = os.path.join(app.config["UPLOAD_FOLDER"], filename)
            file.save(bill_path)

            # information extraction from the bill
            results = docreco.process_bill(bill_path, file_extension)

            # return results

            # Delete the bill uploaded
            os.remove(bill_path)

            contract_number = __get_first_value(results["Datos del contrato"]["ReferenciaContrato"]) \
                 .split('/')[0] \
                 .split('-')[0] \
                 .split(' ')[0]

            if contract_number:
                contract = Contract.get_by_contract_number(contract_number)
                if not contract:
                    company_name = __get_first_value(
                        results["Datos de la factura"]["Comercializadora"])
                    if company_name:
                        trading_company = Company.get_trading_company_by_name(
                            company_name, unidecode.unidecode(company_name))
                        if trading_company:
                            cif = trading_company.cif
                        else:
                            flash(
                                "No se encuentra la comercializadora ni existe cif en la factura"
                            )
                            return redirect(url_for("customer.my_bills"))
                    else:
                        flash(
                            "No se encuentra el nombre de la comercializadora en la factura"
                        )
                        return redirect(url_for("customer.my_bills"))

                    contract_data = __get_contract_data(results)
                    contract = Contract(
                        contract_number=contract_number,
                        contracted_power=contract_data["contracted_power"],
                        toll_access=contract_data["toll_access"],
                        end_date=contract_data["end_date"],
                        CNAE=contract_data["CNAE"],
                        tariff_access=contract_data["tariff_access"],
                        cif=cif)
                    contract.save()
            else:
                flash("No se encuentra el número de referencia del contrato")
                return redirect(url_for("customer.my_bills"))

            invoice_data = __get_invoice_data(results, contract_number)

            invoice = Invoice(
                invoice_number=invoice_data["invoice_number"],
                contracted_power_amount=invoice_data[
                    "contracted_power_amount"],
                consumed_energy_amount=invoice_data["consumed_energy_amount"],
                issue_date=invoice_data["issue_date"],
                charge_date=invoice_data["charge_date"],
                init_date=invoice_data["init_date"],
                end_date=invoice_data["end_date"],
                total_amount=invoice_data["total_amount"],
                contract_reference=invoice_data["contract_reference"],
                contract_number=invoice_data["contract_number"],
                document=file.read())

            try:
                invoice.save()
            except IntegrityError:
                flash("Esta factura ya está registrada")
                return redirect(url_for("customer.my_bills"))

            cups = __get_first_value(results["Datos del contrato"]["CUPS"])

            if cups and not Dwelling.get_by_cups(cups):
                __create_dwelling_with_cups(results, cups)
            else:
                cups = __create_dwelling_with_random_cups(results)

            customer_dwelling_contract = Customer_Dwelling_Contract(
                nif=Customer.get_by_user_id(current_user.id).nif,
                cups=cups,
                contract_number=contract_number)
            customer_dwelling_contract.save()

            flash("La factura se ha guardado con éxito")
            return redirect(
                url_for("customer.show_bill",
                        invoice_number=invoice.invoice_number))

        else:
            flash(
                "Los tipos de fichero permitidos son txt, pdf, png, jpg, jpeg, gif"
            )
            return redirect(url_for("customer.upload_bill"))

    return "Error POST"
示例#7
0
def process_bill():
    try:
        file = request.files["file"]
    except BadRequestKeyError:
        return {
            "message": "No se ha seleccionado ningún archivo",
            "type": "error"
        }, 200

    if not __allowed_file(file.filename):
        return {
            "message":
            "Los tipos de fichero permitidos son txt, pdf, png, jpg, jpeg, gif",
            "type": "error"
        }, 200

    # save file to upload directory with a hash code
    file_extension = file.filename.rsplit(".", 1)[1].lower()
    filename = str(uuid.uuid4()) + "." + file_extension

    bill_path = os.path.join(app.config["UPLOAD_FOLDER"], filename)
    file.save(bill_path)

    # information extraction from the bill
    results = docreco.process_bill(bill_path, file_extension)

    # Delete the bill uploaded
    os.remove(bill_path)

    contract_number = __get_first_value(
        results["Datos del contrato"]["ReferenciaContrato"]).split(
            '/')[0].split('-')[0].split(' ')[0]

    if contract_number:
        contract = Contract.get_by_contract_number(contract_number)
        if not contract:
            cif = __get_first_value(results["Datos de la factura"]["CIF"])
            if cif:
                trading_company = Company.get_by_cif(cif)
                if not trading_company:
                    return {
                        "message": "No se encuentra la comercializadora",
                        "type": "error"
                    }, 200
            else:
                company_name = __get_first_value(
                    results["Datos de la factura"]["Comercializadora"])
                if company_name:
                    trading_company = Company.get_trading_company_by_name(
                        company_name, unidecode.unidecode(company_name))
                    if trading_company:
                        cif = trading_company.cif
                    else:
                        return {
                            "message":
                            "No se encuentra la comercializadora ni el cif en la factura",
                            "type": "error"
                        }, 200
                else:
                    return {
                        "message":
                        "No se encuentra el nombre de la comercializadora en la factura",
                        "type": "error"
                    }, 200
            contract_data = __get_contract_data(results)
            contract = Contract(
                contract_number=contract_number,
                contracted_power=contract_data["contracted_power"],
                toll_access=contract_data["toll_access"],
                end_date=contract_data["end_date"],
                CNAE=contract_data["CNAE"],
                tariff_access=contract_data["tariff_access"],
                cif=cif)
            contract.save()
    else:
        return {
            "message": "No se encuentra el número de referencia del contrato",
            "type": "error"
        }, 200
    invoice_data = __get_invoice_data(results, contract_number)

    invoice = Invoice(
        invoice_number=invoice_data["invoice_number"],
        contracted_power_amount=invoice_data["contracted_power_amount"],
        consumed_energy_amount=invoice_data["consumed_energy_amount"],
        issue_date=invoice_data["issue_date"],
        charge_date=invoice_data["charge_date"],
        init_date=invoice_data["init_date"],
        end_date=invoice_data["end_date"],
        total_amount=invoice_data["total_amount"],
        contract_reference=invoice_data["contract_reference"],
        contract_number=invoice_data["contract_number"],
        document=file.read())

    try:
        invoice.save()
    except IntegrityError:
        return {
            "message": "Esta factura ya está registrada",
            "type": "error"
        }, 200

    cups = __get_first_value(results["Datos del contrato"]["CUPS"])

    if cups:
        if not Dwelling.get_by_cups(cups):
            __create_dwelling_with_cups(results, cups)
    else:
        cups = __create_dwelling_with_random_cups(results)

    logged_user = User.get_by_username(get_jwt_identity())
    logged_customer = Customer.get_by_user_id(logged_user.id)
    nif = logged_customer.nif

    if not Customer_Dwelling_Contract.get_by_nif_and_contract_number(
            nif, contract_number):
        customer_dwelling_contract = Customer_Dwelling_Contract(
            nif=nif, cups=cups, contract_number=contract_number)
        try:
            customer_dwelling_contract.save()
        except IntegrityError:
            pass
    return {
        "message": "La factura se ha guardado con éxito",
        "type": "success"
    }, 200
示例#8
0
def get_consumption_data():
    contract_invoices = {}
    logged_user = User.get_by_username(get_jwt_identity())
    if logged_user.user_type == 1:
        logged_customer = Customer.get_by_user_id(logged_user.id)
        customers_dwellings_contracts = Customer_Dwelling_Contract.get_by_nif(
            logged_customer.nif)
        contracts = []
        for customer_dwelling_contract in customers_dwellings_contracts:
            contracts.append(
                Contract.get_by_contract_number(
                    customer_dwelling_contract.contract_number))
        for contract in contracts:
            invoices = Invoice.get_by_contract_number(contract.contract_number)
            for invoice in invoices:
                year = int(invoice.init_date.strftime("%Y"))
                total_amount_list = [0 for _ in range(12)]
                consumed_energy_list = [0 for _ in range(12)]
                contracted_power_amount_list = [0 for _ in range(12)]
                consumed_energy_amount_list = [0 for _ in range(12)]
                tax_amount_list = [0 for _ in range(12)]
                if year in contract_invoices:
                    total_amount_list = contract_invoices[year][
                        "total_amount_list"]
                    consumed_energy_list = contract_invoices[year][
                        "consumed_energy_list"]
                    contracted_power_amount_list = contract_invoices[year][
                        "contracted_power_amount_list"]
                    consumed_energy_amount_list = contract_invoices[year][
                        "consumed_energy_amount_list"]
                    tax_amount_list = contract_invoices[year][
                        "tax_amount_list"]
                month = int(invoice.init_date.strftime("%m")) - 1

                if invoice.total_amount:
                    total_amount_list[month] = round(invoice.total_amount, 2)
                else:
                    total_amount_list[month] = 0

                if invoice.consumed_energy:
                    consumed_energy_list[month] = invoice.consumed_energy
                else:
                    consumed_energy_list[month] = 0

                if invoice.contracted_power_amount:
                    contracted_power_amount_list[month] = round(
                        invoice.contracted_power_amount, 2)
                else:
                    contracted_power_amount_list[month] = 0

                if invoice.consumed_energy_amount:
                    consumed_energy_amount_list[month] = round(
                        invoice.consumed_energy_amount, 2)
                else:
                    consumed_energy_amount_list[month] = 0

                if invoice.tax_amount:
                    tax_amount_list[month] = round(invoice.tax_amount, 2)
                else:
                    tax_amount_list[month] = 0
                contract_invoices[year] = {
                    "total_amount_list": total_amount_list,
                    "consumed_energy_list": consumed_energy_list,
                    "contracted_power_amount_list":
                    contracted_power_amount_list,
                    "consumed_energy_amount_list": consumed_energy_amount_list,
                    "tax_amount_list": tax_amount_list
                }
    else:
        return "No tienes permiso", 403
    return contract_invoices