<commit_before># Copyright 2008-2016 <NAME>
# License: BSD (see file COPYING for details)

"""
Database models for `lino_xl.lib.finan`.
"""
from __future__ import unicode_literals
import six

import logging
logger = logging.getLogger(__name__)

from django.db import models

from lino.utils.xmlgen.html import E, join_elems
from lino_xl.lib.accounts.utils import ZERO, DEBIT, CREDIT
from lino_xl.lib.ledger.fields import DcAmountField
from lino_xl.lib.ledger.choicelists import VoucherTypes
from lino_xl.lib.ledger.roles import LedgerUser, LedgerStaff
from lino_xl.lib.ledger.mixins import ProjectRelated
from lino_xl.lib.sepa.mixins import BankAccount
from lino.modlib.printing.mixins import Printable

from lino.api import dd, rt, _

from .mixins import (FinancialVoucher, FinancialVoucherItem,
                     DatedFinancialVoucher, DatedFinancialVoucherItem)

from .actions import WriteXML


ledger = dd.resolve_app('ledger')


def warn_jnl_account(jnl):
    fld = jnl._meta.get_field('account')
    raise Warning(_("Field '{0}' in journal '{0}' is empty!").format(
        fld.verbose_name, jnl))


class ShowSuggestions(dd.Action):
    # started as a copy of ShowSlaveTable
    # TABLE2ACTION_ATTRS = tuple('help_text icon_name label sort_index'.split())
    TABLE2ACTION_ATTRS = tuple('help_text label sort_index'.split())
    show_in_bbar = False
    show_in_workflow = True
    readonly = False

    @classmethod
    def get_actor_label(self):
        return self._label or self.slave_table.label

    def attach_to_actor(self, actor, name):
        if actor.suggestions_table is None:
            # logger.info("%s has no suggestions_table", actor)
            return  # don't attach
        if isinstance(actor.suggestions_table, six.string_types):
            T = rt.modules.resolve(actor.suggestions_table)
            if T is None:
                raise Exception("No table named %s" % actor.suggestions_table)
            actor.suggestions_table = T
        for k in self.TABLE2ACTION_ATTRS:
            setattr(self, k, getattr(actor.suggestions_table, k))
        return super(ShowSuggestions, self).attach_to_actor(actor, name)

    # def get_action_permission(self, ar, obj, state):
        
    #     return super(ShowSuggestions, self).get_action_permission(
    #         ar, obj, state)

    def run_from_ui(self, ar, **kw):
        obj = ar.selected_rows[0]
        sar = ar.spawn(ar.actor.suggestions_table, master_instance=obj)
        js = ar.renderer.request_handler(sar)
        ar.set_response(eval_js=js)


class JournalEntry(DatedFinancialVoucher, ProjectRelated):
    """This is the model for "journal entries" ("operations diverses").

    """
    class Meta:
        app_label = 'finan'
        abstract = dd.is_abstract_model(__name__, 'JournalEntry')
        verbose_name = _("Journal Entry")
        verbose_name_plural = _("Journal Entries")

    def get_wanted_movements(self):
        # dd.logger.info("20151211 FinancialVoucher.get_wanted_movements()")
        amount, movements_and_items = self.get_finan_movements()
        if amount:
            raise Exception("Missing amount %s in movements" % amount)
        return movements_and_items

class PaymentOrder(FinancialVoucher, Printable):
    """A **payment order** is when a user instructs a bank to execute a
    series of outgoing transactions from a given bank account.

    """
    class Meta:
        app_label = 'finan'<commit_msg>bugfix #1875<commit_after># Copyright 2008-2016 <NAME>
# License: BSD (see file COPYING for details)

"""
Database models for `lino_xl.lib.finan`.
"""
from __future__ import unicode_literals
import six

import logging
logger = logging.getLogger(__name__)

from django.db import models

from lino.utils.xmlgen.html import E, join_elems
from lino_xl.lib.accounts.utils import ZERO, DEBIT, CREDIT
from lino_xl.lib.ledger.fields import DcAmountField
from lino_xl.lib.ledger.choicelists import VoucherTypes
from lino_xl.lib.ledger.roles import LedgerUser, LedgerStaff
from lino_xl.lib.ledger.mixins import ProjectRelated
from lino_xl.lib.sepa.mixins import BankAccount
from lino.modlib.printing.mixins import Printable

from lino.api import dd, rt, _

from .mixins import (FinancialVoucher, FinancialVoucherItem,
                     DatedFinancialVoucher, DatedFinancialVoucherItem)

from .actions import WriteXML


ledger = dd.resolve_app('ledger')


def warn_jnl_account(jnl):
    fld = jnl._meta.get_field('account')
    raise Warning(_("Field '{0}' in journal '{0}' is empty!").format(
        fld.verbose_name, jnl))


class ShowSuggestions(dd.Action):
    # started as a copy of ShowSlaveTable
    # TABLE2ACTION_ATTRS = tuple('help_text icon_name label sort_index'.split())
    TABLE2ACTION_ATTRS = tuple('help_text label sort_index'.split())
    show_in_bbar = False
    show_in_workflow = True
    readonly = False

    @classmethod
    def get_actor_label(self):
        return self._label or self.slave_table.label

    def attach_to_actor(self, actor, name):
        if actor.suggestions_table is None:
            # logger.info("%s has no suggestions_table", actor)
            return  # don't attach
        if isinstance(actor.suggestions_table, six.string_types):
            T = rt.modules.resolve(actor.suggestions_table)
            if T is None:
                raise Exception("No table named %s" % actor.suggestions_table)
            actor.suggestions_table = T
        for k in self.TABLE2ACTION_ATTRS:
            setattr(self, k, getattr(actor.suggestions_table, k))
        return super(ShowSuggestions, self).attach_to_actor(actor, name)

    # def get_action_permission(self, ar, obj, state):
        
    #     return super(ShowSuggestions, self).get_action_permission(
    #         ar, obj, state)

    def run_from_ui(self, ar, **kw):
        obj = ar.selected_rows[0]
        sar = ar.spawn(ar.actor.suggestions_table, master_instance=obj)
        js = ar.renderer.request_handler(sar)
        ar.set_response(eval_js=js)


class JournalEntry(DatedFinancialVoucher, ProjectRelated):
    """This is the model for "journal entries" ("operations diverses").

    """
    class Meta:
        app_label = 'finan'
        abstract = dd.is_abstract_model(__name__, 'JournalEntry')
        verbose_name = _("Journal Entry")
        verbose_name_plural = _("Journal Entries")

    def get_wanted_movements(self):
        # dd.logger.info("20151211 FinancialVoucher.get_wanted_movements()")
        amount, movements_and_items = self.get_finan_movements()
        if amount:
            raise Exception(_("Missing amount {} in movements").format(
                amount))
        for m, i in movements_and_items:
            yield m

class PaymentOrder(FinancialVoucher, Printable):
    """A **payment order** is when a user instructs a bank to execute a
    series of outgoing transactions from a given bank account.

    """
    class Meta:
        app_label = 'finan'
{'fraction_non_alphanumeric': 0.09236542069562707, 'fraction_numerical': 0.006927406552172031, 'mean_word_length': 3.3339587242026267, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3683127', 'n_tokens_mistral': 2220, 'n_tokens_neox': 2104, 'n_words': 538}