diff --git a/models/acc_operation.py b/models/acc_operation.py index f7c8bf63db20dccfa0b8df5330e1709a752ded65..e7fc362ca2f8096d4b2692a297f960db2b919615 100644 --- a/models/acc_operation.py +++ b/models/acc_operation.py @@ -73,7 +73,13 @@ class AccOperation(models.Model): return action def get_power_by_cons(self, acc_delivery_id, date_start, date_end): - + """ + Fonction retournant l'énergie consommée d'un consommateur donné pour une période donnée. + :param acc_delivery_id: consommateur + date_start: date début + date_end: date de fin + :return: un dictionnaire de valeurs (consommateur + power + type) + """ power_tab_by_cons = self.env['acc.enedis.cdc'].read_group( [('acc_operation_id', '=', self.id), ('acc_counter_id', '=', acc_delivery_id.id), @@ -86,7 +92,13 @@ class AccOperation(models.Model): return power_tab_by_cons def get_power_by_prod(self, acc_injection_id, date_start, date_end): - + """ + Fonction retournant l'énergie produite et en surplus d'un producteur donné pour une période donnée. + :param acc_injection_id: producteur + date_start: date début + date_end: date de fin + :return: un dictionnaire de valeurs (producteur + power + type) + """ power_tab_by_prod = self.env['acc.enedis.cdc'].read_group( [('acc_operation_id', '=', self.id), ('acc_counter_id', '=', acc_injection_id.id), @@ -100,6 +112,13 @@ class AccOperation(models.Model): return power_tab_by_prod def create_account(self, date_month, account_periodicity): + """ + Fonction permettant de générer les factures à une date donné. + Création des factures + Création des lignes de facture associées. + :param date_month: date à laquelle on lance la facture + account_periodicity: périodicité (1 pour mois, 3 pour trimestre, 6 pour semestre, 12 pour année) + :return: + """ account_list = [] date_end = date_utils.end_of(date_month, 'month') @@ -109,27 +128,12 @@ class AccOperation(models.Model): date_start = date_end - relativedelta(months=int(account_periodicity)-1) date_start = date_utils.start_of(date_start, 'month') - power_tab_by_prod = self.env['acc.enedis.cdc'].read_group( - [('acc_operation_id', '=', self.id), - ('date_slot', '>=', date_start), - ('date_slot', '<=', date_end), - '|', ('comp_data_type', '=', 'prod'), - ('comp_data_type', '=', 'surplus')], - ['power', 'acc_operation_id', 'date_slot'], - ['comp_data_type'], orderby='comp_data_type', lazy=False) - - prod_tot = power_tab_by_prod[0]['power'] - power_tab_by_prod[1]['power'] - for acc_injection_id in self.acc_injection_ids: if acc_injection_id.is_account_auto_activate: for acc_delivery_id in self.acc_delivery_ids: - power_tab_by_prod = self.get_power_by_prod(acc_injection_id, date_start, date_end) - - power_autoprod_percent = ((power_tab_by_prod[0]['power'] - power_tab_by_prod[1][ - 'power']) * 100) / prod_tot - power_tab_by_cons = self.get_power_by_cons(acc_delivery_id, date_start, date_end) - power_total = (power_tab_by_cons[0]['power'] / 1000 / 2) * (power_autoprod_percent / 100) + power_total = self.calc_power_by_cons_by_prod(acc_injection_id, acc_delivery_id, + date_start, date_end) Account = self.env['acc.account'] acc_account = Account.create({ @@ -142,21 +146,27 @@ class AccOperation(models.Model): 'tax_tcfe': self.tax_tcfe, }) - price_ids = self.env['acc.sale.price'].search([ + interval_ids = self.env['acc.sale.price'].search([ ('acc_operation_id', '=', self.id), ('acc_injection_id', '=', acc_injection_id.id), ('acc_delivery_id', '=', acc_delivery_id.id), - '|', ('start_date', '<=', date_start), - ('end_date', '>=', date_end) + ('start_date', '<=', date_start), + ('end_date', '=', False) ]) - price2_ids = self.env['acc.sale.price'].search([ - ('acc_operation_id', '=', self.id), - ('acc_injection_id', '=', acc_injection_id.id), - ('acc_delivery_id', '=', acc_delivery_id.id), - ('start_date', '>', date_start), - ('end_date', '<=', date_end) - ]) - interval_ids = price_ids + price2_ids + if not interval_ids: + price_ids = self.env['acc.sale.price'].search([ + ('acc_operation_id', '=', self.id), + ('acc_injection_id', '=', acc_injection_id.id), + ('acc_delivery_id', '=', acc_delivery_id.id), + ('end_date', '>=', date_start), + ]) + price2_ids = self.env['acc.sale.price'].search([ + ('acc_operation_id', '=', self.id), + ('acc_injection_id', '=', acc_injection_id.id), + ('acc_delivery_id', '=', acc_delivery_id.id), + ('start_date', '<=', date_end) + ]) + interval_ids = price_ids & price2_ids for interval in interval_ids: if date_start > interval.start_date: @@ -164,17 +174,15 @@ class AccOperation(models.Model): else: date_interval_start = interval.start_date - if date_end < interval.end_date: - date_interval_end = date_end + if interval.end_date: + if date_end < interval.end_date: + date_interval_end = date_end + else: + date_interval_end = interval.end_date else: - date_interval_end = interval.end_date - - power_tab_by_prod = self.get_power_by_prod(acc_injection_id, date_interval_start, date_interval_end) + date_interval_end = date_end - power_autoprod_percent = ((power_tab_by_prod[0]['power'] - power_tab_by_prod[1][ - 'power']) * 100) / prod_tot - power_tab_by_cons = self.get_power_by_cons(acc_delivery_id, date_interval_start, date_interval_end) - power_total = (power_tab_by_cons[0]['power'] / 1000 / 2) * (power_autoprod_percent / 100) + power_total = self.calc_power_by_cons_by_prod(acc_injection_id, acc_delivery_id, date_interval_start, date_interval_end) AccountLine = self.env['acc.account.line'] @@ -197,56 +205,116 @@ class AccOperation(models.Model): ] return action + def calc_power_by_cons_by_prod(self, acc_injection_id, acc_delivery_id, date_start, date_end): + """ + Fonction permettant de calculer la puissance en kWh consommée pour un consommateur donnée en fonction + en rapport à un producteur défini pour un intervalle de temps donné. + :param acc_injection_id: producteur + acc_delivery_id: consommateur + date_start: date début + date_end: date de fin + :return: une valeur représentant la puissance consommée en kWh + """ + power_tab_by_prod = self.env['acc.enedis.cdc'].read_group( + [('acc_operation_id', '=', self.id), + ('date_slot', '>=', date_start), + ('date_slot', '<=', date_end), + '|', ('comp_data_type', '=', 'prod'), + ('comp_data_type', '=', 'surplus')], + ['power', 'acc_operation_id', 'date_slot'], + ['comp_data_type'], orderby='comp_data_type', lazy=False) + + prod_tot = power_tab_by_prod[0]['power'] - power_tab_by_prod[1]['power'] + + power_tab_by_prod = self.get_power_by_prod(acc_injection_id, date_start, date_end) + + power_autoprod_percent = ((power_tab_by_prod[0]['power'] - power_tab_by_prod[1][ + 'power']) * 100) / prod_tot + power_tab_by_cons = self.get_power_by_cons(acc_delivery_id, date_start, date_end) + power_total = (power_tab_by_cons[0]['power'] / 1000 / 2) * (power_autoprod_percent / 100) + return power_total + def create_account_surplus(self, date_month, account_periodicity): + """ + Fonction permettant de générer les factures se surplus à une date donnée. + Création des factures + Création des lignes de facture associées. + :param date_month: date à laquelle on lance la facture + account_periodicity: périodicité (1 pour mois, 3 pour trimestre, 6 pour semestre, 12 pour année) + :return: + """ account_list = [] date_end = date_utils.end_of(date_month, 'month') date_start = date_end - relativedelta(months=int(account_periodicity)-1) date_start = date_utils.start_of(date_start, 'month') - power_tab_by_prod = self.env['acc.enedis.cdc'].read_group( - [('acc_operation_id', '=', self.id), - ('date_slot', '>=', date_start), - ('date_slot', '<=', date_end), - ('comp_data_type', '=', 'surplus')], - ['power', 'acc_counter_id', 'date_slot'], - ['acc_counter_id'], orderby='comp_data_type', lazy=False) + for injection_id in self.acc_injection_ids: - for power_tab_prod in power_tab_by_prod: - injection_id = self.env['acc.counter'].browse(power_tab_prod['acc_counter_id'][0]) if injection_id.is_account_surplus_activate: - price_kwh = self.env['acc.sale.price.buyer'].search([ - ('acc_injection_id', '=', injection_id.id), - ('acc_buyer_id', '=', injection_id.buyer_id.id), - ('start_date', '<=', date_month), - ('end_date', '>=', date_month) - ]).price - + power_surplus = self.get_power_by_prod(injection_id, date_start, date_end) Account = self.env['acc.account'] - AccountLine = self.env['acc.account.line'] acc_account = Account.create({ 'acc_operation_id': self.id, - 'buyer_id': self.buyer_id.id, + 'buyer_id': injection_id.buyer_id.id, 'acc_injection_id': injection_id.id, - 'power_cons': power_tab_prod['power'], + 'power_cons': power_surplus[1]['power'], 'start_date': date_start, 'end_date': date_end, - 'price_kwh': price_kwh, + # 'price_kwh': price_kwh, 'is_account_buyer': True, 'tax_tcfe': self.tax_tcfe, }) - # Création de la ligne du mois - acc_account_line = AccountLine.create({ - 'quantity': power_tab_prod['power'], - 'price_unit': price_kwh, - 'acc_account_id': acc_account.id, - 'start_date': date_start, - 'end_date': date_end, - 'description': 'Facture pour le mois de ' + date_month.strftime("%B") - }) - account_list.append(acc_account) + interval_ids = self.env['acc.sale.price.buyer'].search([ + ('acc_operation_id', '=', self.id), + ('acc_injection_id', '=', injection_id.id), + ('acc_buyer_id', '=', injection_id.buyer_id.id), + ('start_date', '<=', date_start), + ('end_date', '=', False) + ]) + if not interval_ids: + price_ids = self.env['acc.sale.price.buyer'].search([ + ('acc_operation_id', '=', self.id), + ('acc_injection_id', '=', injection_id.id), + ('acc_buyer_id', '=', injection_id.buyer_id.id), + ('end_date', '>=', date_start), + ]) + price2_ids = self.env['acc.sale.price.buyer'].search([ + ('acc_operation_id', '=', self.id), + ('acc_injection_id', '=', injection_id.id), + ('acc_buyer_id', '=', injection_id.buyer_id.id), + ('start_date', '<=', date_end) + ]) + interval_ids = price_ids & price2_ids + + for interval in interval_ids: + if date_start > interval.start_date: + date_interval_start = date_start + else: + date_interval_start = interval.start_date + + if interval.end_date: + if date_end < interval.end_date: + date_interval_end = date_end + else: + date_interval_end = interval.end_date + else: + date_interval_end = date_end + + power_surplus = self.get_power_by_prod(injection_id, date_interval_start, date_interval_end) + AccountLine = self.env['acc.account.line'] + + # Création de la ligne du mois + acc_account_line = AccountLine.create({ + 'quantity': power_surplus[1]['power'], + 'price_unit': interval.price, + 'acc_account_id': acc_account.id, + 'start_date': date_interval_start, + 'end_date': date_interval_end, + 'description': 'Période du ' + str(date_interval_start) + ' au ' + str(date_interval_end) + }) + account_list.append(acc_account) action = self.env["ir.actions.actions"]._for_xml_id( "acc_account.action_acc_account" @@ -257,6 +325,11 @@ class AccOperation(models.Model): return action def _open_x2m_matrix(self, view_xmlid): + """ + Fonction qui appelle le wizard matrix 2D pour la défintion des prix par consommateur/producteur + :param view_xmlid: Vue appelée + :return: action + """ wiz = self.env["acc.sale.price.wizard"].create({}) view_id = self.env.ref( "acc_account.%s" % view_xmlid, diff --git a/report/account_template.xml b/report/account_template.xml index e16caebee0198a9ef6dc566bb583743634cad152..c54316011a8ab4c017e7700f35e059fd1f6783d6 100644 --- a/report/account_template.xml +++ b/report/account_template.xml @@ -109,14 +109,14 @@ <t t-set="current_subtotal" t-value="0"/> <t t-foreach="lines" t-as="line"> <t t-set="current_subtotal" t-value="current_subtotal + line.price_total"/> -<!-- <t t-set="current_subtotal" t-value="current_subtotal + line.price_total" groups="account.group_show_line_subtotals_tax_included"/>--> - <tr class="border-black"> <t name="account_invoice_line_accountable"> - <td name="account_invoice_line_name"><span t-field="line.description" t-options="{'widget': 'text'}"/></td> + <td name="account_invoice_line_name"> + <span t-field="line.description" t-options="{'widget': 'text'}"/><br/> + à <span class="text-nowrap" t-field="line.price_unit"/> € + </td> <td class="text-right"> <span class="text-nowrap" t-field="line.price_total"/> -<!-- <span class="text-nowrap" t-field="line.price_total" groups="account.group_show_line_subtotals_tax_included"/>--> </td> </t> </tr> diff --git a/views/acc_account_views.xml b/views/acc_account_views.xml index afa3c482cac259173518ac3eeb6613ce55f20196..0c3a336f8e8d406c7f4bbc15dcb1d6a7610bca83 100644 --- a/views/acc_account_views.xml +++ b/views/acc_account_views.xml @@ -209,7 +209,7 @@ widget="section_and_note_one2many" mode="tree,kanban" context="{'default_currency_id': currency_id}"> - <tree editable="bottom" string="Lignes de facture"> + <tree editable="bottom" string="Lignes de facture" default_order="description asc"> <field name="acc_account_id" options="{'no_create': True}" invisible="1"/> <field name="description"/>