Skip to content

Commit

Permalink
BAH-3779|Deepthi M|Fixed the issue with creating order lines when sal…
Browse files Browse the repository at this point in the history
…e price mark up rule is enabled (#170)

* BAH-3779|Deepthi M|Fixed the issue with creating order lines when sale price mark up rule is enabled

* BAH-3779-fix|Deepthi M|Refactored the code.
  • Loading branch information
deepthi-mantena authored May 27, 2024
1 parent 9ea6f40 commit e3ed6bb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
46 changes: 20 additions & 26 deletions bahmni_api_feed/models/order_save_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,27 +349,23 @@ def get_available_batch_details(self, product_id, sale_order):
context['search_in_child'] = True
shop_location_id = sale_order.shop_id.location_id.id if sale_order.shop_id.location_id.id else self.order_id.shop_id.location_id.id
stock_quant_lot = self.env['stock.quant'].search([
('product_id','=', product_id.id if type(product_id) != list else product_id[0]),
('product_id', '=', product_id.id if type(product_id) != list else product_id[0]),
('location_id', '=', shop_location_id),
('quantity', '>' , 0)
('quantity', '>', 0)
])
already_used_batch_ids = [line.lot_id.id for line in sale_order.order_line if line.lot_id]
available_batches = {}
total_quantity = 0
available_batches = []
for prodlot in stock_quant_lot:
if prodlot.lot_id.id not in already_used_batch_ids:
if prodlot.lot_id.expiration_date and prodlot.quantity > 0:
date_length = len(str(prodlot.lot_id.expiration_date))
formatted_ts = datetime.strptime(str(prodlot.lot_id.expiration_date), "%Y-%m-%d %H:%M:%S.%f").strftime("%Y-%m-%d %H:%M:%S") if date_length > 20 else datetime.strptime(str(prodlot.lot_id.expiration_date), "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d %H:%M:%S")
if formatted_ts and datetime.strptime(str(formatted_ts), DTF) > datetime.today():
available_batches[prodlot.lot_id.id] = {'expiration_date': prodlot.lot_id.expiration_date, 'stock_forecast': prodlot.quantity}
total_quantity += prodlot.quantity
available_batches.append(prodlot)
elif prodlot.quantity > 0:
available_batches[prodlot.lot_id.id] = {'expiration_date': False, 'stock_forecast': prodlot.quantity}
total_quantity += prodlot.quantity
available_batches.append(prodlot)
return available_batches


@api.model
def _create_sale_order_line_function(self, sale_order, order):
stored_prod_ids = self._get_product_ids(order)
Expand All @@ -378,8 +374,8 @@ def _create_sale_order_line_function(self, sale_order, order):
prod_obj = self.env['product.product'].browse(prod_id)
sale_order_line_obj = self.env['sale.order.line']
# Get available batches
prod_lot = self.get_available_batch_details(prod_id, sale_order)
sorted_batches = sorted(prod_lot.items(), key=lambda x: x[1]['expiration_date'])
prod_lots = self.get_available_batch_details(prod_id, sale_order)
sorted_batches = sorted(prod_lots, key=lambda x: x.lot_id.expiration_date)
actual_quantity = order['quantity']
default_quantity_total = self.env['res.config.settings'].group_default_quantity
_logger.info(f"default_quantity_total: {default_quantity_total}")
Expand Down Expand Up @@ -415,23 +411,22 @@ def _create_sale_order_line_function(self, sale_order, order):
else:
self._create_single_order_line(sale_order, prod_id, prod_obj, sorted_batches, actual_quantity, order_line_uom, description, order_line_dispensed, order)


def _create_multiple_order_lines(self, sale_order, prod_id, prod_obj, sorted_batches, product_uom_qty, order_line_uom, description, order_line_dispensed, order):
sale_order_line_obj = self.env['sale.order.line']
total_quantity_available = sum([lot['stock_forecast'] for _, lot in sorted_batches])
available_batches = [(lot_id, lot) for lot_id, lot in sorted_batches if lot['stock_forecast'] > 0]
# Calculate total_quantity_available from prodlot objects
total_quantity_available = sum([lot.quantity for lot in sorted_batches])

if not available_batches or total_quantity_available < product_uom_qty:
if total_quantity_available < product_uom_qty:
_logger.info("Creating single order line")
self._create_single_order_line(sale_order, prod_id, prod_obj, sorted_batches, product_uom_qty, order_line_uom, description, order_line_dispensed, order)
else:
_logger.info("Creating multiple order lines")
for lot_id, lot in available_batches:
for lot in sorted_batches:
if product_uom_qty <= 0:
break
qty_to_create = min(product_uom_qty, lot['stock_forecast'])
qty_to_create = min(product_uom_qty, lot.quantity)
product_uom_qty -= qty_to_create
self._create_order_line(sale_order_line_obj, sale_order, prod_id, prod_obj, lot_id, lot, qty_to_create, order_line_uom, description, order_line_dispensed, order)
self._create_order_line(sale_order_line_obj, sale_order, prod_id, prod_obj, lot, qty_to_create, order_line_uom, description, order_line_dispensed, order)

_logger.info("Completed _create_multiple_order_lines")

Expand All @@ -440,12 +435,9 @@ def _create_single_order_line(self, sale_order, prod_id, prod_obj, sorted_batche

nearest_batch = sorted_batches[0] if sorted_batches else None
if nearest_batch:
lot_id, lot = nearest_batch
qty_to_create = min(actual_quantity, lot['stock_forecast'])
self._create_order_line(sale_order_line_obj, sale_order, prod_id, prod_obj, nearest_batch, actual_quantity, order_line_uom, description, order_line_dispensed, order)

self._create_order_line(sale_order_line_obj, sale_order, prod_id, prod_obj, lot_id, lot, actual_quantity, order_line_uom, description, order_line_dispensed, order)

def _create_order_line(self, sale_order_line_obj, sale_order, prod_id, prod_obj, lot_id, lot, qty_to_create, order_line_uom, description, order_line_dispensed, order):
def _create_order_line(self, sale_order_line_obj, sale_order, prod_id, prod_obj, lot, qty_to_create, order_line_uom, description, order_line_dispensed, order):
sale_order_line = {
'product_id': prod_id[0],
'price_unit': prod_obj.list_price,
Expand All @@ -457,8 +449,8 @@ def _create_order_line(self, sale_order_line_obj, sale_order, prod_id, prod_obj,
'name': description,
'state': 'draft',
'dispensed': order_line_dispensed,
'lot_id': lot_id,
'expiry_date': lot['expiration_date'],
'lot_id': lot.lot_id.id,
'expiry_date': lot.lot_id.expiration_date,
}

sale_line = sale_order_line_obj.create(sale_order_line)
Expand All @@ -477,12 +469,14 @@ def _create_order_line(self, sale_order_line_obj, sale_order, prod_id, prod_obj,
price = self.env['account.tax']._fix_tax_included_price_company(sale_line._get_display_price(), prod_obj.taxes_id, sale_line.tax_id, sale_line.company_id)
_logger.info(f"price: {price}")

# Check if lot has attribute sale_price before accessing it
if lot and bool(self.env['ir.config_parameter'].sudo().get_param('bahmni_sale.sale_price_markup')) == True:
sale_line.price_unit = lot.sale_price if lot.sale_price > 0.0 else sale_line.price_unit
sale_line.price_unit = lot.sale_price if hasattr(lot, 'sale_price') and lot.sale_price > 0.0 else sale_line.price_unit
else:
sale_line.price_unit = price if price > 0.0 else sale_line.price_unit



def _fetch_parent(self, all_orders, child_order):
for order in all_orders:
if(order.get("orderId") == child_order.get("previousOrderId")):
Expand Down
2 changes: 1 addition & 1 deletion bahmni_sale/models/res_config_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ResConfigSettings(models.TransientModel):
is_delivery_automated = fields.Boolean(string="Enable auto delivery on sale order confirm action", config_parameter="bahmni_sale.is_delivery_automated")
sale_price_markup = fields.Boolean(string="Sale Price Markup Rule", config_parameter="bahmni_sale.sale_price_markup" )
is_invoice_automated = fields.Boolean(string="Enable auto invoice on sale order confirm action", config_parameter="bahmni_sale.is_invoice_automated")
allocate_quantity_from_multiple_batches = fields.Boolean(string="allocate quantity from multiple batches", config_parameter="bahmni_sale.allocate_quantity_from_multiple_batches")
allocate_quantity_from_multiple_batches = fields.Boolean(string="Allocate quantity from multiple batches", config_parameter="bahmni_sale.allocate_quantity_from_multiple_batches")

group_final_so_charge = fields.Boolean(string="Allow to enter final Sale Order charge",
implied_group='bahmni_sale.group_allow_change_so_charge')
Expand Down
2 changes: 1 addition & 1 deletion bahmni_sale/views/res_config_settings_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<field name="allocate_quantity_from_multiple_batches"/>
</div>
<div class="o_setting_right_pane">
<label for="allocate_quantity_from_multiple_batches" string="allocate quantity from multiple batches"/>
<label for="allocate_quantity_from_multiple_batches" string="Allocate quantity from multiple batches"/>
<div class="text-muted">
Quotation should select product from multiple batches if the nearest expiry batch does not have sufficient quantity when order syncs from Bahmni.
</div>
Expand Down

0 comments on commit e3ed6bb

Please sign in to comment.