|
- # Copyright 2016 上海开阖软件有限公司 (http://www.osbzr.com)
- # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
-
- from odoo import fields, models, api
- from odoo import exceptions
-
-
- class CostLine(models.Model):
- _name = 'cost.line'
- _description = "采购销售费用"
-
- @api.depends('amount', 'tax_rate')
- def _compute_tax(self):
- """
- 计算字段根据 amount 和 tax_rate 是否变化进行判定tax 是否需要重新计算
- :return:
- """
- for s in self:
- s.tax = s.amount * s.tax_rate * 0.01
-
- def _inverse_subtotal(self):
- """
- 计算字段根据 amount 和 tax_rate 是否变化进行判定tax 是否需要重新计算
- :return:
- """
- for s in self:
- s.amount = 100 * s.subtotal / (100 + s.tax_rate)
-
- partner_id = fields.Many2one('partner', '供应商', ondelete='restrict',
- required=True,
- help='采购/销售费用对应的业务伙伴')
- category_id = fields.Many2one('core.category', '类别',
- required=True,
- ondelete='restrict',
- domain=[('type', '=', 'expense')],
- help='分类:采购')
- amount = fields.Float('金额',
- required=True,
- digits='Amount',
- help='采购/销售费用金额')
- tax_rate = fields.Float(
- '税率(%)',
- default=lambda self: self.env.user.company_id.import_tax_rate,
- help='默认值取公司进项税率')
- tax = fields.Float('税额',
- digits='Amount',
- compute=_compute_tax, store=True,
- help='采购/销售费用税额')
- subtotal = fields.Float('价税合计', digits='Amount', compute='_compute_all_currency', inverse=_inverse_subtotal, store=True)
- note = fields.Char('备注',
- help='该采购/销售费用添加的一些标识信息')
- company_id = fields.Many2one(
- 'res.company',
- string='公司',
- change_default=True,
- default=lambda self: self.env.company)
-
- standard_amount = fields.Float('本位币金额', compute='_compute_all_currency',
- store=True, readonly=True,
- digits='总数',
- help='本位币的成交金额')
- currency_id = fields.Many2one('res.currency',
- '外币币别', store=True, compute='_compute_all_currency',
- help='外币币别')
- currency_rate = fields.Float('汇率', digits='Price', store=True, compute='_compute_all_currency')
- standard_subtotal = fields.Float('本位币价税合计', digits='Price', store=True, compute='_compute_all_currency')
-
- @api.onchange('partner_id', 'category_id')
- def _onchange_partner_id_category_id(self):
- err = ''
- for record in self:
- if record.partner_id and record.category_id:
- partner_c = record.partner_id.s_category_id.account_id.currency_id or self.env.company.currency_id
- category_c = record.category_id.account_id.currency_id or self.env.company.currency_id
- if partner_c != category_c:
- err += "采购费用行 供应商%s的币种%s 与类别币种%s不相同\n" % (
- record.partner_id.name, partner_c.name, category_c.name)
- if err:
- raise exceptions.ValidationError(err)
-
- @api.depends('category_id', 'amount', 'tax','tax_rate')
- def _compute_all_currency(self):
- for record in self:
- record.currency_id = record.category_id.account_id.currency_id or self.env.company.currency_id
- record.currency_rate = record.currency_id.rate
- record.standard_amount = record.amount * record.currency_rate
- record.subtotal = record.amount + record.tax
- record.standard_subtotal = record.subtotal * record.currency_id.rate
- # if (record.tax > 0 or record. tax_rate) and record.currency_id !=self.env.company.currency_id:
- # raise UserError('外币费用的税率只能为0')
|