diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/sale_expense/models | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/sale_expense/models')
| -rw-r--r-- | addons/sale_expense/models/__init__.py | 7 | ||||
| -rw-r--r-- | addons/sale_expense/models/account_move.py | 39 | ||||
| -rw-r--r-- | addons/sale_expense/models/hr_expense.py | 46 | ||||
| -rw-r--r-- | addons/sale_expense/models/product_template.py | 24 | ||||
| -rw-r--r-- | addons/sale_expense/models/sale_order.py | 28 |
5 files changed, 144 insertions, 0 deletions
diff --git a/addons/sale_expense/models/__init__.py b/addons/sale_expense/models/__init__.py new file mode 100644 index 00000000..c0512829 --- /dev/null +++ b/addons/sale_expense/models/__init__.py @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import hr_expense +from . import sale_order +from . import product_template +from . import account_move diff --git a/addons/sale_expense/models/account_move.py b/addons/sale_expense/models/account_move.py new file mode 100644 index 00000000..c2e90b80 --- /dev/null +++ b/addons/sale_expense/models/account_move.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import api, models + + +class AccountMoveLine(models.Model): + _inherit = 'account.move.line' + + def _sale_can_be_reinvoice(self): + """ determine if the generated analytic line should be reinvoiced or not. + For Expense flow, if the product has a 'reinvoice policy' and a Sales Order is set on the expense, then we will reinvoice the AAL + """ + self.ensure_one() + if self.expense_id: # expense flow is different from vendor bill reinvoice flow + return self.expense_id.product_id.expense_policy in ['sales_price', 'cost'] and self.expense_id.sale_order_id + return super(AccountMoveLine, self)._sale_can_be_reinvoice() + + def _sale_determine_order(self): + """ For move lines created from expense, we override the normal behavior. + Note: if no SO but an AA is given on the expense, we will determine anyway the SO from the AA, using the same + mecanism as in Vendor Bills. + """ + mapping_from_invoice = super(AccountMoveLine, self)._sale_determine_order() + + mapping_from_expense = {} + for move_line in self.filtered(lambda move_line: move_line.expense_id): + mapping_from_expense[move_line.id] = move_line.expense_id.sale_order_id or None + + mapping_from_invoice.update(mapping_from_expense) + return mapping_from_invoice + + def _sale_prepare_sale_line_values(self, order, price): + # Add expense quantity to sales order line and update the sales order price because it will be charged to the customer in the end. + self.ensure_one() + res = super()._sale_prepare_sale_line_values(order, price) + if self.expense_id: + res.update({'product_uom_qty': self.expense_id.quantity}) + return res diff --git a/addons/sale_expense/models/hr_expense.py b/addons/sale_expense/models/hr_expense.py new file mode 100644 index 00000000..fc88dcbb --- /dev/null +++ b/addons/sale_expense/models/hr_expense.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import api, fields, models + + +class Expense(models.Model): + _inherit = "hr.expense" + + sale_order_id = fields.Many2one('sale.order', compute='_compute_sale_order_id', store=True, string='Customer to Reinvoice', readonly=False, tracking=True, + states={'approved': [('readonly', True)], 'done': [('readonly', True)], 'refused': [('readonly', True)]}, + # NOTE: only confirmed SO can be selected, but this domain in activated throught the name search with the `sale_expense_all_order` + # context key. So, this domain is not the one applied. + domain="[('state', '=', 'sale'), ('company_id', '=', company_id)]", + help="If the product has an expense policy, it will be reinvoiced on this sales order") + can_be_reinvoiced = fields.Boolean("Can be reinvoiced", compute='_compute_can_be_reinvoiced') + analytic_account_id = fields.Many2one(compute='_compute_analytic_account_id', store=True, readonly=False) + + @api.depends('product_id.expense_policy') + def _compute_can_be_reinvoiced(self): + for expense in self: + expense.can_be_reinvoiced = expense.product_id.expense_policy in ['sales_price', 'cost'] + + @api.depends('can_be_reinvoiced') + def _compute_sale_order_id(self): + for expense in self.filtered(lambda e: not e.can_be_reinvoiced): + expense.sale_order_id = False + + @api.depends('sale_order_id') + def _compute_analytic_account_id(self): + for expense in self.filtered('sale_order_id'): + expense.analytic_account_id = expense.sale_order_id.sudo().analytic_account_id # `sudo` required for normal employee without sale access rights + + def action_move_create(self): + """ When posting expense, if the AA is given, we will track cost in that + If a SO is set, this means we want to reinvoice the expense. But to do so, we + need the analytic entries to be generated, so a AA is required to reinvoice. So, + we ensure the AA if a SO is given. + """ + for expense in self.filtered(lambda expense: expense.sale_order_id and not expense.analytic_account_id): + if not expense.sale_order_id.analytic_account_id: + expense.sale_order_id._create_analytic_account() + expense.write({ + 'analytic_account_id': expense.sale_order_id.analytic_account_id.id + }) + return super(Expense, self).action_move_create() diff --git a/addons/sale_expense/models/product_template.py b/addons/sale_expense/models/product_template.py new file mode 100644 index 00000000..85388f28 --- /dev/null +++ b/addons/sale_expense/models/product_template.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import api, models + + +class ProductTemplate(models.Model): + _inherit = 'product.template' + + def _default_visible_expense_policy(self): + visibility = self.user_has_groups('hr_expense.group_hr_expense_user') + return visibility or super(ProductTemplate, self)._default_visible_expense_policy() + + @api.depends('can_be_expensed') + def _compute_visible_expense_policy(self): + expense_products = self.filtered(lambda p: p.can_be_expensed) + for product_template in self - expense_products: + product_template.visible_expense_policy = False + + super(ProductTemplate, expense_products)._compute_visible_expense_policy() + visibility = self.user_has_groups('hr_expense.group_hr_expense_user') + for product_template in expense_products: + if not product_template.visible_expense_policy: + product_template.visible_expense_policy = visibility diff --git a/addons/sale_expense/models/sale_order.py b/addons/sale_expense/models/sale_order.py new file mode 100644 index 00000000..fef55214 --- /dev/null +++ b/addons/sale_expense/models/sale_order.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import api, fields, models +from odoo import SUPERUSER_ID +from odoo.osv import expression + + +class SaleOrder(models.Model): + _inherit = 'sale.order' + + expense_ids = fields.One2many('hr.expense', 'sale_order_id', string='Expenses', domain=[('state', '=', 'done')], readonly=True, copy=False) + expense_count = fields.Integer("# of Expenses", compute='_compute_expense_count', compute_sudo=True) + + @api.model + def _name_search(self, name='', args=None, operator='ilike', limit=100, name_get_uid=None): + """ For expense, we want to show all sales order but only their name_get (no ir.rule applied), this is the only way to do it. """ + if self._context.get('sale_expense_all_order'): + domain = expression.AND([args or [], ['&', ('state', '=', 'sale'), ('company_id', 'in', self.env.companies.ids)]]) + return super(SaleOrder, self.sudo())._name_search(name=name, args=domain, operator=operator, limit=limit, name_get_uid=SUPERUSER_ID) + return super(SaleOrder, self)._name_search(name=name, args=args, operator=operator, limit=limit, name_get_uid=name_get_uid) + + @api.depends('expense_ids') + def _compute_expense_count(self): + expense_data = self.env['hr.expense'].read_group([('sale_order_id', 'in', self.ids)], ['sale_order_id'], ['sale_order_id']) + mapped_data = dict([(item['sale_order_id'][0], item['sale_order_id_count']) for item in expense_data]) + for sale_order in self: + sale_order.expense_count = mapped_data.get(sale_order.id, 0) |
