# -*- coding: utf-8 -*- # License, author and contributors information in: # __openerp__.py file at the root folder of this module. from openerp import api, models, fields from openerp.exceptions import ValidationError, except_orm, Warning, RedirectWarning import logging _log = logging.getLogger(__name__) class WorkOrder(models.Model): _name = 'repair.workorder' _description = 'Work order' _inherit = ['mail.thread', 'ir.needaction_mixin'] def _get_user(self): return self.env.uid def _get_number(self): return self.env['ir.sequence'].get('repair.workorder') or '*' name = fields.Char( string=u'Code', readonly=True, default=_get_number ) user_id = fields.Many2one( comodel_name='res.users', string='Engineer', default=_get_user ) partner_id = fields.Many2one( comodel_name='res.partner', string='Partner' ) file_import = fields.Binary(string='Archivo Adjunto') filename=fields.Char(string='Nombre de Archivo') file_import_a = fields.Binary(string='Archivo Adjunto') filename_a=fields.Char(string='Nombre de Archivo') file_import_b = fields.Binary(string='Archivo Adjunto') filename_b=fields.Char(string='Nombre de Archivo') type = fields.Char( string='Tipo de servicio' ) line_ids = fields.One2many( comodel_name='repair.workorder.line', inverse_name='workorder_id', string='Products delivered' ) consumed_ids = fields.One2many( comodel_name='repair.workorder.consumed', inverse_name='workorder_id', string='Product & Services consumed' ) order_date = fields.Datetime( string='Order date', default=fields.Datetime.now ) planned_start_date = fields.Datetime( string='Planned start date' ) planned_end_date = fields.Datetime( string='Planned end date' ) next_action_date = fields.Date( string='Fecha Próx. Acción' ) diagnostic = fields.Text( string='Diagnostic' ) causes = fields.Text( string='Causes' ) actions = fields.Text( string='Actions' ) recommendations = fields.Text( string="recommendations" ) state = fields.Selection([ ('draft', 'Pending'), ('in_progress', 'In progress'), ('done', 'Done'), ('canceled', 'Canceled'), ('invoiced', 'Invoiced')], string='State', default='draft' ) invoice_ids = fields.One2many('account.invoice', 'work_invoice_id') invoice_count = fields.Integer( string='Facturas', compute='_get_invoice_count', ) @api.multi def button_draft(self): if self.invoice_count > 0: raise Warning('Este trabajo tiene una factura asociada') if self.invoice_count == 0: for work in self: work.write({'state': 'draft'}) return True @api.one @api.depends('invoice_ids') def _get_invoice_count(self): self.invoice_count = len(self.invoice_ids) @api.one def onchange_partner_id(self, partner_id): _log.info('-'*100) _log.info(partner_id) @api.one def button_in_progress(self): self.state = 'in_progress' @api.one def button_in_progress_back(self): self.state = 'draft' @api.one def button_done(self): product = self.line_ids works = self.consumed_ids if not product or not works: raise Warning('El trabajo debe tener productos y trabajos asociados') else: self.state = 'done' @api.one def button_done_back(self): self.state = 'in_progress' @api.one def button_cancel(self): self.state = 'canceled' @api.multi def Facturado(self): inv_obj = self.env['account.invoice'] inv_line_obj = self.env['account.invoice.line'] customer = self.partner_id if not customer.name: raise osv.except_osv(_('UserError!'), _('Please select a Customer.')) company_id = self.env['res.users'].browse(1).company_id self.ensure_one() ir_values = self.env['ir.values'] inv_data = { 'name': customer.name, 'reference': customer.name, 'account_id': customer.property_account_receivable.id, 'partner_id': customer.id, 'origin': self.name, 'work_invoice_id': self.id } inv_id = inv_obj.create(inv_data) for records in self.consumed_ids: if records.product_id.id: income_account = records.product_id.categ_id.property_account_income_categ.id if not income_account: raise osv.except_osv(_('UserError!'), _('There is no income account defined ' 'for this product: "%s".') % (records.product_id.name,)) inv_line_data = { 'name': records.product_id.name, 'account_id': income_account, 'price_unit': records.price_unit, 'quantity': records.quantity, 'product_id': records.product_id.id, 'invoice_id': inv_id.id, 'invoice_line_tax_id': [(6, 0, [x.id for x in records.product_id.taxes_id])], } inv_line_obj.create(inv_line_data) self.state = 'invoiced' imd = self.env['ir.model.data'] action = imd.xmlid_to_object('account.action_invoice_tree1') list_view_id = imd.xmlid_to_res_id('account.invoice_tree') form_view_id = imd.xmlid_to_res_id('account.invoice_form') result = { 'name': action.name, 'help': action.help, 'type': 'ir.actions.act_window', 'views': [[list_view_id, 'tree'], [form_view_id, 'form'], [False, 'graph'], [False, 'kanban'], [False, 'calendar'], [False, 'pivot']], 'target': action.target, 'context': action.context, 'res_model': 'account.invoice', } if len(inv_id) > 1: result['domain'] = "[('id','in',%s)]" % inv_id.ids elif len(inv_id) == 1: result['views'] = [(form_view_id, 'form')] result['res_id'] = inv_id.ids[0] else: result = {'type': 'ir.actions.act_window_close'} invoiced_records = self.env['repair.workorder'] total = 0 self.stage_id = 3 for rows in invoiced_records: invoiced_date = rows.date invoiced_date = invoiced_date[0:10] if invoiced_date == str(date.today()): total = total + rows.price_subtotal inv_id.signal_workflow('invoice_open') return result class WorkOrderLine(models.Model): _name = 'repair.workorder.line' _description = 'Product to repair' _inherit = ['mail.thread', 'ir.needaction_mixin'] workorder_id = fields.Many2one( comodel_name='repair.workorder', string='Work order') description = fields.Char(string='Description') quantity = fields.Float(string='Quantity', default=1.0) brand = fields.Char(string='Marca') number = fields.Char(string="Numero de serie") class WorkOrderConsumed(models.Model): _name = 'repair.workorder.consumed' _description = 'Services for repair' _inherit = ['mail.thread', 'ir.needaction_mixin'] workorder_id = fields.Many2one( comodel_name='repair.workorder', string='Work order' ) product_id = fields.Many2one( comodel_name='product.product', string='Product' ) type = fields.Selection([ ('service', 'Service'), ('product', 'Product')], string='Type', required=True, default='service' ) description = fields.Char( string='Description', required=True ) quantity = fields.Float( string='Quantity', default=1 ) price_unit = fields.Float( string='Price unit' ) subtotal = fields.Float( string='Subtotal', compute='compute_subtotal' ) @api.one @api.depends('quantity', 'price_unit') def compute_subtotal(self): self.subtotal = self.quantity * self.price_unit @api.onchange('product_id') def onchange_product_id(self): if self.product_id: self.description = self.product_id.name self.type = 'service' if self.product_id.type == 'service' \ else 'product' # @ TODO impuestos?? # Obtener el precio del producto a partir de la tarifa del cliente self.price_unit = self.product_id.list_price class AccountInvoice(models.Model): _inherit = 'account.invoice' work_invoice_id = fields.Many2one('repair.workorder')