|
@@ -4,7 +4,6 @@ from openerp.http import request
|
|
from werkzeug.wrappers import Response
|
|
from werkzeug.wrappers import Response
|
|
from werkzeug.datastructures import Headers
|
|
from werkzeug.datastructures import Headers
|
|
from datetime import datetime
|
|
from datetime import datetime
|
|
-from StringIO import StringIO
|
|
|
|
from gzip import GzipFile
|
|
from gzip import GzipFile
|
|
from StringIO import StringIO as IO
|
|
from StringIO import StringIO as IO
|
|
import simplejson as json
|
|
import simplejson as json
|
|
@@ -12,13 +11,15 @@ import gzip
|
|
import logging
|
|
import logging
|
|
|
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
+DATE_FORMAT = '%Y-%m-%d'
|
|
|
|
+GZIP_COMPRESSION_LEVEL = 9
|
|
|
|
|
|
class PaymentsSales(http.Controller):
|
|
class PaymentsSales(http.Controller):
|
|
'''
|
|
'''
|
|
Get server date
|
|
Get server date
|
|
'''
|
|
'''
|
|
def get_server_date(self):
|
|
def get_server_date(self):
|
|
- return datetime.now().strftime('%y-%m-%d')
|
|
|
|
|
|
+ return datetime.now().strftime(DATE_FORMAT)
|
|
|
|
|
|
'''
|
|
'''
|
|
Get partner customer
|
|
Get partner customer
|
|
@@ -205,7 +206,7 @@ class PaymentsSales(http.Controller):
|
|
'''
|
|
'''
|
|
def make_gzip_response(self, data=None, status=200):
|
|
def make_gzip_response(self, data=None, status=200):
|
|
gzip_buffer = IO()
|
|
gzip_buffer = IO()
|
|
- with GzipFile(mode='wb', compresslevel=9, fileobj=gzip_buffer) as gzip_file:
|
|
|
|
|
|
+ with GzipFile(mode='wb', compresslevel=GZIP_COMPRESSION_LEVEL, fileobj=gzip_buffer) as gzip_file:
|
|
gzip_file.write(json.dumps(data))
|
|
gzip_file.write(json.dumps(data))
|
|
|
|
|
|
contents = gzip_buffer.getvalue()
|
|
contents = gzip_buffer.getvalue()
|
|
@@ -238,3 +239,148 @@ class PaymentsSales(http.Controller):
|
|
'journals': self.get_journals(),
|
|
'journals': self.get_journals(),
|
|
'currencies': self.get_currency()
|
|
'currencies': self.get_currency()
|
|
})
|
|
})
|
|
|
|
+
|
|
|
|
+ '''
|
|
|
|
+ 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 / Currency
|
|
|
|
+ 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)
|
|
|
|
+
|
|
|
|
+ # Create Line Voucher
|
|
|
|
+ line_cr_ids = []
|
|
|
|
+ amount = float(amountPayments)
|
|
|
|
+
|
|
|
|
+ for line in move_line:
|
|
|
|
+ line_cr_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), line.amount_residual),
|
|
|
|
+ 'reconcile': line.move_id.date <= line.date_maturity,
|
|
|
|
+ 'currency_id': currency_id
|
|
|
|
+ }])
|
|
|
|
+ amount -= min(abs(amount), line.amount_residual)
|
|
|
|
+
|
|
|
|
+ values = {
|
|
|
|
+ 'reference': invoice.number,
|
|
|
|
+ 'type': 'receipt',
|
|
|
|
+ 'journal_id': journal_id.id,
|
|
|
|
+ 'company_id': company_id,
|
|
|
|
+ 'pre_line': True,
|
|
|
|
+ 'amount': float(amountPayments),
|
|
|
|
+ '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_cr_ids': line_cr_ids
|
|
|
|
+ }
|
|
|
|
+ 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.write({
|
|
|
|
+ 'state': '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)])
|
|
|
|
+ # Create line bank
|
|
|
|
+ bank_statement_line = [[0, False, {
|
|
|
|
+ 'name': account_voucher.reference,
|
|
|
|
+ 'partner_id': account_voucher.partner_id.id,
|
|
|
|
+ 'amount': account_voucher.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'
|
|
|
|
+ }]]
|
|
|
|
+
|
|
|
|
+ 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',
|
|
|
|
+ 'line_ids': bank_statement_line
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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
|
|
|
|
+
|
|
|
|
+ '''
|
|
|
|
+ Payment process
|
|
|
|
+ '''
|
|
|
|
+ @http.route('/eiru_payments/payment_process', type='json', auth='user', methods=['POST'], cors='*')
|
|
|
|
+ def payment_process(self, **kw):
|
|
|
|
+ self.make_info_log('Processing payments...')
|
|
|
|
+ # 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('moveLines'), kw.get('customerId'))
|
|
|
|
+ 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')
|
|
|
|
+
|
|
|
|
+ return {
|
|
|
|
+ 'process': True
|
|
|
|
+ }
|