mrp.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
  6. # Copyright (C) 2012-2012 ChriCar Beteiligungs- und Beratungs- GmbH (<http://www.camptocamp.at>)
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. # FIXME remove logger lines or change to debug
  23. from openerp.osv import fields, osv
  24. import openerp.netsvc
  25. from openerp.tools.translate import _
  26. import time
  27. import logging
  28. class mrp_production(osv.osv):
  29. _inherit = 'mrp.production'
  30. def allow_reopen(self, cr, uid, ids, context=None):
  31. _logger = logging.getLogger(__name__)
  32. return True
  33. def action_reopen(self, cr, uid, ids, context=None):
  34. """ Changes mrp state from done to draft.
  35. @return: True
  36. """
  37. _logger = logging.getLogger(__name__)
  38. self.allow_reopen(cr, uid, ids, context=None)
  39. move_line_obj = self.pool.get('stock.move')
  40. account_move_line_obj = self.pool.get('account.move.line')
  41. account_move_obj = self.pool.get('account.move')
  42. report_xml_obj = self.pool.get('ir.actions.report.xml')
  43. attachment_obj = self.pool.get('ir.attachment')
  44. now = ' ' + _('Invalid') + time.strftime(' [%Y%m%d %H%M%S]')
  45. for mrp in self.browse(cr, uid, ids):
  46. _logger.debug('FGF production action reopen mrp %s ' %(mrp.name) )
  47. # FIXME
  48. # current implementation deletes all stock_moves (creates some work
  49. # to reinsert lots etc)
  50. # reset to waiting is better but needs complex workflow modification
  51. # no time to do and test this
  52. #move_line_obj.write(cr, uid, ml_ids, {'state':'waiting'})
  53. ml_ids = []
  54. for ml in mrp.move_lines:
  55. ml_ids.append(ml.id)
  56. for ml in mrp.move_lines2:
  57. ml_ids.append(ml.id)
  58. for ml in mrp.move_created_ids:
  59. ml_ids.append(ml.id)
  60. for ml in mrp.move_created_ids2:
  61. ml_ids.append(ml.id)
  62. _logger.debug('FGF production action reopen mrp %s ' %(ml_ids) )
  63. #move_line_obj.write(cr, uid, ml_ids, {'state':'waiting'})
  64. move_line_obj.write(cr, uid, ml_ids, {'state':'draft'})
  65. # client has also produciton orders without BOM, so we do not want
  66. # to delete the entered lines, and no new ones will be created
  67. # automatically
  68. if mrp.bom_id:
  69. move_line_obj.unlink(cr, uid, ml_ids)
  70. # we have to handle real time accounting stock moves
  71. #FIXME - performance, should be an id - link to production
  72. #aml_ids = account_move_line_obj.search(cr, uid, [('production_id','=',mrp.id)])
  73. aml_ids = account_move_line_obj.search(cr, uid, [('ref','=',mrp.name)])
  74. _logger.debug('FGF production action reopen move_lines %s ' %(aml_ids) )
  75. move_ids = []
  76. for aml in account_move_line_obj.browse(cr, uid, aml_ids):
  77. if aml.move_id.id not in move_ids:
  78. move_ids.append(aml.move_id.id)
  79. _logger.debug('FGF production action reopen move %s ' %(move_ids) )
  80. account_move_line_obj.write(cr, uid, [aml.id], {'ref': aml.ref or '' + now})
  81. for move in account_move_obj.browse(cr, uid, move_ids):
  82. account_move_obj.write(cr, uid, [move.id], {'name': move.name + now})
  83. move_copy_id = account_move_obj.copy(cr, uid, move.id,)
  84. account_move_obj.write(cr, uid, [move_copy_id], {'name': move.name + now + '*' })
  85. cr.execute("""update account_move_line
  86. set debit=credit, credit=debit,
  87. ref = ref||'*'
  88. where move_id = %s;""" % (move_copy_id))
  89. # rename attachments (reports)
  90. # for some reason datas_fname has .pdf.pdf extension
  91. report_ids = report_xml_obj.search(cr, uid, [('model','=', 'stock.production'), ('attachment','!=', False)])
  92. for report in report_xml_obj.browse(cr, uid, report_ids):
  93. if report.attachment:
  94. aname = report.attachment.replace('object','mrp')
  95. if eval(aname):
  96. aname = eval(aname)+'.pdf'
  97. attachment_ids = attachment_obj.search(cr, uid,
  98. [('res_model','=','mrp.production'),('datas_fname', '=', aname),('res_id','=',mrp.id)])
  99. for a in attachment_obj.browse(cr, uid, attachment_ids):
  100. vals = {
  101. 'name': a.name.replace('.pdf', now+'.pdf'),
  102. 'datas_fname': a.datas_fname.replace('.pdf.pdf', now+'.pdf.pdf')
  103. }
  104. attachment_obj.write(cr, uid, a.id, vals)
  105. #self.write(cr, uid, mrp.id, {'state':'confirmed'})
  106. self.write(cr, uid, mrp.id, {'state':'draft'})
  107. wf_service = openerp.workflow
  108. wf_service.trg_delete(uid, 'mrp.production', mrp.id, cr)
  109. wf_service.trg_create(uid, 'mrp.production', mrp.id, cr)
  110. message = _("Manufacturing order '%s' is reset to waiting") % (
  111. mrp.name,)
  112. self.log(cr, uid, mrp.id, message)
  113. return True
  114. # def button_reopen(self, cr, uid, ids, context=None):
  115. # _logger = logging.getLogger(__name__)
  116. # self.allow_reopen(cr, uid, ids, context)
  117. # _logger.debug('FGF production allow open ' )
  118. # self.write(cr, uid, ids, {'state':'draft'})
  119. # _logger.debug('FGF production draft ' )
  120. # self.log_production(cr, uid, ids, context=context)
  121. # _logger.debug('FGF production log' )
  122. mrp_production()