construction_order.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. # -*- coding: utf-8 -*-
  2. from openerp import models, fields, tools, api
  3. import openerp.addons.decimal_precision as dp
  4. class constructionOrder(models.Model):
  5. _name = 'construction.order'
  6. name = fields.Char('name')
  7. date = fields.Date()
  8. active = fields.Boolean('Active', default=True)
  9. work_id = fields.Many2one('construction.work', string='obra', required=True)
  10. partner_id = fields.Many2one('res.partner', string='owner', help='owner of the work', required=True)
  11. order_street = fields.Char('street')
  12. order_city = fields.Char('city')
  13. total_area = fields.Float('Superficie', required=True)
  14. ref = fields.Char('Ref')
  15. comment = fields.Text('Comment', help='information')
  16. type = fields.Selection([('budget', 'Presupuesto'),('order', 'orden')])
  17. line_ids = fields.One2many('construction.order.line', 'order_ids', string='lines')
  18. state = fields.Selection([('draft', 'Borrador'),('done', 'Hecho')], default='draft')
  19. currency_id = fields.Many2one('res.currency', string="Currency", help="Moneda de la operación", required=True)
  20. ''' order & budget'''
  21. budget_id = fields.Many2one('construction.order', string='Budget', ondelete='restrict', index=True)
  22. order_id = fields.Many2one('construction.order', string='Orden', ondelete='restrict', index=True)
  23. amount_total = fields.Float('Amount Total', digits_compute=dp.get_precision('Account'), default=0.0)
  24. @api.model
  25. def create(self, vals):
  26. vals['type'] = self._context.get('type')
  27. if (self._context.get('type') == 'budget'):
  28. vals['name'] = self.env['ir.sequence'].get('construction.order.budget') or '/'
  29. else:
  30. vals['name'] = self.env['ir.sequence'].get('construction.order.order') or '/'
  31. return super(constructionOrder, self).create(vals)
  32. @api.model
  33. def get_currency_order(self, id):
  34. order = self.env['construction.order'].browse(id)
  35. company = self.env.user.company_id
  36. currencyID = order.currency_id.id or company.currency_id.id
  37. currency = self.env['res.currency'].search([('id', '=', currencyID)])
  38. if (not currency):
  39. return False
  40. return {
  41. 'thousandsSeparator': currency.thousands_separator,
  42. 'decimalPlaces': currency.decimal_places,
  43. 'decimalSeparator': currency.decimal_separator,
  44. 'name': currency.local_name,
  45. 'symbol ':currency.symbol,
  46. }
  47. @api.model
  48. def eiru_add_task(self, id,tasks):
  49. line = self.env['construction.order.line'].search([('order_ids', '=', id )])
  50. lineTask = []
  51. if ( line ):
  52. for i in line:
  53. i.unlink()
  54. for task in tasks:
  55. lineTask= {
  56. 'code':task['code'],
  57. 'task_name': task['taskName'],
  58. 'uom_id': task['uomId'],
  59. 'task_id': task['taskId'],
  60. 'task_line_id': task['taskLineId'],
  61. 'qty': task['qty'],
  62. 'amount': task['amount'],
  63. 'amount_total': task['amountTotal'],
  64. 'type': task['type'],
  65. 'order_ids': id
  66. }
  67. line.create(lineTask)
  68. order = self.env['construction.order'].browse(id)
  69. if (not order):
  70. return {'state': False }
  71. ammount = 0
  72. for line in order.line_ids:
  73. if (line.type =='activity'):
  74. ammount +=line.amount_total
  75. order.write({'amount_total': ammount})
  76. return {'state': True }
  77. @api.model
  78. def action_confirm_budget(self, idBudget):
  79. orderBudget = self.env['construction.order'].browse(idBudget)
  80. if (not orderBudget):
  81. return
  82. order = orderBudget.copy()
  83. if (not order):
  84. return
  85. order.write({
  86. 'name': self.env['ir.sequence'].get('construction.order.order') or '/',
  87. 'type': 'order',
  88. 'budget_id': orderBudget.id
  89. })
  90. lines = orderBudget.line_ids.copy()
  91. for line in lines:
  92. line.write({'order_ids': order.id})
  93. ''' Actualizar el numero de orden en elregistro de Subcontratista'''
  94. for contractor in orderBudget.contractor_budget_ids:
  95. contractor.write({'order_id': order.id})
  96. ''' Actualizar en numero de orden en el presupuesto '''
  97. orderBudget.write({'state': 'done', 'order_id': order.id})
  98. return True
  99. @api.model
  100. def action_confirm_order(self, idOrder):
  101. move = []
  102. moveLines = []
  103. order = self.env['construction.order'].browse(idOrder)
  104. if (not order):
  105. return False
  106. for line in order.line_ids:
  107. moveLines.append([0, False,{
  108. 'code': line.code,
  109. 'task_name': line.task_name,
  110. 'uom_id': line.uom_id.id,
  111. 'task_id': line.task_id.id,
  112. 'task_line_id': line.task_line_id.id,
  113. 'type': line.type,
  114. 'amount_total': line.amount_total,
  115. 'amount_paid': 0,
  116. 'residual': line.amount_total,
  117. }])
  118. move = {
  119. 'partner_id': order.partner_id.id,
  120. 'order_id': order.id,
  121. 'work_id': order.work_id.id,
  122. 'amount_total': order.amount_total,
  123. 'amount_paid': 0,
  124. 'residual': order.amount_total,
  125. 'type': 'order',
  126. 'move_lines_ids': moveLines,
  127. 'currency_id': order.currency_id.id
  128. }
  129. # create
  130. moveOrder = self.env['construction.move'].create(move)
  131. if (not moveOrder):
  132. return False
  133. moveOrder.createInvoiceMove()
  134. ''' Contratista '''
  135. move = []
  136. for contractor in order.contractor_order_ids:
  137. moveLines = []
  138. for line in contractor.line_ids:
  139. moveLines.append([0, False,{
  140. 'code': line.code,
  141. 'task_name': line.task_name,
  142. 'uom_id': line.uom_id.id,
  143. 'task_id': line.task_id.id,
  144. 'task_line_id': line.task_line_id.id,
  145. 'type': line.type,
  146. 'amount_total': line.amount_total,
  147. 'amount_paid': 0,
  148. 'residual': line.amount_total,
  149. }])
  150. move = {
  151. 'partner_id': contractor.partner_id.id,
  152. 'order_id': order.id,
  153. 'work_id': order.work_id.id,
  154. 'amount_total': contractor.amount_total,
  155. 'amount_paid': 0,
  156. 'residual': contractor.amount_total,
  157. 'type': 'contractor',
  158. 'move_lines_ids': moveLines,
  159. 'currency_id': order.currency_id.id
  160. }
  161. moveOrder = self.env['construction.move'].create(move)
  162. if (not moveOrder):
  163. return False
  164. order.write({'state': 'done'})
  165. ''' RES PARTNER '''
  166. class ResPartnerOrder(models.Model):
  167. _inherit = 'res.partner'
  168. construction_budget = fields.One2many('construction.order', 'partner_id', string='Budget', domain=[('type', '=', 'budget')])
  169. construction_order = fields.One2many('construction.order', 'partner_id', string='Order', domain=[('type', '=', 'order')])
  170. class constructionOrderLine(models.Model):
  171. _name = 'construction.order.line'
  172. code = fields.Char('Code', size=32, required=True, select=True)
  173. task_name = fields.Char('name')
  174. uom_id = fields.Many2one('construction.uom', string='Unidad de medida')
  175. task_id = fields.Many2one('construction.task', string='Tarea')
  176. task_line_id = fields.Many2one('construction.task.line', string='Actividad')
  177. qty = fields.Float('Cantidad', default=0)
  178. amount = fields.Float('Amount Unit', digits_compute=dp.get_precision('Account'), default=0.0)
  179. amount_total = fields.Float('Amount Total', digits_compute=dp.get_precision('Account'), default=0.0)
  180. type = fields.Selection([('task', 'Tarea'),('activity', 'Actividad')])
  181. order_ids = fields.Many2one('construction.order', string='Orden', ondelete='cascade', index=True, required=True)
  182. ''' Get Line in order '''
  183. def get_construction_order_line(self, orderID):
  184. return [{
  185. 'id': line.id,
  186. 'code': line.code,
  187. 'taskName': line.task_name,
  188. 'uomId': line.uom_id.id if(line.uom_id) else "",
  189. 'uomName': line.uom_id.name if(line.uom_id) else "" ,
  190. 'taskId': line.task_id.id,
  191. 'taskLineId': line.task_line_id.id if(line.task_line_id) else "",
  192. 'qty': line.qty,
  193. 'amount': line.amount,
  194. 'amountTotal': line.amount_total,
  195. 'type': line.type,
  196. } for line in self.env['construction.order.line'].search([('order_ids', '=', orderID)])]