Code Duplication    Length = 12-13 lines in 6 locations

chezbetty/models/transaction.py 6 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
            r = r.filter(event.Event.timestamp<end.replace(tzinfo=None))
793
        return utility.group(r.all(), period)
794
795
    @classmethod
796
    def profit_on_sales(cls, start=None, end=None):
797
        r = DBSession.query(func.sum(cls.amount-(cls.wholesale*cls.quantity)).label('p'))\
798
                        .join(Transaction)\
799
                        .join(event.Event)\
800
                        .filter(event.Event.deleted==False)
801
        if start:
802
            r = r.filter(event.Event.timestamp>=start.replace(tzinfo=None))
803
        if end:
804
            r = r.filter(event.Event.timestamp<end.replace(tzinfo=None))
805
806
        return r.one().p or Decimal(0.0)
807
808
    @classmethod
809
    def item_sale_quantities(cls, item_id):
@@ 782-793 (lines=12) @@
779
            r = r.filter(event.Event.timestamp<end.replace(tzinfo=None))
780
        return utility.group(r.all(), period)
781
782
    @classmethod
783
    def virtual_revenue_by_period(cls, period, start=None, end=None):
784
        r = DBSession.query(cls.amount.label('summable'), event.Event.timestamp)\
785
                     .join(Transaction)\
786
                     .join(event.Event)\
787
                     .filter(event.Event.deleted==False)\
788
                     .order_by(event.Event.timestamp)
789
        if start:
790
            r = r.filter(event.Event.timestamp>=start.replace(tzinfo=None))
791
        if end:
792
            r = r.filter(event.Event.timestamp<end.replace(tzinfo=None))
793
        return utility.group(r.all(), period)
794
795
    @classmethod
796
    def profit_on_sales(cls, start=None, end=None):
@@ 769-780 (lines=12) @@
766
        SubTransaction.__init__(self, transaction, amount, item.id, quantity, wholesale)
767
        self.price = price
768
769
    @classmethod
770
    def quantity_by_period(cls, period, start=None, end=None):
771
        r = DBSession.query(cls.quantity.label('summable'), event.Event.timestamp)\
772
                     .join(Transaction)\
773
                     .join(event.Event)\
774
                     .filter(event.Event.deleted==False)\
775
                     .order_by(event.Event.timestamp)
776
        if start:
777
            r = r.filter(event.Event.timestamp>=start.replace(tzinfo=None))
778
        if end:
779
            r = r.filter(event.Event.timestamp<end.replace(tzinfo=None))
780
        return utility.group(r.all(), period)
781
782
    @classmethod
783
    def virtual_revenue_by_period(cls, period, start=None, end=None):
@@ 481-492 (lines=12) @@
478
class Deposit(Transaction):
479
    __mapper_args__ = {'polymorphic_identity': 'deposit'}
480
481
    @classmethod
482
    def deposits_by_period(cls, period, start=None, end=None):
483
        r = DBSession.query(cls.amount.label('summable'), event.Event.timestamp)\
484
                     .join(event.Event)\
485
                     .order_by(event.Event.timestamp)\
486
                     .filter(event.Event.deleted==False)
487
        if start:
488
            r = r.filter(event.Event.timestamp>=start.replace(tzinfo=None))
489
        if end:
490
            r = r.filter(event.Event.timestamp<end.replace(tzinfo=None))
491
492
        return utility.group(r.all(), period)
493
494
495
class CashDeposit(Deposit):