<commit_before># -*- coding: utf-8 -*-

from __future__ import absolute_import

from osv import osv, fields
from tools.translate import _
from tools import cache
import netsvc
from datetime import datetime


class GiscedataFacturacioFactura(osv.osv):
    """Afegim la funció de Generation kWh.
    """
    _name = 'giscedata.facturacio.factura'
    _inherit = 'giscedata.facturacio.factura'

    def unlink(self, cursor, uid, ids, context=None):
        """Return gkwh rights to owner when gkwh invoice is droped"""
        line_obj = self.pool.get('giscedata.facturacio.factura.linia')
        gkwh_lineowner_obj = self.pool.get('generationkwh.invoice.line.owner')
        gkwh_dealer_obj = self.pool.get('generationkwh.dealer')

        invoice_fields = [
            'type', 'polissa_id', 'tarifa_acces_id', 'linies_energia'
        ]

        line_fields = [
            'data_desde', 'data_fins', 'product_id', 'quantity'
        ]

        has_gkwh_rights = False
        for invoice_id in ids:
            rights_dict = self.get_gkwh_shares_dict(
                cursor, uid, invoice_id, context=context
            )
            gkwh_line_ids = self.get_gkwh_lines(
                cursor, uid, invoice_id, context=context
            )
            if gkwh_line_ids:
                has_gkwh_rights = True
                line_obj.unlink(
                    cursor, uid, gkwh_line_ids, context=context
                )
            if has_gkwh_rights:
                invoice_vals = self.read(
                    cursor, uid, invoice_id, invoice_fields, context=context
                )
                if invoice_vals['type'] == 'out_refund':
                    # Re-use rights through dealer when refund invoice
                    # WARNING: The real use may not be the original one

                    contract_id = invoice_vals['polissa_id'][0]
                    fare_id = invoice_vals['tarifa_acces_id'][0]

                    for period_id in rights_dict.keys():
                        quantity = sum([v['kwh'] for v in
                                        rights_dict[period_id]])
                        from_date = max([v['from'] for v in
                                        rights_dict[period_id]])
                        to_date = min([v['to'] for v in
                                      rights_dict[period_id]])

                        gkwh_dealer_obj.use_kwh(
                            cursor,
                            uid,
                            contract_id,
                            from_date,
                            to_date,
                            fare_id,
                            period_id,
                            quantity,
                            context=context
                        )

        return super(GiscedataFacturacioFactura, self).unlink(
            cursor, uid, ids, context
        )

    def anullar(self, cursor, uid, ids, tipus='A', context=None):
        """ Returns gkwh rights on invoice refund """
        gkwh_lineowner_obj = self.pool.get('generationkwh.invoice.line.owner')
        gkwh_dealer_obj = self.pool.get('generationkwh.dealer')
        fact_line_obj = self.pool.get('giscedata.facturacio.factura.linia')

        fields_to_read = ['is_gkwh', 'gkwh_linia_ids', 'type', 'polissa_id',
                          'tarifa_acces_id']
        refund_ids = []
        for inv_vals in self.read(cursor, uid, ids, fields_to_read, context):
            if inv_vals['is_gkwh']:
                inv_id = inv_vals['id']
                # refund gkwh rights
                for glo_id in inv_vals['gkwh_linia_ids']:
                    gkwh_lineowner = gkwh_lineowner_obj.browse(
                        cursor, uid, glo_id, context=context
                    )
                    owner_id = gkwh_lineowner.owner_id.id
                    contract_id = inv_vals['polissa_id'][0]
                    fare_id = inv_vals['tarifa_acces_id'][0]
                    line = gkwh_lineowner.factura_line_id
                    period_id = self.get_fare_period(
                        cursor, uid, line.product_id.id, context=context
                    )
                    # returns rights through dealer
                    gkwh_dealer_obj.refund_kwh(
                        cursor,
                        uid,
                        contract_id,
                        line.data_desde,
                        line.data_fins,
                        fare_id,
                        period_id,
                        line.quantity,
                        owner_id,
                        context=context
                    )
            # refund invoice creation
            refund_id = super(GiscedataFacturacioFactura, self).anullar(
                cursor, uid, [inv_id], tipus, context=context
            )[0]
            refund_ids.append(refund_id)
            # drops invoice lines
            refund_invoice_lines = self.get_gkwh_lines(
                cursor, uid, refund_id, context=context
            )
            ctx = context.copy()
            ctx.update({'gkwh_manage_rights': False})
            fact_line_obj.unlink(
                cursor, uid, refund_invoice_lines, context=ctx
            )
            # drop incorrect owner lines
            bad_owner_ids = gkwh_lineowner_obj.search(
                cursor, uid, [('factura_id', '=', refund_id)], context=context
            )
            gkwh_lineowner_obj.unlink(
                cursor, uid, bad_owner_ids, context=context
            )

            # recreates lines with original invoice rights<commit_msg>FIX No GkWh invoices refund/generation<commit_after># -*- coding: utf-8 -*-

from __future__ import absolute_import

from osv import osv, fields
from tools.translate import _
from tools import cache
import netsvc
from datetime import datetime


