Example #1
0
    def production_ci_na5(self, party, _group, _periods):
        """ Average of
            1989 HCFC production + 2.8 per cent of 1989 CFC production
            and
            1989 HCFC consumption + 2.8 per cent of 1989 CFC consumption
        """

        hcfc_group = self.groups.get('CI')
        cfc_group = self.groups.get('AI')
        hcfc_1989 = self._get_prodcons(party, hcfc_group, '1989')
        cfc_1989 = self._get_prodcons(party, cfc_group, '1989')
        if hcfc_1989 and cfc_1989:
            # Note: For EU members, real consumption is included in the formula
            # even if they have NULL calculated_consumption

            average_prod = sum_decimals(
                hcfc_1989.calculated_production,
                decimal_zero_if_none(cfc_1989.calculated_production) *
                Decimal('0.028'),
                hcfc_1989.get_calc_consumption(),
                cfc_1989.get_calc_consumption() * Decimal('0.028'),
            )
            average_prod = round_decimal_half_up(
                average_prod / 2,
                1  # always 1 decimal for 1989
            )
            return average_prod if average_prod > 0 else Decimal('0')
        return None
Example #2
0
def join_labuse_data(imports, production):
    data = {}

    for item in imports:
        substance_name = get_substance_or_blend_name(item)
        if substance_name not in data:
            data[substance_name] = {
                'group': get_group_name(item),
                'substance': substance_name,
                'consumption': item.quantity_laboratory_analytical_uses,
                'production': Decimal('0.0'),
                'remark': item.decision_laboratory_analytical_uses or '',
            }
        else:
            data[substance_name]['consumption'] = sum_decimals(
                data[substance_name]['consumption'],
                item.quantity_laboratory_analytical_uses)
            data[substance_name]['remark'] = ' '.join(
                filter(None, [
                    data[substance_name]['remark'],
                    item.decision_laboratory_analytical_uses
                ]))

    for item in production:
        substance_name = get_substance_or_blend_name(item)
        if substance_name not in data:
            data[substance_name] = {
                'group': get_group_name(item),
                'substance': substance_name,
                'consumption': Decimal('0.0'),
                'production': item.quantity_laboratory_analytical_uses,
                'remark': item.decision_laboratory_analytical_uses or '',
            }
        else:
            data[substance_name]['production'] = sum_decimals(
                data[substance_name]['production'],
                item.quantity_laboratory_analytical_uses)
            data[substance_name]['remark'] = ' '.join(
                filter(None, [
                    data[substance_name]['remark'],
                    item.decision_laboratory_analytical_uses
                ]))

    return data
Example #3
0
def merge(items):
    if len(items) <= 1:
        return None
    sub_item = items[0].__class__()
    sub_item.substance = items[0].substance
    sub_item.blend = items[0].blend
    sub_item.is_subtotal = True
    for x in items:
        for f in x.QUANTITY_FIELDS + ['quantity_polyols']:
            setattr(sub_item, f,
                    sum_decimals(getattr(sub_item, f), getattr(x, f)))
    return sub_item
Example #4
0
 def on_hand_func(total_available, item):
     return subtract_decimals(
         total_available,
         sum_decimals(
             item.quantity_used,
             sum_decimals(item.quantity_exported, item.quantity_destroyed)))
Example #5
0
def get_table_data_essen_crit(data, reporting_period, base_row_index,
                              on_hand_func):
    rows = list()
    styles = list()
    for item in data:
        imports = list(
            item.imports.filter(
                quantity__gt=0  # filter the default Unspecified=0 rows persisted
            ).order_by('party__name').all())
        total_imported = Decimal(0.0)
        for imp in imports:
            total_imported = sum_decimals(total_imported, imp.quantity)
        total_acquired = sum_decimals(item.quantity_production, total_imported)
        authorized_not_acquired = subtract_decimals(item.quantity_exempted,
                                                    total_acquired)
        total_available = sum_decimals(item.on_hand_start_year, total_acquired)
        on_hand_end_year = on_hand_func(total_available, item)
        first_import = imports.pop(0) if imports else None
        source_party = ''
        if first_import:
            if first_import.party:
                source_party = first_import.party.name
            elif first_import.quantity:
                source_party = _('Unspecified')
        rows.append((
            sm_c(reporting_period.name),  # A
            sm_l(item.substance.name),  # B (essen) or hidden (crit)
            sm_r(format_decimal(item.quantity_exempted)),  # C | B
            sm_r(format_decimal(item.quantity_production)),  # D | C
            sm_r(format_decimal(first_import.quantity))
            if first_import else '',  # E|D - amount
            sm_l(source_party),  # E|D - source country
            sm_r(format_decimal(total_acquired)),  # F|E
            sm_r(format_decimal(authorized_not_acquired)),  # G|F
            sm_r(format_decimal(item.on_hand_start_year)),  # H|G
            sm_r(format_decimal(total_available)),  # I|H
            sm_r(format_decimal(item.quantity_used)),  # J|I
            sm_r(format_decimal(item.quantity_exported)),  # K|J
            sm_r(format_decimal(item.quantity_destroyed)),  # L|K
            sm_r(format_decimal(on_hand_end_year)),  # M|L
            sm_c(_('Yes')) if item.is_emergency else '',  # Emergency
            sm_l(get_remarks(item)),  # Remark
        ))
        row_index = base_row_index + len(rows)
        for imp in imports:
            # Add more rows if multiple import sources
            rows.append((
                # Don't repeat previously shown fields, only show E
                '',
                '',
                '',
                '',
                sm_r(format_decimal(imp.quantity)),
                sm_l(imp.party.name)
                if imp.party else _('Unspecified') if imp.quantity else '',
                '',
                '',
                '',
                '',
                '',
                '',
                '',
                '',
                '',
                '',
            ))
        if imports:
            # merge rows when multiple import sources (except columns E)
            span_columns = (*range(0, 4), *range(6, 16))
            styles.extend([
                #  Vertical span of common columns for all exempted rows
                ('SPAN', (col, row_index), (col, row_index + len(imports)))
                for col in span_columns
            ])

    return rows, styles