# -*- coding: utf-8 -*- from openerp import models, fields, api from openerp.exceptions import except_orm, Warning, RedirectWarning class CarCarModify(models.Model): _inherit = 'car.car' name = fields.Many2one('car.service', string='Nombre del vehiculo', required=True) agent_id = fields.Many2one('res.partner', string='Agente de Seguro') policy = fields.Boolean() @api.one @api.onchange('name') def onchange_name(self): self.partner_id = self.name.partner_id.id self.agent_id = self.name.agent_id.id self.policy = self.name.policy date_in = fields.Date('Fecha de entrada', default=fields.Date.today()) date_out = fields.Date('Fecha de Salida') image_medium = fields.Binary('Binary File', related="name.logo") @api.multi def unlink(self): for project in self: if project.state in ('close'): raise Warning('No puedes borrar un proyecto en estado cerrado') if len(project.task_ids) > 0: raise Warning('No puedes borrar un proyecto que tiene tareas') return super(CarCarModify, self).unlink() def on_change_vehicle(self): if not self.name: return {} model = self.pool.get('car.service').browse(self.name) return { 'value': { 'image_medium': model.logo, } } class AccountInvoiceModify(models.Model): _inherit = 'account.invoice' work_invoice_id = fields.Many2one('car.workshop') class CarWorkshopModify(models.Model): _inherit = 'car.workshop' invoice_ids = fields.One2many('account.invoice', 'work_invoice_id') invoice_count = fields.Integer( string='Facturas', compute='_get_invoice_count', ) @api.one @api.depends('invoice_ids','state') def _get_invoice_count(self): self.invoice_count = len(self.invoice_ids) @api.multi def button_draft(self): if self.invoice_count > 0: raise Warning('Esta tarea tiene una factura asociada') if self.invoice_count == 0: for work in self: work.write({'state': 'waiting'}) return True @api.multi def unlink(self): for task in self: if task.state in ('workshop_create_invoices'): raise Warning(('No puedes borrar una tarea ya facturada')) if len(task.planned_works)>0: raise Warning(('No puedes borrar una tarea que tiene actividades')) return super(CarWorkshopModify, self).unlink() @api.multi def workshop_create_invoices(self): self.state = 'workshop_create_invoices' 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 currency_value = company_id.currency_id.id self.ensure_one() ir_values = self.env['ir.values'] journal_id = ir_values.get_default('workshop.config.setting', 'invoice_journal_type') if not journal_id: journal_id = 1 inv_data = { 'name': customer.name, 'reference': customer.name, 'account_id': customer.property_account_receivable.id, 'partner_id': customer.id, 'currency_id': currency_value, 'journal_id': journal_id, 'origin': self.name, 'company_id': company_id.id, 'work_invoice_id': self.id } inv_id = inv_obj.create(inv_data) for records in self.planned_works: if records.planned_work.id: income_account = records.planned_work.property_account_income.id if not income_account: raise osv.except_osv(_('UserError!'), _('There is no income account defined ' 'for this product: "%s".') % (records.planned_work.name,)) inv_line_data = { 'name': records.planned_work.name, 'account_id': income_account, 'price_unit': records.work_cost, 'quantity': 1, 'product_id': records.planned_work.id, 'invoice_id': inv_id.id, } inv_line_obj.create(inv_line_data) for records in self.materials_used: if records.material.id: income_account = records.material.property_account_income.id if not income_account: raise osv.except_osv(_('UserError!'), _('There is no income account defined ' 'for this product: "%s".') % (records.material.name,)) if self.include_materials==True: inv_line_data = { 'name': records.material.name, 'account_id': records.material.property_account_income.id, 'price_unit': records.price, 'quantity': records.amount, 'product_id': records.material.id, 'invoice_id': inv_id.id, } inv_line_obj.create(inv_line_data) 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['car.workshop'] total = 0 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 return result