123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- # -*- 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 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'
- GZIP_COMPRESSION_LEVEL = 9
- class PaymentsSales(http.Controller):
- '''
- Get server date
- '''
- def get_server_date(self):
- return datetime.now().strftime(DATE_FORMAT)
- '''
- Get partner customer
- '''
- def get_user(self):
- user = request.env.user
- return {
- 'id': user.id,
- 'name': user.name,
- 'displayName': user.display_name,
- 'company': {
- 'id': user.company_id.id,
- 'name': user.company_id.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,
- 'rateSilent': user.company_id.currency_id.rate_silent,
- 'thousandsSeparator': user.company_id.currency_id.thousands_separator,
- 'decimalSeparator': user.company_id.currency_id.decimal_separator,
- 'decimalPlaces': user.company_id.currency_id.decimal_places,
- 'position': user.company_id.currency_id.position
- }
- }
- '''
- Get Supplier
- '''
- def get_supplier(self):
- decimal_precision = request.env['decimal.precision'].precision_get('Account')
- return [{
- 'id': supplier.id,
- 'name': supplier.name,
- 'displayName': supplier.display_name,
- 'ruc': supplier.ruc,
- 'imageMedium': supplier.image_medium,
- 'phone': supplier.phone,
- 'mobile': supplier.mobile,
- 'email':supplier.email,
- 'debit': supplier.debit,
- 'invoices': [{
- 'id': invoice.id,
- 'number': invoice.number ,
- 'dateInvoice': invoice.date_invoice,
- 'amountTotal': invoice.amount_total,
- 'residual': invoice.residual,
- 'currencyInvoice': {
- 'id': invoice.currency_id.id,
- 'displayName': invoice.currency_id.display_name,
- 'symbol': invoice.currency_id.symbol,
- 'rateSilent': invoice.currency_id.rate_silent,
- 'rate': invoice.currency_id.rate,
- 'thousandsSeparator': invoice.currency_id.thousands_separator,
- 'decimalSeparator': invoice.currency_id.decimal_separator,
- 'decimalPlaces': invoice.currency_id.decimal_places,
- 'position': invoice.currency_id.position
- },
- 'moveLines' :[{
- 'id': line.id,
- 'invoiceNumber': invoice.number,
- 'amountResidual': line.amount_residual,
- 'credit': line.credit,
- 'debit': line.debit,
- 'dateMaturity': line.date_maturity,
- 'amountCurrency': (line.amount_currency * -1) if (line.amount_currency != 0) else line.credit,
- 'amountResidualCurrency': line.amount_residual_currency,
- 'currencyAmount': [{
- 'id': currency.id,
- 'displayName': currency.display_name,
- 'symbol': currency.symbol,
- 'rateSilent': currency.rate_silent,
- 'rate': currency.rate,
- 'thousandsSeparator': currency.thousands_separator,
- 'decimalSeparator': currency.decimal_separator,
- 'decimalPlaces': currency.decimal_places,
- 'position': currency.position,
- 'base': currency.base,
- 'accuracy': currency.accuracy,
- 'rounding': currency.rounding,
- 'amountCurencyResidual': round((line.amount_residual_currency * (currency.rate / invoice.currency_id.rate)), decimal_precision)
- } for currency in request.env['res.currency'].search([('active', '=', True)])]
- } for line in invoice.move_id.line_id if (line.amount_residual > 0 and line.state != "draft" and line.debit <= 0)],
- } for invoice in supplier.invoice_ids if (invoice.state == 'open') ],
- } for supplier in request.env['res.partner'].search([('active', '=', True), ('supplier', '=', True), ('debit', '<', 0) ])]
- '''
- Get Journal
- '''
- def get_journals(self):
- # domain =[('active', '=', True),('type', 'in',['bank', 'cash']), ('default_credit_account_id.currency_id', '=', False)]
- domain =[('active', '=', True),('type', 'in',['bank', 'cash'])]
- paymentsJournals = []
- for journal in request.env['account.journal'].search(domain, order="id"):
- if not (journal.store_ids >= request.env.user.store_ids):
- continue
- paymentsJournals.append({
- '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,
- 'symbol': journal.currency.symbol,
- 'rateSilent': journal.currency.rate_silent,
- 'thousandsSeparator':journal.currency.thousands_separator,
- 'decimalSeparator': journal.currency.decimal_separator,
- 'decimalPlaces': journal.currency.decimal_places,
- 'position': journal.currency.position
- },
- 'defaultCreditAccount':{
- 'id': journal.default_credit_account_id.id,
- 'name': journal.default_credit_account_id.name,
- 'displayName': journal.default_credit_account_id.display_name,
- 'code': journal.default_credit_account_id.code,
- 'exchangeRate': 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,
- 'symbol': journal.default_credit_account_id.company_currency_id.symbol,
- 'rateSilent': journal.default_credit_account_id.company_currency_id.rate_silent
- },
- '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,
- 'symbol': journal.default_credit_account_id.currency_id.symbol,
- 'rateSilent': journal.default_credit_account_id.currency_id.rate_silent
- },
- }
- })
- return paymentsJournals
- '''
- Get Currency
- '''
- def get_currency(self):
- return [{
- 'id': currency.id,
- 'name': currency.name,
- 'displayName': currency.display_name,
- 'base': currency.base,
- 'accuracy': currency.accuracy,
- 'rateSilent': currency.rate_silent,
- 'rounding': currency.rounding,
- 'symbol': currency.symbol,
- 'position': currency.position,
- 'thousandsSeparator': currency.thousands_separator,
- 'decimalSeparator': currency.decimal_separator,
- 'decimalPlaces': currency.decimal_places
- } for currency in request.env['res.currency'].search([('active', '=', True)])]
- '''
- Make JSON response
- '''
- def make_json_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(log)
- '''
- New Payments resource router
- '''
- @http.route('/eiru_payments_purchases/init', auth='user', methods=['GET'], cors='*')
- def init_payments(self, **kw):
- self.make_info_log('Sending JSON response')
- return self.make_gzip_response({
- 'date': self.get_server_date(),
- 'user': self.get_user(),
- 'supplier': self.get_supplier(),
- 'journals': self.get_journals(),
- 'currencies': self.get_currency()
- })
|