Beispiel #1
0
    def save(self):
        """ Write payment to disk """
        if "guid" not in self._payment:
            self._payment["guid"] = identifier.get_guid()
            self._payment["creation_date"] = datetime.datetime.now().isoformat(
            )
        elif self._payment["guid"] == "":
            self._payment["guid"] = identifier.get_guid()
            self._payment["creation_date"] = datetime.datetime.now().isoformat(
            )

        current_payments = get_payments()
        new_payments = {"payments": []}

        updated = False
        for pay in current_payments["payments"]:
            if pay["guid"] == self._payment["guid"]:
                new_payments["payments"].append(self._payment)
                updated = True
            else:
                new_payments["payments"].append(pay)

        if not updated:
            new_payments["payments"].append(self._payment)

        write_payments_to_disk(new_payments)
Beispiel #2
0
def create_credit_card_transaction(bank: str,
                                   description: str,
                                   card: str,
                                   amount: float,
                                   currency: str = None,
                                   pay_date: str = None):
    """ Creates a new credit card payment with the given details """
    if currency is None:
        writeable_currency = config.CONSTANTS["HOME_CURRENCY"]
    else:
        writeable_currency = currency

    now_date = datetime.datetime.now().isoformat()

    if pay_date is None:
        writeable_date = now_date
    else:
        writeable_date = pay_date

    trn_pay_json = {
        "guid": identifier.get_guid(),
        "creation_date": now_date,
        "company": bank,
        "description": f"{description} - transfer to {card}",
        "invoice_guid": "",
        "direction": DIRECTION_TRANSFER,
        "amount": amount,
        "currency": writeable_currency,
        "cleared": False
    }

    trn_scheme_json = {
        "frequency":
        1,
        "period":
        PERIOD_DAILY,
        "start":
        writeable_date,
        "repeat":
        1,
        "recurrence": [{
            "recurrence_date": writeable_date,
            "expected_payment_date": writeable_date,
            "amount": trn_pay_json["amount"],
            "currency": trn_pay_json["currency"],
            "cleared": False,
            "collections": []
        }]
    }

    trn_scheme = Scheme(trn_scheme_json)
    trn_pay = Payment(trn_pay_json)
    trn_pay.scheme = trn_scheme
    trn_pay.save()
Beispiel #3
0
def get_invoice_obj_from_activities(activities: List) -> Invoice:
    """ Creates a new invoice from the given activities """
    invoice_date = Invoice.get_invoice_date_suggestion()
    due_date = Invoice.get_due_date_suggestion(invoice_date)

    inv_json = {
        "guid": identifier.get_guid(),
        "serial": "",
        "payer": "",
        "invoice_date": invoice_date.isoformat(),
        "due_date": due_date.isoformat(),
        "amount": 0,
        "currency": "",
        "vat_rate": 0,
        "income_tax_rate": 0
    }

    vat_rate_set = False
    payer_set = False
    currency_set = False

    for act in activities:
        proj = act.project
        payer_name = proj.payer.name
        earned_amount, earned_curr = proj.get_earned_amount(act.hours)
        vat_rate = proj.vat_rate

        if not payer_set:
            inv_json["payer"] = payer_name
            payer_set = True
        elif inv_json["payer"] != payer_name:
            raise MultiValueError(MultiValueError.ErrorCode.multiple_payers)

        if not currency_set:
            inv_json["currency"] = earned_curr
            currency_set = True
        elif inv_json["currency"] != earned_curr:
            raise MultiValueError(MultiValueError.ErrorCode.multiple_currencies)

        if not vat_rate_set:
            inv_json["vat_rate"] = vat_rate
            vat_rate_set = True
        elif inv_json["vat_rate"] != vat_rate:
            raise MultiValueError(MultiValueError.ErrorCode.multiple_vat_rates)

        inv_json["amount"] += earned_amount

    inc_tax_calc = IncomeTaxCalculatorFactory.get_instance()
    inv_json["income_tax_rate"] = inc_tax_calc.calc_invoice_tax_rate(
        invoice_date.year,
        inv_json["amount"])

    return Invoice(inv_json)
Beispiel #4
0
    def save(self):
        """ Writes invoice data to disk """
        if "guid" not in self._invoice:
            self._invoice["guid"] = identifier.get_guid()
        elif self._invoice["guid"] == "":
            self._invoice["guid"] = identifier.get_guid()

        current_invoices = get_invoices()
        new_invoices = {"invoices": []}

        updated = False
        for inv in current_invoices["invoices"]:
            if inv["guid"] == self._invoice["guid"]:
                new_invoices["invoices"].append(self._invoice)
                updated = True
            else:
                new_invoices["invoices"].append(inv)

        if not updated:
            new_invoices["invoices"].append(self._invoice)

        Invoice._write_invoices_to_disk(new_invoices)
