123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- # -*- coding: utf-8 -*-
- from openerp import models, fields, tools, api
- from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT, DEFAULT_SERVER_DATE_FORMAT
- from pytz import timezone
- from datetime import datetime,timedelta
- import logging
- _logger = logging.getLogger(__name__)
- class EiruPosSessionConfirm(models.Model):
- _inherit = 'pos.session'
- ''' Timezone '''
- def get_timezone(self):
- return timezone(self._context.get('tz') or self.env.user.tz)
- ''' Datetime '''
- def get_datetime(self):
- return datetime.now(self.get_timezone()).strftime(DEFAULT_SERVER_DATETIME_FORMAT)
- ''' Date '''
- def get_date(self):
- return datetime.now(self.get_timezone()).strftime(DEFAULT_SERVER_DATE_FORMAT)
- ''' GET POS SESSION + Statemen '''
- @api.model
- def eiru_get_pos_session_statement(self, id):
- _logger.info('Obtener los detalles de las caja de las seccion')
- statementId = []
- sessionPos = []
- posSession = self.env['pos.session'].search([('id', '=', id ), ('state', '=','opened')])
- if (not posSession):
- return {
- 'state': False,
- 'message': "Error en obtener la sesión del pos.",
- }
- statementIds = map(lambda x: x.id, posSession.statement_ids)
- bankStatement = self.env['account.bank.statement'].search([('id', 'in', statementIds),('state', '=', 'open')])
- if (not bankStatement):
- return {
- 'state':False,
- 'message': "No existe ninguna caja asociada con la sesión",
- }
- for statement in bankStatement:
- statementGeneral = []
- amount = 0.00
- AccountJournal = self.env['account.journal'].browse(statement.journal_id.id)
- statementType = self.env['account.bank.statement.type'].search([('code', '=', 'GENERAL')])
- statementG = self.env['account.bank.statement'].search([('journal_id.id', '=', AccountJournal.id), ('type_statement.id', '=', statementType.id),('state', '=', 'open')])
- for generalStatement in statementG:
- statementGeneral.append({
- 'id': generalStatement.id,
- 'name': generalStatement.name
- })
- for line in statement.line_ids:
- amount += line.amount
- if (statement.balance_start > 0):
- amount += statement.balance_start
- statementId.append({
- 'id': statement.id,
- 'name': statement.name,
- 'amount': amount,
- 'joutnalType': AccountJournal.type,
- 'journalId': AccountJournal.id,
- 'statementGeneral': statementGeneral,
- 'currency':[{
- 'id': resCurrency.id,
- 'name': resCurrency.name,
- 'symbol': resCurrency.symbol,
- 'localName': resCurrency.local_name,
- 'rate': resCurrency.rate,
- 'thousandsSeparator': resCurrency.thousands_separator,
- 'decimalSeparator': resCurrency.decimal_separator,
- 'decimalPlaces': resCurrency.decimal_places,
- 'position': resCurrency.position,
- }for resCurrency in self.env['res.currency'].browse(statement.currency.id)]
- })
- sessionPos = {
- 'id': posSession.id,
- 'name': posSession.name,
- 'state': posSession.state,
- 'statement': statementId,
- }
- return sessionPos
- ''' ***********************************************************************
- ____ _ ____ _
- / ___| | ___ ___ ___ / ___| ___ ___ ___(_) ___ _ __
- | | | |/ _ \/ __|/ _ \ \___ \ / _ \/ __/ __| |/ _ \| '_ \
- | |___| | (_) \__ \ __/ ___) | __/\__ \__ \ | (_) | | | |
- \____|_|\___/|___/\___| |____/ \___||___/___/_|\___/|_| |_|
- *************************************************************************'''
- @api.model
- def eiru_pos_session_close(self, id):
- _logger.info('Validar y contabilizar asiento(s) de cierre')
- posSession = self.env['pos.session'].browse(id)
- return posSession.signal_workflow('close')
- ''' ***********************************************************************
- ____ _ _ _ ____
- / ___|| |_ __ _| |_ ___ _ __ ___ ___ _ __ | |_ | _ \ ___ ___
- \___ \| __/ _` | __/ _ \ '_ ` _ \ / _ \ '_ \| __| | |_) / _ \/ __|
- ___) | || (_| | || __/ | | | | | __/ | | | |_ | __/ (_) \__ \
- |____/ \__\__,_|\__\___|_| |_| |_|\___|_| |_|\__| |_| \___/|___/
- *************************************************************************'''
- @api.model
- def eiru_statement_confirm_pos(self, sessionId, statements):
- _logger.info('Confirm Statement Pos')
- user = self.env.user
- dateServer = self.get_date()
- for statement in statements:
- bankStatement = self.confirm_pos_get_statement(statement['id'])
- if (not bankStatement):
- return {
- 'state': False,
- 'message': "Error en Obtener la lineas"
- }
- amountStatement = 0.0
- for line in bankStatement.line_ids:
- amountStatement += line.amount
- if (bankStatement.balance_start > 0):
- amountStatement += bankStatement.balance_start
- balanceLine = []
- if(amountStatement != statement['amountConfirm']):
- # amountConfirm = (statement['amountConfirm']) - (amountStatement)
- if (amountStatement < 0):
- balanceNegative = self.confirm_pos_create_statement_line(bankStatement, "Ajuste saldo negativo PTV", abs(amountStatement), "Perdida")
- amountStatement = 0.0
- amountConfirm = (statement['amountConfirm']) - (amountStatement)
- name = "Ajuste de cierre de caja (POS)"
- referencia = "Ganancia" if(amountConfirm >= 0) else "Perdida"
- # referencia = "Ganancia" if((amountConfirm >= 0) and (amountStatement >= 0) ) else "Perdida"
- # import web_pdb; web_pdb.set_trace()
- balanceLine = self.confirm_pos_create_statement_line( bankStatement, name, amountConfirm, referencia)
- cashboxConfirm = {
- 'name': "CIERRE DE CAJA (%s)" %(bankStatement.name),
- 'date': dateServer,
- 'ref': "Cierre de caja (TPV)",
- 'statement_id': bankStatement.id,
- 'user_statement': bankStatement.user_id.id,
- 'user_confirm': user.id,
- 'journal_id' : bankStatement.journal_id.id,
- 'amount_statement': amountStatement,
- 'amount_real': statement['amountConfirm'],
- 'line_difference': balanceLine.id if (balanceLine) else '',
- 'amount_difference': balanceLine.amount if (balanceLine) else 0.0,
- 'amount_next_open': 0.0,
- 'state_avaliable': False,
- }
- statementConfirm = self.confirm_pos_create_cashbox_statement_confirm(cashboxConfirm, bankStatement)
- '''
- Salida de Dinero.
- '''
- if (statement['OutputPos']):
- #Crear Line en caja
- name = "Retiro de dinero"
- lineOutput = self.confirm_pos_create_statement_line( bankStatement, name, (- statement['amountOutput']), "TPV")
- # Crear Salida
- cashboxOutput = self.confirm_pos_create_cashbox_output(lineOutput, statement['amountOutput'], statementConfirm)
- '''
- Transferencia.
- '''
- if (statement['transferPos']):
- statementTransfer = self.confirm_pos_get_statement(statement['statementTransfer'])
- if (not statementTransfer):
- return {
- 'state': False,
- 'message': "Erro, no se pudo encontrar la caja destino de la transferencia."
- }
- ## Salida
- name = "TRANSFERENCIA/%s (Salida)" % (statementTransfer.name)
- transferOutput = self.confirm_pos_create_statement_line( bankStatement, name, (- statement['amountTransfer']), "TPV")
- ## Entrada
- name = "TRANSFERENCIA/%s (Entrada)" % (bankStatement.name)
- transferinput = self.confirm_pos_create_statement_line( statementTransfer, name, statement['amountTransfer'], "TPV")
- ## Transferencia
- transferencia = self.confirm_pos_create_cashbox_transfer("TPV", statement['amountTransfer'], transferOutput, transferinput, statementConfirm)
- '''
- Saldo para próxima apertura de caja.
- '''
- amountStatement = 0.0
- for line in bankStatement.line_ids:
- amountStatement += line.amount
- if (bankStatement.balance_start > 0):
- amountStatement += bankStatement.balance_start
- # import web_pdb; web_pdb.set_trace()
- if (amountStatement > 0.0):
- name="Saldo para próxima apertura de caja"
- lineNextOpen = self.confirm_pos_create_statement_line(bankStatement, name, (- amountStatement), "TPV")
- if (not lineNextOpen):
- return {
- 'state' : False,
- 'message': "Error, En el registro de saldo."
- }
- statementConfirm.write({
- 'line_next_open': lineNextOpen.id,
- 'amount_next_open': abs(amountStatement),
- 'state_avaliable': True,
- })
- return {
- 'state': True,
- 'message': "Operación exitosa"
- }
- ''' Get Statement '''
- def confirm_pos_get_statement(self,id):
- return self.env['account.bank.statement'].browse(id)
- ''' Create Line Statemen '''
- def confirm_pos_create_statement_line(self, statement, name, amount, ref):
- statementLine = {
- 'statement_id': statement.id,
- 'name': name,
- 'amount': amount,
- 'ref': ref,
- 'account_id': statement.journal_id.internal_account_id.id,
- 'journal_id': statement.journal_id.id,
- 'is_deleted': True
- }
- return self.env['account.bank.statement.line'].create(statementLine)
- ''' Create cashbox Confirm '''
- def confirm_pos_create_cashbox_statement_confirm(self,values, statement):
- casbox = self.env['cashbox.statement.confirm'].search([('statement_id.id', '=', statement.id)])
- if (not casbox):
- confirm = self.env['cashbox.statement.confirm'].create(values)
- else:
- confirm = casbox.write(values)
- if (confirm):
- confirm = self.env['cashbox.statement.confirm'].search([('statement_id.id', '=', statement.id)])
- return confirm
- ''' Create cashBox output '''
- def confirm_pos_create_cashbox_output(self, line, amount, confirm):
- cash = {
- 'name': "Retiro de dinero (%s)" % (line.statement_id.name),
- 'amount': amount,
- 'ref': 'TPV',
- 'date': line.date,
- 'line_id': line.id,
- 'statement_id': line.statement_id.id,
- 'cashbox_confirm_id': confirm.id
- }
- return self.env['cash.box.out'].create(cash)
- ''' Create cash box transfer '''
- def confirm_pos_create_cashbox_transfer(self, ref, amount, lineOutput, lineInput, confirm):
- cash = {
- 'name': "TRANSFERENCIA %s a %s" %((lineOutput.statement_id.name),(lineInput.statement_id.name)),
- 'amount': amount,
- 'ref': ref,
- 'input_line': lineInput.id,
- 'output_line': lineOutput.id,
- 'input_statement': lineInput.statement_id.id,
- 'output_statement': lineOutput.statement_id.id,
- 'date': lineOutput.date,
- 'cashbox_confirm_id': confirm.id,
- }
- return self.env['cash.box.transfer'].create(cash)
- '''
- ____ ____ ___ _ _ _____ ____ _____ _ _____ _____ __ __ _____ _ _ _____
- | _ \| _ \|_ _| \ | |_ _| / ___|_ _|/ \|_ _| ____| \/ | ____| \ | |_ _|
- | |_) | |_) || || \| | | | \___ \ | | / _ \ | | | _| | |\/| | _| | \| | | |
- | __/| _ < | || |\ | | | ___) || |/ ___ \| | | |___| | | | |___| |\ | | |
- |_| |_| \_\___|_| \_| |_| |____/ |_/_/ \_\_| |_____|_| |_|_____|_| \_| |_|
- '''
- ''' Get Pos Session Statemen Confirm Print'''
- @api.model
- def confirm_pos_get_statement_print(self, id):
- _logger.info('Generar resume de cierre de las caja')
- posStatement = []
- posSession = self.env['pos.session'].search([('id', '=', id),('state', '=', 'closed')])
- if (not posSession):
- return False
- statementIds = map(lambda x: x.id, posSession.statement_ids)
- statements = self.env['account.bank.statement'].search([('id', 'in', statementIds),('state', '=', 'confirm')])
- if (not statements):
- return False
- statemntPos = []
- for statement in statements:
- statementConfirm = []
- accountJournal = self.env['account.journal'].browse(statement.journal_id.id)
- ''' Verificar si existe cierre '''
- confirm = self.env['cashbox.statement.confirm'].search([('statement_id.id', '=', statement.id)])
- if (not confirm):
- amountStatement = 0.0
- for line in statement.line_ids:
- amountStatement += line.amount
- if (statement.balance_start > 0):
- amountStatement += statement.balance_start
- statementConfirm.append({
- 'amountStatement': amountStatement,
- 'amountConfirm': 0,
- 'amountDifference': 0,
- 'amountOutput': 0,
- 'amountTransfer': 0,
- 'amountNextOpen': 0,
- })
- if (confirm):
- amountDifference = self.confirm_pos_get_statement_line(confirm.line_difference.id)
- amountNextOpen = abs(self.confirm_pos_get_statement_line(confirm.line_next_open.id))
- transferID = map(lambda x: x.id, confirm.cashbox_transfer_ids)
- amountTransfer = self.confirm_pos_get_casbox_transfer(transferID)
- outputIds = map(lambda x: x.id, confirm.cashbox_output_ids)
- amountOutput =self.confirm_pos_get_casbox_output(outputIds)
- statementConfirm.append({
- 'amountStatement': confirm.amount_statement,
- 'amountConfirm': confirm.amount_real,
- 'amountDifference': amountDifference,
- 'amountNextOpen': amountNextOpen,
- 'amountTransfer': amountTransfer,
- 'amountOutput': amountOutput,
- })
- statemntPos.append({
- 'id': statement.id,
- 'name':statement.name,
- 'journalType': accountJournal.type,
- 'journalName': accountJournal.name,
- 'journalId': accountJournal.id,
- 'confirm': statementConfirm,
- 'currency':[{
- 'id': resCurrency.id,
- 'name': resCurrency.name,
- 'symbol': resCurrency.symbol,
- 'localName': resCurrency.local_name,
- 'rate': resCurrency.rate,
- 'thousandsSeparator': resCurrency.thousands_separator,
- 'decimalSeparator': resCurrency.decimal_separator,
- 'decimalPlaces': resCurrency.decimal_places,
- 'position': resCurrency.position,
- }for resCurrency in self.env['res.currency'].browse(statement.currency.id)]
- })
- posStatement.append({
- 'name': posSession.name,
- 'startAt': posSession.start_at,
- 'stopAt': posSession.stop_at,
- 'userName': posSession.user_id.name,
- 'statemnt': statemntPos
- })
- return posStatement
- ''' get amount statemnt Line '''
- def confirm_pos_get_statement_line(self, id):
- _logger.info('Obtener la linea de la caja.')
- line = self.env['account.bank.statement.line'].search([('id', '=', id)])
- return line.amount if(line) else 0.0
- ''' Get cashBox.transfer '''
- def confirm_pos_get_casbox_transfer(self,ids):
- _logger.info('Obtener la transferencia.')
- transfer = self.env['cash.box.transfer'].search([('id', 'in', ids)])
- return transfer.amount if(transfer) else 0.0
- ''' Get cashBox.output '''
- def confirm_pos_get_casbox_output(self,ids):
- _logger.info('Obtener los retiro de dinero .')
- output = self.env['cash.box.out'].search([('id', 'in', ids)])
- return output.amount if(output) else 0.0
|