from odoo import fields, models, api from odoo.exceptions import UserError class StockMove(models.Model): _inherit = 'stock.move' def _account_entry_move(self, qty, description, svl_id, cost): """ Accounting Valuation Entries """ self.ensure_one() if self.product_id.type != 'product': # no stock valuation for consumable products return False if self.restrict_partner_id: # if the move isn't owned by the company, we don't make any valuation return False company_from = self._is_out() and self.mapped('move_line_ids.location_id.company_id') or False company_to = self._is_in() and self.mapped('move_line_ids.location_dest_id.company_id') or False journal_id, acc_src, acc_dest, acc_valuation = self._get_accounting_data_for_valuation() # Create Journal Entry for products arriving in the company; in case of routes making the link between several # warehouse of the same company, the transit location belongs to this company, so we don't need to create accounting entries if self._is_in(): if self._is_returned(valued_type='in'): self.with_company(company_to)._create_account_move_line(acc_dest, acc_valuation, journal_id, qty, description, svl_id, cost) else: self.with_company(company_to)._create_account_move_line(acc_src, acc_valuation, journal_id, qty, description, svl_id, cost) # Create Journal Entry for products leaving the company if self._is_out(): cost = -1 * cost if self._is_returned(valued_type='out'): self.with_company(company_from)._create_account_move_line(acc_valuation, acc_src, journal_id, qty, description, svl_id, cost) else: # custom here for ppn internal use, acc_dest = 538, acc_valuation = 400, charge from virtual location and persediaan self.with_company(company_from)._create_account_move_line(acc_valuation, acc_dest, journal_id, qty, description, svl_id, cost) self.with_company(company_from)._create_account_move_line(440,538,journal_id,qty,description,svl_id,cost * (11 / 100)) if self.company_id.anglo_saxon_accounting: # Creates an account entry from stock_input to stock_output on a dropship move. https://github.com/odoo/odoo/issues/12687 if self._is_dropshipped(): if cost > 0: self.with_company(self.company_id)._create_account_move_line(acc_src, acc_valuation, journal_id, qty, description, svl_id, cost) else: cost = -1 * cost self.with_company(self.company_id)._create_account_move_line(acc_valuation, acc_dest, journal_id, qty, description, svl_id, cost) elif self._is_dropshipped_returned(): if cost > 0: self.with_company(self.company_id)._create_account_move_line(acc_valuation, acc_src, journal_id, qty, description, svl_id, cost) else: cost = -1 * cost self.with_company(self.company_id)._create_account_move_line(acc_dest, acc_valuation, journal_id, qty, description, svl_id, cost) if self.company_id.anglo_saxon_accounting: # Eventually reconcile together the invoice and valuation accounting entries on the stock interim accounts self._get_related_invoices()._stock_account_anglo_saxon_reconcile_valuation(product=self.product_id) # def _create_account_move_line(self, credit_account_id, debit_account_id, journal_id, qty, description, svl_id, # cost): # self.ensure_one() # AccountMove = self.env['account.move'].with_context(default_journal_id=journal_id) # # move_lines = self._prepare_account_move_line(qty, cost, credit_account_id, debit_account_id, description) # if move_lines: # date = self._context.get('force_period_date', fields.Date.context_today(self)) # new_account_move = AccountMove.sudo().create({ # 'journal_id': journal_id, # 'line_ids': move_lines, # 'date': date, # 'ref': description, # 'stock_move_id': self.id, # 'stock_valuation_layer_ids': [(6, None, [svl_id])], # 'move_type': 'entry', # }) # new_account_move._post() # # def _prepare_account_move_line(self, qty, cost, credit_account_id, debit_account_id, description): # """ # Generate the account.move.line values to post to track the stock valuation difference due to the # processing of the given quant. # """ # self.ensure_one() # # # the standard_price of the product may be in another decimal precision, or not compatible with the coinage of # # the company currency... so we need to use round() before creating the accounting entries. # debit_value = self.company_id.currency_id.round(cost) # credit_value = debit_value # # valuation_partner_id = self._get_partner_id_for_valuation_lines() # res = [(0, 0, line_vals) for line_vals in # self._generate_valuation_lines_data(valuation_partner_id, qty, debit_value, credit_value, # debit_account_id, credit_account_id, description).values()] # # return res # # def _generate_valuation_lines_data(self, partner_id, qty, debit_value, credit_value, debit_account_id, # credit_account_id, description): # # This method returns a dictionary to provide an easy extension hook to modify the valuation lines (see purchase for an example) # self.ensure_one() # debit_line_vals = { # 'name': description, # 'product_id': self.product_id.id, # 'quantity': qty, # 'product_uom_id': self.product_id.uom_id.id, # 'ref': description, # 'partner_id': partner_id, # 'debit': debit_value if debit_value > 0 else 0, # 'credit': -debit_value if debit_value < 0 else 0, # 'account_id': debit_account_id, # } # # credit_line_vals = { # 'name': description, # 'product_id': self.product_id.id, # 'quantity': qty, # 'product_uom_id': self.product_id.uom_id.id, # 'ref': description, # 'partner_id': partner_id, # 'credit': credit_value if credit_value > 0 else 0, # 'debit': -credit_value if credit_value < 0 else 0, # 'account_id': credit_account_id, # } # # rslt = {'credit_line_vals': credit_line_vals, 'debit_line_vals': debit_line_vals} # if credit_value != debit_value: # # for supplier returns of product in average costing method, in anglo saxon mode # diff_amount = debit_value - credit_value # price_diff_account = self.product_id.property_account_creditor_price_difference # # if not price_diff_account: # price_diff_account = self.product_id.categ_id.property_account_creditor_price_difference_categ # if not price_diff_account: # raise UserError( # _('Configuration error. Please configure the price difference account on the product or its category to process this operation.')) # # rslt['price_diff_line_vals'] = { # 'name': self.name, # 'product_id': self.product_id.id, # 'quantity': qty, # 'product_uom_id': self.product_id.uom_id.id, # 'ref': description, # 'partner_id': partner_id, # 'credit': diff_amount > 0 and diff_amount or 0, # 'debit': diff_amount < 0 and -diff_amount or 0, # 'account_id': price_diff_account.id, # } # return rslt