Beispiel #5
0
    def save(self):
        """ Write activity to disk """
        if "guid" not in self._activity:
            self._activity["guid"] = identifier.get_guid()
        elif self._activity["guid"] == "":
            self._activity["guid"] = identifier.get_guid()

        current_activities = Activity.get_activities()
        new_activities = {"activities": []}

        updated = False
        for act in current_activities["activities"]:
            if act["guid"] == self._activity["guid"]:
                new_activities["activities"].append(self._activity)
                updated = True
            else:
                new_activities["activities"].append(act)

        if not updated:
            new_activities["activities"].append(self._activity)

        Activity._write_activities_to_disk(new_activities)
Beispiel #6
0
def set_asset(asset: dict):
    """ Sets a single asset to disk """
    all_assets = get_assets()
    asset_updated = False

    for old_asset in all_assets["assets"]:
        if old_asset["guid"] != asset["guid"]:
            continue
        for asset_field in asset:
            old_asset[asset_field] = asset[asset_field]
        asset_updated = True
        break

    if not asset_updated:
        if asset["guid"] == "":
            asset["guid"] = identifier.get_guid()
        all_assets["assets"].append(asset)

    set_assets(all_assets)
Beispiel #7
0
def get_payment_objects_from_invoice(invoice: Invoice) -> list:
    """ Extracts new payment objects out of an invoice """
    # Preparation
    output = []

    currency_converter = CurrencyConverter()
    inc_tax_calc = IncomeTaxCalculatorFactory.get_instance()

    invoice_currency = invoice.currency
    invoice_date = invoice.invoice_date
    invoice_payer = invoice.payer
    invoice_serial = invoice.serial
    payment_amount = invoice.amount_plus_vat

    description_prefix = invoice_payer.name + " - " + date_time.get_formatted_date(
        invoice_date) + "(" + invoice_serial + ")"

    # Incoming payment

    incoming_payment_json = {
        "guid": identifier.get_guid(),
        "creation_date": datetime.datetime.now().isoformat(),
        "company": invoice_payer.name,
        "description": description_prefix + " - Incoming payment",
        "invoice_guid": invoice.guid,
        "direction": DIRECTION_IN,
        "amount": payment_amount,
        "currency": invoice_currency,
        "cleared": False
    }

    incoming_scheme_json = {
        "frequency":
        1,
        "period":
        PERIOD_DAILY,
        "start":
        invoice.due_date.isoformat(),
        "repeat":
        1,
        "recurrence": [{
            "recurrence_date": invoice.due_date.isoformat(),
            "expected_payment_date": invoice.due_date.isoformat(),
            "amount": payment_amount,
            "currency": invoice_currency,
            "cleared": False,
            "collections": []
        }]
    }

    incoming_scheme_obj = Scheme(incoming_scheme_json)

    incoming_payment_obj = Payment(incoming_payment_json)
    incoming_payment_obj.scheme = incoming_scheme_obj

    output.append(incoming_payment_obj)

    # VAT transfer

    if invoice.is_vat_liable:
        vat_amount = invoice.vat_amount_in_local_currency

        vat_transfer_json = {
            "guid": identifier.get_guid(),
            "creation_date": datetime.datetime.now().isoformat(),
            "company": config.CONSTANTS["DEFAULT_BANK"],
            "description": description_prefix + " - VAT transfer",
            "invoice_guid": "",
            "direction": DIRECTION_TRANSFER,
            "amount": vat_amount,
            "currency": config.CONSTANTS["HOME_CURRENCY"],
            "cleared": False
        }

        vat_transfer_date = invoice.vat_transfer_date

        vat_transfer_scheme_json = {
            "frequency":
            1,
            "period":
            PERIOD_DAILY,
            "start":
            vat_transfer_date.isoformat(),
            "repeat":
            1,
            "recurrence": [{
                "recurrence_date":
                vat_transfer_date.isoformat(),
                "expected_payment_date":
                vat_transfer_date.isoformat(),
                "amount":
                vat_amount,
                "currency":
                config.CONSTANTS["HOME_CURRENCY"],
                "cleared":
                False,
                "collections": []
            }]
        }

        vat_transfer_scheme_obj = Scheme(vat_transfer_scheme_json)

        vat_transfer_payment_obj = Payment(vat_transfer_json)
        vat_transfer_payment_obj.scheme = vat_transfer_scheme_obj

        output.append(vat_transfer_payment_obj)

    # VAT payment

    if invoice.is_vat_liable:
        vat_amount = invoice.vat_amount_in_local_currency

        vat_payment_json = {
            "guid": identifier.get_guid(),
            "creation_date": datetime.datetime.now().isoformat(),
            "company": config.CONSTANTS["HOME_GOVERNMENT"],
            "description": description_prefix + " - VAT payment",
            "invoice_guid": "",
            "direction": DIRECTION_OUT,
            "amount": vat_amount,
            "currency": config.CONSTANTS["HOME_CURRENCY"],
            "cleared": False,
            "is_vat": True
        }

        vat_payment_date = invoice.vat_payment_date

        vat_payment_scheme_json = {
            "frequency":
            1,
            "period":
            PERIOD_DAILY,
            "start":
            vat_payment_date.isoformat(),
            "repeat":
            1,
            "recurrence": [{
                "recurrence_date":
                vat_payment_date.isoformat(),
                "expected_payment_date":
                vat_payment_date.isoformat(),
                "amount":
                vat_amount,
                "currency":
                config.CONSTANTS["HOME_CURRENCY"],
                "cleared":
                False,
                "collections": []
            }]
        }

        vat_payment_scheme_obj = Scheme(vat_payment_scheme_json)

        vat_payment_payment_obj = Payment(vat_payment_json)
        vat_payment_payment_obj.scheme = vat_payment_scheme_obj

        output.append(vat_payment_payment_obj)

    # Income tax investment & transfer

    itax_amount = currency_converter.convert_to_local_currency(
        invoice.income_tax_amount, invoice_currency)

    itax_investment_rate = 100
    itax_investment_rate -= inc_tax_calc.safety_tax_rate
    itax_investment_rate -= inc_tax_calc.temp_tax_rate
    itax_investment_amount = itax_amount * itax_investment_rate / 100
    itax_amount -= itax_investment_amount

    itax_transfer_json = {
        "guid": identifier.get_guid(),
        "creation_date": datetime.datetime.now().isoformat(),
        "company": config.CONSTANTS["DEFAULT_BANK"],
        "description": description_prefix + " - income tax investment",
        "invoice_guid": "",
        "direction": DIRECTION_TRANSFER,
        "amount": itax_investment_amount,
        "currency": config.CONSTANTS["HOME_CURRENCY"],
        "cleared": False
    }

    itax_transfer_date = invoice.income_tax_transfer_date

    itax_transfer_scheme_json = {
        "frequency":
        1,
        "period":
        PERIOD_DAILY,
        "start":
        itax_transfer_date.isoformat(),
        "repeat":
        1,
        "recurrence": [{
            "recurrence_date": itax_transfer_date.isoformat(),
            "expected_payment_date": itax_transfer_date.isoformat(),
            "amount": itax_amount,
            "currency": config.CONSTANTS["HOME_CURRENCY"],
            "cleared": False,
            "collections": []
        }]
    }

    itax_transfer_scheme_obj = Scheme(itax_transfer_scheme_json)
    itax_transfer_payment_obj = Payment(itax_transfer_json)
    itax_transfer_payment_obj.scheme = itax_transfer_scheme_obj
    output.append(itax_transfer_payment_obj)

    itax_transfer_json = {
        "guid": identifier.get_guid(),
        "creation_date": datetime.datetime.now().isoformat(),
        "company": config.CONSTANTS["DEFAULT_BANK"],
        "description": description_prefix + " - income tax transfer",
        "invoice_guid": "",
        "direction": DIRECTION_TRANSFER,
        "amount": itax_amount,
        "currency": config.CONSTANTS["HOME_CURRENCY"],
        "cleared": False
    }

    itax_transfer_date = invoice.income_tax_transfer_date

    itax_transfer_scheme_json = {
        "frequency":
        1,
        "period":
        PERIOD_DAILY,
        "start":
        itax_transfer_date.isoformat(),
        "repeat":
        1,
        "recurrence": [{
            "recurrence_date": itax_transfer_date.isoformat(),
            "expected_payment_date": itax_transfer_date.isoformat(),
            "amount": itax_amount,
            "currency": config.CONSTANTS["HOME_CURRENCY"],
            "cleared": False,
            "collections": []
        }]
    }

    itax_transfer_scheme_obj = Scheme(itax_transfer_scheme_json)
    itax_transfer_payment_obj = Payment(itax_transfer_json)
    itax_transfer_payment_obj.scheme = itax_transfer_scheme_obj
    output.append(itax_transfer_payment_obj)

    # Income tax payment

    itax_amount = currency_converter.convert_to_local_currency(
        invoice.income_tax_amount, invoice_currency)

    itax_payment_json = {
        "guid": identifier.get_guid(),
        "creation_date": datetime.datetime.now().isoformat(),
        "company": config.CONSTANTS["HOME_GOVERNMENT"],
        "description": description_prefix + " - income tax payment",
        "invoice_guid": "",
        "direction": DIRECTION_OUT,
        "amount": itax_amount,
        "currency": config.CONSTANTS["HOME_CURRENCY"],
        "cleared": False,
        "is_income_tax": True
    }

    itax_payment_date = invoice.income_tax_payment_date

    itax_payment_scheme_json = {
        "frequency":
        1,
        "period":
        PERIOD_DAILY,
        "start":
        itax_payment_date.isoformat(),
        "repeat":
        1,
        "recurrence": [{
            "recurrence_date": itax_payment_date.isoformat(),
            "expected_payment_date": itax_payment_date.isoformat(),
            "amount": itax_amount,
            "currency": config.CONSTANTS["HOME_CURRENCY"],
            "cleared": False,
            "collections": []
        }]
    }

    itax_payment_scheme_obj = Scheme(itax_payment_scheme_json)

    itax_payment_payment_obj = Payment(itax_payment_json)
    itax_payment_payment_obj.scheme = itax_payment_scheme_obj

    output.append(itax_payment_payment_obj)

    # Alms

    alms_amount = currency_converter.convert_to_local_currency(
        invoice.alms_amount, invoice_currency)

    alms_payment_json = {
        "guid": identifier.get_guid(),
        "creation_date": datetime.datetime.now().isoformat(),
        "company": config.CONSTANTS["COMPANY_NAME_UNKNOWN"],
        "description": description_prefix + " - alms",
        "invoice_guid": "",
        "direction": DIRECTION_OUT,
        "amount": alms_amount,
        "currency": config.CONSTANTS["HOME_CURRENCY"],
        "cleared": False
    }

    alms_payment_date = invoice.alms_payment_date

    alms_payment_scheme_json = {
        "frequency":
        1,
        "period":
        PERIOD_DAILY,
        "start":
        alms_payment_date.isoformat(),
        "repeat":
        1,
        "recurrence": [{
            "recurrence_date": alms_payment_date.isoformat(),
            "expected_payment_date": alms_payment_date.isoformat(),
            "amount": alms_amount,
            "currency": config.CONSTANTS["HOME_CURRENCY"],
            "cleared": False,
            "collections": []
        }]
    }

    alms_payment_scheme_obj = Scheme(alms_payment_scheme_json)

    alms_payment_payment_obj = Payment(alms_payment_json)
    alms_payment_payment_obj.scheme = alms_payment_scheme_obj

    output.append(alms_payment_payment_obj)

    # Flush
    return output
