22
22
##############################################################################
26
from osv import fields, osv
25
import openerp.netsvc as netsvc
26
from openerp.osv import osv, fields
27
27
from mx import DateTime
28
28
from tools import config
29
from tools.translate import _
31
class sale_order(osv.osv):
29
from openerp.tools.translate import _
33
class sale_order(osv.Model):
32
34
_inherit = "sale.order"
34
36
def check_limit(self, cr, uid, ids, context={}):
36
38
partner = so.partner_id
38
40
moveline_obj = self.pool.get('account.move.line')
39
movelines = moveline_obj.search(cr, uid, [('partner_id', '=', partner.id),('account_id.type', 'in', ['receivable', 'payable']), ('state', '<>', 'draft'), ('reconcile_id', '=', False)])
41
movelines = moveline_obj.search(cr, uid, [('partner_id', '=', partner.id), ('account_id.type', 'in', [
42
'receivable', 'payable']), ('state', '<>', 'draft'), ('reconcile_id', '=', False)])
40
43
movelines = moveline_obj.browse(cr, uid, movelines)
42
45
debit, credit = 0.0, 0.0
43
46
debit_maturity, credit_maturity = 0.0, 0.0
45
48
for line in movelines:
46
if line.date_maturity < time.strftime('%Y-%m-%d') and line.date_maturity <> False:
49
if line.date_maturity < time.strftime('%Y-%m-%d') and line.date_maturity != False:
47
50
credit_maturity += line.debit
48
51
debit_maturity += line.credit
49
52
credit += line.debit
53
56
saldo_maturity = credit_maturity - debit_maturity
55
58
if (saldo_maturity + so.amount_total) > partner.credit_maturity_limit or (saldo + so.amount_total) > partner.credit_limit:
56
if not partner.over_credit :
59
if not partner.over_credit:
57
60
if (saldo + so.amount_total) > partner.credit_limit and partner.credit_limit > 0.00:
58
msg = _('Can not validate the Sale Order because it has exceeded the credit limit \nCredit Limit: %s \nCheck the credit limits on Partner')%(partner.credit_limit)
61
msg = _('Can not validate the Sale Order because it has exceeded the credit limit \nCredit Limit: %s \nCheck the credit limits on Partner') % (
59
63
#'Can not validate Invoice because Total Invoice is greater than credit_limit: %s\nCheck Partner Accounts or Credit Limits !'%(partner.credit_limit)
60
64
raise osv.except_osv(_('Credit Over Limits !'), (msg))
63
self.pool.get('res.partner').write(cr, uid, [partner.id], {'credit_limit':credit - debit + so.amount_total})
67
self.pool.get('res.partner').write(cr, uid, [partner.id], {
68
'credit_limit': credit - debit + so.amount_total})
65
70
if not partner.maturity_over_credit:
66
71
if (saldo_maturity + so.amount_total) > partner.credit_maturity_limit and partner.credit_maturity_limit > 0.00:
67
72
#~ msg = 'Can not validate Invoice, Total mature due Amount %s as on %s !\nCheck Partner Accounts or Credit Limits !' % (credit - debit, time.strftime('%Y-%m-%d'))
68
msg = _('Can not validate the Sale Order because it has exceeded the credit limit up to date: %s \nMaturity Amount: %s \nMaturity Credit Limit: %s \nCheck the credit limits on Partner')%(time.strftime('%Y-%m-%d'), saldo_maturity, partner.credit_maturity_limit)
73
msg = _('Can not validate the Sale Order because it has exceeded the credit limit up to date: %s \nMaturity Amount: %s \nMaturity Credit Limit: %s \nCheck the credit limits on Partner') % (
74
time.strftime('%Y-%m-%d'), saldo_maturity, partner.credit_maturity_limit)
70
raise osv.except_osv(_('Maturity Credit Over Limits !'), (msg))
76
raise osv.except_osv(_(
77
'Maturity Credit Over Limits !'), (msg))
75
self.pool.get('res.partner').write(cr, uid, [partner.id], {'credit_maturity_limit':credit_maturity - debit_maturity + so.amount_total})
82
self.pool.get('res.partner').write(cr, uid, [partner.id], {
83
'credit_maturity_limit': credit_maturity - debit_maturity + so.amount_total})