Beispiel #1
0
    def get_wanted_movements(self):
        item_sums = self.get_payable_sums_dict()
        # logger.info("20120901 get_wanted_movements %s", sums_dict)
        counter_sums = SumCollector()
        partner = self.get_partner()
        for k, amount in item_sums.items():
            acc, prj = k
            yield self.create_movement(
                None,
                acc,
                prj,
                self.journal.dc,
                amount,
                partner=partner if acc.needs_partner else None)  # 20160413
            counter_sums.collect(prj, amount)

        acc = self.get_trade_type().get_partner_account()
        if acc is None:
            if len(counter_sums.items()):
                raise Exception("Could not find partner account")
        else:
            for prj, amount in counter_sums.items():
                yield self.create_movement(
                    None,
                    acc,
                    prj,
                    not self.journal.dc,
                    amount,
                    partner=partner if acc.needs_partner else None,
                    match=self.match or self.get_default_match())
Beispiel #2
0
    def get_wanted_movements(self):
        item_sums = self.get_payable_sums_dict()
        # logger.info("20120901 get_wanted_movements %s", sums_dict)
        counter_sums = SumCollector()
        partner = self.get_partner()
        has_vat = dd.is_installed('vat')
        kw = dict()
        for k, amount in item_sums.items():
            # amount = myround(amount)
            # first item of each tuple k is itself a tuple (account, ana_account)
            acc_tuple, prj, vat_class, vat_regime = k
            account, ana_account = acc_tuple
            # if not isinstance(acc_tuple, tuple):
            #     raise Exception("Not a tuple: {}".format(acc_tuple))
            if not isinstance(account, rt.models.ledger.Account):
                raise Exception("Not an account: {}".format(account))
            if has_vat:
                kw.update(
                    vat_class=vat_class, vat_regime=vat_regime)

            if account.needs_partner:
                kw.update(partner=partner)
            yield self.create_movement(
                None, acc_tuple, prj, self.journal.dc, amount, **kw)
            counter_sums.collect(prj, amount)

        tt = self.get_trade_type()
        if tt is None:
            if len(counter_sums.items()):
                raise Warning("No trade type for {}".format(self))
            return
        acc = self.get_trade_type().get_main_account()
        if acc is None:
            if len(counter_sums.items()):
                raise Warning("No main account for {}".format(tt))
            return

        total_amount = 0
        for prj, amount in counter_sums.items():
            total_amount += amount
            yield self.create_movement(
                None, (acc, None), prj, not self.journal.dc, amount,
                partner=partner if acc.needs_partner else None,
                match=self.get_match())

        if dd.plugins.ledger.worker_model \
                and TradeTypes.clearings.main_account \
                and self.payment_term_id and self.payment_term.worker:
            worker = self.payment_term.worker
            dc = self.journal.dc
            # one movement to nullify the credit that was booked to the partner account,
            # another movment to book it to the worker's account:
            yield self.create_movement(
                None, (acc, None), None, dc, total_amount,
                partner=partner, match=self.get_match())
            yield self.create_movement(
                None, (TradeTypes.clearings.get_main_account(), None), None, not dc, total_amount,
                partner=worker, match=self.get_match())
Beispiel #3
0
def check_clearings(qs, matches=[]):
    qs = qs.select_related('voucher', 'voucher__journal')
    if len(matches):
        qs = qs.filter(match__in=matches)
    # fcu = dd.plugins.ledger.suppress_movements_until
    # if fcu:
    #     qs = qs.exclude(value_date__lte=fcu)
    sums = SumCollector()
    for mvt in qs:
        # k = (mvt.get_match(), mvt.account)
        k = (mvt.match, mvt.account)
        mvt_dc = mvt.dc
        # if mvt.voucher.journal.invert_due_dc:
        #     mvt_dc = mvt.dc
        # else:
        #     mvt_dc = not mvt.dc
        if mvt_dc == DEBIT:
            sums.collect(k, mvt.amount)
        else:
            sums.collect(k, -mvt.amount)

    for k, balance in sums.items():
        match, account = k
        sat = (balance == ZERO)
        qs.filter(account=account, match=match).update(cleared=sat)
Beispiel #4
0
def check_clearings(partner, matches=[]):
    """Check whether involved movements are cleared or not, and update
    their :attr:`cleared` field accordingly.

    """
    qs = rt.modules.ledger.Movement.objects.filter(
        partner=partner, account__clearable=True).order_by('match')
    qs = qs.select_related('voucher', 'voucher__journal')
    if len(matches):
        qs = qs.filter(match__in=matches)
    sums = SumCollector()
    for mvt in qs:
        # k = (mvt.get_match(), mvt.account)
        k = (mvt.match, mvt.account)
        mvt_dc = mvt.dc
        # if mvt.voucher.journal.invert_due_dc:
        #     mvt_dc = mvt.dc
        # else:
        #     mvt_dc = not mvt.dc
        if mvt_dc == DEBIT:
            sums.collect(k, mvt.amount)
        else:
            sums.collect(k, -mvt.amount)

    for k, balance in sums.items():
        match, account = k
        sat = (balance == ZERO)
        qs.filter(account=account, match=match).update(cleared=sat)

    on_ledger_movement.send(sender=partner.__class__, instance=partner)