Beispiel #8
0
 def guid(self) -> str:
     """ Unique payment guid """
     if "guid" not in self._payment:
         self._payment["guid"] = identifier.get_guid()
     return self._payment["guid"]
Beispiel #9
0
def _create_investment_transaction(bank: str, description: str,
                                   trn_account: str, inv_account: str,
                                   amount: float):
    trn_pay_json = {
        "guid": identifier.get_guid(),
        "creation_date": datetime.datetime.now().isoformat(),
        "company": bank,
        "description": f"{description} - transfer to {trn_account}",
        "invoice_guid": "",
        "direction": DIRECTION_TRANSFER,
        "amount": amount,
        "currency": config.CONSTANTS["HOME_CURRENCY"],
        "cleared": False
    }

    trn_scheme_json = {
        "frequency":
        1,
        "period":
        PERIOD_DAILY,
        "start":
        trn_pay_json["creation_date"],
        "repeat":
        1,
        "recurrence": [{
            "recurrence_date": trn_pay_json["creation_date"],
            "expected_payment_date": trn_pay_json["creation_date"],
            "amount": trn_pay_json["amount"],
            "currency": trn_pay_json["currency"],
            "cleared": False,
            "collections": []
        }]
    }

    trn_scheme = Scheme(trn_scheme_json)
    trn_pay = Payment(trn_pay_json)
    trn_pay.scheme = trn_scheme

    inv_pay_json = {
        "guid": identifier.get_guid(),
        "creation_date": datetime.datetime.now().isoformat(),
        "company": bank,
        "description": f"{description} - buy to {inv_account}",
        "invoice_guid": "",
        "direction": DIRECTION_TRANSFER,
        "amount": amount,
        "currency": config.CONSTANTS["HOME_CURRENCY"],
        "cleared": False
    }

    inv_scheme_json = {
        "frequency":
        1,
        "period":
        PERIOD_DAILY,
        "start":
        trn_pay_json["creation_date"],
        "repeat":
        1,
        "recurrence": [{
            "recurrence_date": inv_pay_json["creation_date"],
            "expected_payment_date": inv_pay_json["creation_date"],
            "amount": inv_pay_json["amount"],
            "currency": inv_pay_json["currency"],
            "cleared": False,
            "collections": []
        }]
    }

    inv_scheme = Scheme(inv_scheme_json)
    inv_pay = Payment(inv_pay_json)
    inv_pay.scheme = inv_scheme

    trn_pay.save()
    inv_pay.save()