<s> from datetime import datetime <EOL> from decimal import Decimal <EOL> from casexml . apps . stock . models import StockReport , StockTransaction <EOL> from corehq . apps . commtrack . models import SupplyPointCase <EOL> from corehq . apps . locations . models import LocationType , Location , SQLLocation <EOL> from corehq . apps . products . models import SQLProduct <EOL> from corehq . apps . sms . api import send_sms_to_verified_number <EOL> from corehq . util . translation import localize <EOL> from custom . ilsgateway . models import SupplyPointStatus , ILSGatewayConfig , GroupSummary , SupplyPointStatusTypes , DeliveryGroups <EOL> from dimagi . utils . dates import get_business_day_of_month_before <EOL> from django . db . models . aggregates import Max <EOL> GROUPS = ( '<STR_LIT:A>' , '<STR_LIT:B>' , '<STR_LIT:C>' ) <EOL> def get_next_meta_url ( has_next , meta , next_url ) : <EOL> if not meta . get ( '<STR_LIT>' , False ) : <EOL> has_next = False <EOL> else : <EOL> next_url = meta [ '<STR_LIT>' ] . split ( '<STR_LIT:?>' ) [ <NUM_LIT:1> ] <EOL> return has_next , next_url <EOL> def get_current_group ( ) : <EOL> month = datetime . utcnow ( ) . month <EOL> return GROUPS [ ( month + <NUM_LIT:2> ) % <NUM_LIT:3> ] <EOL> def send_for_all_domains ( date , reminder_class , ** kwargs ) : <EOL> for domain in ILSGatewayConfig . get_all_enabled_domains ( ) : <EOL> reminder_class ( domain = domain , date = date , ** kwargs ) . send ( ) <EOL> def send_for_day ( date , cutoff , reminder_class , ** kwargs ) : <EOL> now = datetime . utcnow ( ) <EOL> date = get_business_day_of_month_before ( now . year , now . month , date ) <EOL> cutoff = get_business_day_of_month_before ( now . year , now . month , cutoff ) <EOL> if now . day == date . day : <EOL> send_for_all_domains ( cutoff , reminder_class , ** kwargs ) <EOL> def supply_points_with_latest_status_by_datespan ( locations , status_type , status_value , datespan ) : <EOL> """<STR_LIT>""" <EOL> ids = [ loc . location_id for loc in locations ] <EOL> inner = SupplyPointStatus . objects . filter ( location_id__in = ids , <EOL> status_type = status_type , <EOL> status_date__gte = datespan . startdate , <EOL> status_date__lte = datespan . enddate ) . annotate ( pk = Max ( '<STR_LIT:id>' ) ) <EOL> ids = SupplyPointStatus . objects . filter ( <EOL> id__in = inner . values ( '<STR_LIT>' ) . query , <EOL> status_type = status_type , <EOL> status_value = status_value ) . distinct ( ) . values_list ( "<STR_LIT>" , flat = True ) <EOL> return [ SupplyPointCase . get ( id ) for id in ids ] <EOL> def send_translated_message ( user , message , ** kwargs ) : <EOL> verified_number = user . get_verified_number ( ) <EOL> if not verified_number : <EOL> return False <EOL> with localize ( user . get_language_code ( ) ) : <EOL> send_sms_to_verified_number ( verified_number , message % kwargs ) <EOL> return True <EOL> def make_loc ( code , name , domain , type , metadata = None , parent = None ) : <EOL> name = name or code <EOL> LocationType . objects . get ( domain = domain , name = type ) <EOL> loc = Location ( site_code = code , name = name , domain = domain , location_type = type , parent = parent ) <EOL> loc . metadata = metadata or { } <EOL> loc . save ( ) <EOL> return loc <EOL> def create_stock_report ( location , products_quantities , date = datetime . utcnow ( ) ) : <EOL> sql_location = location . sql_location <EOL> report = StockReport . objects . create ( <EOL> form_id = '<STR_LIT>' , <EOL> domain = sql_location . domain , <EOL> type = '<STR_LIT>' , <EOL> date = date <EOL> ) <EOL> for product_code , quantity in products_quantities . iteritems ( ) : <EOL> StockTransaction ( <EOL> stock_on_hand = Decimal ( quantity ) , <EOL> report = report , <EOL> type = '<STR_LIT>' , <EOL> section_id = '<STR_LIT>' , <EOL> case_id = sql_location . supply_point_id , <EOL> product_id = SQLProduct . objects . get ( domain = sql_location . domain , code = product_code ) . product_id <EOL> ) . save ( ) <EOL> def last_location_group ( location ) : <EOL> try : <EOL> gs = GroupSummary . objects . filter ( <EOL> total = <NUM_LIT:1> , org_summary__location_id = location . get_id , title = SupplyPointStatusTypes . DELIVERY_FACILITY <EOL> ) . latest ( '<STR_LIT>' ) <EOL> except GroupSummary . DoesNotExist : <EOL> return <EOL> delivery_groups = DeliveryGroups ( gs . org_summary . date . month ) <EOL> return delivery_groups . current_delivering_group ( ) <EOL> def get_sql_locations_by_domain_and_group ( domain , group ) : <EOL> for sql_location in SQLLocation . objects . filter ( domain = domain ) : <EOL> if sql_location . metadata . get ( '<STR_LIT>' ) == group : <EOL> yield sql_location </s>
