[IMP] account : Improved the code
[odoo/odoo.git] / addons / account / wizard / account_period_close.py
index 5342f4b..5217b11 100644 (file)
@@ -39,19 +39,24 @@ class account_period_close(osv.osv_memory):
         @param uid: the current user’s ID for security checks,
         @param ids: account period close’s ID or list of IDs
          """
+        period_pool = self.pool.get('account.period')
+        account_move_obj = self.pool.get('account.move')
 
         mode = 'done'
         for form in self.read(cr, uid, ids, context=context):
             if form['sure']:
                 for id in context['active_ids']:
+                    account_move_ids = account_move_obj.search(cr, uid, [('period_id', '=', id), ('state', '=', "draft")], context=context)
+                    if account_move_ids:
+                        raise osv.except_osv(_('Invalid action !'), _('In order to close a period, you must first post related journal items.'))
+
                     cr.execute('update account_journal_period set state=%s where period_id=%s', (mode, id))
                     cr.execute('update account_period set state=%s where id=%s', (mode, id))
 
                     # Log message for Period
-                    period_pool = self.pool.get('account.period')
                     for period_id, name in period_pool.name_get(cr, uid, [id]):
                         period_pool.log(cr, uid, period_id, "Period '%s' is closed, no more modification allowed for this period." % (name))
-        return {}
+        return {'type': 'ir.actions.act_window_close'}
 
 account_period_close()