| @ -0,0 +1,51 @@ | |||
| .. image:: https://img.shields.io/badge/license-AGPL--3-blue.svg | |||
|     :target: https://www.gnu.org/licenses/agpl-3.0-standalone.html | |||
|     :alt: License: AGPL-3 | |||
| 
 | |||
| Costing Method: Last Purchase Price | |||
| =================================== | |||
| This module introduces a new costing method to Odoo. That will update a product's cost price when a new purchase | |||
| happens with the purchasing rate. if you enables automatic stock valuation and provided a price difference account, | |||
| this module will generate stock journal entry to update the stock value according to the price change. | |||
| 
 | |||
| Configuration | |||
| ============= | |||
| - No additional configuration required. | |||
| 
 | |||
| Company | |||
| ------- | |||
| * `Cybrosys Techno Solutions <https://cybrosys.com/>`__ | |||
| 
 | |||
| Credits | |||
| ------- | |||
| Developers: (V16) Sruthi Renjith, | |||
|             (V17) Fathima Mazlin AM, | |||
|             (V18) Ammu Raj, Ayana KP, | |||
| Contact: odoo@cybrosys.com | |||
| 
 | |||
| Contacts | |||
| -------- | |||
| * Mail Contact : odoo@cybrosys.com | |||
| * Website : https://cybrosys.com | |||
| 
 | |||
