Code Duplication    Length = 12-13 lines in 7 locations

chezbetty/models/transaction.py 7 locations

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