SlideShare a Scribd company logo
What is Monkey Patching &
How It Can Be Applied in Odoo
17
Enterprise
Introduction
Enterprise
This slide explores the concept of "monkey patching," a
technique used to augment existing functionality in Odoo 17.
Let's delve into what monkey patching entails and how it can be
implemented within the Odoo 17 environment.
Enterprise
What is Monkey Patching?
Monkey patching is a technique that allows for the modification
or extension of the behavior of existing code at runtime. This
approach facilitates changes to the code without modifying the
source code itself. It is particularly useful for introducing new
features, fixing bugs, or overriding existing code without directly
altering the source code.
Enterprise
A typical monkey patching implementation follows a
structured approach:
# Original class
class MyClass:
def original_method(self):
print("This is the original method")
# Monkey patching the class with a new method
def new_method(self):
print("This is the new method")
MyClass.original_method = new_method
Enterprise
By assigning new_method to MyClass.original_method, the
original method is replaced with the new method. Consequently,
whenever the original method is called, the new method is
executed, allowing changes without modifying the original
method itself.
Enterprise
Example: Splitting Deliveries in Sale Orders
Let's explore a scenario where we need to split deliveries for
each product listed in a sales order. Typically, a single delivery,
referred to as stock picking, is generated for the entire sales
order, including all products. To achieve this, we need to
introduce a field in the order line to specify the delivery date for
each product and modify the _assign_picking method within the
stock.move model to create a pick for each move.
Enterprise
Adding a Field in the Order Line
First, we need to add a delivery date field within the sales order line:
class SaleOrderline(models.Model):
"""Inheriting sale order line to add delivery date field"""
_inherit = 'sale.order.line'
delivery_date = fields.Date("Delivery Date")
Enterprise
Next, we incorporate the field into the views:
<odoo>
<record id="view_order_form" model="ir.ui.view">
<field name="name">sale.order.inherit.monkey.patching</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="model">sale.order</field>
<field name="arch" type="xml">
<xpath
expr="//field[@name='order_line']/tree/field[@name='price_unit']"
position="before">
<field name="delivery_date"/>
</xpath>
</field>
</record>
</odoo>
Enterprise
Modifying the _prepare_procurement_values Method
To split deliveries based on the delivery date, we need to
monkey patch the _prepare_procurement_values method of
the sale order line model:
Enterprise
def _prepare_procurement_values(self, group_id=False):
order_date = self.date_order
order_id = self.order_id
deadline_date = self.delivery_date or (order_id.date_order + timedelta(days=self.customer_lead or
0.0))
planned_date = deadline_date - timedelta(days=self.order_id.company_id.security_lead)
values = {
'group_id': group_id,
'sale_line_id': self.id,
'date_planned': planned_date,
'date_deadline': deadline_date,
'route_ids': self.route_id,
'warehouse_id': order_id.warehouse_id or False,
'product_description_variants':
self.with_context(lang=order_id.partner_id.lang)._get_sale_order_line_multiline_description_variants(),
'company_id': order_id.company_id,
'product_packaging_id': self.product_packaging_id,
'sequence': self.sequence,
}
return values
SaleOrderLine._prepare_procurement_values = _prepare_procurement_values
Enterprise
In this instance, we adjust the date_deadline value to match
the delivery date specified in the corresponding order line. If
the delivery date is not specified, we set it to the order date
plus the customer lead time of the product.
Enterprise
Inheriting the stock.move Model
Next, we extend the functionality of the stock.move model by
inheriting it:
class StockMoveInherits(models.Model):
"""Inheriting the stock move model"""
_inherit = 'stock.move'
Enterprise
Adjusting the _assign_picking Method
Finally, we modify the _assign_picking method to accomplish
the splitting of deliveries:
Enterprise
def _assign_picking(self):
pickings = self.env['stock.picking']
grouped_moves = groupby(self, key=lambda m: m._key_assign_picking())
for group, moves in grouped_moves:
moves = self.env['stock.move'].concat(*moves)
new_picking = False
pickings = moves[0]._search_picking_for_assignation()
if pickings:
vals = {}
if any(pickings.partner_id.id != m.partner_id.id for m in moves):
vals['partner_id'] = False
if any(pickings.origin != m.origin for m in moves):
vals['origin'] = False
if vals:
pickings.write(vals)
Enterprise
else:
moves = moves.filtered(lambda m: float_compare(m.product_uom_qty, 0.0,
precision_rounding=m.product_uom.rounding) >= 0)
if not moves:
continue
new_picking = True
pick_values = moves._get_new_picking_values()
sale_order = self.env['sale.order'].search([('name', '=',
pick_values['origin'])])
for move in moves:
picking = pickings.create(move._get_new_picking_values())
move.write({'picking_id': picking.id})
move._assign_picking_post_process(new=new_picking)
return True
StockMove._assign_picking = _assign_picking
Enterprise
In this implementation:
● We initialize pickings as an empty record of the
stock.picking model.
● We group the moves based on a key derived from the
_key_assign_picking method.
● We search for a picking using the
_search_picking_for_assignation method.
● If a picking is found, we update its fields; otherwise, we
create a new picking for each move.
Enterprise
Through these applied monkey patches, we have successfully
implemented the functionality to split deliveries within the
sales order. While monkey patching is a powerful tool, it's
advisable to explore alternative approaches when appropriate.
For More Info.
Check our company website for related blogs
and Odoo book.
Check our YouTube channel for
functional and technical videos in Odoo.
Enterprise
www.cybrosys.com
Ad