Beispiel #5
0
def check_clearings(qs, matches=[]):
    """Check whether involved movements are cleared or not, and update
    their :attr:`cleared` field accordingly.

    """
    qs = qs.select_related('voucher', 'voucher__journal')
    if len(matches):
        qs = qs.filter(match__in=matches)
    sums = SumCollector()
    for mvt in qs:
        # k = (mvt.get_match(), mvt.account)
        k = (mvt.match, mvt.account)
        mvt_dc = mvt.dc
        # if mvt.voucher.journal.invert_due_dc:
        #     mvt_dc = mvt.dc
        # else:
        #     mvt_dc = not mvt.dc
        if mvt_dc == DEBIT:
            sums.collect(k, mvt.amount)
        else:
            sums.collect(k, - mvt.amount)

    for k, balance in sums.items():
        match, account = k
        sat = (balance == ZERO)
        qs.filter(account=account, match=match).update(cleared=sat)
Beispiel #6
0
def check_clearings(partner, matches=[]):
    """Check whether involved movements are cleared or not, and update
    their :attr:`cleared` field accordingly.

    """
    qs = rt.modules.ledger.Movement.objects.filter(
        partner=partner, account__clearable=True).order_by('match')
    qs = qs.select_related('voucher', 'voucher__journal')
    if len(matches):
        qs = qs.filter(match__in=matches)
    sums = SumCollector()
    for mvt in qs:
        # k = (mvt.get_match(), mvt.account)
        k = (mvt.match, mvt.account)
        mvt_dc = mvt.dc
        # if mvt.voucher.journal.invert_due_dc:
        #     mvt_dc = mvt.dc
        # else:
        #     mvt_dc = not mvt.dc
        if mvt_dc == DEBIT:
            sums.collect(k, mvt.amount)
        else:
            sums.collect(k, - mvt.amount)

    for k, balance in sums.items():
        match, account = k
        sat = (balance == ZERO)
        qs.filter(account=account, match=match).update(cleared=sat)

    on_ledger_movement.send(sender=partner.__class__, instance=partner)
Beispiel #7
0
    def get_wanted_movements(self):
        """Implements
        :meth:`lino_xl.lib.ledger.Voucher.get_wanted_movements`.

        """
        item_sums = self.get_payable_sums_dict()
        # logger.info("20120901 get_wanted_movements %s", sums_dict)
        counter_sums = SumCollector()
        partner = self.get_partner()
        has_vat = dd.is_installed('vat')
        kw = dict()
        for k, amount in item_sums.items():
            # amount = myround(amount)
            acc_tuple, prj, vat_class, vat_regime = k
            # acc_tuple is a tuple (account, ana_account)
            if not isinstance(acc_tuple, tuple):
                raise Exception("Not a tuple: {}".format(acc_tuple))
            if not isinstance(acc_tuple[0], rt.models.ledger.Account):
                raise Exception("Not an account: {}".format(acc_tuple[0]))
            if has_vat:
                kw.update(
                    vat_class=vat_class, vat_regime=vat_regime)
                
            if acc_tuple[0].needs_partner:
                kw.update(partner=partner)
            yield self.create_movement(
                None, acc_tuple, prj, self.journal.dc, amount, **kw)
            counter_sums.collect(prj, amount)

        tt = self.get_trade_type()
        if tt is None:
            if len(counter_sums.items()):
                raise Warning("No trade type for {}".format(self))
            return
        acc = self.get_trade_type().get_main_account()
        if acc is None:
            if len(counter_sums.items()):
                raise Warning("No main account for {}".format(tt))
        else:
            for prj, amount in counter_sums.items():
                # amount = myround(amount)
                yield self.create_movement(
                    None, (acc, None), prj, not self.journal.dc, amount,
                    partner=partner if acc.needs_partner else None,
                    match=self.get_match())
Beispiel #8
0
    def get_wanted_movements(self):
        item_sums = self.get_payable_sums_dict()
        # logger.info("20120901 get_wanted_movements %s", sums_dict)
        counter_sums = SumCollector()
        partner = self.get_partner()
        for k, amount in item_sums.items():
            acc, prj = k
            yield self.create_movement(
                None, acc, prj, self.journal.dc, amount,
                partner=partner if acc.needs_partner else None)  # 20160413
            counter_sums.collect(prj, amount)

        acc = self.get_trade_type().get_partner_account()
        if acc is None:
            if len(counter_sums.items()):
                raise Exception("Could not find partner account")
        else:
            for prj, amount in counter_sums.items():
                yield self.create_movement(
                    None, acc, prj, not self.journal.dc, amount,
                    partner=partner if acc.needs_partner else None,
                    match=self.match or self.get_default_match())
Beispiel #9
0
def check_clearings(qs, matches=[]):
    qs = qs.select_related('voucher', 'voucher__journal')
    if len(matches):
        qs = qs.filter(match__in=matches)
    # fcu = dd.plugins.ledger.suppress_movements_until
    # if fcu:
    #     qs = qs.exclude(value_date__lte=fcu)
    sums = SumCollector()
    for mvt in qs:
        k = (mvt.match, mvt.account)
        sums.collect(k, mvt.amount)

    for k, balance in sums.items():
        match, account = k
        sat = (balance == ZERO)
        # if not sat:
        #     print("20201014 {} != {}".format(balance, ZERO))
        qs.filter(account=account, match=match).update(cleared=sat)