Skip to content

Commit

Permalink
[Karthi] | BAH-3598 | Added. Calculate sale price of a lot based on m…
Browse files Browse the repository at this point in the history
…arkup configuration. (#64)
  • Loading branch information
karthikeyansp91 authored Mar 4, 2024
1 parent 86cb784 commit c0790bd
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 17 deletions.
1 change: 1 addition & 0 deletions bahmni_stock/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'data': ['views/stock_production_lot_view.xml',
'views/stock_picking_view.xml',
'views/account_invoice_line.xml',
'views/stock_pick_lot_view.xml',
],
'demo': [],
'qweb': [],
Expand Down
1 change: 1 addition & 0 deletions bahmni_stock/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
from . import stock_production_lot
from . import stock_picking
from . import stock_move
from . import stock_move_line
from . import product
from . import account_invoice_line
89 changes: 89 additions & 0 deletions bahmni_stock/models/stock_move_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@

from datetime import datetime
from dateutil import tz
from collections import Counter, defaultdict

from odoo import models, fields, api
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT as DTF

from odoo.exceptions import UserError, ValidationError, AccessError, RedirectWarning

class StockMoveLine(models.Model):
_inherit = 'stock.move.line'

sale_price = fields.Float(string="Sale Price")
mrp = fields.Float(string="MRP")
cost_price = fields.Float(string="Cost Price")



@api.constrains('mrp')
def _check_fields_values(self):
for rec in self:
if rec.mrp > 0.00:
if rec.sale_price > rec.mrp:
raise ValidationError('Sales Price should not be greater than MRP Rate.')
else:
pass

@api.onchange('cost_price')
def _check_cost_price_values(self):
if self.cost_price > 0.00:
markup_table_line = self.env['price.markup.table'].search([('lower_price', '<', self.cost_price),
'|', ('higher_price', '>=', self.cost_price),
('higher_price', '=', 0)],limit=1)
if markup_table_line:
self.sale_price = self.cost_price + (self.cost_price * markup_table_line.markup_percentage / 100)

else:
pass

@api.model
def default_get(self, fields):
res = super(StockMoveLine, self).default_get(fields)
cost_value = 0.00
move_ids = self.env['stock.move'].search([('picking_id', '=', res.get('picking_id')),('product_id', '=', res.get('product_id'))],limit=1)
if move_ids:
cost_value = move_ids.purchase_line_id.price_unit + (move_ids.purchase_line_id.price_tax / move_ids.purchase_line_id.product_qty)
if cost_value > 0.00:
markup_table_line = self.env['price.markup.table'].search([('lower_price', '<', cost_value),
'|', ('higher_price', '>=', cost_value),
('higher_price', '=', 0)],limit=1)
if markup_table_line:
res.update({'cost_price': cost_value,'sale_price': cost_value + (cost_value * markup_table_line.markup_percentage / 100)})

else:
pass
return res

def _create_and_assign_production_lot(self):
""" Creates and assign new production lots for move lines."""
lot_vals = []
# It is possible to have multiple time the same lot to create & assign,
# so we handle the case with 2 dictionaries.
key_to_index = {} # key to index of the lot
key_to_mls = defaultdict(lambda: self.env['stock.move.line']) # key to all mls
for ml in self:
key = (ml.company_id.id, ml.product_id.id, ml.lot_name, ml.cost_price, ml.sale_price, ml.mrp, ml.expiration_date)
key_to_mls[key] |= ml
if ml.tracking != 'lot' or key not in key_to_index:
key_to_index[key] = len(lot_vals)
lot_vals.append(ml._get_value_production_lot())
lots = self.env['stock.lot'].create(lot_vals)
for key, mls in key_to_mls.items():
lot = lots[key_to_index[key]].with_prefetch(lots._ids) # With prefetch to reconstruct the ones broke by accessing by index
mls.write({'lot_id': lot.id})

def _get_value_production_lot(self):
self.ensure_one()

return {
'company_id': self.company_id.id,
'name': self.lot_name,
'product_id': self.product_id.id,
'cost_price': self.cost_price,
'sale_price': self.sale_price,
'mrp': self.mrp,
'expiration_date': self.expiration_date
}

15 changes: 0 additions & 15 deletions bahmni_stock/views/stock_pack_operation_view.xml

This file was deleted.

18 changes: 18 additions & 0 deletions bahmni_stock/views/stock_pick_lot_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>

<record id="view_stock_move_line_operation_tree_inherit" model="ir.ui.view">
<field name="name">stock.move.line.operations.tree.inherit</field>
<field name="model">stock.move.line</field>
<field name="inherit_id" ref="stock.view_stock_move_line_operation_tree"/>
<field name="arch" type="xml">
<xpath expr="//tree/field[@name='product_uom_id']" position="after">

<field name="cost_price"/>
<field name="sale_price"/>
<field name="mrp"/>
</xpath>
</field>
</record>

</odoo>
5 changes: 3 additions & 2 deletions bahmni_stock/views/stock_production_lot_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
<field name="model">stock.lot</field>
<field name="inherit_id" ref="stock.view_production_lot_tree"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='product_id']" position="after">
<field name="stock_forecast" invisible="0"/>
<xpath expr="//field[@name='expiration_date']" position="after">
<field name="cost_price"/>
<field name="sale_price"/>
</xpath>
</field>
</record>
Expand Down

0 comments on commit c0790bd

Please sign in to comment.