More Related Content

Similar to What is Monkey Patching & How It Can Be Applied in Odoo 17 (20)

How To Extend Odoo Form View using js_class_
How To Extend Odoo Form View using js_class_How To Extend Odoo Form View using js_class_
How To Extend Odoo Form View using js_class_
Celine George
 
How to perform product search based on a custom field from the PoS screen of ...
How to perform product search based on a custom field from the PoS screen of ...How to perform product search based on a custom field from the PoS screen of ...
How to perform product search based on a custom field from the PoS screen of ...
Celine George
 
How to Create Cohort View in Odoo 17 - Odoo 17 Slides
How to Create Cohort View in Odoo 17 - Odoo 17 SlidesHow to Create Cohort View in Odoo 17 - Odoo 17 Slides
How to Create Cohort View in Odoo 17 - Odoo 17 Slides
Celine George
 
Asp netmvc e03
Asp netmvc e03Asp netmvc e03
Asp netmvc e03
Yu GUAN
 
Pruebas unitarias con django
Pruebas unitarias con djangoPruebas unitarias con django
Pruebas unitarias con django
Tomás Henríquez
 
Forms, Getting Your Money's Worth
Forms, Getting Your Money's WorthForms, Getting Your Money's Worth
Forms, Getting Your Money's Worth
Alex Gaynor
 
Solid angular
Solid angularSolid angular
Solid angular
Nir Kaufman
 
Patterns in PHP
Patterns in PHPPatterns in PHP
Patterns in PHP
Diego Lewin
 
Programming Primer EncapsulationVB
Programming Primer EncapsulationVBProgramming Primer EncapsulationVB
Programming Primer EncapsulationVB
sunmitraeducation
 
Company segmentation - an approach with R
Company segmentation - an approach with RCompany segmentation - an approach with R
Company segmentation - an approach with R
Casper Crause
 
How to Setup Default Value for a Field in Odoo 17
How to Setup Default Value for a Field in Odoo 17How to Setup Default Value for a Field in Odoo 17
How to Setup Default Value for a Field in Odoo 17
Celine George
 
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
Damien Carbery
 
PyCon KR 2018 Effective Tips for Django ORM in Practice
PyCon KR 2018 Effective Tips for Django ORM in PracticePyCon KR 2018 Effective Tips for Django ORM in Practice
PyCon KR 2018 Effective Tips for Django ORM in Practice
Seomgi Han
 
Behavioral pattern 4
Behavioral pattern 4Behavioral pattern 4
Behavioral pattern 4
Naga Muruga
 
How to make a component and add it to systray in Odoo
How to make a component and add it to systray in OdooHow to make a component and add it to systray in Odoo
How to make a component and add it to systray in Odoo
Celine George
 
Programming Primer Encapsulation CS
Programming Primer Encapsulation CSProgramming Primer Encapsulation CS
Programming Primer Encapsulation CS
sunmitraeducation
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
Celine George
 
Competition 1 (blog 1)
Competition 1 (blog 1)Competition 1 (blog 1)
Competition 1 (blog 1)
TarunPaparaju
 
How to Super Create Write Functions in Odoo 17
How to Super Create Write Functions in Odoo 17How to Super Create Write Functions in Odoo 17
How to Super Create Write Functions in Odoo 17
Celine George
 
