Skip to content

Commit

Permalink
Added rmag to genkwh profit calc
Browse files Browse the repository at this point in the history
  • Loading branch information
susu105 committed Aug 12, 2022
1 parent 3631964 commit 947b03b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
40 changes: 40 additions & 0 deletions som_generationkwh/giscedata_facturacio.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,24 @@ def get_gkwh_lines(self, cursor, uid, ids, context=None):
return line_ids
return res

def get_rmag_lines(self, cursor, uid, ids, context=None):
""" Returns a id list of giscedata.facturacio.factura.linia with
RMAG products
"""
if isinstance(ids, (list, tuple)):
ids = ids[0]

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

res = []
fields_to_read = ['linies_energia']
inv_vals = self.read(cursor, uid, ids, fields_to_read, context)
if inv_vals['linies_energia']:
is_rmag = line_obj.is_rmag(cursor, uid, inv_vals['linies_energia'])
line_ids = [l for l, v in is_rmag.items() if v]
return line_ids
return res

def _search_is_gkwh(self, cursor, uid, obj, name, args, context=None):
"""Search function for is_gkwh"""
if not args:
Expand Down Expand Up @@ -713,6 +731,12 @@ def get_gkwh_products(self, cursor, uid, context=None):
)
return product_obj.search(cursor, uid, [('categ_id', 'in', cat_ids)])

@cache()
def get_rmag_products(self, cursor, uid, context=None):
"""Returns rmag products list"""
product_obj = self.pool.get('product.product')
return product_obj.search(cursor, uid, [('default_code', '=', 'RMAG')])

def is_gkwh(self, cursor, uid, ids, context=None):
"""Checks invoice line is gkwh"""
if not isinstance(ids, (tuple, list)):
Expand All @@ -728,6 +752,22 @@ def is_gkwh(self, cursor, uid, ids, context=None):
res[l['id']] = True
return res

def is_rmag(self, cursor, uid, ids, context=None):
"""Checks invoice line is rmag"""
if not isinstance(ids, (tuple, list)):
ids = [ids]

res = dict([(i, False) for i in ids])

# check if product is rmag
l_vals = self.read(cursor, uid, ids, ['product_id'], context=context)
rmag_products = self.get_rmag_products(cursor, uid)

for l in l_vals:
if l['product_id'] and l['product_id'][0] in rmag_products:
res[l['id']] = True
return res

def unlink(self, cursor, uid, ids, context=None):
"""Return gkwh rights to owner when gkwh invoice line is droped"""
if not context:
Expand Down
17 changes: 15 additions & 2 deletions som_generationkwh/som_generationkwh.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,14 +529,27 @@ def getPriceWithoutGeneration(self, cr, uid, line):

def getProfit(self, cr, uid, line):
ai_obj = self.pool.get('account.invoice')
cfg_obj = self.pool.get('res.config')

if line['quantity'] == 0:
return 0

start_date_mecanisme_ajust_gas = cfg_obj.get(
cr, uid, 'start_date_mecanisme_ajust_gas', '2022-10-01')
end_date_mecanisme_ajust_gas = cfg_obj.get(
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 \
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']
else:
profit = (priceNoGen - line['price_unit']) * line['quantity']
if ai_obj.read(cr, uid, line['invoice_id'][0], ['type'])['type'] == 'out_refund':
return round((priceNoGen - line['price_unit']) * line['quantity'] * -1,2)
return round((priceNoGen - line['price_unit']) * line['quantity'],2)
return round(profit * -1, 2)
return round(profit ,2)


def _ff_saving_generation(self, cursor, uid, ids, field_name, arg,
Expand Down

0 comments on commit 947b03b

Please sign in to comment.