|  | @@ -0,0 +1,236 @@
 | 
	
		
			
				|  |  | +# -*- 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 StringIO import StringIO
 | 
	
		
			
				|  |  | +from gzip import GzipFile
 | 
	
		
			
				|  |  | +from StringIO import StringIO as IO
 | 
	
		
			
				|  |  | +import simplejson as json
 | 
	
		
			
				|  |  | +import gzip
 | 
	
		
			
				|  |  | +import logging
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +LOGGER = logging.getLogger(__name__)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +class PaymentsSales(http.Controller):
 | 
	
		
			
				|  |  | +    '''
 | 
	
		
			
				|  |  | +        Get server date
 | 
	
		
			
				|  |  | +    '''
 | 
	
		
			
				|  |  | +    def get_server_date(self):
 | 
	
		
			
				|  |  | +        return datetime.now().strftime('%y-%m-%d')
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    '''
 | 
	
		
			
				|  |  | +        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
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    '''
 | 
	
		
			
				|  |  | +        Get partner customer
 | 
	
		
			
				|  |  | +    '''
 | 
	
		
			
				|  |  | +    def get_customers(self):
 | 
	
		
			
				|  |  | +        domain = [('customer', '=', True), ('active', '=', True), ('credit', '>', 0)]
 | 
	
		
			
				|  |  | +        partners = []
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        for customer in request.env['res.partner'].search(domain):
 | 
	
		
			
				|  |  | +            invoices = []
 | 
	
		
			
				|  |  | +            partner_invoice = []
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            for invoice_ids in customer.invoice_ids:
 | 
	
		
			
				|  |  | +                if invoice_ids.state == 'open':
 | 
	
		
			
				|  |  | +                    partner_invoice.append(invoice_ids.id)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            if customer.is_company == True:
 | 
	
		
			
				|  |  | +                for child in customer.child_ids:
 | 
	
		
			
				|  |  | +                    for invoice_ids in child.invoice_ids:
 | 
	
		
			
				|  |  | +                        if invoice_ids.state == 'open':
 | 
	
		
			
				|  |  | +                            partner_invoice.append(invoice_ids.id)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            # for invoice in request.env['account.invoice'].browse(partner_invoice):
 | 
	
		
			
				|  |  | +            for invoice in request.env['account.invoice'].search([('id', 'in', partner_invoice),('state', '=', 'open')]):
 | 
	
		
			
				|  |  | +                movelines = []
 | 
	
		
			
				|  |  | +                moves = []
 | 
	
		
			
				|  |  | +                currency_symbol = ""
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                for move in invoice.move_id:
 | 
	
		
			
				|  |  | +                    for moveline in move.line_id:
 | 
	
		
			
				|  |  | +                        if moveline.amount_residual > 0 and moveline.state != "draft" and moveline.credit <= 0:
 | 
	
		
			
				|  |  | +                            movelines.append({
 | 
	
		
			
				|  |  | +                                'id': moveline.id,
 | 
	
		
			
				|  |  | +                                'amountResidual': moveline.amount_residual,
 | 
	
		
			
				|  |  | +                                'credit': moveline.credit,
 | 
	
		
			
				|  |  | +                                'debit': moveline.debit,
 | 
	
		
			
				|  |  | +                                'dateMaturity': moveline.date_maturity,
 | 
	
		
			
				|  |  | +                                'invoice': invoice.id
 | 
	
		
			
				|  |  | +                            })
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                invoices.append({
 | 
	
		
			
				|  |  | +                    'id': invoice.id,
 | 
	
		
			
				|  |  | +                    'number': invoice.number,
 | 
	
		
			
				|  |  | +                    'dateInvoice': invoice.date_invoice,
 | 
	
		
			
				|  |  | +                    'amountTotal': invoice.amount_total,
 | 
	
		
			
				|  |  | +                    'residual': invoice.residual,
 | 
	
		
			
				|  |  | +                    'moveLines': movelines,
 | 
	
		
			
				|  |  | +                    'currency' : {
 | 
	
		
			
				|  |  | +                        'id': invoice.currency_id.id,
 | 
	
		
			
				|  |  | +                        'name': invoice.currency_id.name,
 | 
	
		
			
				|  |  | +                        'displayName': invoice.currency_id.display_name,
 | 
	
		
			
				|  |  | +                        'symbol': invoice.currency_id.symbol,
 | 
	
		
			
				|  |  | +                        'rateSilent': invoice.currency_id.rate_silent,
 | 
	
		
			
				|  |  | +                        'thousandsSeparator': invoice.currency_id.thousands_separator,
 | 
	
		
			
				|  |  | +                        'decimalSeparator': invoice.currency_id.decimal_separator,
 | 
	
		
			
				|  |  | +                        'decimalPlaces': invoice.currency_id.decimal_places
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                })
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            partners.append({
 | 
	
		
			
				|  |  | +                'id': customer.id,
 | 
	
		
			
				|  |  | +                'name': customer.name,
 | 
	
		
			
				|  |  | +                'displayName': customer.display_name,
 | 
	
		
			
				|  |  | +                'ruc': customer.ruc,
 | 
	
		
			
				|  |  | +                'imageMedium': customer.image_medium,
 | 
	
		
			
				|  |  | +                'phone': customer.phone,
 | 
	
		
			
				|  |  | +                'mobile': customer.mobile,
 | 
	
		
			
				|  |  | +                'email': customer.email,
 | 
	
		
			
				|  |  | +                'credit': customer.credit,
 | 
	
		
			
				|  |  | +                'creditLimit': customer.credit_limit,
 | 
	
		
			
				|  |  | +                'invoices': invoices
 | 
	
		
			
				|  |  | +            })
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return partners
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    '''
 | 
	
		
			
				|  |  | +        Get Journal
 | 
	
		
			
				|  |  | +    '''
 | 
	
		
			
				|  |  | +    def get_journals(self):
 | 
	
		
			
				|  |  | +        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
 | 
	
		
			
				|  |  | +                },
 | 
	
		
			
				|  |  | +                '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=9, 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/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(),
 | 
	
		
			
				|  |  | +            'customers': self.get_customers(),
 | 
	
		
			
				|  |  | +            'journals': self.get_journals(),
 | 
	
		
			
				|  |  | +            'currencies': self.get_currency()
 | 
	
		
			
				|  |  | +        })
 |