Difference between-action-support
Difference between-action-supportDifference between-action-support
Difference between-action-support
Abdul Mujeeb Shaik
 
How To Extend Odoo Form View using js_class_
How To Extend Odoo Form View using js_class_How To Extend Odoo Form View using js_class_
How To Extend Odoo Form View using js_class_
Celine George
 
How to perform product search based on a custom field from the PoS screen of ...
How to perform product search based on a custom field from the PoS screen of ...How to perform product search based on a custom field from the PoS screen of ...
How to perform product search based on a custom field from the PoS screen of ...
Celine George
 
How to Create Cohort View in Odoo 17 - Odoo 17 Slides
How to Create Cohort View in Odoo 17 - Odoo 17 SlidesHow to Create Cohort View in Odoo 17 - Odoo 17 Slides
How to Create Cohort View in Odoo 17 - Odoo 17 Slides
Celine George
 
Asp netmvc e03
Asp netmvc e03Asp netmvc e03
Asp netmvc e03
Yu GUAN
 
Pruebas unitarias con django
Pruebas unitarias con djangoPruebas unitarias con django
Pruebas unitarias con django
Tomás Henríquez
 
Forms, Getting Your Money's Worth
Forms, Getting Your Money's WorthForms, Getting Your Money's Worth
Forms, Getting Your Money's Worth
Alex Gaynor
 
Programming Primer EncapsulationVB
Programming Primer EncapsulationVBProgramming Primer EncapsulationVB
Programming Primer EncapsulationVB
sunmitraeducation
 
Company segmentation - an approach with R
Company segmentation - an approach with RCompany segmentation - an approach with R
Company segmentation - an approach with R
Casper Crause
 
How to Setup Default Value for a Field in Odoo 17
How to Setup Default Value for a Field in Odoo 17How to Setup Default Value for a Field in Odoo 17
How to Setup Default Value for a Field in Odoo 17
Celine George
 
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
Damien Carbery
 
PyCon KR 2018 Effective Tips for Django ORM in Practice
PyCon KR 2018 Effective Tips for Django ORM in PracticePyCon KR 2018 Effective Tips for Django ORM in Practice
PyCon KR 2018 Effective Tips for Django ORM in Practice
Seomgi Han
 
Behavioral pattern 4
Behavioral pattern 4Behavioral pattern 4
Behavioral pattern 4
Naga Muruga
 
How to make a component and add it to systray in Odoo
How to make a component and add it to systray in OdooHow to make a component and add it to systray in Odoo
How to make a component and add it to systray in Odoo
Celine George
 
Programming Primer Encapsulation CS
Programming Primer Encapsulation CSProgramming Primer Encapsulation CS
Programming Primer Encapsulation CS
sunmitraeducation
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
Celine George
 
Competition 1 (blog 1)
Competition 1 (blog 1)Competition 1 (blog 1)
Competition 1 (blog 1)
TarunPaparaju
 
How to Super Create Write Functions in Odoo 17
How to Super Create Write Functions in Odoo 17How to Super Create Write Functions in Odoo 17
How to Super Create Write Functions in Odoo 17
Celine George
 
Difference between-action-support
Difference between-action-supportDifference between-action-support
Difference between-action-support
Abdul Mujeeb Shaik
 

More from Celine George (20)

How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18
Celine George
 
How to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 SalesHow to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 Sales
Celine George
 
How to Add Customer Note in Odoo 18 POS - Odoo Slides
How to Add Customer Note in Odoo 18 POS - Odoo SlidesHow to Add Customer Note in Odoo 18 POS - Odoo Slides
How to Add Customer Note in Odoo 18 POS - Odoo Slides
Celine George
 
How to Create A Todo List In Todo of Odoo 18
How to Create A Todo List In Todo of Odoo 18How to Create A Todo List In Todo of Odoo 18
How to Create A Todo List In Todo of Odoo 18
Celine George
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18
Celine George
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
How to Manage a Customer Account in Odoo 17 Sales
How to Manage a Customer Account in Odoo 17 SalesHow to Manage a Customer Account in Odoo 17 Sales
How to Manage a Customer Account in Odoo 17 Sales
Celine George
 
How to Open a Wizard When Clicking on the Kanban Tile in Odoo 18
How to Open a Wizard When Clicking on the Kanban Tile in Odoo 18How to Open a Wizard When Clicking on the Kanban Tile in Odoo 18
How to Open a Wizard When Clicking on the Kanban Tile in Odoo 18
Celine George
 
