def has_change_permission(self, obj): return current_user.has_shop_access( obj.retail_shop_id) and current_user.has_permission('change_tag')
def has_delete_permission(self, obj): return current_user.has_shop_access(obj.retail_shop_id)
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')
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
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')
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
def has_delete_permission(self, obj): return current_user.has_shop_access( obj.retail_shop_id) and current_user.has_permission('remove_combo')
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')
def has_delete_permission(self, obj): return current_user.has_shop_access(obj.retail_shop_id) and \ current_user.has_permission('remove_distributor_bill')
def has_add_permission(self, obj): return current_user.has_shop_access(obj.store_id) and current_user.has_permission('create_order_item')
def has_delete_permission(self, obj): return current_user.has_shop_access(obj.store_id) and current_user.has_permission('remove_order_item')
def has_change_permission(self, obj): return current_user.has_shop_access(obj.store_id)
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)