~vauxoo/addons-vauxoo/8.0-import_tax_tariff-dev-yani-rev-2

« back to all changes in this revision

Viewing changes to m321_customization/wizard/cancel_order.py

  • Committer: Nhomar Hernandez
  • Date: 2013-04-19 20:33:12 UTC
  • mfrom: (542.1.314 addons-vauxoo)
  • Revision ID: nhomar@gmail.com-20130419203312-o35v7dn79l6vur0t
[MERGE - PEP8 AND V7-MIG] All migrated to V7 Just
improved osv.osv => osv.Model, osv.osv_memory => osv.TransientModel
import inside openerp.* enviroment
Erased class instansiation no necesarry anymore in V7
AUTOPEP8 run, Left PEP8 long lines manually.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
###############################################################################
28
 
from osv import osv
29
 
from osv import fields
30
 
from tools.translate import _
 
28
from openerp.osv import fields, osv
 
29
from openerp.tools.translate import _
 
30
 
31
31
import datetime
32
32
 
33
 
class cancel_orders(osv.osv_memory):
 
33
 
 
34
class cancel_orders(osv.TransientModel):
34
35
    """
35
36
    M321 Customizations to cancel orders that are confirmed but are not paid
36
37
    """
37
38
    _name = "cancel.orders"
38
 
    
 
39
 
39
40
    _columns = {
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")
43
 
        }
 
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")
 
44
    }
44
45
    _defaults = {
45
 
     'n_days':2
46
 
    
47
 
    } 
48
 
 
49
 
    def cancel_orders(self,cr,uid,ids=False,days=1,context=None):
 
46
        'n_days': 2
 
47
 
 
48
    }
 
49
 
 
50
    def cancel_orders(self, cr, uid, ids=False, days=1, context=None):
50
51
        if context is None:
51
52
            context = {}
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) 
65
 
        sale_ids = [] 
 
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)
 
70
        sale_ids = []
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 (
 
75
                        'confirmed', 'done')]
 
76
                    invoice = [False for invoice in sale.invoice_ids if invoice and invoice.state in (
 
77
                        'paid', 'open')]
71
78
                    all(pick) and all(invoice) and sale_ids.append(sale.id)
72
 
            
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) 
76
 
     
 
79
 
 
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)
 
86
 
77
87
        else:
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'}
80
91
 
81
 
cancel_orders()