# -*- coding: utf-8 -*- from openerp import http from openerp.http import request from werkzeug.wrappers import Response from werkzeug.datastructures import Headers from datetime import datetime from dateutil.relativedelta import relativedelta as rd from dateutil.parser import parse from pytz import timezone from gzip import GzipFile from StringIO import StringIO as IO import simplejson as json import gzip import logging LOGGER = logging.getLogger(__name__) DATE_FORMAT = '%Y-%m-%d' DATETIME_FORMAT = '%Y-%m-%d %H:%m:%S' GZIP_COMPRESSION_LEVEL = 9 class Statement(http.Controller): ''' Get timezone ''' def get_timezone(self): return timezone(request.context['tz']) ''' Get server date to send ''' def get_server_date(self): return datetime.now(self.get_timezone()).strftime(DATE_FORMAT) ''' Get current user information ''' def get_user(self): user = request.env.user return { 'id': user.id, 'name': user.name, 'displayName': user.display_name, 'currency': { 'id': user.company_id.currency_id.id, 'name': user.company_id.currency_id.name, 'displayName': user.company_id.currency_id.display_name, 'symbol': user.company_id.currency_id.symbol }, 'company': { 'id': user.company_id.id, 'name': user.company_id.name, 'displayName': user.company_id.display_name } } ''' Get currencies from journals ''' def get_currencies_from_journal(self): domain = [ ('type', 'in', ['bank', 'cash']), # ('default_debit_account_id.currency_id', '=', False), ('active', '=', True) ] currencies = [] for j in request.env['account.journal'].search(domain): c = j.currency or j.company_id.currency_id currencies.append({ 'id': c.id, 'name': c.display_name, 'base': c.base, 'symbol': c.symbol, 'position': c.position, 'rateSilent': c.rate_silent, 'decimalSeparator': c.decimal_separator, 'decimalPlaces': c.decimal_places, 'thousandsSeparator': c.thousands_separator }) return {c['id']:c for c in currencies}.values() ''' all active journals ''' def get_journals(self, type=None): if (not type[0]): type= ['bank', 'cash'] user = request.env.user journal = [] for store in user.store_ids: for journalID in store.journal_ids: if (journalID.type in ['bank', 'cash']): journal.append(journalID.id) # domain = [('type', 'in', type), ('default_debit_account_id.currency_id', '=', False), ('active', '=', True)] domain = [('type', 'in', type),('active', '=', True)] if (journal): domain.append(('id', 'in', journal )) return [{ 'id': journal.id, 'name': journal.name, 'displayName': journal.display_name, 'code': journal.code, 'cashControl': journal.cash_control, 'type': journal.type, 'storeIds': map(lambda x: x.id, journal.store_ids), 'currency': { 'id': journal.currency.id, 'name': journal.currency.name, 'displayName': journal.currency.display_name }, 'defaultDebitAccount': { 'id': journal.default_debit_account_id.id, 'name': journal.default_debit_account_id.name, 'displayName': journal.default_debit_account_id.display_name, 'code': journal.default_debit_account_id.code, 'exchange_rate': journal.default_credit_account_id.exchange_rate, 'foreignBalance': journal.default_credit_account_id.foreign_balance, 'reconcile': journal.default_credit_account_id.reconcile, 'debit': journal.default_credit_account_id.debit, 'credit': journal.default_credit_account_id.credit, 'currencyMode': journal.default_credit_account_id.currency_mode, 'companyCurrency': { 'id': journal.default_credit_account_id.company_currency_id.id, 'name': journal.default_credit_account_id.company_currency_id.name, 'displayName': journal.default_credit_account_id.company_currency_id.display_name, }, 'currency': { 'id': journal.default_credit_account_id.currency_id.id, 'name': journal.default_credit_account_id.currency_id.name, 'displayName': journal.default_credit_account_id.currency_id.display_name } } } for journal in request.env['account.journal'].search(domain, order='id')] ''' account.bank.statement ''' def get_account_bank_statement(self, journalIds=None): if (not journalIds): return False domain = [('journal_id.id', 'in', journalIds)] # domain = [('journal_id.id', 'in', journalIds), ('user_id', '=', request.env.user.id)] return [{ 'id': statement.id, 'name': statement.name, 'date': statement.date, 'state': statement.state, 'balanceEnd': statement.balance_end, 'user': { 'id': statement.user_id.id, 'name': statement.user_id.name, 'displayName': statement.user_id.display_name }, 'userSession': request.env.user.id, 'journal': { 'id': statement.journal_id.id, 'name': statement.journal_id.name, 'displayName': statement.journal_id.display_name, 'code': statement.journal_id.code, 'cashControl': statement.journal_id.cash_control, 'type': statement.journal_id.type, 'currency': { 'id': statement.journal_id.currency.id, 'name': statement.journal_id.currency.name, 'displayName': statement.journal_id.currency.display_name } }, 'line': [{ 'id': line.id, 'date': line.date, 'name': line.name, 'ref': line.ref, 'amount': line.amount, 'patner':{ 'id': line.partner_id.id, 'name': line.partner_id.name, 'displayName': line.partner_id.display_name }, } for line in statement.line_ids], 'typeStatement': { 'id': statement.type_statement.id, 'name': statement.type_statement.name, 'code': statement.type_statement.code }, 'currency':{ 'id': statement.currency.id, 'name': statement.currency.display_name, 'base': statement.currency.base, 'symbol': statement.currency.symbol, 'position': statement.currency.position, 'rateSilent': statement.currency.rate_silent, 'decimalSeparator': statement.currency.decimal_separator, 'decimalPlaces': statement.currency.decimal_places, 'thousandsSeparator': statement.currency.thousands_separator } } for statement in request.env['account.bank.statement'].search(domain)] ''' Configuracion de Caja ''' def get_account_bank_statement_config(self): 'account.bank.statement.config' return [{ 'transfer': { 'userIds': map(lambda x: x.id, config.transfer_user_ids), 'statementIds': map(lambda x: x.id, config.transfer_statement_ids) , 'negativeAmount': config.transfer_negative_amount }, 'inputCashBox': { 'userIds': map(lambda x: x.id, config.input_cash_box_user_id), 'statementIds': map(lambda x: x.id, config.input_cash_box_statement_ids) }, 'outputCashBox': { 'userIds': map(lambda x: x.id, config.output_cash_box_user_id), 'statementIds': map(lambda x: x.id, config.output_cash_box_statement_ids), 'negativeAmount': config.output_negative_amount }, 'delete': { 'outputUserIds': map(lambda x: x.id, config.delete_output_user_ids), 'inputUserIds': map(lambda x: x.id, config.delete_input_user_ids), 'transferUserIds': map(lambda x: x.id, config.delete_transfer_user_ids) }, 'statementOpen': config.statement_open_config, 'statementConfirm' :{ 'userIds': map(lambda x: x.id, config.statement_confirm_user), 'transferUserIds': map(lambda x: x.id , config.statement_confirm_transfer_user), 'balanceUserIds': map(lambda x: x.id, config.statement_confirm_balance), 'negativeAmountUserIds': map(lambda x: x.id, config.statement_confirm_negative_amount) }, 'statementCancelUserIds': map(lambda x: x.id, config.statement_cancel_user), 'statementUnlinkUserIds': map(lambda x: x.id, config.statement_unlink_user) } for config in request.env['account.bank.statement.config'].search([('active', '=', True)],order='id')] ''' Make JSON response to send ''' def make_response(self, data=None, status=200): return Response(json.dumps(data), status=status, content_type='application/json') ''' Make GZIP to JSON response ''' def make_gzip_response(self, data=None, status=200): gzip_buffer = IO() with GzipFile(mode='wb', compresslevel=GZIP_COMPRESSION_LEVEL, fileobj=gzip_buffer) as gzip_file: gzip_file.write(json.dumps(data)) contents = gzip_buffer.getvalue() gzip_buffer.close() headers = Headers() headers.add('Content-Encoding', 'gzip') headers.add('Vary', 'Accept-Encoding') headers.add('Content-Length', len(contents)) return Response(contents, status=status, headers=headers, content_type='application/json') ''' Logger info ''' def make_info_log(self, log): LOGGER.info('\033[1;34m[INFO] --> \033[m{}'.format(log)) ''' ██ ███ ██ ██ ████████ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ''' @http.route('/eiru_bank_statement/init', auth='user', methods=['GET'], cors='*') def init_bank_statement(self,**kw): from account_bank_statement_type import get_account_bank_statement_type from res_store import get_res_store from res_users import get_res_users self.make_info_log('Preparing data to {}'.format(kw.get('mode'))) store = get_res_store(kw.get('mode')) return self.make_gzip_response({ 'date': self.get_server_date(), 'user': self.get_user(), 'currencies': self.get_currencies_from_journal(), 'journals': self.get_journals([kw.get('mode')]), # 'statement': self.get_account_bank_statement(map(lambda x: x['id'], journals[0])), 'statement': self.get_account_bank_statement(store['journalIds']), 'statementConfig' : self.get_account_bank_statement_config(), 'statementType': get_account_bank_statement_type(), 'resUsers': get_res_users(store['userIds']) }) ''' ██████ █████ ███████ ██ ██ ██████ ██████ ██ ██ ███ ███ ██████ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ████ ██ ██ ██ ██ ██ ██ ███████ ███████ ███████ ██████ ██ ██ ███ ██ ████ ██ ██ ██ ██ ██ █████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██████ ██ ██ ███████ ██ ██ ██████ ██████ ██ ██ ██ ██ ██████ ████ ███████ * Transfer. * Input. * Output. ''' @http.route('/eiru_bank_statement/create_cashbox_move', type='json', auth='user', methods=['POST'],cors='*') def _create_cashbox_move(self, **kw): from account_bank_statement_line import create_statement_line from cashbox_transfer import create_cashbox_transfer from cashbox_input import create_cashbox_input from cashbox_output import create_cashbox_output data = kw.get('data') ''' Transferencia ''' if (data['transfer']): return create_cashbox_transfer(data) ''' Entrada de Dinero ''' if (data['input']): return create_cashbox_input(data) ''' Salida de Dinero ''' if (data['output']): return create_cashbox_output(data) ''' ██████ ██████ ███████ █████ ████████ ███████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██████ █████ ███████ ██ █████ ██ ██ ██ ██ ██ ██ ██ ██ ██████ ██ ██ ███████ ██ ██ ██ ███████ Statement ''' @http.route('/eiru_bank_statement/create_account_bank_statement', type='json', auth='user', methods=['POST'],cors='*') def _create_account_bank_statement(self, **kw): from account_bank_statement import create_account_bank_statement return create_account_bank_statement(kw.get('data')) ''' ██████ ██ ██████ ███████ ██ ███ ██ ██████ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██████ ███████ ██████ ███████ ██ ██ ████ ██████ STATEMENT ''' @http.route('/eiru_bank_statement/process_closing_statement', type='json', auth='user', methods=['POST'],cors='*') def _process_closing_statement(self, **kw): from account_bank_statement import closing_statement return closing_statement(kw.get('data'))