Code Duplication    Length = 12-13 lines in 7 locations

chezbetty/models/transaction.py 7 locations

@@ 176-188 (lines=13) @@
173
        return r.one().d or Decimal(0.0)
174
175
    # Get the total amount of fees people have paid for being in debt
176
    @classmethod
177
    def fees(cls, start=None, end=None):
178
        r = DBSession.query(func.sum((cls.amount / (1-cls.discount)) - cls.amount).label('f'))\
179
                        .join(event.Event)\
180
                        .filter(cls.discount < 0)\
181
                        .filter(event.Event.deleted==False)
182
183
        if start:
184
            r = r.filter(event.Event.timestamp>=start)
185
        if end:
186
            r = r.filter(event.Event.timestamp<end)
187
188
        return r.one().f or Decimal(0.0)
189
190
    # Returns an array of tuples where the first item is a millisecond timestamp,
191
    # the next is the total amount of debt, and the next is the total amount
@@ 161-173 (lines=13) @@
158
159
    # Get the total amount of discounts people have received for keeping
160
    # money in their account
161
    @classmethod
162
    def discounts(cls, start=None, end=None):
163
        r = DBSession.query(func.sum((cls.amount / (1-cls.discount)) - cls.amount).label('d'))\
164
                        .join(event.Event)\
165
                        .filter(cls.discount > 0)\
166
                        .filter(event.Event.deleted==False)
167
168
        if start:
169
            r = r.filter(event.Event.timestamp>=start)
170
        if end:
171
            r = r.filter(event.Event.timestamp<end)
172
173
        return r.one().d or Decimal(0.0)
174
175
    # Get the total amount of fees people have paid for being in debt
176
    @classmethod
@@ 795-806 (lines=12) @@
792
                     .join(Transaction)\
793
                     .join(event.Event)\
794
                     .filter(event.Event.deleted==False)\
795
                     .order_by(event.Event.timestamp)
796
        if start:
797
            r = r.filter(event.Event.timestamp>=start.replace(tzinfo=None))
798
        if end:
799
            r = r.filter(event.Event.timestamp<end.replace(tzinfo=None))
800
        return utility.group(r.all(), period)
801
802
    @classmethod
803
    def virtual_revenue_by_period(cls, period, start=None, end=None):
804
        r = DBSession.query(cls.amount.label('summable'), event.Event.timestamp)\
805
                     .join(Transaction)\
806
                     .join(event.Event)\
807
                     .filter(event.Event.deleted==False)\
808
                     .order_by(event.Event.timestamp)
809
        if start:
@@ 782-793 (lines=12) @@
779
                            .filter(event.Event.deleted==False)\
780
                            .order_by(desc(event.Event.timestamp))
781
782
class PurchaseLineItem(SubTransaction):
783
    __mapper_args__ = {'polymorphic_identity': 'purchaselineitem'}
784
    price           = Column(Numeric)
785
    def __init__(self, transaction, amount, item, quantity, price, wholesale):
786
        SubTransaction.__init__(self, transaction, amount, item.id, quantity, wholesale)
787
        self.price = price
788
789
    @classmethod
790
    def quantity_by_period(cls, period, start=None, end=None):
791
        r = DBSession.query(cls.quantity.label('summable'), event.Event.timestamp)\
792
                     .join(Transaction)\
793
                     .join(event.Event)\
794
                     .filter(event.Event.deleted==False)\
795
                     .order_by(event.Event.timestamp)
796
        if start:
@@ 769-780 (lines=12) @@
766
    @limitable_all
767
    def all(cls, trans_type=None):
768
        if not trans_type:
769
            return DBSession.query(cls)\
770
                            .join(Transaction)\
771
                            .join(event.Event)\
772
                            .filter(event.Event.deleted==False)\
773
                            .order_by(desc(event.Event.timestamp))
774
        else:
775
            return DBSession.query(cls)\
776
                            .join(Transaction)\
777
                            .join(event.Event)\
778
                            .filter(cls.type==trans_type)\
779
                            .filter(event.Event.deleted==False)\
780
                            .order_by(desc(event.Event.timestamp))
781
782
class PurchaseLineItem(SubTransaction):
783
    __mapper_args__ = {'polymorphic_identity': 'purchaselineitem'}
@@ 481-492 (lines=12) @@
478
    if previous_cb_empty:
479
        q = q.filter(event.Event.timestamp >= previous_cb_empty.timestamp)
480
481
    return q.all()
482
event.EmptyCashBox.relevant_cash_deposits = __relevant_cash_deposits
483
484
################################################################################
485
## Related Classes
486
################################################################################
487
488
class Purchase(Transaction):
489
    __mapper_args__ = {'polymorphic_identity': 'purchase'}
490
    discount = Column(Numeric)
491
492
    def __init__(self, event, user, discount=None):
493
        chezbetty_v = account.get_virt_account("chezbetty")
494
        Transaction.__init__(self, event, user, chezbetty_v, None, None, Decimal(0.0))
495
        self.discount = discount
@@ 146-157 (lines=12) @@
143
                            .filter(cls.type==trans_type)\
144
                            .filter(event.Event.deleted==False).one().c
145
146
    @classmethod
147
    def total(cls, start=None, end=None):
148
        r = DBSession.query(func.sum(cls.amount).label('a'))\
149
                        .join(event.Event)\
150
                        .filter(event.Event.deleted==False)
151
152
        if start:
153
            r = r.filter(event.Event.timestamp>=start)
154
        if end:
155
            r = r.filter(event.Event.timestamp<end)
156
157
        return r.one().a or Decimal(0.0)
158
159
    # Get the total amount of discounts people have received for keeping
160
    # money in their account