def stock_category(self): return state_stock_category(self)
def aggregated_data(self, stock_states): def _convert_to_daily(consumption): return consumption / 30 if consumption is not None else None if self._include_advanced_data(): product_aggregation = {} for state in stock_states: if state.product_id in product_aggregation: product = product_aggregation[state.product_id] product['current_stock'] = format_decimal( product['current_stock'] + state.stock_on_hand ) consumption = state.get_monthly_consumption() if product['consumption'] is None: product['consumption'] = consumption elif consumption is not None: product['consumption'] += consumption product['count'] += 1 if state.sql_location is not None: location_type = state.sql_location.location_type product['category'] = stock_category( product['current_stock'], _convert_to_daily(product['consumption']), location_type.understock_threshold, location_type.overstock_threshold, ) else: product['category'] = 'nodata' product['months_remaining'] = months_of_stock_remaining( product['current_stock'], _convert_to_daily(product['consumption']) ) else: product = Product.get(state.product_id) consumption = state.get_monthly_consumption() product_aggregation[state.product_id] = { 'product_id': product._id, 'location_id': None, 'product_name': product.name, 'location_lineage': None, 'resupply_quantity_needed': None, 'current_stock': format_decimal(state.stock_on_hand), 'count': 1, 'consumption': consumption, 'category': state_stock_category(state), 'months_remaining': months_of_stock_remaining( state.stock_on_hand, _convert_to_daily(consumption) ) } return product_aggregation.values() else: # If we don't need advanced data, we can # just do some orm magic. # # Note: this leaves out some harder to get quickly # values like location_id, but shouldn't be needed # unless we expand what uses this. aggregated_states = stock_states.values_list( 'sql_product__name', 'sql_product__product_id', ).annotate(stock_on_hand=Sum('stock_on_hand')) result = [] for ag in aggregated_states: result.append({ 'product_name': ag[0], 'product_id': ag[1], 'current_stock': format_decimal(ag[2]) }) return result
def aggregated_data(self, stock_states): def _convert_to_daily(consumption): return consumption / 30 if consumption is not None else None if self._include_advanced_data(): product_aggregation = {} for state in stock_states: if state.product_id in product_aggregation: product = product_aggregation[state.product_id] product['current_stock'] = format_decimal( product['current_stock'] + state.stock_on_hand) consumption = state.get_monthly_consumption() if product['consumption'] is None: product['consumption'] = consumption elif consumption is not None: product['consumption'] += consumption product['count'] += 1 if state.sql_location is not None: location_type = state.sql_location.location_type product['category'] = stock_category( product['current_stock'], _convert_to_daily(product['consumption']), location_type.understock_threshold, location_type.overstock_threshold, ) else: product['category'] = 'nodata' product['months_remaining'] = months_of_stock_remaining( product['current_stock'], _convert_to_daily(product['consumption'])) else: product = Product.get(state.product_id) consumption = state.get_monthly_consumption() product_aggregation[state.product_id] = { 'product_id': product._id, 'location_id': None, 'product_name': product.name, 'location_lineage': None, 'resupply_quantity_needed': None, 'current_stock': format_decimal(state.stock_on_hand), 'count': 1, 'consumption': consumption, 'category': state_stock_category(state), 'months_remaining': months_of_stock_remaining( state.stock_on_hand, _convert_to_daily(consumption)) } return product_aggregation.values() else: # If we don't need advanced data, we can # just do some orm magic. # # Note: this leaves out some harder to get quickly # values like location_id, but shouldn't be needed # unless we expand what uses this. aggregated_states = stock_states.values_list( 'sql_product__name', 'sql_product__product_id', ).annotate(stock_on_hand=Sum('stock_on_hand')) result = [] for ag in aggregated_states: result.append({ 'product_name': ag[0], 'product_id': ag[1], 'current_stock': format_decimal(ag[2]) }) return result