123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- # -*- coding: utf-8 -*-
- from datetime import date
- from dateutil.relativedelta import relativedelta
- from openerp import models, api, fields, tools, _
- from openerp.exceptions import ValidationError, except_orm, Warning, RedirectWarning
- from openerp.osv import osv
- class ProjectServiceTask(models.Model):
- _name = 'project.service.task'
- name = fields.Char(string='Title', compute='_get_work_name', store=True)
- project_id = fields.Many2one('project.service', required=True, track_visibility='onchange')
- priority = fields.Selection([('0', 'Normal'), ('1', 'High')], string='Priority', select=True)
- partner_id = fields.Many2one('res.partner', string='Customer')
- date_assign = fields.Datetime(string='Assigning Date', select=True, copy=False, readonly=True)
- date = fields.Datetime(string='Deadline', select=True, copy=False)
- color = fields.Integer(string='Color Index')
- stage_id = fields.Many2one('project.service.stage', string='Stage', track_visibility='onchange', copy=False)
- activity_ids = fields.One2many('project.service.activity', 'task_id', string='Planned/Ordered Works')
- materials_used = fields.One2many('project.service.material', 'task_id', string='Materials Used')
- amount_total = fields.Float(string='Total Amount', compute="get_amount_total", readonly=True)
- include_materials = fields.Boolean('Incluir')
- state = fields.Selection([
- ('Preparado', 'Preparado'),
- ('Facturado', 'Facturado'),
- ('Cancelado', 'Cancelado'),
- ], string='Status', readonly=True, default='Preparado', track_visibility='onchange', select=True)
- sequence = fields.Integer(string='Sequence', select=True, help="Gives the sequence order when displaying a list of tasks.")
- invoice_ids = fields.One2many('account.invoice', 'task_invoice_id')
- invoice_count = fields.Integer(
- string='Facturas',
- compute='_get_invoice_count',
- )
- works_done = fields.One2many('project.service.activity', 'task_id', string='Work Done', domain=[('completed', '=', True)])
- reserve_id = fields.Many2one('project.service.reserve', string='Reserva')
- _defaults = {
- 'stage_id': 1,
- 'state': 'Preparado',
- 'date': fields.datetime.now(),
- }
- @api.onchange('reserve_id')
- def get_reserve(self):
- self.partner_id = self.reserve_id.partner_id
- self.project_id = self.reserve_id.place
- @api.one
- @api.depends('partner_id','project_id')
- def _get_work_name(self):
- self.name = self.partner_id.name + ' / ' + self.project_id.name
-
- @api.multi
- def cancel(self):
- self.state = 'Cancelado'
- self.stage_id = 4
- @api.depends('activity_ids.work_cost', 'materials_used.price')
- def get_amount_total(self):
- for records in self:
- for hour in records:
- amount_totall = 0.0
- for line in hour.activity_ids:
- amount_totall += line.work_cost
- if self.include_materials==True:
- for line2 in hour.materials_used:
- amount_totall += line2.price
- records.amount_total = amount_totall
- @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:
- self.stage_id = 1
- for work in self:
- work.write({'state': 'Preparado'})
- return True
- @api.multi
- def unlink(self):
- for task in self:
- if task.state in ('Facturado'):
- raise Warning(('No puedes borrar una tarea ya facturada'))
- if len(task.activity_ids)>0:
- raise Warning(('No puedes borrar una tarea que tiene actividades'))
- return super(ProjectServiceTask, self).unlink()
- @api.multi
- def Facturado(self):
- activity = self.activity_ids
- if not activity:
- raise osv.except_osv(_('UserError!'), _('No puedes facturas una tarea sin actividades.'))
- self.state = 'Facturado'
- 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,
- 'task_invoice_id': self.id,
- }
- inv_id = inv_obj.create(inv_data)
- for records in self.activity_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,))
- if records.completed == False:
- raise osv.except_osv(_('UserError!'), _('Esta actividad aun no ha sido concluida '
- '"%s".') % (records.product_id.name,))
- inv_line_data = {
- 'name': records.product_id.name,
- 'account_id': income_account,
- 'price_unit': records.work_cost,
- 'quantity': 1,
- '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)
- for records in self.materials_used:
- 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,))
- if self.include_materials==True:
- inv_line_data = {
- 'name': records.product_id.name,
- 'account_id': income_account,
- 'price_unit': records.price,
- 'quantity': records.amount,
- '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)
- 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['project.service.task']
- 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
|