~noviat/openobject-addons/extra-6.0

« back to all changes in this revision

Viewing changes to account_pain/wizard/pain_wizard.py

  • Committer: root
  • Date: 2012-04-10 20:37:39 UTC
  • Revision ID: root@oerp61-20120410203739-flykplw8jzpqa15c
update noviat v6.0 accounting modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
import netsvc
32
32
logger=netsvc.Logger()
33
33
 
 
34
from operator import itemgetter
 
35
class account_move_line(osv.osv):
 
36
    _inherit = "account.move.line"
 
37
 
 
38
    def amount_to_pay(self, cr, uid, ids, name, arg={}, context=None):
 
39
        """ Return the amount still to pay regarding all the payment orders
 
40
        (excepting cancelled orders)"""
 
41
        if not ids:
 
42
            return {}
 
43
        cr.execute("""SELECT ml.id,
 
44
                    CASE WHEN ml.amount_currency < 0
 
45
                        THEN - ml.amount_currency
 
46
                        ELSE ml.credit
 
47
                    END -
 
48
                    (SELECT coalesce(sum(amount_currency),0)
 
49
                        FROM payment_line pl
 
50
                            INNER JOIN payment_order po
 
51
                                ON (pl.order_id = po.id)
 
52
                        WHERE move_line_id = ml.id
 
53
                        AND po.state != 'cancel') AS amount
 
54
                    FROM account_move_line ml
 
55
                    WHERE id IN %s""", (tuple(ids),))
 
56
        r = dict(cr.fetchall())
 
57
        return r
 
58
 
 
59
    def _to_pay_search(self, cr, uid, obj, name, args, context=None):
 
60
        if not args:
 
61
            return []
 
62
        line_obj = self.pool.get('account.move.line')
 
63
        query = line_obj._query_get(cr, uid, context={})
 
64
        where = ' and '.join(map(lambda x: '''(SELECT
 
65
        CASE WHEN l.amount_currency < 0
 
66
            THEN - l.amount_currency
 
67
            ELSE l.credit
 
68
        END - coalesce(sum(pl.amount_currency), 0)
 
69
        FROM payment_line pl
 
70
        INNER JOIN payment_order po ON (pl.order_id = po.id)
 
71
        WHERE move_line_id = l.id
 
72
        AND po.state != 'cancel'
 
73
        ) %(operator)s %%s ''' % {'operator': x[1]}, args))
 
74
        sql_args = tuple(map(itemgetter(2), args))
 
75
 
 
76
        cr.execute(('''SELECT id
 
77
            FROM account_move_line l
 
78
            WHERE account_id IN (select id
 
79
                FROM account_account
 
80
                WHERE type in %s AND active)
 
81
            AND reconcile_id IS null
 
82
            AND credit > 0
 
83
            AND ''' + where + ' and ' + query), (('payable','receivable'),)+sql_args ) # fix Noviat to include sale refunds 
 
84
 
 
85
        res = cr.fetchall()
 
86
        if not res:
 
87
            return [('id', '=', '0')]
 
88
        return [('id', 'in', map(lambda x:x[0], res))]
 
89
 
 
90
    _columns = {
 
91
        'amount_to_pay': fields.function(amount_to_pay, method=True,
 
92
            type='float', string='Amount to pay', fnct_search=_to_pay_search),
 
93
    }
 
94
 
 
95
account_move_line()    
 
96
 
34
97
class payment_order_create(osv.osv_memory):
35
98
    _inherit = 'payment.order.create'
36
99
    
45
108
        data = self.read(cr, uid, ids, [], context=context)[0]
46
109
        search_due_date = data['duedate']
47
110
        # Search for move line to pay:
48
 
        domain = [('reconcile_id', '=', False), ('account_id.type', '=', 'payable'), ('amount_to_pay', '>', 0)]
 
111
        domain = [('reconcile_id', '=', False), ('account_id.type', 'in', ['payable', 'receivable']), ('amount_to_pay', '>', 0)]  # update Noviat
49
112
        domain = domain + ['|', ('date_maturity', '<=', search_due_date), ('date_maturity', '=', False)]
50
 
        domain = domain + [('journal_id.type', '=', 'purchase')] #update Noviat
 
113
        domain = domain + [('journal_id.type', 'in', ['purchase', 'sale_refund'])] # update Noviat
51
114
        line_ids = line_obj.search(cr, uid, domain, context=context)
52
115
        context.update({'line_ids': line_ids})
53
116
        model_data_ids = mod_obj.search(cr, uid,[('model', '=', 'ir.ui.view'), ('name', '=', 'view_create_payment_order_lines')], context=context)