def find_all():
     """Find all distribution codes valid today."""
     current_app.logger.debug('<find_all')
     data = {'items': []}
     distribution_codes = DistributionCodeModel.find_all()
     distribution_code_schema = DistributionCodeSchema()
     data['items'] = distribution_code_schema.dump(distribution_codes,
                                                   many=True)
     current_app.logger.debug('>find_all')
     return data
Exemple #2
0
    def _build_lines(cls, payment_line_items: List[PaymentLineItemModel], negate: bool = False):
        """Build lines for the invoice."""
        # Fetch all distribution codes to reduce DB hits. Introduce caching if needed later
        distribution_codes: List[DistributionCodeModel] = DistributionCodeModel.find_all()
        lines_map = defaultdict(dict)  # To group all the lines with same GL code together.
        index: int = 0
        for line_item in payment_line_items:
            if line_item.total == 0:
                continue
            # Find the distribution from the above list
            distribution_code = [dist for dist in distribution_codes if
                                 dist.distribution_code_id == line_item.fee_distribution_id][0] \
                if line_item.fee_distribution_id else None

            # Check if a line with same GL code exists, if YES just add up the amount. if NO, create a new line.
            line = lines_map[distribution_code.distribution_code_id]

            if not line:
                index = index + 1
                distribution = [dict(
                    dist_line_number=index,
                    amount=cls._get_amount(line_item.total, negate),
                    account=f'{distribution_code.client}.{distribution_code.responsibility_centre}.'
                            f'{distribution_code.service_line}.{distribution_code.stob}.'
                            f'{distribution_code.project_code}.000000.0000'
                )] if distribution_code else None

                line = {
                    'line_number': index,
                    'line_type': CFS_LINE_TYPE,
                    'description': line_item.description,
                    'unit_price': cls._get_amount(line_item.total, negate),
                    'quantity': 1,
                    # 'attribute1': line_item.invoice_id,
                    # 'attribute2': line_item.id,
                    'distribution': distribution
                }
            else:
                # Add up the price and distribution
                line['unit_price'] = line['unit_price'] + cls._get_amount(line_item.total, negate)
                line['distribution'][0]['amount'] = line['distribution'][0]['amount'] + \
                    cls._get_amount(line_item.total, negate)

            lines_map[distribution_code.distribution_code_id] = line

            if line_item.service_fees > 0:
                service_fee_distribution: DistributionCodeModel = DistributionCodeModel.find_by_id(
                    distribution_code.service_fee_distribution_code_id)
                service_line = lines_map[service_fee_distribution.distribution_code_id]

                if not service_line:
                    index = index + 1
                    service_line = {
                        'line_number': index,
                        'line_type': CFS_LINE_TYPE,
                        'description': 'Service Fee',
                        'unit_price': cls._get_amount(line_item.service_fees, negate),
                        'quantity': 1,
                        # 'attribute1': line_item.invoice_id,
                        # 'attribute2': line_item.id,
                        'distribution': [
                            {
                                'dist_line_number': index,
                                'amount': cls._get_amount(line_item.service_fees, negate),
                                'account': f'{service_fee_distribution.client}.'
                                           f'{service_fee_distribution.responsibility_centre}.'
                                           f'{service_fee_distribution.service_line}.'
                                           f'{service_fee_distribution.stob}.'
                                           f'{service_fee_distribution.project_code}.000000.0000'
                            }
                        ]
                    }

                else:
                    # Add up the price and distribution
                    service_line['unit_price'] = service_line['unit_price'] + \
                        cls._get_amount(line_item.service_fees, negate)
                    service_line['distribution'][0]['amount'] = service_line['distribution'][0]['amount'] + \
                        cls._get_amount(line_item.service_fees, negate)
                lines_map[service_fee_distribution.distribution_code_id] = service_line
        return list(lines_map.values())
Exemple #3
0
    def _build_lines(cls,
                     payment_line_items: List[PaymentLineItemModel],
                     negate: bool = False):
        """Build lines for the invoice."""
        # Fetch all distribution codes to reduce DB hits. Introduce caching if needed later
        distribution_codes: List[
            DistributionCodeModel] = DistributionCodeModel.find_all()
        lines = []
        index: int = 0
        for line_item in payment_line_items:
            # Find the distribution from the above list
            distribution_code = [dist for dist in distribution_codes if
                                 dist.distribution_code_id == line_item.fee_distribution_id][0] \
                if line_item.fee_distribution_id else None
            index = index + 1
            distribution = [
                dict(
                    dist_line_number=index,
                    amount=cls._get_amount(line_item.total, negate),
                    account=
                    f'{distribution_code.client}.{distribution_code.responsibility_centre}.'
                    f'{distribution_code.service_line}.{distribution_code.stob}.'
                    f'{distribution_code.project_code}.000000.0000')
            ] if distribution_code else None
            lines.append({
                'line_number': index,
                'line_type': CFS_LINE_TYPE,
                'description': line_item.description,
                'unit_price': cls._get_amount(line_item.total, negate),
                'quantity': 1,
                'attribute1': line_item.invoice_id,
                'attribute2': line_item.id,
                'distribution': distribution
            })

            if line_item.service_fees > 0:
                service_fee_distribution: DistributionCodeModel = DistributionCodeModel.find_by_id(
                    distribution_code.service_fee_distribution_code_id)
                index = index + 1
                lines.append({
                    'line_number':
                    index,
                    'line_type':
                    CFS_LINE_TYPE,
                    'description':
                    'Service Fee',
                    'unit_price':
                    cls._get_amount(line_item.service_fees, negate),
                    'quantity':
                    1,
                    'attribute1':
                    line_item.invoice_id,
                    'attribute2':
                    line_item.id,
                    'distribution': [{
                        'dist_line_number':
                        index,
                        'amount':
                        cls._get_amount(line_item.service_fees, negate),
                        'account':
                        f'{service_fee_distribution.client}.'
                        f'{service_fee_distribution.responsibility_centre}.'
                        f'{service_fee_distribution.service_line}.'
                        f'{service_fee_distribution.stob}.'
                        f'{service_fee_distribution.project_code}.000000.0000'
                    }]
                })

        return lines