|
@@ -0,0 +1,260 @@
|
|
|
+# -*- 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 Purchases(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)]
|
|
|
+
|
|
|
+ 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,
|
|
|
+ '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,
|
|
|
+ 'balanceEnd': statement.balance_end,
|
|
|
+ 'user': {
|
|
|
+ 'id': statement.user_id.id,
|
|
|
+ 'name': statement.user_id.name,
|
|
|
+ 'displayName': statement.user_id.display_name
|
|
|
+ },
|
|
|
+ '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
|
|
|
+ }
|
|
|
+ } 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))
|
|
|
+
|
|
|
+ ''' Bank statement Init '''
|
|
|
+ @http.route('/eiru_bank_statement/init', auth='user', methods=['GET'], cors='*')
|
|
|
+ def init_bank_statement(self,**kw):
|
|
|
+
|
|
|
+ self.make_info_log('Preparing data to {}'.format(kw.get('mode')))
|
|
|
+ journals = self.get_journals([kw.get('mode')]),
|
|
|
+
|
|
|
+ return self.make_gzip_response({
|
|
|
+ 'date': self.get_server_date(),
|
|
|
+ 'user': self.get_user(),
|
|
|
+ 'currencies': self.get_currencies_from_journal(),
|
|
|
+ 'journals': journals,
|
|
|
+ 'statement': self.get_account_bank_statement(map(lambda x: x['id'], journals[0])),
|
|
|
+ 'statementConfig' : self.get_account_bank_statement_config(),
|
|
|
+ # 'suppliers': self.get_suppliers(),
|
|
|
+ # 'products': self.get_products(kw.get('mode')),
|
|
|
+ # 'pickingTypes': self.get_picking_types(),
|
|
|
+ # 'paymentTerms': self.get_payment_terms(),
|
|
|
+ # 'banks': self.get_banks(),
|
|
|
+ # 'bankPaymentTypes': self.get_bank_payment_types(),
|
|
|
+ # 'chequeTypes': self.get_cheque_types()
|
|
|
+ })
|