25
25
# along with this program; if not, write to the Free Software
26
26
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27
27
###############################################################################
29
from osv import fields
30
from tools.translate import _
28
from openerp.osv import fields, osv
29
from openerp.tools.translate import _
33
class cancel_orders(osv.osv_memory):
34
class cancel_orders(osv.TransientModel):
35
36
M321 Customizations to cancel orders that are confirmed but are not paid
37
38
_name = "cancel.orders"
40
'sure': fields.boolean("Sure?", help="Check if are sure"),
41
'are_sure': fields.boolean("Are Sure?", help="Check if really are sure"),
42
'n_days':fields.integer('Number Days',help="Number of day to cancel sales orders by defaults 2")
41
'sure': fields.boolean("Sure?", help="Check if are sure"),
42
'are_sure': fields.boolean("Are Sure?", help="Check if really are sure"),
43
'n_days': fields.integer('Number Days', help="Number of day to cancel sales orders by defaults 2")
49
def cancel_orders(self,cr,uid,ids=False,days=1,context=None):
50
def cancel_orders(self, cr, uid, ids=False, days=1, context=None):
50
51
if context is None:
52
53
sale_obj = self.pool.get('sale.order')
53
54
picking_obj = self.pool.get('stock.picking')
54
55
invoice_obj = self.pool.get('account.invoice')
55
56
journal_obj = self.pool.get('account.journal')
56
journal_ids = journal_obj.search(cr,uid,[],context=context)
57
[journal_obj.write(cr,uid,[i.id],{'update_posted':True},context=context) for i in journal_obj.browse(cr,uid,journal_ids,context=context) if hasattr(i, "update_posted") if i.type in ('sale','sale_refund') ]
58
wz_brw = ids and self.browse(cr,uid,ids[0],context=context) or False
57
journal_ids = journal_obj.search(cr, uid, [], context=context)
58
[journal_obj.write(cr, uid, [i.id], {'update_posted': True}, context=context) for i in journal_obj.browse(
59
cr, uid, journal_ids, context=context) if hasattr(i, "update_posted") if i.type in ('sale', 'sale_refund')]
60
wz_brw = ids and self.browse(cr, uid, ids[0], context=context) or False
59
61
evalu = wz_brw and 'wz_brw.sure and wz_brw.are_sure' or 'True'
60
62
date = datetime.datetime.today()
61
date = date and date - datetime.timedelta(days= wz_brw and eval('wz_brw.n_days') or days)
63
date = date and date - datetime.timedelta(
64
days=wz_brw and eval('wz_brw.n_days') or days)
62
65
date = date and date.strftime('%Y-%m-%d')
63
sales_ids = sale_obj.search(cr,uid,[('date_order','<=',date)],context=context)
64
sale_brw = sales_ids and sale_obj.browse(cr,uid,sales_ids,context=context)
66
sales_ids = sale_obj.search(cr, uid, [(
67
'date_order', '<=', date)], context=context)
68
sale_brw = sales_ids and sale_obj.browse(
69
cr, uid, sales_ids, context=context)
66
71
if eval(evalu) and sale_brw:
67
72
for sale in sale_brw:
68
if sale.state in ('manual','progress'):
69
pick = [False for pick in sale.picking_ids if pick and pick.state in ('confirmed','done') ]
70
invoice = [False for invoice in sale.invoice_ids if invoice and invoice.state in ('paid','open') ]
73
if sale.state in ('manual', 'progress'):
74
pick = [False for pick in sale.picking_ids if pick and pick.state in (
76
invoice = [False for invoice in sale.invoice_ids if invoice and invoice.state in (
71
78
all(pick) and all(invoice) and sale_ids.append(sale.id)
73
sale_ids and picking_obj.action_cancel(cr, uid,[d.id for i in sale_obj.browse(cr,uid,sale_ids,context=context) for d in i.picking_ids], context=context)
74
sale_ids and invoice_obj.action_cancel(cr, uid,[d.id for i in sale_obj.browse(cr,uid,sale_ids,context=context) for d in i.invoice_ids],)
75
sale_ids and sale_obj.action_cancel(cr, uid,sale_ids, context=context)
80
sale_ids and picking_obj.action_cancel(cr, uid, [d.id for i in sale_obj.browse(
81
cr, uid, sale_ids, context=context) for d in i.picking_ids], context=context)
82
sale_ids and invoice_obj.action_cancel(cr, uid, [d.id for i in sale_obj.browse(
83
cr, uid, sale_ids, context=context) for d in i.invoice_ids],)
84
sale_ids and sale_obj.action_cancel(
85
cr, uid, sale_ids, context=context)
78
raise osv.except_osv(_('Processing Error'), _('Must select the 2 options to make sure the operation'))
88
raise osv.except_osv(_('Processing Error'), _(
89
'Must select the 2 options to make sure the operation'))
79
90
return {'type': 'ir.actions.act_window_close'}