In below example, I have written Python script for account cashflow operations. see below python code in .py file :
import time
from datetime import datetime, date, timedelta
import tools
from osv import osv, fields
import decimal_precision as dp
from lxml import etree
import logging
_logger = logging.getLogger(__name__)
from tools.translate import _
class account_cashflow_code(osv.osv):
_name = 'account.cashflow.code'
_description = 'Cash Flow Code'
_order = 'sequence,type desc,code'
def format_date(self, cr, uid, date, context):
''' format date according to language from context '''
if not context:
return date
lang = context.get('lang')
lang_obj = self.pool.get('res.lang')
lang_id = lang_obj.search(cr, uid, [('code','=',lang)])[0]
date_format = str(lang_obj.browse(cr, uid, lang_id).date_format)
return date.strftime(date_format)
def _balance_period(self, cr, uid, ids, field_name=None, arg=None, context=None, date_start=None, date_stop=None, day=None):
if context is None:
context = {}
if not date_start:
date_start = context.get('date_start', None)
if not date_stop:
date_stop = context.get('date_stop', None)
company_id = self.pool.get('res.users').browse(cr, uid, uid, context).company_id.id
journal_ids = context.get('journal_ids')
if day:
date_start = day
cr.execute('SELECT cashflow_code_id, sum(balance) FROM account_cashflow_balance ' \
'WHERE date = %s AND journal_id IN %s GROUP BY cashflow_code_id',
(day, tuple(journal_ids)))
elif date_start:
cr.execute('SELECT cashflow_code_id, sum(balance) FROM account_cashflow_balance ' \
'WHERE date >= %s AND date <= %s AND journal_id IN %s GROUP BY cashflow_code_id',
(date_start, date_stop, tuple(journal_ids)))
else:
cr.execute('SELECT id, 0.0 as amount FROM account_cashflow_code ')
balances = dict(cr.fetchall())
# add 'init' balance
balance_init_id = context.get('balance_init_id', None)
if date_start:
balopen_obj = self.pool.get('account.cashflow.opening.balance')
balance_init = {balance_init_id: balopen_obj.calc_opening_balance(cr, uid, date_start, balance_init_id, journal_ids)}
else:
balance_init = {balance_init_id: 0.0}
balances.update(balance_init)
# calculate period balances
dp = self.pool.get('decimal.precision').precision_get(cr, uid, 'Account')
period_balances = {}
def _rec_get(record):
amount = balances.get(record.id, 0.0)
for rec in record.child_ids:
amount += _rec_get(rec) * rec.parent_sign
return amount
for record in self.browse(cr, uid, ids, context=context):
period_balances[record.id] = round(_rec_get(record), dp)
return period_balances
def _balance_day(self, cr, uid, ids, field_name, arg, context):
if context is None:
context = {}
day = None
if context.get('date_start', None):
date_start = context.get('date_start')
nbr_days = int(context.get('nbr_days'))
x = int(field_name[-2:])
if x == 1:
day = date_start
elif x <= nbr_days:
day = (datetime.strptime(date_start, '%Y-%m-%d').date() + timedelta(days = int(field_name[-2:])-1)).isoformat()
return self._balance_period(cr, uid, ids, field_name, arg, context, day=day)
0 Comment(s)