#NEWLINE# @ property #NEWLINE# def chart_data( self ) : #NEWLINE# #INDENT# def calculate_weeks_remaining( state , daily_consumption , date ) : #NEWLINE# #INDENT# if ( not daily_consumption ) : #NEWLINE# #INDENT# return 0 #NEWLINE# #UNINDENT# consumption = round ( ( float ( daily_consumption ) * 30.0 ) ) #NEWLINE# quantity = ( float ( state . stock_on_hand ) - ( int ( ( ( date - state . report . date ) . days / 7.0 ) ) * consumption ) ) #NEWLINE# if ( consumption and ( consumption > 0 ) and ( quantity > 0 ) ) : #NEWLINE# #INDENT# return ( quantity / consumption ) #NEWLINE# #UNINDENT# return 0 #NEWLINE# #UNINDENT# enddate = self . config [ 'enddate' ] #NEWLINE# startdate = ( self . config [ 'startdate' ] if ( 'custom_date' in self . config ) else ( enddate - timedelta ( days = 30 ) ) ) #NEWLINE# loc = SQLLocation . objects . get ( location_id = self . config [ 'location_id' ] ) #NEWLINE# stoke_states = StockState . objects . filter ( case_id = loc . supply_point_id , section_id = STOCK_SECTION_TYPE , sql_product__in = loc . products ) #NEWLINE# consumptions = { ss . product_id : ss . get_daily_consumption ( ) for ss in stoke_states } #NEWLINE# st = StockTransaction . objects . filter ( case_id = loc . supply_point_id , sql_product__in = loc . products , type = 'stockonhand' , report__date__lte = enddate ) . select_related ( 'report' , 'sql_product' ) . order_by ( 'report__date' ) #NEWLINE# rows = OrderedDict ( ) #NEWLINE# weeks = ceil ( ( ( enddate - startdate ) . days / 7.0 ) ) #NEWLINE# for state in st : #NEWLINE# #INDENT# product_name = '{0} ({1})' . format ( state . sql_product . name , state . sql_product . code ) #NEWLINE# if ( product_name not in rows ) : #NEWLINE# #INDENT# rows [ product_name ] = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# for i in range ( 1 , int ( ( weeks + 1 ) ) ) : #NEWLINE# #INDENT# date = ( startdate + timedelta ( weeks = i ) ) #NEWLINE# if ( state . report . date < self ) : #NEWLINE# #INDENT# rows [ product_name ] [ i ] = calculate_weeks_remaining ( state , consumptions . get ( state . product_id , None ) , date ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# for ( k , v ) in rows . iteritems ( ) : #NEWLINE# #INDENT# rows [ k ] = [ { #NEWLINE# #INDENT# 'x' : key , #NEWLINE# 'y' : value , #NEWLINE# #UNINDENT# } for ( key , value ) in v . iteritems ( ) ] #NEWLINE# #UNINDENT# rows [ 'Understock' ] = [ ] #NEWLINE# rows [ 'Overstock' ] = [ ] #NEWLINE# for i in range ( 1 , int ( ( weeks + 1 ) ) ) : #NEWLINE# #INDENT# rows [ 'Understock' ] . append ( { #NEWLINE# #INDENT# 'x' : i , #NEWLINE# 'y' : float ( loc . location_type . understock_threshold ) , #NEWLINE# #UNINDENT# } ) #NEWLINE# rows [ 'Overstock' ] . append ( { #NEWLINE# #INDENT# 'x' : i , #NEWLINE# 'y' : float ( loc . location_type . overstock_threshold ) , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# return rows