コード例 #1
0
 def has_change_permission(self, obj):
     return current_user.has_shop_access(
         obj.retail_shop_id) and current_user.has_permission('change_tag')
コード例 #2
0
ファイル: resources.py プロジェクト: saurabh1e/open-pos-api
 def has_delete_permission(self, obj):
     return current_user.has_shop_access(obj.retail_shop_id)
コード例 #3
0
 def has_delete_permission(self, obj, data):
     return current_user.has_shop_access(
         obj.retail_shop_id) and current_user.has_permission(
             'remove_brand_distributor')
コード例 #4
0
 def has_add_permission(self, obj, data):
     if not current_user.has_permission('create_brand_distributor') or \
             not current_user.has_shop_access(Product.query.with_entities(Brand.retail_shop_id)
                                                      .filter(Brand.id == obj.brand_id).scalar()):
         return False
     return True
コード例 #5
0
 def has_delete_permission(self, obj, data):
     return current_user.has_shop_access(
         obj.retail_shop_id) and current_user.has_permission(
             'remove_product_tax')
コード例 #6
0
 def has_add_permission(self, obj, data):
     if not current_user.has_permission('create_product_tax') or \
             not current_user.has_shop_access(Product.query.with_entities(Product.retail_shop_id)
                                                      .filter(Product.id == obj.product_id).scalar()):
         return False
     return True
コード例 #7
0
 def has_delete_permission(self, obj):
     return current_user.has_shop_access(
         obj.retail_shop_id) and current_user.has_permission('remove_combo')
コード例 #8
0
 def has_change_permission(self, obj, data):
     return current_user.has_shop_access(obj.retail_shop_id) and \
            current_user.has_permission('change_product_salt')
コード例 #9
0
 def has_delete_permission(self, obj):
     return current_user.has_shop_access(obj.retail_shop_id) and \
            current_user.has_permission('remove_distributor_bill')
コード例 #10
0
 def has_add_permission(self, obj):
     return current_user.has_shop_access(obj.store_id) and current_user.has_permission('create_order_item')
コード例 #11
0
 def has_delete_permission(self, obj):
     return current_user.has_shop_access(obj.store_id) and current_user.has_permission('remove_order_item')
コード例 #12
0
 def has_change_permission(self, obj):
     return current_user.has_shop_access(obj.store_id)
コード例 #13
0
    def get(self):

        shops = request.args.getlist('__retail_shop_id__in')
        for shop in shops:
            if not current_user.has_shop_access(shop):
                return make_response(jsonify({'message': 'Access Forbidden'}),
                                     403)

        brand_id = request.args.get('__retail_brand_id__equal')
        if len(shops) == 1:
            shops = shops[0].split(',')
        from_date = datetime.strptime(request.args['__created_on__gte'],
                                      '%Y-%m-%dT%H:%M:%S.%fZ').date()
        to_date = datetime.strptime(request.args['__created_on__lte'],
                                    '%Y-%m-%dT%H:%M:%S.%fZ').date()

        days = (to_date - from_date).days

        collection_type = 'day'
        if days > 56:
            collection_type = 'week'
            if days > 360:
                collection_type = 'month'

        new_customers = self.model.query.join(Order, and_(Order.customer_id == self.model.id)) \
            .with_entities(func.Count(func.Distinct(Order.customer_id)), func.Sum(Order.total), func.avg(Order.total),
                           func.cast(func.date_trunc(collection_type, func.cast(self.model.created_on, Date)), Text)
                           .label('dateWeek')
                           ) \
            .filter(and_(self.model.created_on.between(from_date, to_date), Order.created_on.between(from_date, to_date),
                    Order.retail_shop_id.in_(shops))).group_by('dateWeek').order_by('dateWeek')\
            .having(func.Count(func.Distinct(Order.id)) > 0).all()

        return_customers = self.model.query.join(Order, and_(Order.customer_id == self.model.id)) \
            .with_entities(func.Count(func.Distinct(Order.customer_id)), func.Sum(Order.total), func.avg(Order.total),
                           func.cast(func.date_trunc(collection_type, func.cast(self.model.created_on, Date)), Text)
                           .label('dateWeek')
                           ) \
            .filter(and_(self.model.created_on.between(from_date, to_date), Order.created_on.between(from_date, to_date))) \
            .having(func.Count(func.Distinct(Order.id)) > 1).group_by('dateWeek').order_by('dateWeek').all()

        old_customers = Order.query.join(Customer, and_(Customer.id == Order.customer_id)) \
            .with_entities(func.Count(func.Distinct(Order.customer_id)), func.Sum(Order.total), func.avg(Order.total),
                           func.cast(func.date_trunc(collection_type, func.cast(Order.created_on, Date)), Text)
                           .label('dateWeek')
                           ) \
            .filter(and_(Customer.created_on <= from_date, Order.created_on.between(from_date, to_date),
                    Order.retail_shop_id.in_(shops), Customer.retail_brand_id == brand_id))\
            .group_by('dateWeek').order_by('dateWeek') \
            .having(func.Count(func.Distinct(Order.id)) > 0).all()

        total_due = self.model.query.outerjoin(Order, and_(Order.customer_id == self.model.id)) \
            .outerjoin(CustomerTransaction, and_(CustomerTransaction.customer_id == self.model.id)) \
            .with_entities(func.coalesce(cast(func.Sum(Order.total), Float), 0.0) -
                           func.coalesce(cast(func.Sum(Order.amount_paid), Float), 0.0)
                           - func.coalesce(cast(func.Sum(CustomerTransaction.amount), Float), 0.0)) \
            .filter(self.model.retail_brand_id == brand_id).scalar()

        top_customers = self.model.query.outerjoin(Order, and_(Order.customer_id == self.model.id)) \
            .with_entities(func.Count(func.Distinct(Order.id)), self.model.name) \
            .filter(self.model.retail_brand_id == brand_id, Order.created_on.between(from_date, to_date),
                    Order.retail_shop_id.in_(shops)).group_by(Order.customer_id, self.model.name) \
            .order_by(-func.Count(func.Distinct(Order.id))).limit(10).all()

        top_billed_customers = self.model.query.outerjoin(Order, and_(Order.customer_id == self.model.id)) \
            .with_entities(func.Sum(Order.total), self.model.name) \
            .filter(self.model.retail_brand_id == brand_id, Order.created_on.between(from_date, to_date),
                    Order.retail_shop_id.in_(shops)).group_by(Order.customer_id, self.model.name) \
            .order_by(-func.Sum(Order.total)).limit(10).all()

        return make_response(
            jsonify(
                dict(new_customers=new_customers,
                     return_customers=return_customers,
                     old_customers=old_customers,
                     top_billed_customers=top_billed_customers,
                     total_due=total_due,
                     top_customers=top_customers)), 200)