Пример #1
0
def __create_dwelling_with_cups(results, cups):
    dwelling = Dwelling(cups=cups,
                        address=__get_first_value(
                            results["Datos del cliente"]["Direccion"]),
                        meter_box_number=__get_first_value(
                            results["Datos del contrato"]["NumeroContador"]))
    dwelling.save()
Пример #2
0
def __create_dwelling_with_random_cups(results):
    cups = 'ES' + str(random.randint(10**15, 10**16 - 1))
    cups += ''.join(random.choice(string.ascii_uppercase) for _ in range(4))
    dwelling = Dwelling(cups=cups,
                        address=__get_first_value(
                            results["Datos del cliente"]["Direccion"]),
                        meter_box_number=__get_first_value(
                            results["Datos del contrato"]["NumeroContador"]))
    dwelling.save()
    return cups
Пример #3
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)
Пример #4
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"
Пример #5
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