class GiscedataFacturacioFactura(osv.osv):
    """Afegim la funció de Generation kWh.
    """
    _name = 'giscedata.facturacio.factura'
    _inherit = 'giscedata.facturacio.factura'

    def unlink(self, cursor, uid, ids, context=None):
        """Return gkwh rights to owner when gkwh invoice is droped"""
        line_obj = self.pool.get('giscedata.facturacio.factura.linia')
        gkwh_lineowner_obj = self.pool.get('generationkwh.invoice.line.owner')
        gkwh_dealer_obj = self.pool.get('generationkwh.dealer')

        invoice_fields = [
            'type', 'polissa_id', 'tarifa_acces_id', 'linies_energia'
        ]

        line_fields = [
            'data_desde', 'data_fins', 'product_id', 'quantity'
        ]

        has_gkwh_rights = False
        for invoice_id in ids:
            rights_dict = self.get_gkwh_shares_dict(
                cursor, uid, invoice_id, context=context
            )
            gkwh_line_ids = self.get_gkwh_lines(
                cursor, uid, invoice_id, context=context
            )
            if gkwh_line_ids:
                has_gkwh_rights = True
                line_obj.unlink(
                    cursor, uid, gkwh_line_ids, context=context
                )
            if has_gkwh_rights:
                invoice_vals = self.read(
                    cursor, uid, invoice_id, invoice_fields, context=context
                )
                if invoice_vals['type'] == 'out_refund':
                    # Re-use rights through dealer when refund invoice
                    # WARNING: The real use may not be the original one

                    contract_id = invoice_vals['polissa_id'][0]
                    fare_id = invoice_vals['tarifa_acces_id'][0]

                    for period_id in rights_dict.keys():
                        quantity = sum([v['kwh'] for v in
                                        rights_dict[period_id]])
                        from_date = max([v['from'] for v in
                                        rights_dict[period_id]])
                        to_date = min([v['to'] for v in
                                      rights_dict[period_id]])

                        gkwh_dealer_obj.use_kwh(
                            cursor,
                            uid,
                            contract_id,
                            from_date,
                            to_date,
                            fare_id,
                            period_id,
                            quantity,
                            context=context
                        )

        return super(GiscedataFacturacioFactura, self).unlink(
            cursor, uid, ids, context
        )

    def anullar(self, cursor, uid, ids, tipus='A', context=None):
        """ Returns gkwh rights on invoice refund """
        gkwh_lineowner_obj = self.pool.get('generationkwh.invoice.line.owner')
        gkwh_dealer_obj = self.pool.get('generationkwh.dealer')
        fact_line_obj = self.pool.get('giscedata.facturacio.factura.linia')

        fields_to_read = ['is_gkwh', 'gkwh_linia_ids', 'type', 'polissa_id',
                          'tarifa_acces_id']
        refund_ids = []
        for inv_vals in self.read(cursor, uid, ids, fields_to_read, context):
            inv_id = inv_vals['id']
            if inv_vals['is_gkwh']:
                # refund gkwh rights
                for glo_id in inv_vals['gkwh_linia_ids']:
                    gkwh_lineowner = gkwh_lineowner_obj.browse(
                        cursor, uid, glo_id, context=context
                    )
                    owner_id = gkwh_lineowner.owner_id.id
                    contract_id = inv_vals['polissa_id'][0]
                    fare_id = inv_vals['tarifa_acces_id'][0]
                    line = gkwh_lineowner.factura_line_id
                    period_id = self.get_fare_period(
                        cursor, uid, line.product_id.id, context=context
                    )
                    # returns rights through dealer
                    gkwh_dealer_obj.refund_kwh(
                        cursor,
                        uid,
                        contract_id,
                        line.data_desde,
                        line.data_fins,
                        fare_id,
                        period_id,
                        line.quantity,
                        owner_id,
                        context=context
                    )
            # refund invoice creation
            refund_id = super(GiscedataFacturacioFactura, self).anullar(
                cursor, uid, [inv_id], tipus, context=context
            )[0]
            refund_ids.append(refund_id)
            # drops invoice lines
            refund_invoice_lines = self.get_gkwh_lines(
                cursor, uid, refund_id, context=context
            )
            if refund_invoice_lines:
                ctx = context.copy()
                ctx.update({'gkwh_manage_rights': False})
                fact_line_obj.unlink(
                    cursor, uid, refund_invoice_lines, context=ctx
                )
            # drop incorrect owner lines
            bad_owner_ids = gkwh_lineowner_obj.search(
                cursor, uid, [('factura_id', '=', refund_id)], context=context
            )
            if bad_owner_ids:
                gkwh_lineowner_obj.unlink(
                    cursor, uid, bad_owner_ids, context=context
                )

            # recreates lines with original invoice rights
{'fraction_non_alphanumeric': 0.07926829268292683, 'fraction_numerical': 0.0010604453870625664, 'mean_word_length': 1.358691121300542, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5888108', 'n_tokens_mistral': 3242, 'n_tokens_neox': 3074, 'n_words': 609}