Пример #1
0
    def post(self, request, *args, **kwargs):
        InputStockFormSet = formset_factory(InputStockForm)
        formset = InputStockFormSet(request.POST)
        if formset.is_valid():
            try:
                sql_location = SQLLocation.objects.get(site_code=kwargs['site_code'], domain=self.domain)
            except SQLLocation.DoesNotExist:
                raise Http404()
            text = ''
            for form in formset:
                product = Product.get(docid=form.cleaned_data['product_id'])
                if form.cleaned_data['stock_on_hand'] is not None:
                    text += '{} {}.{} '.format(
                        product.code, form.cleaned_data['stock_on_hand'], form.cleaned_data['receipts'] or 0
                    )

                amount = form.cleaned_data['default_consumption']
                if amount is not None:
                    set_default_consumption_for_supply_point(
                        self.domain, product.get_id, sql_location.supply_point_id, amount
                    )
            if text:
                WebSubmissionHandler(self.request.couch_user, self.domain, Msg(text), sql_location).handle()
            url = make_url(
                StockStatus,
                self.domain,
                '?location_id=%s&filter_by_program=%s&startdate='
                '&enddate=&report_type=&filter_by_product=%s',
                (sql_location.location_id, ALL_OPTION, ALL_OPTION)
            )
            return HttpResponseRedirect(url)
        context = self.get_context_data(**kwargs)
        context['formset'] = formset
        return self.render_to_response(context)
Пример #2
0
    def post(self, request, *args, **kwargs):
        InputStockFormSet = formset_factory(InputStockForm)
        formset = InputStockFormSet(request.POST)
        if formset.is_valid():
            try:
                sql_location = SQLLocation.objects.get(site_code=kwargs['site_code'], domain=self.domain)
            except SQLLocation.DoesNotExist:
                raise Http404()
            text = ''
            for form in formset:
                product = Product.get(docid=form.cleaned_data['product_id'])
                if form.cleaned_data['stock_on_hand'] is not None:
                    text += '{} {}.{} '.format(
                        product.code, form.cleaned_data['stock_on_hand'], form.cleaned_data['receipts'] or 0
                    )

                amount = form.cleaned_data['default_consumption']
                if amount is not None:
                    set_default_consumption_for_supply_point(
                        self.domain, product.get_id, sql_location.supply_point_id, amount
                    )
            if text:
                WebSubmissionHandler(self.request.couch_user, self.domain, Msg(text), sql_location).handle()
            url = make_url(
                StockStatus,
                self.domain,
                '?location_id=%s&filter_by_program=%s&startdate='
                '&enddate=&report_type=&filter_by_product=%s',
                (sql_location.location_id, ALL_OPTION, ALL_OPTION)
            )
            return HttpResponseRedirect(url)
        context = self.get_context_data(**kwargs)
        context['formset'] = formset
        return self.render_to_response(context)
Пример #3
0
def submit_form(domain, parent, form_data, properties, existing, location_type, consumption):
    # don't save if there is nothing to save
    if no_changes_needed(domain, existing, properties, form_data, consumption):
        return {
            'id': existing._id,
            'message': 'no changes for %s %s' % (location_type, existing.name)
        }

    form_data.update(properties)

    form = make_form(domain, parent, form_data, existing)
    form.strict = False  # optimization hack to turn off strict validation
    if form.is_valid():
        loc = form.save()

        sp = SupplyPointCase.get_by_location(loc) if consumption else None

        if consumption and sp:
            for product_code, value in consumption:
                try:
                    amount = Decimal(value)

                    # only set it if there is a non-negative/non-null value
                    if amount and amount >= 0:
                        set_default_consumption_for_supply_point(
                            domain,
                            Product.get_by_code(domain, product_code)._id,
                            sp._id,
                            amount
                        )
                except (TypeError, InvalidOperation):
                    # should inform user, but failing hard due to non numbers
                    # being used on consumption is strange since the
                    # locations would be in a very inconsistent state
                    continue

        if existing:
            message = 'updated %s %s' % (location_type, loc.name)
        else:
            message = 'created %s %s' % (location_type, loc.name)

        return {
            'id': loc._id,
            'message': message
        }
    else:
        message = 'Form errors when submitting: '
        # TODO move this to LocationForm somehow
        forms = filter(None, [form, form.sub_forms.get(location_type)])
        for k, v in itertools.chain(*(f.errors.iteritems() for f in forms)):
            if k != '__all__':
                message += u'{0} {1}; {2}: {3}. '.format(
                    location_type, form_data.get('name', 'unknown'), k, v[0]
                )

        return {
            'id': None,
            'message': message
        }
