# -*- coding: utf-8 -*- from odoo import api, fields, models class PurchaseWorkflow(models.Model): _name = 'purchase.workflow' _description = '采购审批单' _inherit = ['soong.workflow.mixin', 'mail.thread.main.attachment', 'mail.activity.mixin'] code = fields.Char(string="编号", default=lambda self: self.env['ir.sequence'].next_by_code('purchase_order_sequence_code')) name = fields.Char(string="标题") name_title = fields.Char(string="默认标题", default=lambda self: self._description, readonly=True) user_id = fields.Many2one('res.users', string="申请人", default=lambda self: self.env.user, required=True) department_id = fields.Many2one(related='user_id.employee_id.department_id', string="申请部门", store=True, readonly=True) company_id = fields.Many2one('res.company', string="公司", default=lambda self: self.env.company, required=True) start_date = fields.Date(string="申请日期", default=fields.Date.today(), required=True) date_order = fields.Datetime(string="订单日期", default=fields.Date.today()) date_planned = fields.Datetime(string="预计到货日期", default=fields.Date.today()) date_application = fields.Date(string="申请日期", default=fields.Date.today(), readonly=True) partner_id = fields.Many2one('res.partner', string="供应商/乙方", required=True) reason_text = fields.Text(string="申请事由") urgency_level = fields.Selection([('normal', '普件'), ('urgent', '急件'), ('emergency', '特急件')], string="紧急度", default='normal', required=True) pay_way = fields.Selection([('onetime', '一次性'), ('installment', '分期')], string="付款方式", default='onetime', required=True) total = fields.Float(string="合计金额", readonly=True) document_ids = fields.Many2many('ir.attachment', string="附件") active = fields.Boolean('启用', default=True, tracking=True) work_end = fields.Boolean(string="审批结束", default=False, tracking=True) purchase_type = fields.Selection([('fixed', '固定资产'), ('low', '低值易耗'), ('service', '服务')], string="采购类型", required=True, default='fixed') service_content = fields.Text(string="服务内容") service_start_date = fields.Date(string="服务开始日期") service_end_date = fields.Date(string="服务结束日期") service_year = fields.Char(string="服务年限") license_id = fields.Many2one('ir.attachment', string='营业执照') aptitude_id = fields.Many2one('ir.attachment', string='资质') contract_id = fields.Many2one('ir.attachment', string='合同') fixed_line_ids = fields.One2many('purchase.fixed.line', 'purchase_id', string="固定资产明细") low_line_ids = fields.One2many('purchase.low.line', 'purchase_id', string="低值易耗明细") service_line_ids = fields.One2many('purchase.service.line', 'service_id', string="服务明细") pay_line_ids = fields.One2many('purchase.order.pay.line', 'purchase_id', string="付款详情") @api.onchange('purchase_type') def _onchange_purchase_type(self): if self.purchase_type == 'fixed': self.low_line_ids = [(5, 0, 0)] self.service_line_ids = [(5, 0, 0)] elif self.purchase_type == 'low': self.fixed_line_ids = [(5, 0, 0)] self.service_line_ids = [(5, 0, 0)] elif self.purchase_type == 'service': self.fixed_line_ids = [(5, 0, 0)] self.low_line_ids = [(5, 0, 0)] def end_approval(self): for rec in self: purchase_order = self.env['purchase.order'].create({ 'partner_id': rec.partner_id.id, 'date_order': rec.date_order, 'date_planned': rec.date_planned, 'company_id': rec.company_id.id, 'user_id': rec.user_id.id, }) order_lines = [] if rec.purchase_type == 'fixed': for line in rec.fixed_line_ids: if not line.product_id: continue order_lines.append((0, 0, { 'product_id': line.product_id.id, 'product_qty': line.number, 'name': line.name, 'price_unit': line.price_unit, 'product_packaging_id': line.product_packaging_id.id, 'taxes_id': [(6, 0, line.taxes_id.ids)] if line.taxes_id else False })) elif rec.purchase_type == 'low': for line in rec.low_line_ids: if not line.product_id: continue order_lines.append((0, 0, { 'product_id': line.product_id.id, 'product_qty': line.number, 'product_uom': line.unit_id.id, 'name': line.name, 'price_unit': line.price, 'product_packaging_id': line.product_packaging_id.id, 'taxes_id': [(6, 0, line.taxes_id.ids)] if line.taxes_id else False })) elif rec.purchase_type == 'service': for line in rec.service_line_ids: if not line.product_id: continue order_lines.append((0, 0, { 'product_id': line.product_id.id, 'name': line.name, 'price_unit': line.price, 'product_packaging_id': line.product_packaging_id.id, 'taxes_id': [(6, 0, line.taxes_id.ids)] if line.taxes_id else False })) if order_lines: purchase_order.order_line = order_lines class PurchaseOrderPayLine(models.Model): _name = "purchase.order.pay.line" _description = "付款详情" purchase_id = fields.Many2one('purchase.workflow', string="关联采购工作流") payment_stage = fields.Char(string="付款阶段") amount = fields.Char(string="比例/金额") payment_date = fields.Date(string="付款日期") remake = fields.Char(string="条件") class PurchaseFixedLine(models.Model): _name = 'purchase.fixed.line' _description = '固定资产采购明细' purchase_id = fields.Many2one('purchase.workflow', string="关联采购工作流") product_id = fields.Many2one('product.product', string="物品名称") brand = fields.Char(string="品牌") specs = fields.Char(string="规格") number = fields.Integer(string="数量",) name = fields.Text(string="描述") product_packaging_id = fields.Many2one('product.packaging', string="包装") taxes_id = fields.Many2one('account.tax', string='税项') unit_id = fields.Many2one(related='product_id.uom_id', string="单位") price_unit = fields.Float(related='product_id.standard_price', string="单价", store=True) total = fields.Float(string="合计", compute='_compute_total', store=True) @api.depends('price_unit', 'number') def _compute_total(self): for line in self: line.total = round(line.price_unit * line.number, 2) @api.onchange('product_id') def _onchange_product_id(self): if self.product_id: self.name = self.product_id.display_name class PurchaseLowLine(models.Model): _name = 'purchase.low.line' _description = '低值易耗采购明细' purchase_id = fields.Many2one('purchase.workflow', string="关联采购工作流") product_id = fields.Many2one('product.product', string="物品名称") brand = fields.Char(related="product_id.brand", string="品牌") specs = fields.Char(related="product_id.specs", string="规格") name = fields.Text(string="描述") product_packaging_id = fields.Many2one('product.packaging', string="包装") taxes_id = fields.Many2one('account.tax', string='税项') number = fields.Integer(string="数量") unit_id = fields.Many2one(related='product_id.uom_id', string="单位") price = fields.Float(related='product_id.standard_price', string="单价", store=True) total = fields.Float(string="合计", compute='_compute_total', store=True) @api.depends('price', 'number') def _compute_total(self): for line in self: line.total = round(line.price * line.number, 2) @api.onchange('product_id') def _onchange_product_id(self): if self.product_id: self.name = self.product_id.display_name class PurchaseServiceLine(models.Model): _name = 'purchase.service.line' _description = '服务采购明细' service_id = fields.Many2one('purchase.workflow', string="关联采购工作流") product_id = fields.Many2one('product.product', string="服务项目") frequency = fields.Integer(string='频次') fee = fields.Float(string="费用") name = fields.Text(string="描述", default=lambda self: self.product_id.display_name if self.product_id else False ) product_packaging_id = fields.Many2one('product.packaging', string="包装") taxes_id = fields.Many2one('account.tax', string='税项') unit_id = fields.Many2one(related='product_id.uom_id', string="单位") price = fields.Float(related='product_id.standard_price', string="单价", store=True) total = fields.Float(string="合计", compute='_compute_total', store=True) @api.depends('price', 'frequency') def _compute_total(self): for line in self: line.total = round(line.price * line.frequency, 2) @api.onchange('product_id') def _onchange_product_id(self): if self.product_id: self.name = self.product_id.display_name class PurchaseFixedAssets(models.Model): _name = 'purchase.fixed.assets' name = fields.Char(string="名称") class PurchaseServiceOrders(models.Model): _name = 'purchase.service.orders' name = fields.Char(string="名称") class PurchaseLowOrders(models.Model): _name = 'purchase.low.orders' name = fields.Char(string="名称")