Update for new Transaction
This commit is contained in:
@@ -4,7 +4,7 @@ Installing trytond_analytic_purchase
|
||||
Prerequisites
|
||||
-------------
|
||||
|
||||
* Python 2.4 or later (http://www.python.org/)
|
||||
* Python 2.5 or later (http://www.python.org/)
|
||||
* trytond (http://www.tryton.org/)
|
||||
* trytond_analytic_account (http://www.tryton.org/)
|
||||
* trytond_analytic_invoice (http://www.tryton.org/)
|
||||
|
||||
+57
-71
@@ -1,8 +1,9 @@
|
||||
#This file is part of Tryton. The COPYRIGHT file at the top level of
|
||||
#this repository contains the full copyright notices and license terms.
|
||||
"Purchase"
|
||||
from __future__ import with_statement
|
||||
from trytond.model import ModelView, ModelSQL, fields
|
||||
from trytond.pyson import Not, Equal, Eval
|
||||
from trytond.transaction import Transaction
|
||||
|
||||
|
||||
class Purchase(ModelSQL, ModelView):
|
||||
@@ -15,25 +16,24 @@ class Purchase(ModelSQL, ModelView):
|
||||
'on line "%s" (%d)!',
|
||||
})
|
||||
|
||||
def check_for_quotation(self, cursor, user, purchase_id, context=None):
|
||||
def check_for_quotation(self, purchase_id):
|
||||
account_selection_obj = self.pool.get(
|
||||
'analytic_account.account.selection')
|
||||
purchase_line_obj = self.pool.get('purchase.line')
|
||||
|
||||
res = super(Purchase, self).check_for_quotation(cursor, user,
|
||||
purchase_id, context=context)
|
||||
res = super(Purchase, self).check_for_quotation(purchase_id)
|
||||
|
||||
purchase = self.browse(cursor, user, purchase_id, context=context)
|
||||
if not account_selection_obj.check_root(cursor, user,
|
||||
purchase = self.browse(purchase_id)
|
||||
if not account_selection_obj.check_root(
|
||||
[x.analytic_accounts.id for x in purchase.lines
|
||||
if x.analytic_accounts.id]):
|
||||
for line in purchase.lines:
|
||||
if line.type != 'line':
|
||||
continue
|
||||
if not account_selection_obj.check_root(cursor, user,
|
||||
if not account_selection_obj.check_root(
|
||||
[line.analytic_accounts.id]):
|
||||
self.raise_user_error(cursor, 'analytic_account_required',
|
||||
(line.rec_name, line.id), context=context)
|
||||
self.raise_user_error('analytic_account_required',
|
||||
(line.rec_name, line.id))
|
||||
return res
|
||||
|
||||
Purchase()
|
||||
@@ -48,28 +48,25 @@ class PurchaseLine(ModelSQL, ModelView):
|
||||
'invisible': Not(Equal(Eval('type'), 'line')),
|
||||
})
|
||||
|
||||
def _view_look_dom_arch(self, cursor, user, tree, type, context=None):
|
||||
def _view_look_dom_arch(self, tree, type):
|
||||
analytic_account_obj = self.pool.get('analytic_account.account')
|
||||
analytic_account_obj.convert_view(cursor, user, tree, context=context)
|
||||
arch, fields = super(PurchaseLine, self)._view_look_dom_arch(cursor,
|
||||
user, tree, type, context=context)
|
||||
analytic_account_obj.convert_view(tree)
|
||||
arch, fields = super(PurchaseLine, self)._view_look_dom_arch(tree, type)
|
||||
return arch, fields
|
||||
|
||||
def fields_get(self, cursor, user, fields_names=None, context=None):
|
||||
def fields_get(self, fields_names=None):
|
||||
analytic_account_obj = self.pool.get('analytic_account.account')
|
||||
|
||||
res = super(PurchaseLine, self).fields_get(cursor, user, fields_names,
|
||||
context=context)
|
||||
res = super(PurchaseLine, self).fields_get(fields_names)
|
||||
|
||||
analytic_accounts_field = super(PurchaseLine, self).fields_get(cursor,
|
||||
user, ['analytic_accounts'],
|
||||
context=context)['analytic_accounts']
|
||||
analytic_accounts_field = super(PurchaseLine, self).fields_get(
|
||||
['analytic_accounts'])['analytic_accounts']
|
||||
|
||||
res.update(analytic_account_obj.analytic_accounts_fields_get(cursor,
|
||||
user, analytic_accounts_field, fields_names, context=context))
|
||||
res.update(analytic_account_obj.analytic_accounts_fields_get(
|
||||
analytic_accounts_field, fields_names))
|
||||
return res
|
||||
|
||||
def read(self, cursor, user, ids, fields_names=None, context=None):
|
||||
def read(self, ids, fields_names=None):
|
||||
selection_obj = self.pool.get('analytic_account.account.selection')
|
||||
|
||||
int_id = False
|
||||
@@ -83,8 +80,7 @@ class PurchaseLine(ModelSQL, ModelView):
|
||||
else:
|
||||
fields_names2 = fields_names
|
||||
|
||||
res = super(PurchaseLine, self).read(cursor, user, ids,
|
||||
fields_names=fields_names2, context=context)
|
||||
res = super(PurchaseLine, self).read(ids, fields_names=fields_names2)
|
||||
|
||||
if not fields_names:
|
||||
fields_names = list(set(self._columns.keys() \
|
||||
@@ -98,7 +94,7 @@ class PurchaseLine(ModelSQL, ModelView):
|
||||
id2record = {}
|
||||
for record in res:
|
||||
id2record[record['id']] = record
|
||||
lines = self.browse(cursor, user, ids, context=context)
|
||||
lines = self.browse(ids)
|
||||
for line in lines:
|
||||
for root_id in root_ids:
|
||||
id2record[line.id]['analytic_account_' \
|
||||
@@ -120,7 +116,7 @@ class PurchaseLine(ModelSQL, ModelView):
|
||||
return res[0]
|
||||
return res
|
||||
|
||||
def create(self, cursor, user, vals, context=None):
|
||||
def create(self, vals):
|
||||
selection_obj = self.pool.get('analytic_account.account.selection')
|
||||
vals = vals.copy()
|
||||
selection_vals = {}
|
||||
@@ -131,16 +127,13 @@ class PurchaseLine(ModelSQL, ModelView):
|
||||
selection_vals['accounts'].append(('add', vals[field]))
|
||||
del vals[field]
|
||||
if vals.get('analytic_accounts'):
|
||||
selection_obj.write(cursor, user, vals['analytic_accounts'],
|
||||
selection_vals, context=context)
|
||||
selection_obj.write(vals['analytic_accounts'], selection_vals)
|
||||
elif vals.get('type', 'line') == 'line':
|
||||
selection_id = selection_obj.create(cursor, user, selection_vals,
|
||||
context=context)
|
||||
selection_id = selection_obj.create(selection_vals)
|
||||
vals['analytic_accounts'] = selection_id
|
||||
return super(PurchaseLine, self).create(cursor, user, vals,
|
||||
context=context)
|
||||
return super(PurchaseLine, self).create(vals)
|
||||
|
||||
def write(self, cursor, user, ids, vals, context=None):
|
||||
def write(self, ids, vals):
|
||||
selection_obj = self.pool.get('analytic_account.account.selection')
|
||||
vals = vals.copy()
|
||||
if isinstance(ids, (int, long)):
|
||||
@@ -152,19 +145,19 @@ class PurchaseLine(ModelSQL, ModelView):
|
||||
selection_vals[root_id] = vals[field]
|
||||
del vals[field]
|
||||
if selection_vals:
|
||||
lines = self.browse(cursor, user, ids, context=context)
|
||||
lines = self.browse(ids)
|
||||
for line in lines:
|
||||
if line.type != 'line':
|
||||
continue
|
||||
accounts = []
|
||||
if not line.analytic_accounts:
|
||||
# Create missing selection
|
||||
selection_id = selection_obj.create(cursor, 0, {},
|
||||
context=context)
|
||||
self.write(cursor, user, line.id, {
|
||||
with Transaction().set_user(0):
|
||||
selection_id = selection_obj.create({})
|
||||
self.write(line.id, {
|
||||
'analytic_accounts': selection_id,
|
||||
}, context=context)
|
||||
line = self.browse(cursor, user, line.id, context=context)
|
||||
})
|
||||
line = self.browse(line.id)
|
||||
for account in line.analytic_accounts.accounts:
|
||||
if account.root.id in selection_vals:
|
||||
value = selection_vals[account.root.id]
|
||||
@@ -176,63 +169,57 @@ class PurchaseLine(ModelSQL, ModelView):
|
||||
if account_id \
|
||||
and account_id not in accounts:
|
||||
accounts.append(account_id)
|
||||
selection_obj.write(cursor, user, line.analytic_accounts.id, {
|
||||
selection_obj.write(line.analytic_accounts.id, {
|
||||
'accounts': [('set', accounts)],
|
||||
}, context=context)
|
||||
return super(PurchaseLine, self).write(cursor, user, ids, vals,
|
||||
context=context)
|
||||
})
|
||||
return super(PurchaseLine, self).write(ids, vals)
|
||||
|
||||
def delete(self, cursor, user, ids, context=None):
|
||||
def delete(self, ids):
|
||||
selection_obj = self.pool.get('analytic_account.account.selection')
|
||||
|
||||
if isinstance(ids, (int, long)):
|
||||
ids = [ids]
|
||||
selection_ids = []
|
||||
lines = self.browse(cursor, user, ids, context=context)
|
||||
lines = self.browse(ids)
|
||||
for line in lines:
|
||||
if line.analytic_accounts:
|
||||
selection_ids.append(line.analytic_accounts.id)
|
||||
|
||||
res = super(PurchaseLine, self).delete(cursor, user, ids,
|
||||
context=context)
|
||||
selection_obj.delete(cursor, user, selection_ids, context=context)
|
||||
res = super(PurchaseLine, self).delete(ids)
|
||||
selection_obj.delete(selection_ids)
|
||||
return res
|
||||
|
||||
def copy(self, cursor, user, ids, default=None, context=None):
|
||||
def copy(self, ids, default=None):
|
||||
selection_obj = self.pool.get('analytic_account.account.selection')
|
||||
|
||||
new_ids = super(PurchaseLine, self).copy(cursor, user, ids,
|
||||
default=default, context=context)
|
||||
new_ids = super(PurchaseLine, self).copy(ids, default=default)
|
||||
|
||||
int_id = False
|
||||
if isinstance(new_ids, (int, long)):
|
||||
int_id = True
|
||||
new_ids = [new_ids]
|
||||
|
||||
for line in self.browse(cursor, user, new_ids, context=context):
|
||||
for line in self.browse(new_ids):
|
||||
if line.analytic_accounts:
|
||||
selection_id = selection_obj.copy(cursor, user,
|
||||
line.analytic_accounts.id, context=context)
|
||||
self.write(cursor, user, line.id, {
|
||||
selection_id = selection_obj.copy(line.analytic_accounts.id)
|
||||
self.write(line.id, {
|
||||
'analytic_accounts': selection_id,
|
||||
}, context=context)
|
||||
})
|
||||
|
||||
if int_id:
|
||||
return new_ids[0]
|
||||
return new_ids
|
||||
|
||||
def get_invoice_line(self, cursor, user, line, context=None):
|
||||
def get_invoice_line(self, line):
|
||||
account_selection_obj = self.pool.get('analytic_account.account.selection')
|
||||
|
||||
res = super(PurchaseLine, self).get_invoice_line(cursor, user, line,
|
||||
context=context)
|
||||
res = super(PurchaseLine, self).get_invoice_line(line)
|
||||
if not res:
|
||||
return res
|
||||
|
||||
selection_id = False
|
||||
if line.analytic_accounts:
|
||||
selection_id = account_selection_obj.copy(cursor, user,
|
||||
line.analytic_accounts.id, context=context)
|
||||
selection_id = account_selection_obj.copy(line.analytic_accounts.id)
|
||||
for vals in res:
|
||||
vals['analytic_accounts'] = selection_id
|
||||
return res
|
||||
@@ -243,26 +230,25 @@ PurchaseLine()
|
||||
class Account(ModelSQL, ModelView):
|
||||
_name = 'analytic_account.account'
|
||||
|
||||
def delete(self, cursor, user, ids, context=None):
|
||||
def delete(self, ids):
|
||||
purchase_line_obj = self.pool.get('purchase.line')
|
||||
res = super(Account, self).delete(cursor, user, ids, context=context)
|
||||
res = super(Account, self).delete(ids)
|
||||
# Restart the cache on the fields_view_get method of purchase.line
|
||||
purchase_line_obj.fields_view_get(cursor.dbname)
|
||||
purchase_line_obj.fields_view_get.reset()
|
||||
return res
|
||||
|
||||
def create(self, cursor, user, vals, context=None):
|
||||
def create(self, vals):
|
||||
purchase_line_obj = self.pool.get('purchase.line')
|
||||
res = super(Account, self).create(cursor, user, vals, context=context)
|
||||
res = super(Account, self).create(vals)
|
||||
# Restart the cache on the fields_view_get method of purchase.line
|
||||
purchase_line_obj.fields_view_get(cursor.dbname)
|
||||
purchase_line_obj.fields_view_get.reset()
|
||||
return res
|
||||
|
||||
def write(self, cursor, user, ids, vals, context=None):
|
||||
def write(self, ids, vals):
|
||||
purchase_line_obj = self.pool.get('purchase.line')
|
||||
res = super(Account, self).write(cursor, user, ids, vals,
|
||||
context=context)
|
||||
res = super(Account, self).write(ids, vals)
|
||||
# Restart the cache on the fields_view_get method of purchase.line
|
||||
purchase_line_obj.fields_view_get(cursor.dbname)
|
||||
purchase_line_obj.fields_view_get.reset()
|
||||
return res
|
||||
|
||||
Account()
|
||||
|
||||
Reference in New Issue
Block a user