コード例 #1
0
ファイル: forms.py プロジェクト: ansarbek/commcare-hq
    def __init__(self, domain, *args, **kwargs):
        self.domain = domain
        super(ConsumptionForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_tag = False
        self.helper.label_class = 'col-sm-3 col-md-4 col-lg-2'
        self.helper.field_class = 'col-sm-4 col-md-5 col-lg-3'

        layout = []
        products = Product.by_domain(domain)
        for p in products:
            field_name = 'default_%s' % p._id
            display = _('Default %(product_name)s') % {'product_name': p.name}
            layout.append(field_name)
            self.fields[field_name] = forms.DecimalField(
                label=display,
                required=False,
                initial=get_default_monthly_consumption(
                    self.domain,
                    p._id,
                    None,
                    None
                )
            )

        layout.append(hqcrispy.FormActions(
            ButtonHolder(
                Submit('submit', ugettext_lazy('Update Default Consumption Info'))
            )
        ))
        self.helper.layout = Layout(*layout)
コード例 #2
0
ファイル: util.py プロジェクト: NoahCarnahan/commcare-hq
def get_default_column_data(domain, location_types):
    data = {
        'headers': {},
        'values': {}
    }

    if Domain.get_by_name(domain).commtrack_settings.individual_consumption_defaults:
        products = Product.by_domain(domain)

        for loc_type in location_types:
            loc = get_loc_config(domain)[loc_type]
            if not loc.administrative:
                data['headers'][loc_type] = [
                    'default_' +
                    p.code for p in products
                ]

                locations = Location.filter_by_type(domain, loc_type)
                for loc in locations:
                    sp = SupplyPointCase.get_or_create_by_location(loc)

                    data['values'][loc._id] = [
                        get_default_monthly_consumption(
                            domain,
                            p._id,
                            loc_type,
                            sp._id
                        ) or '' for p in products
                    ]
            else:
                data['headers'][loc_type] = []
    return data
コード例 #3
0
    def __init__(self, domain, *args, **kwargs):
        self.domain = domain
        super(ConsumptionForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_tag = False
        self.helper.label_class = 'col-sm-3 col-md-4 col-lg-2'
        self.helper.field_class = 'col-sm-4 col-md-5 col-lg-3'

        layout = []
        products = Product.by_domain(domain)
        for p in products:
            field_name = 'default_%s' % p._id
            display = _('Default %(product_name)s') % {'product_name': p.name}
            layout.append(field_name)
            self.fields[field_name] = forms.DecimalField(
                label=display,
                required=False,
                initial=get_default_monthly_consumption(
                    self.domain,
                    p._id,
                    None,
                    None
                )
            )

        layout.append(hqcrispy.FormActions(
            ButtonHolder(
                Submit('submit', ugettext_lazy('Update Default Consumption Info'))
            )
        ))
        self.helper.layout = Layout(*layout)
コード例 #4
0
ファイル: util.py プロジェクト: thedevelopermw/commcare-hq
def get_default_column_data(domain, location_types):
    data = {
        'headers': {},
        'values': {}
    }

    if Domain.get_by_name(domain).commtrack_settings.individual_consumption_defaults:
        products = Product.by_domain(domain)

        for loc_type in location_types:
            loc = get_loc_config(domain)[loc_type]
            if not loc.administrative:
                data['headers'][loc_type] = [
                    'default_' +
                    p.code for p in products
                ]

                locations = Location.filter_by_type(domain, loc_type)
                for loc in locations:
                    sp = SupplyPointCase.get_or_create_by_location(loc)

                    data['values'][loc._id] = [
                        get_default_monthly_consumption(
                            domain,
                            p._id,
                            loc_type,
                            sp._id
                        ) or '' for p in products
                    ]
            else:
                data['headers'][loc_type] = []
    return data
コード例 #5
0
ファイル: tests.py プロジェクト: tobiasmcnulty/commcare-hq
    def get_default_consumption(self, domain, product_id, location_type, case_id):
        consumption = get_default_monthly_consumption(domain, product_id, location_type, case_id)

        if consumption:
            return consumption / Decimal(DAYS_IN_MONTH)
        else:
            return None
コード例 #6
0
ファイル: forms.py プロジェクト: kkaczmarczyk/commcare-hq
 def __init__(self, domain, *args, **kwargs):
     self.domain = domain
     super(ConsumptionForm, self).__init__(*args, **kwargs)
     products = Product.by_domain(domain)
     for p in products:
         field_name = "default_%s" % p._id
         display = _("Default %(product_name)s") % {"product_name": p.name}
         self.fields[field_name] = forms.DecimalField(
             label=display, required=False, initial=get_default_monthly_consumption(self.domain, p._id, None, None)
         )
コード例 #7
0
ファイル: models.py プロジェクト: elbowink/commcare-hq
 def _default_monthly_consumption(case_id, product_id):
     # note: for now as an optimization hack, per-supply point type is not supported
     # unless explicitly configured, because it will require looking up the case
     facility_type = None
     if self.consumption_config.use_supply_point_type_default_consumption:
         try:
             supply_point = SupplyPointCase.get(case_id)
             facility_type = supply_point.location.location_type
         except ResourceNotFound:
             pass
     return get_default_monthly_consumption(self.domain, product_id, facility_type, case_id)
コード例 #8
0
 def _default_monthly_consumption(case_id, product_id):
     # note: for now as an optimization hack, per-supply point type is not supported
     # unless explicitly configured, because it will require looking up the case
     facility_type = None
     if self.consumption_config.use_supply_point_type_default_consumption:
         try:
             supply_point = SupplyInterface(self.domain).get_supply_point(case_id)
             facility_type = supply_point.sql_location.location_type_name
         except ResourceNotFound:
             pass
     return get_default_monthly_consumption(self.domain, product_id, facility_type, case_id)
コード例 #9
0
ファイル: views.py プロジェクト: ye-man/commcare-hq
 def consumption(self):
     consumptions = []
     for product in Product.by_domain(self.domain):
         consumption = get_default_monthly_consumption(
             self.domain,
             product._id,
             self.location.location_type_name,
             self.location.supply_point_id or None,
         )
         if consumption:
             consumptions.append((product.name, consumption))
     return consumptions
コード例 #10
0
ファイル: views.py プロジェクト: kkrampa/commcare-hq
 def consumption(self):
     consumptions = []
     for product in Product.by_domain(self.domain):
         consumption = get_default_monthly_consumption(
             self.domain,
             product._id,
             self.location.location_type_name,
             self.location.supply_point_id or None,
         )
         if consumption:
             consumptions.append((product.name, consumption))
     return consumptions
コード例 #11
0
ファイル: forms.py プロジェクト: thedevelopermw/commcare-hq
 def __init__(self, domain, *args, **kwargs):
     self.domain = domain
     super(ConsumptionForm, self).__init__(*args, **kwargs)
     products = Product.by_domain(domain)
     for p in products:
         field_name = 'default_%s' % p._id
         display = _('Default %(product_name)s') % {'product_name': p.name}
         self.fields[field_name] = forms.DecimalField(
             label=display,
             required=False,
             initial=get_default_monthly_consumption(
                 self.domain, p._id, None, None))
コード例 #12
0
ファイル: views.py プロジェクト: ekush/commcare-hq
 def consumption(self):
     consumptions = []
     for product in Product.by_domain(self.domain):
         consumption = get_default_monthly_consumption(
             self.domain,
             product._id,
             self.location.location_type,
             # FIXME accessing this value from the sql location
             # would be faster
             self.supply_point._id if self.supply_point else None,
         )
         if consumption:
             consumptions.append((product.name, consumption))
     return consumptions
コード例 #13
0
 def consumption(self):
     consumptions = []
     for product in Product.by_domain(self.domain):
         consumption = get_default_monthly_consumption(
             self.domain,
             product._id,
             self.location.location_type_name,
             # FIXME accessing this value from the sql location
             # would be faster
             self.supply_point.case_id if self.supply_point else None,
         )
         if consumption:
             consumptions.append((product.name, consumption))
     return consumptions
コード例 #14
0
    def get_context_data(self, **kwargs):
        context = super(InputStockView, self).get_context_data(**kwargs)
        try:
            sql_location = SQLLocation.objects.get(
                domain=self.domain, site_code=kwargs.get('site_code'))
        except SQLLocation.DoesNotExist:
            raise Http404()
        InputStockFormSet = formset_factory(InputStockForm, extra=0)
        initial_data = []

        for product in sql_location.products.order_by('name'):
            try:
                stock_state = StockState.objects.get(
                    case_id=sql_location.supply_point_id,
                    product_id=product.product_id)
                stock_on_hand = stock_state.stock_on_hand
                monthly_consumption = stock_state.get_monthly_consumption()
            except StockState.DoesNotExist:
                stock_on_hand = 0
                monthly_consumption = 0
            initial_data.append({
                'product_id':
                product.product_id,
                'product':
                product.name,
                'stock_on_hand':
                int(stock_on_hand),
                'monthly_consumption':
                round(monthly_consumption) if monthly_consumption else 0,
                'default_consumption':
                get_default_monthly_consumption(
                    self.domain, product.product_id,
                    sql_location.location_type.name,
                    sql_location.supply_point_id),
                'units':
                product.units
            })
        context['formset'] = InputStockFormSet(initial=initial_data)
        return context
コード例 #15
0
ファイル: views.py プロジェクト: ansarbek/commcare-hq
    def get_context_data(self, **kwargs):
        context = super(InputStockView, self).get_context_data(**kwargs)
        try:
            sql_location = SQLLocation.objects.get(domain=self.domain, site_code=kwargs.get('site_code'))
        except SQLLocation.DoesNotExist:
            raise Http404()
        InputStockFormSet = formset_factory(InputStockForm, extra=0)
        initial_data = []

        for product in sql_location.products.order_by('name'):
            try:
                stock_state = StockState.objects.get(
                    case_id=sql_location.supply_point_id,
                    product_id=product.product_id
                )
                stock_on_hand = stock_state.stock_on_hand
                monthly_consumption = stock_state.get_monthly_consumption()
            except StockState.DoesNotExist:
                stock_on_hand = 0
                monthly_consumption = 0
            initial_data.append(
                {
                    'product_id': product.product_id,
                    'product': product.name,
                    'stock_on_hand': int(stock_on_hand),
                    'monthly_consumption': round(monthly_consumption) if monthly_consumption else 0,
                    'default_consumption': get_default_monthly_consumption(
                        self.domain,
                        product.product_id,
                        sql_location.location_type.name,
                        sql_location.supply_point_id
                    ),
                    'units': product.units
                }
            )
        context['formset'] = InputStockFormSet(initial=initial_data)
        return context