示例#1
0
    def test_force_consumption(self):
        self.ct_settings.sync_consumption_fixtures = True
        self.ct_settings.sqlconsumptionconfig = SQLConsumptionConfig(
            min_transactions=0,
            min_window=0,
            optimal_window=60,
        )
        self.ct_settings.sqlstockrestoreconfig = SQLStockRestoreConfig(
            section_to_consumption_types={'stock': 'consumption'}, )
        set_default_monthly_consumption_for_domain(self.domain.name, 5)
        self._save_settings_and_clear_cache()

        balance_blocks = _get_ota_balance_blocks(self.domain, self.user)
        self.assertEqual(0, len(balance_blocks))

        self.ct_settings.sqlstockrestoreconfig.force_consumption_case_types = [
            const.SUPPLY_POINT_CASE_TYPE
        ]
        self._save_settings_and_clear_cache()

        balance_blocks = _get_ota_balance_blocks(self.domain, self.user)
        # with no data, there should be no consumption block
        self.assertEqual(0, len(balance_blocks))

        self.ct_settings.sqlstockrestoreconfig.use_dynamic_product_list = True
        self._save_settings_and_clear_cache()

        balance_blocks = _get_ota_balance_blocks(self.domain, self.user)
        self.assertEqual(1, len(balance_blocks))
        [balance_block] = balance_blocks
        element = etree.fromstring(balance_block)
        self.assertEqual(3, len([child for child in element]))
示例#2
0
    def test_ota_consumption(self):
        self.ct_settings.sync_consumption_fixtures = True
        self.ct_settings.sqlconsumptionconfig = SQLConsumptionConfig(
            min_transactions=0,
            min_window=0,
            optimal_window=60,
        )
        self.ct_settings.sqlstockrestoreconfig = SQLStockRestoreConfig(
            section_to_consumption_types={'stock': 'consumption'})
        set_default_monthly_consumption_for_domain(self.domain.name,
                                                   5 * DAYS_IN_MONTH)
        self._save_settings_and_clear_cache()

        amounts = [
            SohReport(section_id='stock', product_id=p._id, amount=i * 10)
            for i, p in enumerate(self.products)
        ]
        report_date = _report_soh(amounts, self.sp.case_id, self.domain.name)
        balance_blocks = _get_ota_balance_blocks(self.domain, self.user)
        self.assertEqual(2, len(balance_blocks))
        stock_block, consumption_block = balance_blocks
        check_xml_line_by_line(
            self,
            balance_ota_block(
                self.sp,
                'stock',
                amounts,
                datestring=report_date,
            ),
            stock_block,
        )
        check_xml_line_by_line(
            self,
            balance_ota_block(
                self.sp,
                'consumption',
                [
                    SohReport(section_id='', product_id=p._id, amount=150)
                    for p in self.products
                ],
                datestring=report_date,
            ),
            consumption_block,
        )
示例#3
0
def _create_commtrack_config_if_needed(domain):
    if SQLCommtrackConfig.for_domain(domain):
        return

    config = SQLCommtrackConfig(domain=domain)
    config.save(
        sync_to_couch=False)  # must be saved before submodels can be saved

    SQLAlertConfig(commtrack_config=config).save()
    SQLConsumptionConfig(commtrack_config=config).save()
    SQLStockLevelsConfig(commtrack_config=config).save()
    SQLStockRestoreConfig(commtrack_config=config).save()
    config.set_actions([
        SQLActionConfig(
            action='receipts',
            keyword='r',
            caption='Received',
        ),
        SQLActionConfig(
            action='consumption',
            keyword='c',
            caption='Consumed',
        ),
        SQLActionConfig(
            action='consumption',
            subaction='loss',
            keyword='l',
            caption='Losses',
        ),
        SQLActionConfig(
            action='stockonhand',
            keyword='soh',
            caption='Stock on hand',
        ),
        SQLActionConfig(
            action='stockout',
            keyword='so',
            caption='Stock-out',
        ),
    ])
    config.save()  # save actions, and sync couch