Ledger Posting in odoo Continental Accounting
Ledger Posting in odoo Continental AccountingLedger Posting in odoo Continental Accounting
Ledger Posting in odoo Continental Accounting
Celine George
 
How to Create & Manage a New User Menu in Odoo 18
How to Create & Manage a New User Menu in Odoo 18How to Create & Manage a New User Menu in Odoo 18
How to Create & Manage a New User Menu in Odoo 18
Celine George
 
How to Add Customer Rating Mixin in the Odoo 18
How to Add Customer Rating Mixin in the Odoo 18How to Add Customer Rating Mixin in the Odoo 18
How to Add Customer Rating Mixin in the Odoo 18
Celine George
 
How To Open The Form View Of Many2many Clicking Tag In Odoo 18
How To Open The Form View Of Many2many Clicking Tag In Odoo 18How To Open The Form View Of Many2many Clicking Tag In Odoo 18
How To Open The Form View Of Many2many Clicking Tag In Odoo 18
Celine George
 
How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18
Celine George
 
How to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 SalesHow to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 Sales
Celine George
 
How to Add Customer Note in Odoo 18 POS - Odoo Slides
How to Add Customer Note in Odoo 18 POS - Odoo SlidesHow to Add Customer Note in Odoo 18 POS - Odoo Slides
How to Add Customer Note in Odoo 18 POS - Odoo Slides
Celine George
 
How to Create A Todo List In Todo of Odoo 18
How to Create A Todo List In Todo of Odoo 18How to Create A Todo List In Todo of Odoo 18
How to Create A Todo List In Todo of Odoo 18
Celine George
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18
Celine George
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
How to Manage a Customer Account in Odoo 17 Sales
How to Manage a Customer Account in Odoo 17 SalesHow to Manage a Customer Account in Odoo 17 Sales
How to Manage a Customer Account in Odoo 17 Sales
Celine George
 
How to Open a Wizard When Clicking on the Kanban Tile in Odoo 18
How to Open a Wizard When Clicking on the Kanban Tile in Odoo 18How to Open a Wizard When Clicking on the Kanban Tile in Odoo 18
How to Open a Wizard When Clicking on the Kanban Tile in Odoo 18
Celine George
 
Ledger Posting in odoo Continental Accounting
Ledger Posting in odoo Continental AccountingLedger Posting in odoo Continental Accounting
Ledger Posting in odoo Continental Accounting
Celine George
 
How to Create & Manage a New User Menu in Odoo 18
How to Create & Manage a New User Menu in Odoo 18How to Create & Manage a New User Menu in Odoo 18
How to Create & Manage a New User Menu in Odoo 18
Celine George
 
How to Add Customer Rating Mixin in the Odoo 18
How to Add Customer Rating Mixin in the Odoo 18How to Add Customer Rating Mixin in the Odoo 18
How to Add Customer Rating Mixin in the Odoo 18
Celine George
 
How To Open The Form View Of Many2many Clicking Tag In Odoo 18
How To Open The Form View Of Many2many Clicking Tag In Odoo 18How To Open The Form View Of Many2many Clicking Tag In Odoo 18
How To Open The Form View Of Many2many Clicking Tag In Odoo 18
Celine George
 
Ad

Recently uploaded (20)

Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
Ad

