123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542 |
- # -*- 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,
- '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
- },
- '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'])]
- 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)])]
- '''
- Get bank
- '''
- def get_bank(self):
- resBank = []
- if self.check_module('eiru_bank_payments_references'):
- resBank = [{
- 'id': bank.id,
- 'name': bank.name
- } for bank in request.env['res.bank'].search([('active', '=', True)])]
- return resBank
- '''
- Get Bank Payments Type
- '''
- def get_bank_payments_type(self):
- resBankType = []
- if self.check_module('eiru_bank_payments_references'):
- resBankType = [{
- 'id': bank_type.id,
- 'name': bank_type.name,
- 'code': bank_type.code,
- 'default_state': bank_type.default_state
- } for bank_type in request.env['res.bank.payments.type'].search([('is_receipt', '=', True)])]
- return resBankType
- '''
- GET BANK PAYMNETS
- '''
- def get_bank_Payments(self):
- bankPaymnets = []
- if self.check_module('eiru_bank_payments_references'):
- bankPaymnets = [{
- 'id': bankPayments.id,
- 'number': bankPayments.number,
- 'dateMaturity': bankPayments.date_maturity,
- 'state': bankPayments.state,
- 'numberCta': bankPayments.number_cta,
- 'nameHolder': bankPayments.name_holder,
- 'amountTotal': bankPayments.amount_total,
- 'amountReceipt': bankPayments.amount_receipt,
- 'amountPayment': bankPayments.amount_payment,
- 'bankId': {
- 'id': bankPayments.bank_id.id,
- 'name': bankPayments.bank_id.name
- },
- 'bankPaymentsType':{
- 'id': bankPayments.bank_payments_type_id.id,
- 'name': bankPayments.bank_payments_type_id.name,
- 'code': bankPayments.bank_payments_type_id.code,
- 'default_state': bankPayments.bank_payments_type_id.default_state
- },
- 'paymentsLine':[{
- 'amount': line.amount,
- 'date': line.date,
- 'typeOperation': line.type_operation
- } for line in request.env['res.bank.payments.line'].search([('bank_payments_id', 'in',[bankPayments.id]),('type_operation','=', 'receipt')])]
- } for bankPayments in request.env['res.bank.payments'].search([('bank_payments_type_id.code','=','CH')])]
- return bankPaymnets
- '''
- Check if module is installed
- '''
- def check_module(self, module_name):
- module = request.env['ir.module.module'].search([('name', '=', module_name), ('state', '=', 'installed')])
- return len(module) != 0
- '''
- 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(),
- 'bank':self.get_bank(),
- 'bankType': self.get_bank_payments_type(),
- 'bankPayments': self.get_bank_Payments()
- })
- '''
- ____ _ __ ____ __ _____ _ _ _____ ____ ____ ____ ___ ____ _____ ____ ____
- | _ \ / \\ \ / / \/ | ____| \ | |_ _/ ___| | _ \| _ \ / _ \ / ___| ____/ ___/ ___|
- | |_) / _ \\ V /| |\/| | _| | \| | | | \___ \ | |_) | |_) | | | | | | _| \___ \___ \
- | __/ ___ \| | | | | | |___| |\ | | | ___) | | __/| _ <| |_| | |___| |___ ___) |__) |
- |_| /_/ \_\_| |_| |_|_____|_| \_| |_| |____/ |_| |_| \_\\___/ \____|_____|____/____/
- '''
- '''
- Get the current period
- '''
- def get_period(self, date_server):
- return request.env['account.period'].search([('date_start','<=', date_server), ('date_stop', '>=', datetime.now().strftime(DATE_FORMAT))])
- '''
- Get Invoice
- '''
- def get_invoice(self, invoice_id):
- return request.env['account.invoice'].search([('id', '=', invoice_id)])
- '''
- Create Voucher
- '''
- def create_voucher(self, period, invoice, company_id, amountPayments, date_server, journalId, move_line_Ids, customerId):
- # Get Journal
- journal_id = request.env['account.journal'].browse(int(journalId))
- currency_id = journal_id.default_credit_account_id.currency_id.id or journal_id.default_credit_account_id.company_currency_id.id
- # Get Move Lines
- move_line = request.env['account.move.line'].browse(move_line_Ids).sorted(key=lambda r: r.id)
- # get customer
- customerId = request.env['res.partner'].browse(customerId)
- decimal_precision = request.env['decimal.precision'].precision_get('Account')
- # Create Line Voucher
- company_currency = request.env.user.company_id.currency_id
- currencyVocuher = request.env['res.currency'].browse(currency_id)
- line_dr_ids = []
- amount = round(float(amountPayments), decimal_precision)
- for line in move_line:
- amount_residual = line.amount_residual
- if (company_currency.id != currencyVocuher.id):
- amount_residual = round((amount_residual * ( currencyVocuher.rate / company_currency.rate)), decimal_precision)
- line_dr_ids.append([0, False, {
- 'date_due': line.date_maturity,
- 'account_id': line.account_id.id,
- 'date_original': line.move_id.date,
- 'move_line_id': line.id,
- 'amount_original': abs(line.credit or line.debit or 0.0),
- 'amount_unreconciled': abs(line.amount_residual),
- 'amount': min(abs(amount), abs(amount_residual)),
- 'reconcile': True if abs(amount_residual) == min(abs(amount), abs(amount_residual)) else False,
- 'currency_id': currency_id
- }])
- amount -= min(abs(amount), amount_residual)
- values = {
- 'reference': invoice.number,
- 'type': 'payment',
- 'journal_id': journal_id.id,
- 'company_id': company_id,
- 'pre_line': True,
- 'amount': round(float(amountPayments), decimal_precision),
- 'period_id': period.id,
- 'date': date_server,
- 'partner_id': customerId.id,
- 'account_id': journal_id.default_credit_account_id.id,
- 'currency_id': currency_id,
- 'line_dr_ids': line_dr_ids,
- 'payment_rate_currency_id': currency_id
- }
- account_voucher = request.env['account.voucher'].create(values)
- account_voucher.action_move_line_create()
- return account_voucher
- '''
- close invoice
- '''
- def close_invoice(self, invoiceid):
- invoice = request.env['account.invoice'].search([('id', '=', invoiceid)])
- if invoice.residual <= 0:
- invoice.confirm_paid()
- return invoice
- '''
- Create bank Statement
- '''
- def create_bank_statement(self, date_server, user_id, account_voucher):
- # Get bank Statamente
- bank_statement = request.env['account.bank.statement'].search([('journal_id', '=', account_voucher.journal_id.id), ('date', '=', date_server)])
- bank = {
- 'journal_id': account_voucher.journal_id.id,
- 'period_id': account_voucher.period_id.id,
- 'date': date_server,
- 'user_id': user_id,
- 'state': 'open' if account_voucher.journal_id.type == 'cash' else 'draft',
- }
- if bank_statement:
- if len(bank_statement) == 1:
- bank_statement.write(bank)
- else:
- bank_statement[len(bank_statement) -1].write(bank)
- else:
- bank_statement = bank_statement.create(bank)
- return bank_statement
- '''
- Create Statemne Line
- '''
- def create_bank_statement_line(self, account_voucher, statement):
- # Create line bank
- amount = account_voucher.amount
- if account_voucher.type == 'payment':
- amount = amount * -1
- bank_statement_line = {
- 'name': account_voucher.reference,
- 'partner_id': account_voucher.partner_id.id,
- 'amount': amount,
- 'voucher_id': account_voucher.id,
- 'journal_id': account_voucher.journal_id.id,
- 'account_id': account_voucher.account_id.id,
- 'journal_entry_id': account_voucher.move_id.id,
- 'currency_id': account_voucher.currency_id.id,
- 'ref': 'NP',
- 'statement_id': statement.id
- }
- line_id = request.env['account.bank.statement.line'].create(bank_statement_line)
- return line_id
- '''
- Create payments reference
- '''
- def create_bank_paymnets_line(self, statement_line, account_voucher, date_server, bankId, bankTypeId, paymentsBankRef, paymentsBankDateMaturity, paymentsBankNumber_cta, paymentsBankName_holder):
- bank_id = request.env['res.bank'].browse(bankId)
- bank_type_id = request.env['res.bank.payments.type'].browse(bankTypeId)
- bank_line ={
- 'number': paymentsBankRef,
- 'date_maturity': paymentsBankDateMaturity,
- 'amount': account_voucher.amount,
- 'date': date_server,
- 'state': 'negotiated',
- 'bank_id': bank_id.id,
- 'bank_payments_type_id': bank_type_id.id,
- 'statement_line_id': statement_line.id,
- 'comment': 'Np',
- 'number_cta': paymentsBankNumber_cta,
- 'name_holder': paymentsBankName_holder,
- 'type_operation': 'payment'
- }
- paymnets_line = request.env['res.bank.payments.line'].create(bank_line)
- return paymnets_line
- '''
- write_bank_payments_line
- '''
- def write_bank_payments_line(self, bankPaymentslineId):
- paymentsId = request.env['res.bank.payments.line'].browse(bankPaymentslineId)
- bank = {
- 'state': 'negotiated'
- }
- if (paymentsId):
- paymentsId.write(bank)
- return paymentsId
- '''
- Payment process
- '''
- @http.route('/eiru_payments_purchases/purchases_process', type='json', auth='user', methods=['POST'], cors='*')
- def purchases_process(self, **kw):
- self.make_info_log('Processing payments purchases...')
- # Get Date Server
- date_server = datetime.now().strftime(DATE_FORMAT)
- #Get Periodo
- period = self.get_period(date_server)
- self.make_info_log('[OK] Getting period')
- # Get invoice
- invoice = self.get_invoice(kw.get('invoiceId'))
- self.make_info_log('[OK] Getting invoice')
- # Get User - company
- user_id = request.env.user.id
- self.make_info_log('[OK] Getting user')
- # Get Company
- company_id = request.env.user.company_id.id
- self.make_info_log('[OK] Getting Company')
- # create voucher
- voucher = self.create_voucher(period, invoice, company_id, kw.get('amountPayments', 0), date_server, kw.get('journalId'), kw.get('moveLinesId'), kw.get('supplierId'))
- self.make_info_log('[OK] creating voucher...')
- # close invoice
- close_invoice = self.close_invoice(kw.get('invoiceId'))
- self.make_info_log('[OK] closing invoice...')
- # Create bank statement
- bank_statement = self.create_bank_statement(date_server, user_id, voucher)
- self.make_info_log('[OK] creating bank statement')
- # Create bank statement Line
- bank_statement_line = self.create_bank_statement_line(voucher, bank_statement)
- self.make_info_log('[OK] creating bank statement Line')
- # Crete Payments Bank Reference
- # if (kw.get('journalType') == 'bank'):
- # bank_paymnets_line = self.create_bank_paymnets_line(bank_statement_line, voucher, date_server, kw.get('bankId'), kw.get('bankTypeId'), kw.get('bankRef'), kw.get('bankDateMaturity'), kw.get('paymentsBankNumber_cta'), kw.get('paymentsBankName_holder'))
- # self.make_info_log('[OK] creating bank payments line')
- #
- # if (kw.get('numberChBankId')):
- # update_bank_paymnets_line = self.write_bank_payments_line(kw.get('numberChBankId'))
- # self.make_info_log('[OK] write bank payments line')
- return {
- 'process': True
- }
|