sale_fast_confirm.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Odoo, Open Source Management Solution
  5. #
  6. # Author: Andrius Laukavičius. Copyright: JSC NOD Baltic
  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. from openerp import models, api
  23. from pytz import timezone
  24. from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT, DEFAULT_SERVER_DATE_FORMAT
  25. from datetime import datetime,timedelta
  26. DATE_FORMAT = '%Y-%m-%d'
  27. # return datetime.now(self.get_timezone()).strftime(DATE_FORMAT)
  28. class SaleOrder(models.Model):
  29. _inherit = 'sale.order'
  30. ''' Timezone '''
  31. def get_timezone(self):
  32. return timezone(self._context.get('tz') or self.env.user.tz)
  33. def _convert_str_to_datetime(self, date):
  34. return datetime.strptime(date,DEFAULT_SERVER_DATETIME_FORMAT)
  35. @api.multi
  36. def order_process_now(self):
  37. """
  38. Confirms order and creates and validates invoice, confirms pickings.
  39. """
  40. for sale in self:
  41. sale.action_button_confirm()
  42. inv_id = sale.action_invoice_create()
  43. if inv_id:
  44. inv = self.env['account.invoice'].browse(inv_id)
  45. date = self._convert_str_to_datetime(sale.date_order)
  46. date = date.strftime(DATE_FORMAT)
  47. inv.write({'date_invoice':date})
  48. self.update_state()
  49. inv.signal_workflow('invoice_open')
  50. for picking in sale.picking_ids:
  51. picking.force_assign()
  52. picking.action_done()
  53. @api.multi
  54. def update_state(self):
  55. for order in self:
  56. order.write({'state': 'done'})
  57. return True
  58. @api.multi
  59. def action_cancel(self):
  60. """
  61. Cancel the sale order if it's in 'draft' or 'sent' state.
  62. """
  63. for order in self:
  64. if order.state in ['draft', 'sent']:
  65. order.write({'state': 'cancel'})
  66. return True