Example #1
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)
Example #2
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)
Example #3
0
File: utils.py Project: khchine5/xl
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)
Example #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)
Example #5
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())
Example #6
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())
Example #7
0
 def get_payable_sums_dict(self):
     tt = self.get_trade_type()
     sums = SumCollector()
     for i in self.items.order_by('seqno'):
         if i.amount:
             b = i.get_base_account(tt)
             if b is None:
                 raise Exception("No base account for %s (amount is %r)" %
                                 (i, i.amount))
             sums.collect((b, i.project or self.project), i.amount)
     return sums
Example #8
0
 def get_payable_sums_dict(self):
     tt = self.get_trade_type()
     sums = SumCollector()
     for i in self.items.order_by('seqno'):
         if i.amount:
             b = i.get_base_account(tt)
             if b is None:
                 raise Exception(
                     "No base account for %s (amount is %r)" % (
                         i, i.amount))
             sums.collect((b, i.project or self.project), i.amount)
     return sums
Example #9
0
 def get_payable_sums_dict(self):
     sums = SumCollector()
     tt = self.get_trade_type()
     vat_account = tt.get_vat_account()
     if vat_account is None:
         raise Exception("No VAT account for %s." % tt)
     for i in self.items.order_by('seqno'):
         if i.total_base:
             b = i.get_base_account(tt)
             if b is None:
                 msg = "No base account for {0} (tt {1}, total_base {2})"
                 raise Exception(msg.format(i, tt, i.total_base))
             sums.collect((b, self.project), i.total_base)
         if i.total_vat:
             sums.collect((vat_account, self.project), i.total_vat)
     return sums
Example #10
0
 def get_payable_sums_dict(self):
     sums = SumCollector()
     tt = self.get_trade_type()
     vat_account = tt.get_vat_account()
     if vat_account is None:
         raise Exception("No VAT account for %s." % tt)
     for i in self.items.order_by("seqno"):
         if i.total_base:
             b = i.get_base_account(tt)
             if b is None:
                 msg = "No base account for {0} (tt {1}, total_base {2})"
                 raise Exception(msg.format(i, tt, i.total_base))
             sums.collect((b, self.project), i.total_base)
         if i.total_vat:
             sums.collect((vat_account, self.project), i.total_vat)
     return sums
Example #11
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())
Example #12
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)
Example #13
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())
Example #14
0
 def get_payable_sums_dict(self):
     # implements sepa.mixins.Payable
     sums = SumCollector()
     tt = self.get_trade_type()
     # vat_account = tt.get_vat_account()
     # if vat_account is None:
     #     raise Exception("No VAT account for %s." % tt)
     for i in self.items.order_by('seqno'):
         rule = i.get_vat_rule(tt)
         b = i.get_base_account(tt)
         ana_account = i.get_ana_account()
         if i.total_base:
             if b is None:
                 msg = "No base account for {0} (tt {1}, total_base {2})"
                 raise Warning(msg.format(i, tt, i.total_base))
             sums.collect(
                 ((b, ana_account), self.project, i.vat_class, self.vat_regime),
                 i.total_base)
         if i.total_vat and rule is not None:
             if not rule.vat_account:
                 msg = _("This rule ({}) does not allow any VAT.")
                 raise Warning(msg.format(rule))
                     
             vat_amount = i.total_vat
             if rule.vat_returnable:
                 if rule.vat_returnable_account is None:
                     acc_tuple = (b, ana_account)
                 else:
                     acc_tuple = (
                         rule.vat_returnable_account.get_object(), None)
                 sums.collect(
                     (acc_tuple, self.project,
                      i.vat_class, self.vat_regime),
                     vat_amount)
                 vat_amount = - vat_amount
             sums.collect(
                 ((rule.vat_account.get_object(), None), self.project,
                  i.vat_class, self.vat_regime),
                 vat_amount)
     return sums