Пример #4
0
def submit_form(domain, parent, form_data, properties, existing, location_type, consumption):
    # don't save if there is nothing to save
    if no_changes_needed(domain, existing, properties, form_data, consumption):
        return {
            'id': existing._id,
            'message': 'no changes for %s %s' % (location_type, existing.name)
        }

    form_data.update(properties)

    form = make_form(domain, parent, form_data, existing)
    form.strict = False  # optimization hack to turn off strict validation
    if form.is_valid():
        loc = form.save()

        sp = SupplyPointCase.get_by_location(loc) if consumption else None

        if consumption and sp:
            for product_code, value in consumption:
                try:
                    amount = Decimal(value)

                    # only set it if there is a non-negative/non-null value
                    if amount and amount >= 0:
                        set_default_consumption_for_supply_point(
                            domain,
                            Product.get_by_code(domain, product_code)._id,
                            sp._id,
                            amount
                        )
                except (TypeError, InvalidOperation):
                    # should inform user, but failing hard due to non numbers
                    # being used on consumption is strange since the
                    # locations would be in a very inconsistent state
                    continue

        if existing:
            message = 'updated %s %s' % (location_type, loc.name)
        else:
            message = 'created %s %s' % (location_type, loc.name)

        return {
            'id': loc._id,
            'message': message
        }
    else:
        message = 'Form errors when submitting: '
        # TODO move this to LocationForm somehow
        forms = filter(None, [form, form.sub_forms.get(location_type)])
        for k, v in itertools.chain(*(f.errors.iteritems() for f in forms)):
            if k != '__all__':
                message += u'{0} {1}; {2}: {3}. '.format(
                    location_type, form_data.get('name', 'unknown'), k, v[0]
                )

        return {
            'id': None,
            'message': message
        }
Пример #5
0
    def submit_form(self, parent_id, form_data, existing, location_type,
                    consumption):
        parent = SQLLocation.objects.get(
            location_id=parent_id) if parent_id else None
        location = existing or SQLLocation(domain=self.domain, parent=parent)
        form = LocationForm(location, form_data, is_new=not bool(existing))
        form.strict = False  # optimization hack to turn off strict validation
        if form.is_valid():
            # don't save if there is nothing to save
            if self.no_changes_needed(existing, form_data, consumption):
                return {
                    'id':
                    existing.location_id,
                    'message':
                    'no changes for %s %s' % (location_type, existing.name)
                }

            loc = form.save()

            sp = loc.linked_supply_point() if consumption else None

            if consumption and sp:
                for product_code, value in consumption:
                    product = self.get_product(product_code)

                    if not product:
                        # skip any consumption column that doesn't match
                        # to a real product. currently there is no easy
                        # way to alert here, though.
                        continue

                    try:
                        amount = Decimal(value)

                        # only set it if there is a non-negative/non-null value
                        if amount and amount >= 0:
                            set_default_consumption_for_supply_point(
                                self.domain, product._id, sp.case_id, amount)
                    except (TypeError, InvalidOperation):
                        # should inform user, but failing hard due to non numbers
                        # being used on consumption is strange since the
                        # locations would be in a very inconsistent state
                        continue

            if existing:
                message = 'updated %s %s' % (location_type, loc.name)
            else:
                message = 'created %s %s' % (location_type, loc.name)

            return {'id': loc.location_id, 'message': message}
        else:
            message = 'Form errors when submitting: '
            for k, v in form.errors.iteritems():
                if k != '__all__':
                    message += u'{0} {1}; {2}: {3}. '.format(
                        location_type, form_data.get('name', 'unknown'), k,
                        v[0])

            return {'id': None, 'message': message}
Пример #6
0
 def testSetForSupplyPoint(self):
     self.assertEqual(None, DefaultConsumption.get_supply_point_default(domain, product_id, supply_point_id))
     default = set_default_consumption_for_supply_point(domain, product_id, supply_point_id, 50)
     self.assertEqual(50, DefaultConsumption.get_supply_point_default(domain, product_id, supply_point_id).default_consumption)
     self.assertEqual(1, _count_consumptions())
     updated = set_default_consumption_for_supply_point(domain, product_id, supply_point_id, 40)
     self.assertEqual(default._id, updated._id)
     self.assertEqual(40, DefaultConsumption.get_supply_point_default(domain, product_id, supply_point_id).default_consumption)
     self.assertEqual(1, _count_consumptions())
