diff --git a/models/acc_operation.py b/models/acc_operation.py index 58e44df74c93c05d157fa3c86d88c8584a98b8e2..f7c8bf63db20dccfa0b8df5330e1709a752ded65 100644 --- a/models/acc_operation.py +++ b/models/acc_operation.py @@ -72,10 +72,11 @@ class AccOperation(models.Model): ] return action - def get_power_by_cons(self, date_start, date_end): + def get_power_by_cons(self, acc_delivery_id, date_start, date_end): power_tab_by_cons = self.env['acc.enedis.cdc'].read_group( [('acc_operation_id', '=', self.id), + ('acc_counter_id', '=', acc_delivery_id.id), ('date_slot', '>=', date_start), ('date_slot', '<=', date_end), ('comp_data_type', '=', 'cons')], @@ -121,46 +122,72 @@ class AccOperation(models.Model): for acc_injection_id in self.acc_injection_ids: if acc_injection_id.is_account_auto_activate: - power_tab_by_prod = self.get_power_by_prod(acc_injection_id, date_start, date_end) + 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(date_start, date_end) - for power_cons in power_tab_by_cons: - - power_month = (power_cons['power'] / 1000 / 2) * (power_autoprod_percent / 100) - - price_kwh = self.env['acc.sale.price'].search([ - ('acc_operation_id', '=', self.id), - ('acc_injection_id', '=', acc_injection_id.id), - ('acc_delivery_id', '=', power_cons['acc_counter_id'][0]), - ('start_date', '<=', date_month), - ('end_date', '>=', date_month) - ]).price + 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) Account = self.env['acc.account'] - AccountLine = self.env['acc.account.line'] acc_account = Account.create({ 'acc_operation_id': self.id, 'acc_injection_id': acc_injection_id.id, - 'acc_delivery_id': power_cons['acc_counter_id'][0], - 'power_cons': power_month, + 'acc_delivery_id': acc_delivery_id.id, + 'power_cons': power_total, 'start_date': date_start, 'end_date': date_end, - 'price_kwh': price_kwh, 'tax_tcfe': self.tax_tcfe, }) - # Création de la ligne du mois - acc_account_line = AccountLine.create({ - 'quantity': power_month, - '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) + 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), + '|', ('start_date', '<=', date_start), + ('end_date', '>=', date_end) + ]) + 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 + + for interval in interval_ids: + if date_start > interval.start_date: + date_interval_start = date_start + else: + date_interval_start = interval.start_date + + if date_end < interval.end_date: + date_interval_end = date_end + 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) + + 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) + + AccountLine = self.env['acc.account.line'] + + # Création de la ligne de facture par intervalle de période de prix + acc_account_line = AccountLine.create({ + 'quantity': power_total, + '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" diff --git a/views/acc_operation_views.xml b/views/acc_operation_views.xml index d024ee2f770e638e32eb4cf9a796f0bdad1c1e42..cc4e78e68f24b2a8ede7a684de104609e2dd46cc 100644 --- a/views/acc_operation_views.xml +++ b/views/acc_operation_views.xml @@ -56,9 +56,9 @@ attrs="{'invisible': [('is_account_active', '=', False)]}"/> </header> <field name="acc_sale_price_ids" widget="one2many"> - <tree editable="bottom"> - <field name="acc_delivery_id" /> + <tree editable="bottom" default_order="acc_injection_id,acc_delivery_id,start_date desc"> <field name="acc_injection_id" /> + <field name="acc_delivery_id" /> <field name="start_date" /> <field name="end_date" /> <field name="currency_id" invisible="1"/> diff --git a/views/acc_sale_price_views.xml b/views/acc_sale_price_views.xml index 99e803261c7dbee9c263a8908311f17fd6a35140..40848846e2649be28d4c7964b0f7e8d61a3f3357 100644 --- a/views/acc_sale_price_views.xml +++ b/views/acc_sale_price_views.xml @@ -32,10 +32,10 @@ <field name="name">acc.acc_sale_price.tree</field> <field name="model">acc.sale.price</field> <field name="arch" type="xml"> - <tree string="Prix de vente"> + <tree string="Prix de vente" default_order="start_date desc"> <field name="acc_operation_id"/> - <field name="acc_delivery_id"/> <field name="acc_injection_id"/> + <field name="acc_delivery_id"/> <field name="start_date"/> <field name="end_date"/> <field name="currency_id" invisible="1"/>