What is Monkey Patching & How It Can Be Applied in Odoo 17

  • 1. What is Monkey Patching & How It Can Be Applied in Odoo 17 Enterprise
  • 2. Introduction Enterprise This slide explores the concept of "monkey patching," a technique used to augment existing functionality in Odoo 17. Let's delve into what monkey patching entails and how it can be implemented within the Odoo 17 environment.
  • 3. Enterprise What is Monkey Patching? Monkey patching is a technique that allows for the modification or extension of the behavior of existing code at runtime. This approach facilitates changes to the code without modifying the source code itself. It is particularly useful for introducing new features, fixing bugs, or overriding existing code without directly altering the source code.
  • 4. Enterprise A typical monkey patching implementation follows a structured approach: # Original class class MyClass: def original_method(self): print("This is the original method") # Monkey patching the class with a new method def new_method(self): print("This is the new method") MyClass.original_method = new_method
  • 5. Enterprise By assigning new_method to MyClass.original_method, the original method is replaced with the new method. Consequently, whenever the original method is called, the new method is executed, allowing changes without modifying the original method itself.
  • 6. Enterprise Example: Splitting Deliveries in Sale Orders Let's explore a scenario where we need to split deliveries for each product listed in a sales order. Typically, a single delivery, referred to as stock picking, is generated for the entire sales order, including all products. To achieve this, we need to introduce a field in the order line to specify the delivery date for each product and modify the _assign_picking method within the stock.move model to create a pick for each move.
  • 7. Enterprise Adding a Field in the Order Line First, we need to add a delivery date field within the sales order line: class SaleOrderline(models.Model): """Inheriting sale order line to add delivery date field""" _inherit = 'sale.order.line' delivery_date = fields.Date("Delivery Date")
  • 8. Enterprise Next, we incorporate the field into the views: <odoo> <record id="view_order_form" model="ir.ui.view"> <field name="name">sale.order.inherit.monkey.patching</field> <field name="inherit_id" ref="sale.view_order_form"/> <field name="model">sale.order</field> <field name="arch" type="xml"> <xpath expr="//field[@name='order_line']/tree/field[@name='price_unit']" position="before"> <field name="delivery_date"/> </xpath> </field> </record> </odoo>
  • 9. Enterprise Modifying the _prepare_procurement_values Method To split deliveries based on the delivery date, we need to monkey patch the _prepare_procurement_values method of the sale order line model:
  • 10. Enterprise def _prepare_procurement_values(self, group_id=False): order_date = self.date_order order_id = self.order_id deadline_date = self.delivery_date or (order_id.date_order + timedelta(days=self.customer_lead or 0.0)) planned_date = deadline_date - timedelta(days=self.order_id.company_id.security_lead) values = { 'group_id': group_id, 'sale_line_id': self.id, 'date_planned': planned_date, 'date_deadline': deadline_date, 'route_ids': self.route_id, 'warehouse_id': order_id.warehouse_id or False, 'product_description_variants': self.with_context(lang=order_id.partner_id.lang)._get_sale_order_line_multiline_description_variants(), 'company_id': order_id.company_id, 'product_packaging_id': self.product_packaging_id, 'sequence': self.sequence, } return values SaleOrderLine._prepare_procurement_values = _prepare_procurement_values
  • 11. Enterprise In this instance, we adjust the date_deadline value to match the delivery date specified in the corresponding order line. If the delivery date is not specified, we set it to the order date plus the customer lead time of the product.
  • 12. Enterprise Inheriting the stock.move Model Next, we extend the functionality of the stock.move model by inheriting it: class StockMoveInherits(models.Model): """Inheriting the stock move model""" _inherit = 'stock.move'
  • 13. Enterprise Adjusting the _assign_picking Method Finally, we modify the _assign_picking method to accomplish the splitting of deliveries:
  • 14. Enterprise def _assign_picking(self): pickings = self.env['stock.picking'] grouped_moves = groupby(self, key=lambda m: m._key_assign_picking()) for group, moves in grouped_moves: moves = self.env['stock.move'].concat(*moves) new_picking = False pickings = moves[0]._search_picking_for_assignation() if pickings: vals = {} if any(pickings.partner_id.id != m.partner_id.id for m in moves): vals['partner_id'] = False if any(pickings.origin != m.origin for m in moves): vals['origin'] = False if vals: pickings.write(vals)
  • 15. Enterprise else: moves = moves.filtered(lambda m: float_compare(m.product_uom_qty, 0.0, precision_rounding=m.product_uom.rounding) >= 0) if not moves: continue new_picking = True pick_values = moves._get_new_picking_values() sale_order = self.env['sale.order'].search([('name', '=', pick_values['origin'])]) for move in moves: picking = pickings.create(move._get_new_picking_values()) move.write({'picking_id': picking.id}) move._assign_picking_post_process(new=new_picking) return True StockMove._assign_picking = _assign_picking
  • 16. Enterprise In this implementation: ● We initialize pickings as an empty record of the stock.picking model. ● We group the moves based on a key derived from the _key_assign_picking method. ● We search for a picking using the _search_picking_for_assignation method. ● If a picking is found, we update its fields; otherwise, we create a new picking for each move.
  • 17. Enterprise Through these applied monkey patches, we have successfully implemented the functionality to split deliveries within the sales order. While monkey patching is a powerful tool, it's advisable to explore alternative approaches when appropriate.
  • 18. For More Info. Check our company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com