示例#4
0
    def testOTASettings(self):
        ct_settings = SQLCommtrackConfig.for_domain(self.domain)
        ct_settings.sqlconsumptionconfig = SQLConsumptionConfig(
            min_transactions=10,
            min_window=20,
            optimal_window=60,
        )
        ct_settings.sqlstockrestoreconfig = SQLStockRestoreConfig(
            section_to_consumption_types={'stock': 'consumption'}, )
        set_default_monthly_consumption_for_domain(self.domain,
                                                   5 * DAYS_IN_MONTH)
        restore_settings = ct_settings.get_ota_restore_settings()
        self.assertEqual(1, len(restore_settings.section_to_consumption_types))
        self.assertEqual(
            'consumption',
            restore_settings.section_to_consumption_types['stock'])
        self.assertEqual(10, restore_settings.consumption_config.min_periods)
        self.assertEqual(20, restore_settings.consumption_config.min_window)
        self.assertEqual(60, restore_settings.consumption_config.max_window)
        self.assertEqual(
            150,
            restore_settings.consumption_config.
            default_monthly_consumption_function('foo', 'bar'))
        self.assertFalse(
            restore_settings.force_consumption_case_filter(
                CommCareCase(type='force-type')))
        self.assertEqual(0, len(restore_settings.default_product_list))

        ct_settings.sqlstockrestoreconfig.force_consumption_case_types = [
            'force-type'
        ]
        ct_settings.sqlstockrestoreconfig.use_dynamic_product_list = True
        restore_settings = ct_settings.get_ota_restore_settings()
        self.assertTrue(
            restore_settings.force_consumption_case_filter(
                CommCareCase(type='force-type')))
        self.assertEqual(3, len(restore_settings.default_product_list))
示例#5
0
    def _create_unsynced_sql(self):
        """
            Create a SQLCommtrackConfig matching the one created by _create_unsynced_couch
        """
        sqlalertconfig = SQLAlertConfig(
            stock_out_facilities=True,
            stock_out_commodities=True,
            stock_out_rates=True,
            non_report=True,
        )
        sqlstockrestoreconfig = SQLStockRestoreConfig(
            section_to_consumption_types={'s1': 'c1'},
            force_consumption_case_types=['type1'],
            use_dynamic_product_list=True,
        )
        sqlconsumptionconfig = SQLConsumptionConfig(
            min_transactions=1,
            min_window=2,
            optimal_window=3,
            use_supply_point_type_default_consumption=True,
            exclude_invalid_periods=False,
        )
        sqlstocklevelsconfig = SQLStockLevelsConfig(
            emergency_level=0.5,
            understock_threshold=1.5,
            overstock_threshold=3,
        )
        sql = SQLCommtrackConfig(
            domain='my_project',
            use_auto_emergency_levels=False,
            sync_consumption_fixtures=False,
            use_auto_consumption=False,
            individual_consumption_defaults=True,
            sqlalertconfig=sqlalertconfig,
            sqlstockrestoreconfig=sqlstockrestoreconfig,
            sqlconsumptionconfig=sqlconsumptionconfig,
            sqlstocklevelsconfig=sqlstocklevelsconfig,
        )
        sql.save(sync_to_couch=False)  # save so that set_actions works

        sql.set_actions([
            SQLActionConfig(
                action='receipts',
                subaction='sub-receipts',
                _keyword='one',
                caption='first action',
            ),
            SQLActionConfig(
                action='receipts',
                subaction='sub-receipts',
                _keyword='two',
                caption='second action',
            ),
        ])
        sql.save(sync_to_couch=False)  # save actions

        # Save submodels
        sqlstockrestoreconfig.commtrack_config = sql
        sqlstockrestoreconfig.save()
        sqlalertconfig.commtrack_config = sql
        sqlalertconfig.save()
        sqlconsumptionconfig.commtrack_config = sql
        sqlconsumptionconfig.save()
        sqlstocklevelsconfig.commtrack_config = sql
        sqlstocklevelsconfig.save()

        return sql