Initial commit of analytic purchase module

This commit is contained in:
Cédric Krier committed 2008-05-26 13:59:20 +02:00
commit 6a20262b53
5 files changed
+288

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
# Copyright (c) 2008 Cedric Krier.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contact a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
from purchase import *
+17
View File
@@ -0,0 +1,17 @@
{
'name': 'Analytic Purchase',
'version': '0.1',
'author': 'B2CK',
'email': 'info@b2ck.com',
'website': 'http://www.tryton.org/',
'category': 'Purchase',
'description': 'Analytic Purchase',
'depends': [
'purchase',
'analytic_account',
'analytic_invoice',
],
'xml': [
'purchase.xml',
],
}
+170
View File
@@ -0,0 +1,170 @@
"Purchase"
from trytond.osv import fields, OSV
class PurchaseLine(OSV):
_name = 'purchase.line'
analytic_accounts = fields.Many2One('analytic_account.account.selection',
'Analytic Accounts', required=True,
states={
'invisible': "type != 'line'",
})
def _view_look_dom_arch(self, cursor, user, node, context=None):
analytic_account_obj = self.pool.get('analytic_account.account')
analytic_account_obj.convert_view(cursor, user, node, context=context)
arch, fields = super(PurchaseLine, self)._view_look_dom_arch(cursor,
user, node, context=context)
return arch, fields
def fields_get(self, cursor, user, fields_names=None, context=None):
analytic_account_obj = self.pool.get('analytic_account.account')
res = super(PurchaseLine, self).fields_get(cursor, user, fields_names,
context=context)
analytic_accounts_field = super(PurchaseLine, self).fields_get(cursor,
user, ['analytic_accounts'],
context=context)['analytic_accounts']
res.update(analytic_account_obj.analytic_accounts_fields_get(cursor,
user, analytic_accounts_field, fields_names, context=context))
return res
def _read_flat(self, cursor, user, ids, fields_names, context=None,
load='_classic_read'):
selection_obj = self.pool.get('analytic_account.account.selection')
res = super(PurchaseLine, self)._read_flat(cursor, user, ids,
fields_names, context=context, load=load)
root_ids = []
for field in fields_names:
if field.startswith('analytic_account_'):
root_ids.append(int(field[len('analytic_account_'):]))
if root_ids:
id2record = {}
for record in res:
id2record[record['id']] = record
lines = self.browse(cursor, user, ids, context=context)
for line in lines:
for account in line.analytic_accounts.accounts:
if account.root.id in root_ids:
id2record[line.id]['analytic_account_' \
+ str(account.root.id)] = account.id
return res
def create(self, cursor, user, vals, context=None):
selection_obj = self.pool.get('analytic_account.account.selection')
vals = vals.copy()
selection_vals = {}
for field in vals.keys():
if field.startswith('analytic_account_'):
if vals[field]:
selection_vals.setdefault('accounts', [])
selection_vals['accounts'].append(('add', vals[field]))
del vals[field]
if 'analytic_accounts' in vals:
selection_obj.write(cursor, user, vals['analytic_accounts'],
selection_vals, context=context)
else:
selection_id = selection_obj.create(cursor, user, selection_vals,
context=context)
vals['analytic_accounts'] = selection_id
return super(PurchaseLine, self).create(cursor, user, vals,
context=context)
def write(self, cursor, user, ids, vals, context=None):
selection_obj = self.pool.get('analytic_account.account.selection')
vals = vals.copy()
selection_vals = {}
for field in vals.keys():
if field.startswith('analytic_account_'):
root_id = int(field[len('analytic_account_'):])
selection_vals[root_id] = vals[field]
del vals[field]
if selection_vals:
lines = self.browse(cursor, user, ids, context=context)
for line in lines:
accounts = []
for account in line.analytic_accounts.accounts:
if account.root.id in selection_vals:
value = selection_vals[account.root.id]
if value:
accounts.append(value)
else:
accounts.append(account.id)
for account_id in selection_vals.values():
if account_id \
and account_id not in accounts:
accounts.append(account_id)
selection_obj.write(cursor, user, line.analytic_accounts.id, {
'accounts': [('set', accounts)],
}, context=context)
return super(PurchaseLine, self).write(cursor, user, ids, vals,
context=context)
def unlink(self, cursor, user, ids, context=None):
selection_obj = self.pool.get('analytic_account.account.selection')
selection_ids = []
lines = self.browse(cursor, user, ids, context=context)
for line in lines:
selection_ids.append(line.analytic_accounts.id)
res = super(PurchaseLine, self).unlink(cursor, user, ids,
context=context)
selection_obj.unlink(cursor, user, selection_ids, context=context)
return res
def copy(self, cursor, user, line_id, default=None, context=None):
selection_obj = self.pool.get('analytic_account.account.selection')
if default is None:
default = {}
default = default.copy()
line = self.browse(cursor, user, line_id, context=context)
default['analytic_accounts'] = selection_obj.copy(cursor, user,
line.analytic_accounts.id, context=context)
return super(PurchaseLine, self).copy(cursor, user, line_id,
default=default, context=context)
def get_invoice_line(self, cursor, user, line, context=None):
res = super(PurchaseLine, self).get_invoice_line(cursor, user, line,
context=context)
res['analytic_accounts'] = line.analytic_accounts.id
return res
PurchaseLine()
class Account(OSV):
_name = 'analytic_account.account'
def unlink(self, cursor, user, ids, context=None):
invoice_line_obj = self.pool.get('purchase.line')
try:
invoice_line_obj.fields_view_get(cursor.dbname)
except:
pass
return super(Account, self).unlink(cursor, user, ids, context=context)
def create(self, cursor, user, vals, context=None):
invoice_line_obj = self.pool.get('purchase.line')
try:
invoice_line_obj.fields_view_get(cursor.dbname)
except:
pass
return super(Account, self).create(cursor, user, vals, context=context)
def write(self, cursor, user, ids, vals, context=None):
invoice_line_obj = self.pool.get('purchase.line')
try:
invoice_line_obj.fields_view_get(cursor.dbname)
except:
pass
return super(Account, self).write(cursor, user, ids, vals,
context=context)
Account()
+22
View File
@@ -0,0 +1,22 @@
<?xml version="1.0"?>
<tryton>
<data>
<record model="ir.ui.view" id="purchase_line_view_form">
<field name="model">purchase.line</field>
<field name="inherit" ref="purchase.purchase_line_view_form"/>
<field name="arch" type="xml">
<![CDATA[
<data>
<xpath
expr="/form/separator[@name=&quot;comment&quot;]"
position="before">
<group col="4" colspan="4" string="Analytic Accounts">
<field name="analytic_accounts"/>
</group>
</xpath>
</data>
]]>
</field>
</record>
</data>
</tryton>
+53
View File
@@ -0,0 +1,53 @@
#!/usr/bin/env python
from distutils.core import setup
from distutils.command import sdist
class mysdist(sdist.sdist):
def add_defaults(self):
sdist.sdist.add_defaults(self)
if self.distribution.has_pure_modules():
build_py = self.get_finalized_command('build_py')
data = []
for package in build_py.packages:
src_dir = build_py.get_package_dir(package)
data.extend(build_py.find_data_files(package, src_dir))
self.filelist.extend(data)
info = eval(file('__tryton__.py').read())
requires = ['trytond'] + ['trytond-' + x for x in info.get('depends', [])]
setup(name='trytond-' + info['name'].lower(),
version=info.get('version', '0'),
description=info.get('description', ''),
author=info.get('author', ''),
author_email=info.get('email', ''),
url=info.get('website', ''),
package_dir={'trytond.modules.' + info['name'].lower(): '.'},
packages=[
'trytond.modules.' + info['name'].lower(),
],
package_data={
'trytond.modules.' + info['name'].lower(): info.get('xml', []),
},
classifiers=[
'Development Status :: 5 - Production/Stable',
'Environment :: Plugins',
'Intended Audience :: Developers',
'Intended Audience :: Financial and Insurance Industry',
'Intended Audience :: Legal Industry',
'Natural Language :: English',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Topic :: Office/Business',
'Topic :: Office/Business :: Financial :: Accounting',
],
license=info.get('license', 'GPL-2'),
#requires=requires,
cmdclass={
'sdist': mysdist,
},
)