Пример #7
0
 def testSetForSupplyPoint(self):
     self.assertEqual(None, DefaultConsumption.get_supply_point_default(domain, product_id, supply_point_id))
     default = set_default_consumption_for_supply_point(domain, product_id, supply_point_id, 50)
     self.assertEqual(50, DefaultConsumption.get_supply_point_default(domain, product_id, supply_point_id).default_consumption)
     self.assertEqual(1, _count_consumptions())
     updated = set_default_consumption_for_supply_point(domain, product_id, supply_point_id, 40)
     self.assertEqual(default._id, updated._id)
     self.assertEqual(40, DefaultConsumption.get_supply_point_default(domain, product_id, supply_point_id).default_consumption)
     self.assertEqual(1, _count_consumptions())
Пример #8
0
def submit_form(domain, parent, form_data, properties, existing, location_type, consumption):
    # don't save if there is nothing to save
    if no_changes_needed(domain, existing, properties, form_data, consumption):
        return {
            'id': existing._id,
            'message': 'no changes for %s %s' % (location_type, existing.name)
        }

    form_data.update(properties)

    form = make_form(domain, parent, form_data, existing)
    form.strict = False  # optimization hack to turn off strict validation
    if form.is_valid():
        loc = form.save()

        sp = SupplyPointCase.get_by_location(loc) if consumption else None

        if consumption and sp:
            for product_code, amount in consumption:
                set_default_consumption_for_supply_point(
                    domain,
                    Product.get_by_code(domain, product_code)._id,
                    sp._id,
                    amount
                )
        if existing:
            message = 'updated %s %s' % (location_type, loc.name)
        else:
            message = 'created %s %s' % (location_type, loc.name)

        return {
            'id': loc._id,
            'message': message
        }
    else:
        message = 'Form errors when submitting: '
        # TODO move this to LocationForm somehow
        forms = filter(None, [form, form.sub_forms.get(location_type)])
        for k, v in itertools.chain(*(f.errors.iteritems() for f in forms)):
            if k != '__all__':
                message += u'{0} {1}; {2}: {3}. '.format(
                    location_type, form_data.get('name', 'unknown'), k, v[0]
                )

        return {
            'id': None,
            'message': message
        }
Пример #9
0
    def setUpClass(cls):
        super(TestStockOut, cls).setUpClass()
        cls.facility2 = make_loc(code="loc2", name="Test Facility 2", type="FACILITY",
                                 domain=TEST_DOMAIN, parent=cls.district)
        cls.user2 = bootstrap_user(
            cls.facility2, username='******', domain=TEST_DOMAIN, home_loc='loc2', phone_number='5551235',
            first_name='test', last_name='Test'
        )
        SLABConfig.objects.create(
            is_pilot=True,
            sql_location=cls.facility.sql_location
        )

        slab_config = SLABConfig.objects.create(
            is_pilot=True,
            sql_location=cls.facility2.sql_location
        )
        slab_config.closest_supply_points.add(cls.facility.sql_location)
        slab_config.save()

        config = CommtrackConfig.for_domain(TEST_DOMAIN)
        config.use_auto_consumption = False
        config.individual_consumption_defaults = True
        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, cls.id.get_id, cls.facility_sp_id, 100)
        set_default_consumption_for_supply_point(TEST_DOMAIN, cls.dp.get_id, cls.facility_sp_id, 100)
        set_default_consumption_for_supply_point(TEST_DOMAIN, cls.ip.get_id, cls.facility_sp_id, 100)
Пример #10
0
    def setUpClass(cls):
        super(TestStockOut, cls).setUpClass()
        cls.facility2 = make_loc(code="loc2", name="Test Facility 2", type="FACILITY",
                                 domain=TEST_DOMAIN, parent=cls.district)
        cls.user2 = bootstrap_user(
            cls.facility2, username='******', domain=TEST_DOMAIN, home_loc='loc2', phone_number='5551235',
            first_name='test', last_name='Test'
        )
        SLABConfig.objects.create(
            is_pilot=True,
            sql_location=cls.facility.sql_location
        )

        slab_config = SLABConfig.objects.create(
            is_pilot=True,
            sql_location=cls.facility2.sql_location
        )
        slab_config.closest_supply_points.add(cls.facility.sql_location)
        slab_config.save()

        config = CommtrackConfig.for_domain(TEST_DOMAIN)
        config.use_auto_consumption = False
        config.individual_consumption_defaults = True
        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, cls.id.get_id, cls.facility_sp_id, 100)
        set_default_consumption_for_supply_point(TEST_DOMAIN, cls.dp.get_id, cls.facility_sp_id, 100)
        set_default_consumption_for_supply_point(TEST_DOMAIN, cls.ip.get_id, cls.facility_sp_id, 100)
