class CompanyEstimationList(GlobalEstimationList): is_admin = False schema = get_list_schema(is_global=False, excludes=("company_id", )) add_template_vars = (u'title', 'is_admin', "with_draft", 'legends') legends = GlobalEstimationList.legends + ( ('status-draft', u"Devis en brouillon"), ('status-wait', u"Devis en attente de validation"), ('status-invalid', u"Devis invalides"), ) @property def with_draft(self): return True @property def title(self): return u"Devis de l'entreprise {0}".format(self.request.context.name) def _get_company_id(self, appstruct): """ Return the current context's company id """ return self.request.context.id def filter_status(self, query, appstruct): """ Filter the estimations by status """ status = appstruct.get('status', 'all') logger.info(" + Status filtering : %s" % status) if status != 'all': query = query.filter(Estimation.status == status) return query
class ProjectEstimationListView(CompanyEstimationList, TreeMixin): route_name = PROJECT_ITEM_ESTIMATION_ROUTE schema = get_list_schema(is_global=False, excludes=( "company_id", 'year', 'customer', )) add_template_vars = CompanyEstimationList.add_template_vars + ("add_url", ) @property def add_url(self): return self.request.route_path(PROJECT_ITEM_ESTIMATION_ROUTE, id=self.request.context.id, _query={'action': 'add'}) @property def title(self): return u"Devis du projet {0}".format(self.request.context.name) def _get_company_id(self, appstruct): """ Return the current context's company id """ return self.request.context.company_id def filter_project(self, query, appstruct): remember_navigation_history(self.request, self.context.id) self.populate_navigation() query = query.filter(Estimation.project_id == self.context.id) return query
class EstimationList(GlobalEstimationList): schema = get_list_schema(is_global=False) is_admin = False @property def title(self): return u"Devis de l'entreprise {0}".format(self.request.context.name) def _get_company_id(self, appstruct): """ Return the current context's company id """ return self.request.context.id
class BusinessEstimationList(CompanyEstimationList, TreeMixin): route_name = BUSINESS_ITEM_ESTIMATION_ROUTE schema = get_list_schema(is_global=False, excludes=( "company_id", 'year', 'customer', )) add_template_vars = ( u'title', 'is_admin', "with_draft", "add_url", ) @property def add_url(self): return self.request.route_path(self.route_name, id=self.request.context.id, _query={'action': 'add'}) @property def title(self): return u"Devis du projet {0}".format(self.request.context.name) def _get_company_id(self, appstruct): """ Return the current context's company id """ return self.request.context.project.company_id def filter_business(self, query, appstruct): remember_navigation_history(self.request, self.context.id) self.populate_navigation() query = query.filter(Estimation.business_id == self.context.id) return query
class GlobalEstimationList(BaseListView): title = u"Devis de la CAE" add_template_vars = (u'title', 'is_admin', 'legends') schema = get_list_schema(is_global=True, excludes=('status', )) sort_columns = dict( date=Estimation.date, customer=Customer.label, company=Company.name, ) default_sort = 'date' default_direction = 'desc' is_admin = True legends = ( ('geninv-True', u"Devis concrétisés en facture"), ('signed-status-signed', u"Devis signés"), ("", u"Devis en cours"), ("signed-status-aborted", u"Devis sans suite"), ) def query(self): query = self.request.dbsession.query( distinct(Estimation.id), Estimation, ) query = query.outerjoin(Task.company) query = query.outerjoin(Task.customer) query = query.options( contains_eager(Task.customer).load_only( Customer.id, Customer.label, )) query = query.options( contains_eager(Task.company).load_only(Company.id, Company.name)) query = query.options( load_only( "name", "internal_number", "status", "signed_status", "geninv", "date", "description", "ht", "tva", "ttc", )) return query def filter_date(self, query, appstruct): period = appstruct.get('period', {}) if period.get('start') not in (colander.null, None): logger.debug(" + Filtering by date : %s" % period) start = period.get('start') end = period.get('end') if end not in (None, colander.null): end = datetime.date.today() query = query.filter(Task.date.between(start, end)) else: year = appstruct.get('year') if year is not None: query = query.filter(extract('year', Estimation.date) == year) return query def filter_ttc(self, query, appstruct): ttc = appstruct.get('ttc', {}) if ttc.get('start') not in (None, colander.null): logger.info(u" + Filtering by ttc amount : %s" % ttc) start = ttc.get('start') end = ttc.get('end') if end in (None, colander.null): query = query.filter(Estimation.ttc >= start) else: query = query.filter(Estimation.ttc.between(start, end)) return query def _get_company_id(self, appstruct): return appstruct.get('company_id') def filter_company(self, query, appstruct): company_id = self._get_company_id(appstruct) if company_id not in (None, colander.null): logger.info(" + Filtering on the company id : %s" % company_id) query = query.filter(Task.company_id == company_id) return query def filter_customer(self, query, appstruct): """ filter estimations by customer """ customer_id = appstruct.get('customer_id') if customer_id not in (None, colander.null): logger.info(" + Filtering on the customer id : %s" % customer_id) query = query.filter(Estimation.customer_id == customer_id) return query def filter_signed_status(self, query, appstruct): """ Filter estimations by signed status """ status = appstruct['signed_status'] logger.info(" + Signed status filtering : %s" % status) if status == 'geninv': query = query.filter(Estimation.geninv == True) elif status != 'all': query = query.filter(Estimation.signed_status == status) return query def filter_status(self, query, appstruct): """ Filter the estimations by status """ query = query.filter(Estimation.status == 'valid') return query def more_template_vars(self, response_dict): """ Add template vars to the response dict :param obj result: A Sqla Query :returns: vars to pass to the template :rtype: dict """ ret_dict = BaseListView.more_template_vars(self, response_dict) records = response_dict['records'] # Les records sont des 2-uples (identifiant, instance) ret_dict['totalht'] = sum(r[1].ht for r in records) ret_dict['totaltva'] = sum(r[1].tva for r in records) ret_dict['totalttc'] = sum(r[1].ttc for r in records) return ret_dict
class GlobalEstimationList(BaseListView): title = u"Devis de la CAE" add_template_vars = ( u'title', 'is_admin', ) schema = get_list_schema(is_global=True) sort_columns = dict( date=Estimation.date, customer=Customer.name, company=Company.name, ) default_sort = 'date' default_direction = 'desc' is_admin = True def query(self): query = self.request.dbsession.query( distinct(Estimation.id), Estimation.name, Estimation.internal_number, Estimation.status, Estimation.signed_status, Estimation.geninv, Estimation.date, Estimation.description, Estimation.ht, Estimation.tva, Estimation.ttc, Customer.id, Customer.name, Company.id, Company.name) query = query.outerjoin(Task.company) query = query.outerjoin(Task.customer) query = query.filter(Estimation.status == 'valid') return query def filter_date(self, query, appstruct): period = appstruct.get('period', {}) if period.get('start') not in (colander.null, None): logger.debug(" + Filtering by date : %s" % period) start = period.get('start') end = period.get('end') if end not in (None, colander.null): end = datetime.date.today() query = query.filter(Task.date.between(start, end)) else: year = appstruct.get('year') if year is not None: query = query.filter(extract('year', Estimation.date) == year) return query def filter_ttc(self, query, appstruct): ttc = appstruct.get('ttc', {}) if ttc.get('start') not in (None, colander.null): logger.info(u" + Filtering by ttc amount : %s" % ttc) start = ttc.get('start') end = ttc.get('end') if end in (None, colander.null): query = query.filter(Estimation.ttc >= start) else: query = query.filter(Estimation.ttc.between(start, end)) return query def _get_company_id(self, appstruct): return appstruct.get('company_id') def filter_company(self, query, appstruct): company_id = self._get_company_id(appstruct) if company_id not in (None, colander.null): logger.info(" + Filtering on the company id : %s" % company_id) query = query.filter(Task.company_id == company_id) return query def filter_customer(self, query, appstruct): """ filter estimations by customer """ customer_id = appstruct.get('customer_id') if customer_id not in (None, colander.null): logger.info(" + Filtering on the customer id : %s" % customer_id) query = query.filter(Estimation.customer_id == customer_id) return query def filter_status(self, query, appstruct): """ Filter estimations by status """ status = appstruct['status'] logger.info(" + Status filtering : %s" % status) if status == 'geninv': query = query.filter(Estimation.geninv == True) elif status != 'all': query = query.filter(Estimation.signed_status == status) return query def more_template_vars(self, response_dict): """ Add template vars to the response dict :param obj result: A Sqla Query :returns: vars to pass to the template :rtype: dict """ ret_dict = BaseListView.more_template_vars(self, response_dict) records = response_dict['records'] ret_dict['totalht'] = sum(r[8] for r in records) ret_dict['totaltva'] = sum(r[9] for r in records) ret_dict['totalttc'] = sum(r[10] for r in records) return ret_dict