| License | |||
| ------- | |||
| Affero General Public License, Version 3 (AGPL v3). | |||
| (https://www.gnu.org/licenses/agpl-3.0-standalone.html) | |||
| 
 | |||
| Bug Tracker | |||
| ----------- | |||
| Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. | |||
| 
 | |||
| Maintainer | |||
| ========== | |||
| .. image:: https://cybrosys.com/images/logo.png | |||
|    :target: https://cybrosys.com | |||
| 
 | |||
| This module is maintained by Cybrosys Technologies. | |||
| 
 | |||
| For support and more information, please visit `Our Website <https://cybrosys.com/>`__ | |||
| 
 | |||
| Further information | |||
| =================== | |||
| HTML Description: `<static/description/index.html>`__ | |||
| @ -0,0 +1,23 @@ | |||
| # -*- coding: utf-8 -*- | |||
| ################################################################################ | |||
| # | |||
| #    Cybrosys Technologies Pvt. Ltd. | |||
| #    Copyright (C) 2024-TODAY Cybrosys Technologies(<https://www.cybrosys.com>). | |||
| #    Author: Ammu Raj (odoo@cybrosys.com) | |||
| # | |||
| #    This program is free software: you can modify | |||
| #    it under the terms of the GNU Affero General Public License (AGPL) as | |||
| #    published by the Free Software Foundation, either version 3 of the | |||
| #    License, or (at your option) any later version. | |||
| # | |||
| #    This program is distributed in the hope that it will be useful, | |||
| #    but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
| #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | |||
| #    GNU Affero General Public License for more details. | |||
| # | |||
| #    You should have received a copy of the GNU Affero General Public License | |||
| #    along with this program.  If not, see <https://www.gnu.org/licenses/>. | |||
| # | |||
| ################################################################################ | |||
| from . import models | |||
| 
 | |||
| @ -0,0 +1,40 @@ | |||
| # -*- coding: utf-8 -*- | |||
| ################################################################################ | |||
| # | |||
| #    Cybrosys Technologies Pvt. Ltd. | |||
| #    Copyright (C) 2024-TODAY Cybrosys Technologies(<https://www.cybrosys.com>). | |||
| #    Author: Ammu Raj (odoo@cybrosys.com) | |||
| # | |||
| #    This program is free software: you can modify | |||
| #    it under the terms of the GNU Affero General Public License (AGPL) as | |||
| #    published by the Free Software Foundation, either version 3 of the | |||
| #    License, or (at your option) any later version. | |||
| # | |||
| #    This program is distributed in the hope that it will be useful, | |||
| #    but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
| #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | |||
| #    GNU Affero General Public License for more details. | |||
| # | |||
| #    You should have received a copy of the GNU Affero General Public License | |||
| #    along with this program.  If not, see <https://www.gnu.org/licenses/>. | |||
| # | |||
| ################################################################################ | |||
| { | |||
|     'name': 'Costing Method: Last Purchase Price', | |||
|     'version': '18.0.1.0.0', | |||
|     'category': 'Warehouse', | |||
|     'summary': "Introducing new costing method in Odoo 'Last Purchase Price'", | |||
|     'description': "Introducing new costing method in Odoo last purchase price'." | |||
|                    "The cost of the product changed based on the last purchase" | |||
|                    "order.", | |||
|     'author': 'Cybrosys Techno solutions', | |||
|     'company': 'Cybrosys Techno Solutions', | |||
|     'maintainer': 'Cybrosys Techno Solutions', | |||
|     'website': 'https://www.openhrms.com', | |||
|     'depends': ['stock_account', 'purchase_stock'], | |||
|     'images': ['static/description/banner.jpg'], | |||
|     'license': 'AGPL-3', | |||
|     'installable': True, | |||
|     'auto_install': False, | |||
|     'application': False, | |||
| } | |||
| @ -0,0 +1,5 @@ | |||
| ## Module <stock_last_purchase_price> | |||
| #### 15.02.2025 | |||
| #### Version 18.0.1.0.0 | |||
| #### ADD | |||
| - Initial commit for Costing Method: Last Purchase Price | |||
| @ -0,0 +1,26 @@ | |||
| # -*- coding: utf-8 -*- | |||
| ################################################################################ | |||
| # | |||
| #    Cybrosys Technologies Pvt. Ltd. | |||
| #    Copyright (C) 2024-TODAY Cybrosys Technologies(<https://www.cybrosys.com>). | |||
| #    Author: Ammu Raj (odoo@cybrosys.com) | |||
| # | |||
| #    This program is free software: you can modify | |||
| #    it under the terms of the GNU Affero General Public License (AGPL) as | |||
| #    published by the Free Software Foundation, either version 3 of the | |||
| #    License, or (at your option) any later version. | |||
| # | |||
| #    This program is distributed in the hope that it will be useful, | |||
| #    but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
| #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | |||
| #    GNU Affero General Public License for more details. | |||
| # | |||
| #    You should have received a copy of the GNU Affero General Public License | |||
| #    along with this program.  If not, see <https://www.gnu.org/licenses/>. | |||
| # | |||
| ################################################################################ | |||
| from . import product_category | |||
| from . import product_product | |||
| from . import product_template | |||
| from . import stock_move | |||
| from . import stock_move_line | |||
| @ -0,0 +1,42 @@ | |||
| # -*- coding: utf-8 -*- | |||
| ################################################################################ | |||
| # | |||
| #    Cybrosys Technologies Pvt. Ltd. | |||
| #    Copyright (C) 2024-TODAY Cybrosys Technologies(<https://www.cybrosys.com>). | |||
| #    Author: Ammu Raj(odoo@cybrosys.com) | |||
| # | |||
| #    This program is free software: you can modify | |||
| #    it under the terms of the GNU Affero General Public License (AGPL) as | |||
| #    published by the Free Software Foundation, either version 3 of the | |||
| #    License, or (at your option) any later version. | |||
| # | |||
| #    This program is distributed in the hope that it will be useful, | |||
| #    but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
| #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | |||
| #    GNU Affero General Public License for more details. | |||
| # | |||
| #    You should have received a copy of the GNU Affero General Public License | |||
| #    along with this program.  If not, see <https://www.gnu.org/licenses/>. | |||
| # | |||
| ################################################################################ | |||
| from odoo import fields, models | |||
| 
 | |||
| 
 | |||
| class ProductCategory(models.Model): | |||
|     """ Class to inherit product_category to add costing method """ | |||
|     _inherit = "product.category" | |||
| 
 | |||
|     property_cost_method = fields.Selection([ | |||
|         ('standard', 'Standard Price'), | |||
|         ('last', 'Last Purchase Price'), | |||
|         ('fifo', 'First In First Out (FIFO)'), | |||
|         ('average', 'Average Cost (AVCO)')], string='Costing Method', | |||
|         company_dependent=True, copy=True, | |||
|         help="""Standard Price: The products are valued at their standard cost  | |||
|             defined on the product. Average Cost (AVCO): The products are  | |||
|             valued at weighted average cost. First In First Out (FIFO): The  | |||
|             products are valued supposing those that enter the company first  | |||
|             will also leave it first. Last Purchase Price: The products are  | |||
|             valued same as 'Standard Price' Method, But standard price defined  | |||
|             on the product will updated automatically with last purchase  | |||
|             price.""") | |||
| @ -0,0 +1,158 @@ | |||
| # -*- coding: utf-8 -*- | |||
| ################################################################################ | |||
| # | |||
| #    Cybrosys Technologies Pvt. Ltd. | |||
| #    Copyright (C) 2024-TODAY Cybrosys Technologies(<https://www.cybrosys.com>). | |||
| #    Author: Ammu Raj (odoo@cybrosys.com) | |||
| # | |||
| #    This program is free software: you can modify | |||
| #    it under the terms of the GNU Affero General Public License (AGPL) as | |||
| #    published by the Free Software Foundation, either version 3 of the | |||
| #    License, or (at your option) any later version. | |||
| # | |||
| #    This program is distributed in the hope that it will be useful, | |||
| #    but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
| #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | |||
| #    GNU Affero General Public License for more details. | |||
| # | |||
| #    You should have received a copy of the GNU Affero General Public License | |||
| #    along with this program.  If not, see <https://www.gnu.org/licenses/>. | |||
| # | |||
| ################################################################################ | |||
| from odoo import api, models, _ | |||
| from odoo.exceptions import UserError | |||
| from odoo.tools import float_is_zero | |||
| 
 | |||
| 
 | |||
| class ProductProduct(models.Model): | |||
|     """ Class to inherit product_product to add some functionalities """ | |||
|     _inherit = 'product.product' | |||
| 
 | |||
|     def create_price_change_account_move(self, new_price, account_id, | |||
|                                          company_id, origin): | |||
|         """ Function to compute and updated the change in price """ | |||
|         account_move = self.env['account.move'] | |||
|         product_accounts = { | |||
|             product.id: product.product_tmpl_id.get_product_accounts() for | |||
|             product in self} | |||
|         for product in self.with_context().filtered( | |||
|                 lambda r: r.valuation == 'real_time'): | |||
|             diff = product.standard_price - new_price | |||
|             if not float_is_zero( | |||
|                     diff, precision_rounding=product.currency_id.rounding): | |||
|                 if not product_accounts[product.id].get('stock_valuation', | |||
|                                                         False): | |||
|                     raise UserError( | |||
|                         _('You don\'t have any stock valuation account ' | |||
|                           'defined on your product category. You must define ' | |||
|                           'one before processing this operation.')) | |||
|                 qty_available = product.qty_available | |||
|                 if qty_available: | |||
|                     # Accounting Entries | |||
|                     if diff * qty_available > 0: | |||
|                         debit_account_id = account_id | |||
|                         credit_account_id = product_accounts[product.id][ | |||
|                             'stock_valuation'].id | |||
|                     else: | |||
|                         debit_account_id = product_accounts[product.id][ | |||
|                             'stock_valuation'].id | |||
|                         credit_account_id = account_id | |||
|                     move_vals = { | |||
|                         'journal_id': product_accounts[product.id][ | |||
|                             'stock_journal'].id, | |||
|                         'company_id': company_id, | |||
|                         'ref': product.default_code, | |||
|                         'line_ids': [(0, 0, { | |||
|                             'name': _('%s changed cost from %s to %s - %s') % ( | |||
|                                 origin, product.standard_price, new_price, | |||
|                                 product.display_name), | |||
|                             'account_id': debit_account_id, | |||
|                             'debit': abs(diff * qty_available), | |||
|                             'credit': 0, | |||
|                             'product_id': product.id, | |||
|                         }), (0, 0, { | |||
|                             'name': _('%s changed cost from %s to %s - %s') % ( | |||
|                                 origin, product.standard_price, new_price, | |||
|                                 product.display_name), | |||
|                             'account_id': credit_account_id, | |||
|                             'debit': 0, | |||
|                             'credit': abs(diff * qty_available), | |||
|                             'product_id': product.id, | |||
|                         })], | |||
|                     } | |||
|                     move = account_move.create(move_vals) | |||
|                     move.action_post() | |||
|         return True | |||
| 
 | |||
|     @api.depends('stock_move_ids.product_qty', 'stock_move_ids.state', | |||
|                  'stock_move_ids.remaining_value', | |||
|                  'product_tmpl_id.cost_method', | |||
|                  'product_tmpl_id.standard_price', | |||
|                  'product_tmpl_id.property_valuation', | |||
|                  'product_tmpl_id.categ_id.property_valuation') | |||
|     def _compute_stock_value(self): | |||
|         """ Function to compute the stock moves and update the values """ | |||
|         stock_move = self.env['stock.move'] | |||
|         to_date = self.env.context.get('to_date') | |||
|         self.env['account.move.line'].check_access_rights('read') | |||
|         fifo_automated_values = {} | |||
|         query = """SELECT aml.product_id, aml.account_id,  | |||
|                 sum(aml.debit) - sum(aml.credit), sum(quantity),  | |||
|                 array_agg(aml.id) FROM account_move_line AS aml | |||
|                 WHERE aml.product_id IS NOT NULL AND aml.company_id=%%s %s | |||
|                 GROUP BY aml.product_id, aml.account_id""" | |||
|         params = (self.env.user.company_id.id,) | |||
|         if to_date: | |||
|             query = query % ('AND aml.date <= %s',) | |||
|             params = params + (to_date,) | |||
|         else: | |||
|             query = query % ('',) | |||
|         self.env.cr.execute(query, params=params) | |||
|         res = self.env.cr.fetchall() | |||
|         for row in res: | |||
|             fifo_automated_values[(row[0], row[1])] = ( | |||
|              row[2], row[3], list(row[4])) | |||
|         for product in self: | |||
|             if product.cost_method in ['standard', 'average', 'last']: | |||
|                 qty_available = product.with_context( | |||
|                     company_owned=True, owner_id=False).qty_available | |||
|                 price_used = product.standard_price | |||
|                 if to_date: | |||
|                     price_used = product.get_history_price( | |||
|                         self.env.user.company_id.id, date=to_date) | |||
|                 product.stock_value = price_used * qty_available | |||
|                 product.qty_at_date = qty_available | |||
|             elif product.cost_method == 'fifo': | |||
|                 if to_date: | |||
|                     if product.product_tmpl_id.valuation == 'manual_periodic': | |||
|                         domain = [('product_id', '=', product.id), | |||
|                                   ('date', '<=', | |||
|                                    to_date)] + stock_move._get_all_base_domain() | |||
|                         moves = stock_move.search(domain) | |||
|                         product.stock_value = sum(moves.mapped('value')) | |||
|                         product.qty_at_date = product.with_context( | |||
|                             company_owned=True, owner_id=False).qty_available | |||
|                         product.stock_fifo_manual_move_ids = stock_move.browse( | |||
|                             moves.ids) | |||
|                     elif product.product_tmpl_id.valuation == 'real_time': | |||
|                         valuation_account_id = product.categ_id.property_stock_valuation_account_id.id | |||
|                         value, quantity, aml_ids = fifo_automated_values.get( | |||
|                             (product.id, valuation_account_id)) or ( | |||
|                                                        0, 0, []) | |||
|                         product.stock_value = value | |||
|                         product.qty_at_date = quantity | |||
|                         product.stock_fifo_real_time_aml_ids = self.env[ | |||
|                             'account.move.line'].browse(aml_ids) | |||
|                 else: | |||
|                     product.stock_value, moves = product._sum_remaining_values() | |||
|                     product.qty_at_date = product.with_context( | |||
|                         company_owned=True, owner_id=False).qty_available | |||
|                     if product.product_tmpl_id.valuation == 'manual_periodic': | |||
|                         product.stock_fifo_manual_move_ids = moves | |||
|                     elif product.product_tmpl_id.valuation == 'real_time': | |||
|                         valuation_account_id = product.categ_id.property_stock_valuation_account_id.id | |||
|                         value, quantity, aml_ids = fifo_automated_values.get( | |||
|                             (product.id, valuation_account_id)) or ( | |||
|                                                        0, 0, []) | |||
|                         product.stock_fifo_real_time_aml_ids = self.env[ | |||
|                             'account.move.line'].browse(aml_ids) | |||
| @ -0,0 +1,56 @@ | |||
| # -*- coding: utf-8 -*- | |||
| ################################################################################ | |||
| # | |||
| #    Cybrosys Technologies Pvt. Ltd. | |||
| #    Copyright (C) 2024-TODAY Cybrosys Technologies(<https://www.cybrosys.com>). | |||
| #    Author: Ammu Raj (odoo@cybrosys.com) | |||
| # | |||
| #    This program is free software: you can modify | |||
| #    it under the terms of the GNU Affero General Public License (AGPL) as | |||
| #    published by the Free Software Foundation, either version 3 of the | |||
| #    License, or (at your option) any later version. | |||
| # | |||
| #    This program is distributed in the hope that it will be useful, | |||
| #    but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
| #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | |||
| #    GNU Affero General Public License for more details. | |||
| # | |||
| #    You should have received a copy of the GNU Affero General Public License | |||
| #    along with this program.  If not, see <https://www.gnu.org/licenses/>. | |||
| # | |||
| ################################################################################ | |||
| from odoo import fields, models | |||
| 
 | |||
| 
 | |||
| class ProductTemplate(models.Model): | |||
|     """ Class to inherit product.template to add costing method """ | |||
|     _inherit = 'product.template' | |||
| 
 | |||
|     property_cost_method = fields.Selection([ | |||
|         ('standard', 'Standard Price'), | |||
|         ('last', 'Last Purchase Price'), | |||
|         ('fifo', 'First In First Out (FIFO)'), | |||
|         ('average', 'Average Cost (AVCO)')], string='Costing Method', | |||
|         company_dependent=True, copy=True, | |||
|         help="""Standard Price: The products are valued  | |||
|         at their standard cost defined on the product. | |||
|         Average Cost (AVCO): The products are valued at weighted average cost. | |||
|         First In First Out (FIFO): The products are valued supposing  | |||
|         those that enter the company first will also leave it first. | |||
|         Last Purchase Price: The products are valued same as 'Standard  | |||
|         Price' Method, But standard price defined on the product will  | |||
|         be updated automatically with last purchase price.""") | |||
| 
 | |||
|     def _set_cost_method(self): | |||
|         """ When going from FIFO to AVCO or to standard, we update the standard | |||
|         price with the average value in stock """ | |||
|         if (self.property_cost_method == 'fifo' and | |||
|                 self.cost_method in ['average', 'standard', 'last']): | |||
|             # Cannot use the `stock_value` computed field as it's already | |||
|             # invalidated when entering this method. | |||
|             valuation = sum([variant._sum_remaining_values()[0] for variant in | |||
|                              self.product_variant_ids]) | |||
|             qty_available = self.with_context(company_owned=True).qty_available | |||
|             if qty_available: | |||
|                 self.standard_price = valuation / qty_available | |||
|         return self.write({'property_cost_method': self.cost_method}) | |||
| @ -0,0 +1,112 @@ | |||
| # -*- coding: utf-8 -*- | |||
| ################################################################################ | |||
| # | |||
| #    Cybrosys Technologies Pvt. Ltd. | |||
| #    Copyright (C) 2024-TODAY Cybrosys Technologies(<https://www.cybrosys.com>). | |||
| #    Author: Ammu Raj (odoo@cybrosys.com) | |||
| # | |||
| #    This program is free software: you can modify | |||
| #    it under the terms of the GNU Affero General Public License (AGPL) as | |||
| #    published by the Free Software Foundation, either version 3 of the | |||
| #    License, or (at your option) any later version. | |||
| # | |||
| #    This program is distributed in the hope that it will be useful, | |||
| #    but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
| #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | |||
| #    GNU Affero General Public License for more details. | |||
| # | |||
| #    You should have received a copy of the GNU Affero General Public License | |||
| #    along with this program.  If not, see <https://www.gnu.org/licenses/>. | |||
| # | |||
| ################################################################################ | |||
| from collections import defaultdict | |||
| from odoo import models, _ | |||
| from odoo.exceptions import UserError | |||
| from odoo.tools import float_is_zero | |||
| 
 | |||
| 
 | |||
| class StockMove(models.Model): | |||
|     """ Class to inherit stock_move to update the product price """ | |||
|     _inherit = "stock.move" | |||
| 
 | |||
|     def product_price_update_before_done(self, forced_qty=None): | |||
|         tmpl_dict = defaultdict(lambda: 0.0) | |||
|         # adapt standard price on incoming moves if the product cost_method is | |||
|         # 'average' | |||
|         std_price_update = {} | |||
|         for move in self.filtered( | |||
|                 lambda move: move._is_in() and move.with_company( | |||
|                     move.company_id).product_id.cost_method == 'average'): | |||
|             product_tot_qty_available = move.product_id.sudo().with_company( | |||
|                 move.company_id).quantity_svl + tmpl_dict[ | |||
|                                             move.product_id.id] | |||
|             rounding = move.product_id.uom_id.rounding | |||
| 
 | |||
|             valued_move_lines = move._get_in_move_lines() | |||
|             qty_done = 0 | |||
|             for valued_move_line in valued_move_lines: | |||
|                 qty_done += valued_move_line.product_uom_id._compute_quantity( | |||
|                     valued_move_line.quantity, move.product_id.uom_id) | |||
|             qty = forced_qty or qty_done | |||
|             if float_is_zero(product_tot_qty_available, | |||
|                              precision_rounding=rounding): | |||
|                 new_std_price = move._get_price_unit() | |||
|             elif float_is_zero(product_tot_qty_available + move.product_qty, | |||
|                                precision_rounding=rounding) or \ | |||
|                     float_is_zero(product_tot_qty_available + qty, | |||
|                                   precision_rounding=rounding): | |||
|                 new_std_price = move._get_price_unit() | |||
|             else: | |||
|                 # Get the standard price | |||
|                 amount_unit = std_price_update.get((move.company_id.id, | |||
|                                                     move.product_id.id)) or move.product_id.with_company( | |||
|                     move.company_id).standard_price | |||
|                 new_std_price = (( | |||
|                                          amount_unit * product_tot_qty_available) + ( | |||
|                                          move._get_price_unit() * qty)) / ( | |||
|                                         product_tot_qty_available + qty) | |||
| 
 | |||
|             tmpl_dict[move.product_id.id] += qty_done | |||
|             # Write the standard price, as SUPERUSER_ID because a warehouse | |||
|             # manager may not have the right to write on products | |||
|             move.product_id.with_company(move.company_id.id).with_context( | |||
|                 disable_auto_svl=True).sudo().write( | |||
|                 {'standard_price': new_std_price}) | |||
|             std_price_update[ | |||
|                 move.company_id.id, move.product_id.id] = new_std_price | |||
|         # adapt standard price on incomming moves if the product cost_method | |||
|         # is 'fifo' | |||
|         for move in self.filtered(lambda move: | |||
|                                   move.with_company( | |||
|                                       move.company_id).product_id.cost_method == 'fifo' | |||
|                                   and float_is_zero( | |||
|                                       move.product_id.sudo().quantity_svl, | |||
|                                       precision_rounding=move.product_id.uom_id.rounding)): | |||
|             move.product_id.with_company(move.company_id.id).sudo().write( | |||
|                 {'standard_price': move._get_price_unit()}) | |||
|             # Add new costing method for 'last' with real-time or | |||
|             # manual_periodic valuation | |||
|         # Filter moves based on conditions | |||
|         for move in self.filtered(lambda move: move.with_company( | |||
|                 move.company_id).product_id.cost_method == 'last' and | |||
|                                                ( | |||
|                                                        move.product_id.valuation == 'real_time' or move.product_id.valuation == 'manual_periodic')): | |||
|             # Get the new standard price for the move | |||
|             new_std_price = move._get_price_unit()[self.env['stock.lot']] | |||
|             # Presumably retrieves incoming move price | |||
|             # Retrieve product details for the move | |||
|             products = self.env['product.product'].browse( | |||
|                 move.product_id.id) | |||
|             # Determine the account ID for price differences | |||
|             account_id = ( | |||
|                     products.property_account_creditor_price_difference.id | |||
|                     or products.categ_id.property_account_creditor_price_difference_categ.id) | |||
|             # Check if account ID is not set, raise an error | |||
|             if not account_id: | |||
|                 raise UserError( | |||
|                     _('Configuration error. Please configure the price ' | |||
|                       'difference account on the product or its category ' | |||
|                       'to process this operation.')) | |||
|             move.product_id.with_company(move.company_id.id).with_context( | |||
|                 disable_auto_svl=True).sudo().write( | |||
|                 {'standard_price': new_std_price}) | |||
| @ -0,0 +1,100 @@ | |||
| # -*- coding: utf-8 -*- | |||
| ################################################################################ | |||
| # | |||
| #    Cybrosys Technologies Pvt. Ltd. | |||
| #    Copyright (C) 2024-TODAY Cybrosys Technologies(<https://www.cybrosys.com>). | |||
| #    Author: Ammu Raj (odoo@cybrosys.com) | |||
| # | |||
| #    This program is free software: you can modify | |||
| #    it under the terms of the GNU Affero General Public License (AGPL) as | |||
| #    published by the Free Software Foundation, either version 3 of the | |||
| #    License, or (at your option) any later version. | |||
| # | |||
| #    This program is distributed in the hope that it will be useful, | |||
| #    but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
| #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | |||
| #    GNU Affero General Public License for more details. | |||
| # | |||
| #    You should have received a copy of the GNU Affero General Public License | |||
| #    along with this program.  If not, see <https://www.gnu.org/licenses/>. | |||
| # | |||
| ################################################################################ | |||
| from odoo import models | |||
| 
 | |||
| 
 | |||
| class StockMoveLine(models.Model): | |||
|     """ Class to inherit the model stock.move.line to override the create | |||
|     function """ | |||
|     _inherit = 'stock.move.line' | |||
| 
 | |||
|     def write(self, vals): | |||
|         """ When editing a done stock.move.line, we impact the valuation. | |||
|         Users may increase or decrease the `qty_done` field. There are three | |||
|         cost method available: standard, average and fifo. We implement the | |||
|         logic in a similar way for standard and average: increase or decrease | |||
|         the original value with the standard or average price of today. In | |||
|         fifo, we have a different logic whether the move is incoming or | |||
|         outgoing. If the move is incoming, we update the value and | |||
|         remaining_value/qty with the unit price of the move. If the move is | |||
|         outgoing and the user increases qty_done, we call _run_fifo, and it'll | |||
|         consume layer(s) in the stack the same way a new outgoing move would | |||
|         have done. If the move is outgoing and the user decreases qty_done, we | |||
|         either increase the last receipt candidate if one is found or we | |||
|         decrease the value with the last fifo price. | |||
|         """ | |||
|         if 'qty_done' in vals: | |||
|             moves_to_update = {} | |||
|             for move_line in self.filtered( | |||
|                     lambda ml: ml.state == 'done' and ( | |||
|                             ml.move_id._is_in() or ml.move_id._is_out())): | |||
|                 moves_to_update[move_line.move_id] = ( | |||
|                         vals['qty_done'] - move_line.qty_done) | |||
|             for move_id, qty_difference in moves_to_update.items(): | |||
|                 move_vals = {} | |||
|                 if move_id.product_id.cost_method in ['standard', 'average', | |||
|                                                       'last']: | |||
|                     correction_value = (qty_difference * | |||
|                                         move_id.product_id.standard_price) | |||
|                     if move_id._is_in(): | |||
|                         move_vals['value'] = move_id.value + correction_value | |||
|                     elif move_id._is_out(): | |||
|                         move_vals['value'] = move_id.value - correction_value | |||
|                 else: | |||
|                     if move_id._is_in(): | |||
|                         correction_value = qty_difference * move_id.price_unit | |||
|                         move_vals['value'] = move_id.value + correction_value | |||
|                         move_vals['remaining_qty'] = (move_id.remaining_qty + | |||
|                                                       qty_difference) | |||
|                         move_vals['remaining_value'] = (move_id.remaining_value | |||
|                                                         + correction_value) | |||
|                     elif move_id._is_out() and qty_difference > 0: | |||
|                         correction_value = self.env['stock.move']._run_fifo( | |||
|                             move_id, quantity=qty_difference) | |||
|                         # No need to adapt `remaining_qty` and | |||
|                         # `remaining_value` as `_run_fifo` took care of it | |||
|                         move_vals['value'] = move_id.value - correction_value | |||
|                     elif move_id._is_out() and qty_difference < 0: | |||
|                         candidates_receipt = self.env['stock.move'].search( | |||
|                             move_id._get_in_domain(), order='date, id desc', | |||
|                             limit=1) | |||
|                         if candidates_receipt: | |||
|                             candidates_receipt.write({ | |||
|                                 'remaining_qty': candidates_receipt.remaining_qty + -qty_difference, | |||
|                                 'remaining_value': candidates_receipt.remaining_value + ( | |||
|                                         -qty_difference * candidates_receipt.price_unit), | |||
|                             }) | |||
|                             correction_value = (qty_difference * | |||
|                                                 candidates_receipt.price_unit) | |||
|                         else: | |||
|                             correction_value = (qty_difference * | |||
|                                                 move_id.product_id.standard_price) | |||
|                         move_vals['value'] = move_id.value - correction_value | |||
|                 move_id.write(move_vals) | |||
|                 if move_id.product_id.valuation == 'real_time': | |||
|                     move_id.with_context( | |||
|                         force_valuation_amount=correction_value, | |||
|                         forced_quantity=qty_difference)._account_entry_move() | |||
|                 if qty_difference > 0: | |||
|                     move_id.product_price_update_before_done( | |||
|                         forced_qty=qty_difference) | |||
|         return super().write(vals) | |||
| After Width: | Height: | Size: 2.2 KiB | 
| After Width: | Height: | Size: 28 KiB | 
| After Width: | Height: | Size: 628 KiB | 
| After Width: | Height: | Size: 1.1 KiB | 
| After Width: | Height: | Size: 210 KiB | 
| After Width: | Height: | Size: 209 KiB | 
| After Width: | Height: | Size: 109 KiB | 
| After Width: | Height: | Size: 495 B | 
| After Width: | Height: | Size: 1.0 KiB | 
| After Width: | Height: | Size: 624 B | 
| After Width: | Height: | Size: 136 KiB | 
| After Width: | Height: | Size: 214 KiB | 
| After Width: | Height: | Size: 36 KiB | 
| After Width: | Height: | Size: 3.6 KiB | 
| After Width: | Height: | Size: 310 B | 
| After Width: | Height: | Size: 929 B | 
| After Width: | Height: | Size: 1.3 KiB | 
| After Width: | Height: | Size: 3.3 KiB | 
| After Width: | Height: | Size: 1.4 KiB | 
| After Width: | Height: | Size: 17 KiB | 
| After Width: | Height: | Size: 542 B | 
| After Width: | Height: | Size: 576 B | 
| After Width: | Height: | Size: 733 B | 
| After Width: | Height: | Size: 4.3 KiB | 
| After Width: | Height: | Size: 1.2 KiB | 
| After Width: | Height: | Size: 4.0 KiB | 
| After Width: | Height: | Size: 1.7 KiB | 
| After Width: | Height: | Size: 738 KiB | 
| After Width: | Height: | Size: 2.2 KiB | 
| After Width: | Height: | Size: 911 B | 
| After Width: | Height: | Size: 1.1 KiB | 
| After Width: | Height: | Size: 1.2 KiB | 
| After Width: | Height: | Size: 1.2 KiB | 
| After Width: | Height: | Size: 600 B | 
| After Width: | Height: | Size: 673 B | 
| After Width: | Height: | Size: 2.0 KiB | 
| After Width: | Height: | Size: 462 B | 
| After Width: | Height: | Size: 2.1 KiB | 
| After Width: | Height: | Size: 926 B | 
| After Width: | Height: | Size: 9.0 KiB | 
| After Width: | Height: | Size: 23 KiB | 
| After Width: | Height: | Size: 7.0 KiB | 
| After Width: | Height: | Size: 878 B | 
| After Width: | Height: | Size: 1.2 KiB | 
| After Width: | Height: | Size: 653 B | 
| After Width: | Height: | Size: 800 B | 
| After Width: | Height: | Size: 905 B | 
| After Width: | Height: | Size: 189 KiB | 
| After Width: | Height: | Size: 4.3 KiB | 
| After Width: | Height: | Size: 839 B | 
| After Width: | Height: | Size: 1.7 KiB | 
| After Width: | Height: | Size: 5.9 KiB | 
| After Width: | Height: | Size: 1.6 KiB | 
| After Width: | Height: | Size: 34 KiB | 
| After Width: | Height: | Size: 26 KiB | 
| After Width: | Height: | Size: 3.8 KiB | 
| After Width: | Height: | Size: 23 KiB | 
| After Width: | Height: | Size: 1.9 KiB | 
| After Width: | Height: | Size: 2.3 KiB | 
| After Width: | Height: | Size: 427 B | 
| After Width: | Height: | Size: 627 B | 
| After Width: | Height: | Size: 1.1 KiB | 
| After Width: | Height: | Size: 1.2 KiB | 
| After Width: | Height: | Size: 988 B | 
| After Width: | Height: | Size: 3.7 KiB | 
| After Width: | Height: | Size: 5.0 KiB | 
| After Width: | Height: | Size: 875 B | 
| After Width: | Height: | Size: 1.2 KiB | 
| After Width: | Height: | Size: 767 KiB | 
| After Width: | Height: | Size: 138 KiB | 
| After Width: | Height: | Size: 760 KiB | 
| After Width: | Height: | Size: 92 KiB | 
| After Width: | Height: | Size: 697 KiB | 
| After Width: | Height: | Size: 1.1 MiB | 
| After Width: | Height: | Size: 107 KiB | 
| After Width: | Height: | Size: 55 KiB | 
| After Width: | Height: | Size: 70 KiB | 
| After Width: | Height: | Size: 81 KiB | 
| After Width: | Height: | Size: 105 KiB | 
| After Width: | Height: | Size: 147 KiB | 
| After Width: | Height: | Size: 262 KiB | 
| After Width: | Height: | Size: 880 KiB | 
| After Width: | Height: | Size: 771 KiB | 
| After Width: | Height: | Size: 53 KiB |