|
@@ -44,6 +44,123 @@ class CarCarModify(models.Model):
|
|
class CarWorkshopModify(models.Model):
|
|
class CarWorkshopModify(models.Model):
|
|
_inherit = 'car.workshop'
|
|
_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 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,))
|
|
|
|
+
|
|
|
|
+ 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
|
|
|
|
+
|
|
@api.multi
|
|
@api.multi
|
|
def unlink(self):
|
|
def unlink(self):
|
|
for task in self:
|
|
for task in self:
|
|
@@ -52,3 +169,8 @@ class CarWorkshopModify(models.Model):
|
|
if len(task.planned_works)>0:
|
|
if len(task.planned_works)>0:
|
|
raise Warning(('No puedes borrar una tarea que tiene actividades'))
|
|
raise Warning(('No puedes borrar una tarea que tiene actividades'))
|
|
return super(CarWorkshopModify, self).unlink()
|
|
return super(CarWorkshopModify, self).unlink()
|
|
|
|
+
|
|
|
|
+class AccountInvoiceModify(models.Model):
|
|
|
|
+ _inherit = 'account.invoice'
|
|
|
|
+
|
|
|
|
+ work_invoice_id = fields.Many2one('car.workshop')
|