Odoo Commit 每日一读/7 - f075b5

今天我们来看下odoo10中的最新 commit- f075b5

改进内容

在创建费用报告时,对费用的支付方式进行判断。

如果混用了由员工支付与由公司支付这两个支付方式。无法创建该报告并发出警告 Expense Reports入口

为什么要修改

我们知道,费用的支付方式有两种:

  1. 由员工代付: 需要在日记账过账后进行支付登记确认。
  2. 由公司直接支付

这两种支付方式是不能混用的。在修改前的odoo版本中,包涵有两种支付方式的费用报告可以被创建。但批准功能却无法使用(因为同时存在2种支付方式)。建立这种费用报告无法进行状态的更新,没有实际意义。

代码示例

+    @api.one
+    @api.constrains('expense_line_ids')
+    def _check_payment_mode(self):
+        payment_mode = set(self.expense_line_ids.mapped('payment_mode'))
+        if len(payment_mode) > 1:
+            raise ValidationError(_('You cannot report expenses with different payment modes.'))

代码比较简单,就是添加了一个constrains方法.

  1. 通过expense_line_ids关系字段拿到所有的费用对象,
  2. 使用mapped方法获取所有费用对象的payment_mode,返回一个列表.使用set去重.
  3. 进行判断,如果同时存在2种支付方式,则发出警告。

原始提交信息

commit f075b52a25c69f767a37760c9dfd739d00b267c6
Author:     Nicolas Martinelli <nim@odoo.com>
AuthorDate: Tue Nov 21 10:39:48 2017 +0100
Commit:     Nicolas Martinelli <nim-odoo@users.noreply.github.com>
CommitDate: Tue Nov 21 14:19:13 2017 +0100

    [FIX] hr_expense: multiple payment modes

    - Create an expense paid by employee
    - Create an expense paid by company
    - Select the two expenses and submit a report

    You are authorized to create this report but you cannot validate it and
    you cannot refuse it because of the mix of an expense paid by employee
    and paid by company. You're blocked.

    We should block the creation of an expense report mixing expenses paid
    by company and paid by employee

    opw-783395
---
 addons/hr_expense/i18n/hr_expense.pot  | 25 ++++++++++++++++++++++++-
 addons/hr_expense/models/hr_expense.py |  7 +++++++
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/addons/hr_expense/i18n/hr_expense.pot b/addons/hr_expense/i18n/hr_expense.pot
index b26c16e..c30ecab 100644
--- a/addons/hr_expense/i18n/hr_expense.pot
+++ b/addons/hr_expense/i18n/hr_expense.pot
@@ -149,6 +149,11 @@ msgid "All Expenses"
 msgstr ""
#. module: hr_expense
+#: model:ir.model.fields,help:hr_expense.field_hr_expense_account_id
+msgid "An expense account is expected"
+msgstr ""
+
+#. module: hr_expense
 #: model:ir.model.fields,field_description:hr_expense.field_hr_expense_analytic_account_id
 #: model:ir.ui.view,arch_db:hr_expense.view_hr_expense_filter
 msgid "Analytic Account"
@@ -827,6 +832,18 @@ msgid "Once your email gateway is configured, come back to this screen\n"
 msgstr ""

 #. module: hr_expense
+#: code:addons/hr_expense/models/hr_expense.py:537
+#, python-format
+msgid "Only HR Officers can approve expenses"
+msgstr ""
+
+#. module: hr_expense
+#: code:addons/hr_expense/models/hr_expense.py:528
+#, python-format
+msgid "Only HR Officers can refuse expenses"
+msgstr ""
+
+#. module: hr_expense
 #: selection:hr.expense.sheet,state:0
 #: model:mail.message.subtype,name:hr_expense.mt_expense_paid
 msgid "Paid"
@@ -1308,7 +1325,13 @@ msgid "You cannot report expenses for different employees in the same report!"
 msgstr ""

 #. module: hr_expense
-#: code:addons/hr_expense/models/hr_expense.py:104
+#: code:addons/hr_expense/models/hr_expense.py:605
+#, python-format
+msgid "You cannot report expenses with different payment modes."
+msgstr ""
+
+#. module: hr_expense
+#: code:addons/hr_expense/models/hr_expense.py:105
 #, python-format
 msgid "You cannot report twice the same line!"
 msgstr ""
diff --git a/addons/hr_expense/models/hr_expense.py b/addons/hr_expense/models/hr_expense.py
index 61a45a4..4bbdb0b 100644
--- a/addons/hr_expense/models/hr_expense.py
+++ b/addons/hr_expense/models/hr_expense.py
@@ -596,3 +596,10 @@ class HrExpenseSheet(models.Model):
         employee_ids = self.expense_line_ids.mapped('employee_id')
         if len(employee_ids) > 1 or (len(employee_ids) == 1 and employee_ids != self.employee_id):
             raise ValidationError(_('You cannot add expense lines of another employee.'))
+
+    @api.one
+    @api.constrains('expense_line_ids')
+    def _check_payment_mode(self):
+        payment_mode = set(self.expense_line_ids.mapped('payment_mode'))
+        if len(payment_mode) > 1:
+            raise ValidationError(_('You cannot report expenses with different payment modes.'))