def _create_commtrack_config_if_needed(domain): if CommtrackConfig.for_domain(domain): return CommtrackConfig( domain=domain, actions=[ CommtrackActionConfig( action='receipts', keyword='r', caption='Received', ), CommtrackActionConfig( action='consumption', keyword='c', caption='Consumed', ), CommtrackActionConfig( action='consumption', subaction='loss', keyword='l', caption='Losses', ), CommtrackActionConfig( action='stockonhand', keyword='soh', caption='Stock on hand', ), CommtrackActionConfig( action='stockout', keyword='so', caption='Stock-out', ), ], ).save()
def get_default_requisition_config(): return CommtrackRequisitionConfig( enabled=True, actions=[ CommtrackActionConfig( action=RequisitionActions.REQUEST, keyword='req', caption='Request', ), # TODO not tested yet, so not included # CommtrackActionConfig( # action=RequisitionActions.APPROVAL, # keyword='approve', # caption='Approved', # ), CommtrackActionConfig( action=RequisitionActions.FULFILL, keyword='fulfill', caption='Fulfilled', ), CommtrackActionConfig( action=RequisitionActions.RECEIPTS, keyword='rec', caption='Requisition Receipts', ), ], )
def bootstrap_commtrack_settings_if_necessary(domain, requisitions_enabled=False): if not(domain and domain.commtrack_enabled and not CommtrackConfig.for_domain(domain.name)): return c = CommtrackConfig( domain=domain.name, multiaction_enabled=True, multiaction_keyword='report', actions=[ CommtrackActionConfig( action='receipts', keyword='r', caption='Received', ), CommtrackActionConfig( action='consumption', keyword='c', caption='Consumed', ), CommtrackActionConfig( action='consumption', subaction='loss', keyword='l', caption='Losses', ), CommtrackActionConfig( action='stockonhand', keyword='soh', caption='Stock on hand', ), CommtrackActionConfig( action='stockout', keyword='so', caption='Stock-out', ), ], location_types=[ LocationType(name='state', allowed_parents=[''], administrative=True), LocationType(name='district', allowed_parents=['state'], administrative=True), LocationType(name='block', allowed_parents=['district'], administrative=True), LocationType(name='village', allowed_parents=['block'], administrative=True), LocationType(name='outlet', allowed_parents=['block', 'village']), ], supply_point_types=[], ) if requisitions_enabled: c.requisition_config = get_default_requisition_config() c.save() program = make_program(domain.name, 'Default', 'def') make_product(domain.name, 'Sample Product 1', 'pp', program.get_id) make_product(domain.name, 'Sample Product 2', 'pq', program.get_id) make_product(domain.name, 'Sample Product 3', 'pr', program.get_id) return c
def make_psi_config(domain): c = CommtrackConfig(domain=domain, multiaction_enabled=True, multiaction_keyword='s', actions=[ CommtrackActionConfig(action_type='stockedoutfor', keyword='d', caption='Stock-out Days'), CommtrackActionConfig(action_type='receipts', keyword='r', caption='Other Receipts'), CommtrackActionConfig(action_type='stockonhand', keyword='b', caption='Balance'), CommtrackActionConfig(action_type='receipts', name='sales', keyword='p', caption='Placements'), ], supply_point_types=[ SupplyPointType(name='CHC', categories=['Public']), SupplyPointType(name='PHC', categories=['Public']), SupplyPointType(name='SC', categories=['Public']), SupplyPointType(name='MBBS', categories=['Private']), SupplyPointType(name='Pediatrician', categories=['Private']), SupplyPointType(name='AYUSH', categories=['Private']), SupplyPointType(name='Medical Store / Chemist', categories=['Traditional']), SupplyPointType(name='RP', categories=['Traditional']), SupplyPointType(name='Asha', categories=['Frontline Workers']), SupplyPointType( name='AWW', categories=['Public', 'Frontline Workers']), SupplyPointType(name='NGO', categories=['Non-traditional']), SupplyPointType(name='CBO', categories=['Non-traditional']), SupplyPointType(name='SHG', categories=['Non-traditional']), SupplyPointType(name='Pan Store', categories=['Traditional']), SupplyPointType(name='General Store', categories=['Traditional']), ]) c.save() return c
def _create_unsynced_couch(self): """ Create a CommtrackConfig matching the one created by _create_unsynced_sql """ couch = CommtrackConfig( domain='my_project', use_auto_emergency_levels=False, sync_consumption_fixtures=False, use_auto_consumption=False, individual_consumption_defaults=True, ota_restore_config=StockRestoreConfig( section_to_consumption_types={'s1': 'c1'}, force_consumption_case_types=['type1'], use_dynamic_product_list=True, ), alert_config=AlertConfig( stock_out_facilities=True, stock_out_commodities=True, stock_out_rates=True, non_report=True, ), actions=[ CommtrackActionConfig( action='receipts', subaction='sub-receipts', _keyword='one', caption='first action', ), CommtrackActionConfig( action='receipts', subaction='sub-receipts', _keyword='two', caption='second action', ), ], consumption_config=ConsumptionConfig( min_transactions=1, min_window=2, optimal_window=3, use_supply_point_type_default_consumption=True, exclude_invalid_periods=False, ), stock_levels_config=StockLevelsConfig( emergency_level=0.5, understock_threshold=1.5, overstock_threshold=3, ) ) couch.save(sync_to_sql=False) return couch
def mk_action(action): return CommtrackActionConfig(**{ 'action': action['type'], 'subaction': action['caption'], 'keyword': action['keyword'], 'caption': action['caption'], })
def incr_actions(self): """action types that increment/decrement stock""" actions = [action_config for action_config in self.config.actions if action_config.action_type in ('receipts', 'consumption')] if not any(a.action_type == 'consumption' for a in actions): # add implicitly calculated consumption -- TODO find a way to refer to this more explicitly once we track different kinds of consumption (losses, etc.) actions.append(CommtrackActionConfig(action_type='consumption', caption='Consumption')) return actions
def prepare_domain(domain_name): from corehq.apps.commtrack.tests import bootstrap_domain domain = bootstrap_domain(domain_name) previous = None for name, administrative in [("MOHSW", True), ("MSDZONE", True), ("REGION", True), ("DISTRICT", True), ("FACILITY", False)]: previous, _ = LocationType.objects.get_or_create( domain=domain_name, name=name, parent_type=previous, administrative=administrative, ) generator.instantiate_accounting_for_tests() account = BillingAccount.get_or_create_account_by_domain( domain.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan_by_domain( domain, edition=SoftwarePlanEdition.ADVANCED) commtrack = domain.commtrack_settings commtrack.actions.append( CommtrackActionConfig(action='receipts', keyword='delivered', caption='Delivered')) commtrack.save() subscription = Subscription.new_domain_subscription( account, domain.name, plan) subscription.is_active = True subscription.save() ils_config = ILSGatewayConfig(enabled=True, domain=domain.name) ils_config.save() return domain
def prepare_commtrack_config(self): """ Bootstraps the domain-level metadata according to the static config. - Sets the proper location types hierarchy on the domain object. - Sets a keyword handler for reporting receipts """ for location_type in LocationType.objects.by_domain(self.domain): location_type.delete() previous = None for loc_type in LOCATION_TYPES: previous, _ = LocationType.objects.get_or_create( domain=self.domain, name=loc_type, parent_type=previous, administrative=(loc_type != 'FACILITY'), ) config = CommtrackConfig.for_domain(self.domain) config.consumption_config.exclude_invalid_periods = True actions = [action.keyword for action in config.actions] if 'delivered' not in actions: config.actions.append( CommtrackActionConfig(action='receipts', keyword='delivered', caption='Delivered')) config.save()
def action_config(self, commtrack_config): action = CommtrackActionConfig(action=self.action, subaction=self.subaction) for a in commtrack_config.all_actions: if a.name == action.name: return a return None
def mk_action(action): action['action_type'] = action['type'] del action['type'] if not action.get('name'): action['name'] = make_action_name(action['caption'], payload['actions']) return CommtrackActionConfig(**action)
def incr_actions(self): """action types that increment/decrement stock""" actions = [action_config for action_config in self.config.actions if action_config.action_type in ('receipts', 'consumption')] if not any(a.action_type == 'consumption' for a in actions): # add implicitly calculated consumption -- TODO find a way to refer to this more explicitly once we track different kinds of consumption (losses, etc.) actions.append(CommtrackActionConfig(action_type='consumption', caption='Consumption')) if is_psi_domain(self.domain): ordering = ['sales', 'receipts', 'consumption'] actions.sort(key=lambda a: (0, ordering.index(a.action_name)) if a.action_name in ordering else (1, a.action_name)) return actions
def setUpClass(cls): domain = prepare_domain(TEST_DOMAIN) p = Product(domain=domain.name, name='Jadelle', code='jd', unit='each') p.save() p2 = Product(domain=domain.name, name='Male Condom', code='mc', unit='each') p2.save() p3 = Product(domain=domain.name, name='Lofem', code='lf', unit='each') p3.save() p4 = Product(domain=domain.name, name='Ng', code='ng', unit='each') p4.save() p5 = Product(domain=domain.name, name='Micro-G', code='mg', unit='each') p5.save() loc = make_loc(code="garms", name="Test RMS", type="Regional Medical Store", domain=domain.name) test.bootstrap(TEST_BACKEND, to_console=True) cls.user1 = bootstrap_user(username='******', first_name='test1', last_name='test1', domain=domain.name, home_loc=loc) cls.user2 = bootstrap_user(username='******', domain=domain.name, home_loc=loc, first_name='test2', last_name='test2', phone_number='222222', user_data={'role': 'In Charge'}) try: XFormInstance.get(docid='test-xform') except ResourceNotFound: xform = XFormInstance(_id='test-xform') xform.save() sql_location = loc.sql_location sql_location.products = SQLProduct.objects.filter(product_id=p5.get_id) sql_location.save() config = CommtrackConfig.for_domain(domain.name) config.actions.append( CommtrackActionConfig(action='receipts', keyword='rec', caption='receipts')) config.consumption_config = ConsumptionConfig(min_transactions=0, min_window=0, optimal_window=60) config.save()
def get_default_requisition_config(): return CommtrackRequisitionConfig( enabled=True, actions=[ CommtrackActionConfig( action=RequisitionActions.REQUEST, keyword='req', caption='Request', ), CommtrackActionConfig( action=RequisitionActions.FULFILL, keyword='fulfill', caption='Fulfilled', ), CommtrackActionConfig( action=RequisitionActions.RECEIPTS, keyword='rec', caption='Requisition Receipts', ), ], )
def prepare_domain(domain_name): from corehq.apps.commtrack.tests.util import bootstrap_domain domain = bootstrap_domain(domain_name) previous = None for name, administrative in [ ("MOHSW", True), ("MSDZONE", True), ("REGION", True), ("DISTRICT", True), ("FACILITY", False) ]: previous, _ = LocationType.objects.get_or_create( domain=domain_name, name=name, parent_type=previous, administrative=administrative, ) generator.instantiate_accounting() account = BillingAccount.get_or_create_account_by_domain( domain.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan( edition=SoftwarePlanEdition.ADVANCED ) commtrack = domain.commtrack_settings commtrack.actions.append( CommtrackActionConfig(action='receipts', keyword='delivered', caption='Delivered') ) commtrack.save() subscription = Subscription.new_domain_subscription( account, domain.name, plan ) subscription.is_active = True subscription.save() ils_config = ILSGatewayConfig(enabled=True, domain=domain.name, all_stock_data=True) ils_config.save() fields_definition = CustomDataFieldsDefinition.get_or_create(domain.name, 'LocationFields') fields_definition.fields.append(CustomDataField( slug='group', label='Group', is_required=False, choices=['A', 'B', 'C'], is_multiple_choice=False )) fields_definition.save() return domain
def commtrack_settings_sync(project): locations_types = ["MOHSW", "REGION", "DISTRICT", "FACILITY"] config = CommtrackConfig.for_domain(project) config.location_types = [] for i, value in enumerate(locations_types): if not any(lt.name == value for lt in config.location_types): allowed_parents = [locations_types[i - 1]] if i > 0 else [""] config.location_types.append( LocationType(name=value, allowed_parents=allowed_parents, administrative=(value != 'FACILITY'))) actions = [action.keyword for action in config.actions] if 'delivered' not in actions: config.actions.append( CommtrackActionConfig( action='receipts', keyword='delivered', caption='Delivered') ) config.save()
def setUpClass(cls): super(EWSScriptTest, cls).setUpClass() cls.backend, cls.sms_backend_mapping = setup_default_sms_test_backend() domain = prepare_domain(TEST_DOMAIN) p = Product(domain=domain.name, name='Jadelle', code='jd', unit='each') p.save() p2 = Product(domain=domain.name, name='Male Condom', code='mc', unit='each') p2.save() p3 = Product(domain=domain.name, name='Lofem', code='lf', unit='each') p3.save() p4 = Product(domain=domain.name, name='Ng', code='ng', unit='each') p4.save() p5 = Product(domain=domain.name, name='Micro-G', code='mg', unit='each') p5.save() Product(domain=domain.name, name='Ad', code='ad', unit='each').save() Product(domain=domain.name, name='Al', code='al', unit='each').save() Product(domain=domain.name, name='Qu', code='qu', unit='each').save() Product(domain=domain.name, name='Sp', code='sp', unit='each').save() Product(domain=domain.name, name='Rd', code='rd', unit='each').save() Product(domain=domain.name, name='Ov', code='ov', unit='each').save() Product(domain=domain.name, name='Ml', code='ml', unit='each').save() national = make_loc(code='country', name='Test national', type='country', domain=domain.name) region = make_loc(code='region', name='Test region', type='region', domain=domain.name, parent=national) loc = make_loc(code="garms", name="Test RMS", type="Regional Medical Store", domain=domain.name, parent=national) loc.save() rms2 = make_loc(code="wrms", name="Test RMS 2", type="Regional Medical Store", domain=domain.name, parent=region) rms2.save() cms = make_loc(code="cms", name="Central Medical Stores", type="Central Medical Store", domain=domain.name, parent=national) cms.save() loc2 = make_loc(code="tf", name="Test Facility", type="CHPS Facility", domain=domain.name, parent=region) loc2.save() supply_point_id = loc.linked_supply_point().get_id supply_point_id2 = loc2.linked_supply_point().get_id cls.user1 = bootstrap_user(username='******', first_name='test1', last_name='test1', domain=domain.name, home_loc=loc) cls.user2 = bootstrap_user(username='******', domain=domain.name, home_loc=loc2, first_name='test2', last_name='test2', phone_number='222222', user_data={'role': ['In Charge']}) FacilityInCharge.objects.create(user_id=cls.user2.get_id, location=loc2.sql_location) cls.user3 = bootstrap_user(username='******', domain=domain.name, home_loc=loc2, first_name='test3', last_name='test3', phone_number='333333') cls.rms_user = bootstrap_user(username='******', domain=domain.name, home_loc=rms2, first_name='test4', last_name='test4', phone_number='44444') cls.cms_user = bootstrap_user(username='******', domain=domain.name, home_loc=cms, first_name='test5', last_name='test5', phone_number='55555') cls.region_user = bootstrap_user(username='******', domain=domain.name, home_loc=region, first_name='test6', last_name='test6', phone_number='66666') cls.without_location = bootstrap_user(username='******', domain=domain.name, first_name='test7', last_name='test7', phone_number='77777') try: XFormInstance.get(docid='test-xform') except ResourceNotFound: xform = XFormInstance(_id='test-xform') xform.save() sql_location = loc.sql_location sql_location.products = [] sql_location.save() sql_location = loc2.sql_location sql_location.products = [] sql_location.save() sql_location = rms2.sql_location sql_location.products = [] sql_location.save() sql_location = cms.sql_location sql_location.products = [] sql_location.save() config = CommtrackConfig.for_domain(domain.name) config.use_auto_consumption = False config.individual_consumption_defaults = True config.actions.append( CommtrackActionConfig(action='receipts', keyword='rec', caption='receipts')) config.consumption_config = ConsumptionConfig( use_supply_point_type_default_consumption=True, exclude_invalid_periods=True) config.save() set_default_consumption_for_supply_point(TEST_DOMAIN, p2.get_id, supply_point_id, 8) set_default_consumption_for_supply_point(TEST_DOMAIN, p3.get_id, supply_point_id, 5) set_default_consumption_for_supply_point(TEST_DOMAIN, p2.get_id, supply_point_id2, 10) set_default_consumption_for_supply_point(TEST_DOMAIN, p3.get_id, supply_point_id2, 10) set_default_consumption_for_supply_point(TEST_DOMAIN, p5.get_id, supply_point_id2, 10)