Пример #11
0
 def setUpClass(cls):
     super(SOHSLABTest, cls).setUpClass()
     SLABConfig.objects.create(
         is_pilot=True,
         sql_location=cls.facility.sql_location
     )
     config = CommtrackConfig.for_domain(TEST_DOMAIN)
     config.use_auto_consumption = False
     config.individual_consumption_defaults = True
     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, cls.id.get_id, cls.facility_sp_id, 100)
     set_default_consumption_for_supply_point(TEST_DOMAIN, cls.dp.get_id, cls.facility_sp_id, 100)
     set_default_consumption_for_supply_point(TEST_DOMAIN, cls.ip.get_id, cls.facility_sp_id, 100)
Пример #12
0
 def setUpClass(cls):
     super(SOHSLABTest, cls).setUpClass()
     SLABConfig.objects.create(is_pilot=True,
                               sql_location=cls.facility.sql_location)
     config = CommtrackConfig.for_domain(TEST_DOMAIN)
     config.use_auto_consumption = False
     config.individual_consumption_defaults = True
     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, cls.id.get_id,
                                              cls.facility_sp_id, 100)
     set_default_consumption_for_supply_point(TEST_DOMAIN, cls.dp.get_id,
                                              cls.facility_sp_id, 100)
     set_default_consumption_for_supply_point(TEST_DOMAIN, cls.ip.get_id,
                                              cls.facility_sp_id, 100)
Пример #13
0
    def submit_form(self, parent, form_data, existing, location_type, consumption):
        location = existing or Location(domain=self.domain, parent=parent)
        form = LocationForm(location, form_data)
        form.strict = False  # optimization hack to turn off strict validation
        if form.is_valid():
            # don't save if there is nothing to save
            if self.no_changes_needed(existing, form_data, consumption):
                return {
                    'id': existing._id,
                    'message': 'no changes for %s %s' % (location_type, existing.name)
                }

            loc = form.save()

            sp = SupplyPointCase.get_by_location(loc) if consumption else None

            if consumption and sp:
                for product_code, value in consumption:
                    product = self.get_product(product_code)

                    if not product:
                        # skip any consumption column that doesn't match
                        # to a real product. currently there is no easy
                        # way to alert here, though.
                        continue

                    try:
                        amount = Decimal(value)

                        # only set it if there is a non-negative/non-null value
                        if amount and amount >= 0:
                            set_default_consumption_for_supply_point(
                                self.domain,
                                product._id,
                                sp._id,
                                amount
                            )
                    except (TypeError, InvalidOperation):
                        # should inform user, but failing hard due to non numbers
                        # being used on consumption is strange since the
                        # locations would be in a very inconsistent state
                        continue

            if existing:
                message = 'updated %s %s' % (location_type, loc.name)
            else:
                message = 'created %s %s' % (location_type, loc.name)

            return {
                'id': loc._id,
                'message': message
            }
        else:
            message = 'Form errors when submitting: '
            for k, v in form.errors.iteritems():
                if k != '__all__':
                    message += u'{0} {1}; {2}: {3}. '.format(
                        location_type, form_data.get('name', 'unknown'), k, v[0]
                    )

            return {
                'id': None,
                'message': message
            }
Пример #14
0
    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)
Пример #15
0
    def setUpClass(cls):
        domain = prepare_domain(TEST_DOMAIN)
        cls.sms_backend_mapping, cls.backend = create_backend()

        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)
        SupplyPointCase.create_from_location(TEST_DOMAIN, loc)
        loc.save()

        rms2 = make_loc(code="wrms", name="Test RMS 2", type="Regional Medical Store", domain=domain.name,
                        parent=region)
        SupplyPointCase.create_from_location(TEST_DOMAIN, rms2)
        rms2.save()

        cms = make_loc(code="cms", name="Central Medical Stores", type="Central Medical Store",
                       domain=domain.name, parent=national)
        SupplyPointCase.create_from_location(TEST_DOMAIN, cms)
        cms.save()

        loc2 = make_loc(code="tf", name="Test Facility", type="CHPS Facility", domain=domain.name, parent=region)
        SupplyPointCase.create_from_location(TEST_DOMAIN, loc2)
        loc2.save()

        supply_point_id = loc.linked_supply_point().get_id
        supply_point_id2 = loc2.linked_supply_point().get_id

        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=loc2,
                                   first_name='test2', last_name='test2',
                                   phone_number='222222', user_data={'role': ['In Charge']})
        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)
Пример #16
0
    def setUpClass(cls):
        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)