Skip to content

Commit

Permalink
substract gkwh quantity mag line
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierteres committed Aug 12, 2022
1 parent 947b03b commit a089442
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
30 changes: 29 additions & 1 deletion som_generationkwh/giscedata_facturacio.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,34 @@ def apply_gkwh(self, cursor, uid, ids, context=None):
'line_owner': lineowner_id
}
)
self.update_maj_quantity(cursor, uid, inv_id, context)

def update_maj_quantity(self, cursor, uid, inv_id, context):
rmag_lines_ids = self.get_rmag_lines(cursor, uid, inv_id, context)
if not rmag_lines_ids:
return

rmag_line = self.browse(cursor, uid, rmag_lines_ids[0])

line_obj = self.pool.get('giscedata.facturacio.factura.linia')
cfg_obj = self.pool.get('res.config')

start_date_mecanisme_ajust_gas = cfg_obj.get(
cursor, uid, 'start_date_mecanisme_ajust_gas', '2022-10-01'
)
end_date_mecanisme_ajust_gas = cfg_obj.get(
cursor, uid, 'end_date_mecanisme_ajust_gas', '2099-12-31'
)
all_gkwh_lines_ids = self.get_gkwh_lines(cursor, uid, inv_id, context)
substract_from_maj_line_ids = line_obj.search(cursor, uid,
[('id', 'in', all_gkwh_lines_ids), ('data_desde', '>=', start_date_mecanisme_ajust_gas),
('data_fins','<=', end_date_mecanisme_ajust_gas)]
)
if substract_from_maj_line_ids:
rmag_original_quantity = rmag_line.quantity
gkwh_data = line_obj.read(cursor, uid, substract_from_maj_line_ids, ['quantity'])
new_quantity = rmag_original_quantity - sum([x['quantity'] for x in gkwh_data])
line_obj.write(cursor, uid, rmag_line.id, {'quantity': new_quantity})

def get_gkwh_lines(self, cursor, uid, ids, context=None):
""" Returns a id list of giscedata.facturacio.factura.linia with
Expand Down Expand Up @@ -551,7 +579,7 @@ def _search_is_gkwh(self, cursor, uid, obj, name, args, context=None):
if not args[0][2]:
# search for False
operator = 'not in'
return [('id', operator, gkwh_ids)]
return [('id', operator, gkwh_ids)]

def _ff_is_gkwh(self, cursor, uid, ids, field_name, arg, context=None):
"""Returns true if invoice has gkwh lines"""
Expand Down
9 changes: 6 additions & 3 deletions som_generationkwh/som_generationkwh.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,13 +540,16 @@ def getProfit(self, cr, uid, line):
cr, uid, 'end_date_mecanisme_ajust_gas', '2099-12-31')

priceNoGen = float(self.getPriceWithoutGeneration(cr, uid, line)['price_unit'])
rmag_lines = self.browse(cr, uid, line).factura_id.get_rmag_lines()
if rmag_lines and \
rmag_lines_ids = self.browse(cr, uid, line).factura_id.get_rmag_lines()

if rmag_lines_ids and \
line['data_desde'] >= start_date_mecanisme_ajust_gas and \
line['data_fins'] <= end_date_mecanisme_ajust_gas:
profit = (priceNoGen + rmag_lines[0]['price_unit'] - line['price_unit']) * line['quantity']
rmag_line = self.browse(cr, uid, rmag_lines_ids[0])
profit = (priceNoGen + rmag_line.price_unit - line['price_unit']) * line['quantity']
else:
profit = (priceNoGen - line['price_unit']) * line['quantity']

if ai_obj.read(cr, uid, line['invoice_id'][0], ['type'])['type'] == 'out_refund':
return round(profit * -1, 2)
return round(profit ,2)
Expand Down

0 comments on commit a089442

Please sign in to comment.