eiru_statement_pos_confirm.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. # -*- coding: utf-8 -*-
  2. from openerp import models, fields, tools, api
  3. from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT, DEFAULT_SERVER_DATE_FORMAT
  4. from pytz import timezone
  5. from datetime import datetime,timedelta
  6. import logging
  7. _logger = logging.getLogger(__name__)
  8. class EiruPosSessionConfirm(models.Model):
  9. _inherit = 'pos.session'
  10. ''' Timezone '''
  11. def get_timezone(self):
  12. return timezone(self._context.get('tz') or self.env.user.tz)
  13. ''' Datetime '''
  14. def get_datetime(self):
  15. return datetime.now(self.get_timezone()).strftime(DEFAULT_SERVER_DATETIME_FORMAT)
  16. ''' Date '''
  17. def get_date(self):
  18. return datetime.now(self.get_timezone()).strftime(DEFAULT_SERVER_DATE_FORMAT)
  19. ''' GET POS SESSION + Statemen '''
  20. @api.model
  21. def eiru_get_pos_session_statement(self, id):
  22. _logger.info('Obtener los detalles de las caja de las seccion')
  23. statementId = []
  24. sessionPos = []
  25. posSession = self.env['pos.session'].search([('id', '=', id ), ('state', '=','opened')])
  26. if (not posSession):
  27. return {
  28. 'state': False,
  29. 'message': "Error en obtener la sesión del pos.",
  30. }
  31. statementIds = map(lambda x: x.id, posSession.statement_ids)
  32. bankStatement = self.env['account.bank.statement'].search([('id', 'in', statementIds),('state', '=', 'open')])
  33. if (not bankStatement):
  34. return {
  35. 'state':False,
  36. 'message': "No existe ninguna caja asociada con la sesión",
  37. }
  38. for statement in bankStatement:
  39. statementGeneral = []
  40. amount = 0.00
  41. AccountJournal = self.env['account.journal'].browse(statement.journal_id.id)
  42. statementType = self.env['account.bank.statement.type'].search([('code', '=', 'GENERAL')])
  43. statementG = self.env['account.bank.statement'].search([('journal_id.id', '=', AccountJournal.id), ('type_statement.id', '=', statementType.id),('state', '=', 'open')])
  44. for generalStatement in statementG:
  45. statementGeneral.append({
  46. 'id': generalStatement.id,
  47. 'name': generalStatement.name
  48. })
  49. for line in statement.line_ids:
  50. amount += line.amount
  51. if (statement.balance_start > 0):
  52. amount += statement.balance_start
  53. statementId.append({
  54. 'id': statement.id,
  55. 'name': statement.name,
  56. 'amount': amount,
  57. 'joutnalType': AccountJournal.type,
  58. 'journalId': AccountJournal.id,
  59. 'statementGeneral': statementGeneral,
  60. 'currency':[{
  61. 'id': resCurrency.id,
  62. 'name': resCurrency.name,
  63. 'symbol': resCurrency.symbol,
  64. 'localName': resCurrency.local_name,
  65. 'rate': resCurrency.rate,
  66. 'thousandsSeparator': resCurrency.thousands_separator,
  67. 'decimalSeparator': resCurrency.decimal_separator,
  68. 'decimalPlaces': resCurrency.decimal_places,
  69. 'position': resCurrency.position,
  70. }for resCurrency in self.env['res.currency'].browse(statement.currency.id)]
  71. })
  72. sessionPos = {
  73. 'id': posSession.id,
  74. 'name': posSession.name,
  75. 'state': posSession.state,
  76. 'statement': statementId,
  77. }
  78. return sessionPos
  79. ''' ***********************************************************************
  80. ____ _ ____ _
  81. / ___| | ___ ___ ___ / ___| ___ ___ ___(_) ___ _ __
  82. | | | |/ _ \/ __|/ _ \ \___ \ / _ \/ __/ __| |/ _ \| '_ \
  83. | |___| | (_) \__ \ __/ ___) | __/\__ \__ \ | (_) | | | |
  84. \____|_|\___/|___/\___| |____/ \___||___/___/_|\___/|_| |_|
  85. *************************************************************************'''
  86. @api.model
  87. def eiru_pos_session_close(self, id):
  88. _logger.info('Validar y contabilizar asiento(s) de cierre')
  89. posSession = self.env['pos.session'].browse(id)
  90. return posSession.signal_workflow('close')
  91. ''' ***********************************************************************
  92. ____ _ _ _ ____
  93. / ___|| |_ __ _| |_ ___ _ __ ___ ___ _ __ | |_ | _ \ ___ ___
  94. \___ \| __/ _` | __/ _ \ '_ ` _ \ / _ \ '_ \| __| | |_) / _ \/ __|
  95. ___) | || (_| | || __/ | | | | | __/ | | | |_ | __/ (_) \__ \
  96. |____/ \__\__,_|\__\___|_| |_| |_|\___|_| |_|\__| |_| \___/|___/
  97. *************************************************************************'''
  98. @api.model
  99. def eiru_statement_confirm_pos(self, sessionId, statements):
  100. _logger.info('Confirm Statement Pos')
  101. user = self.env.user
  102. dateServer = self.get_date()
  103. for statement in statements:
  104. bankStatement = self.confirm_pos_get_statement(statement['id'])
  105. if (not bankStatement):
  106. return {
  107. 'state': False,
  108. 'message': "Error en Obtener la lineas"
  109. }
  110. amountStatement = 0.0
  111. for line in bankStatement.line_ids:
  112. amountStatement += line.amount
  113. if (bankStatement.balance_start > 0):
  114. amountStatement += bankStatement.balance_start
  115. balanceLine = []
  116. if(amountStatement != statement['amountConfirm']):
  117. amountConfirm = statement['amountConfirm'] - amountStatement
  118. name = "Ajuste de cierre de caja (POS)"
  119. referencia = "Ganancia" if(amountConfirm > 0) else "Perdida"
  120. balanceLine = self.confirm_pos_create_statement_line( bankStatement, name, amountConfirm, referencia)
  121. cashboxConfirm = {
  122. 'name': "CIERRE DE CAJA (%s)" %(bankStatement.name),
  123. 'date': dateServer,
  124. 'ref': "Cierre de caja (TPV)",
  125. 'statement_id': bankStatement.id,
  126. 'user_statement': bankStatement.user_id.id,
  127. 'user_confirm': user.id,
  128. 'journal_id' : bankStatement.journal_id.id,
  129. 'amount_statement': amountStatement,
  130. 'amount_real': statement['amountConfirm'],
  131. 'line_difference': balanceLine.id if (balanceLine) else '',
  132. 'amount_difference': balanceLine.amount if (balanceLine) else 0.0,
  133. 'amount_next_open': 0.0,
  134. 'state_avaliable': False,
  135. }
  136. statementConfirm = self.confirm_pos_create_cashbox_statement_confirm(cashboxConfirm, bankStatement)
  137. '''
  138. Salida de Dinero.
  139. '''
  140. if (statement['OutputPos']):
  141. #Crear Line en caja
  142. name = "Retiro de dinero"
  143. lineOutput = self.confirm_pos_create_statement_line( bankStatement, name, (- statement['amountOutput']), "TPV")
  144. # Crear Salida
  145. cashboxOutput = self.confirm_pos_create_cashbox_output(lineOutput, statement['amountOutput'], statementConfirm)
  146. '''
  147. Transferencia.
  148. '''
  149. if (statement['transferPos']):
  150. statementTransfer = self.confirm_pos_get_statement(statement['statementTransfer'])
  151. if (not statementTransfer):
  152. return {
  153. 'state': False,
  154. 'message': "Erro, no se pudo encontrar la caja destino de la transferencia."
  155. }
  156. ## Salida
  157. name = "TRANSFERENCIA/%s (Salida)" % (statementTransfer.name)
  158. transferOutput = self.confirm_pos_create_statement_line( bankStatement, name, (- statement['amountTransfer']), "TPV")
  159. ## Entrada
  160. name = "TRANSFERENCIA/%s (Entrada)" % (bankStatement.name)
  161. transferinput = self.confirm_pos_create_statement_line( statementTransfer, name, statement['amountTransfer'], "TPV")
  162. ## Transferencia
  163. transferencia = self.confirm_pos_create_cashbox_transfer("TPV", statement['amountTransfer'], transferOutput, transferinput, statementConfirm)
  164. '''
  165. Saldo para próxima apertura de caja.
  166. '''
  167. amountStatement = 0.0
  168. for line in bankStatement.line_ids:
  169. amountStatement += line.amount
  170. if (bankStatement.balance_start > 0):
  171. amountStatement += bankStatement.balance_start
  172. if (amountStatement > 0.0):
  173. name="Saldo para próxima apertura de caja"
  174. lineNextOpen = self.confirm_pos_create_statement_line(bankStatement, name, (- amountStatement), "TPV")
  175. if (not lineNextOpen):
  176. return {
  177. 'state' : False,
  178. 'message': "Error, En el registro de saldo."
  179. }
  180. statementConfirm.write({
  181. 'line_next_open': lineNextOpen.id,
  182. 'amount_next_open': abs(amountStatement),
  183. 'state_avaliable': True,
  184. })
  185. return {
  186. 'state': True,
  187. 'message': "Operación exitosa"
  188. }
  189. ''' Get Statement '''
  190. def confirm_pos_get_statement(self,id):
  191. return self.env['account.bank.statement'].browse(id)
  192. ''' Create Line Statemen '''
  193. def confirm_pos_create_statement_line(self, statement, name, amount, ref):
  194. statementLine = {
  195. 'statement_id': statement.id,
  196. 'name': name,
  197. 'amount': amount,
  198. 'ref': ref,
  199. 'account_id': statement.journal_id.internal_account_id.id,
  200. 'journal_id': statement.journal_id.id,
  201. 'is_deleted': True
  202. }
  203. return self.env['account.bank.statement.line'].create(statementLine)
  204. ''' Create cashbox Confirm '''
  205. def confirm_pos_create_cashbox_statement_confirm(self,values, statement):
  206. casbox = self.env['cashbox.statement.confirm'].search([('statement_id.id', '=', statement.id)])
  207. if (not casbox):
  208. confirm = self.env['cashbox.statement.confirm'].create(values)
  209. else:
  210. confirm = casbox.write(values)
  211. if (confirm):
  212. confirm = self.env['cashbox.statement.confirm'].search([('statement_id.id', '=', statement.id)])
  213. return confirm
  214. ''' Create cashBox output '''
  215. def confirm_pos_create_cashbox_output(self, line, amount, confirm):
  216. cash = {
  217. 'name': "Retiro de dinero (%s)" % (line.statement_id.name),
  218. 'amount': amount,
  219. 'ref': 'TPV',
  220. 'date': line.date,
  221. 'line_id': line.id,
  222. 'statement_id': line.statement_id.id,
  223. 'cashbox_confirm_id': confirm.id
  224. }
  225. return self.env['cash.box.out'].create(cash)
  226. ''' Create cash box transfer '''
  227. def confirm_pos_create_cashbox_transfer(self, ref, amount, lineOutput, lineInput, confirm):
  228. cash = {
  229. 'name': "TRANSFERENCIA %s a %s" %((lineOutput.statement_id.name),(lineInput.statement_id.name)),
  230. 'amount': amount,
  231. 'ref': ref,
  232. 'input_line': lineInput.id,
  233. 'output_line': lineOutput.id,
  234. 'input_statement': lineInput.statement_id.id,
  235. 'output_statement': lineOutput.statement_id.id,
  236. 'date': lineOutput.date,
  237. 'cashbox_confirm_id': confirm.id,
  238. }
  239. return self.env['cash.box.transfer'].create(cash)
  240. '''
  241. ____ ____ ___ _ _ _____ ____ _____ _ _____ _____ __ __ _____ _ _ _____
  242. | _ \| _ \|_ _| \ | |_ _| / ___|_ _|/ \|_ _| ____| \/ | ____| \ | |_ _|
  243. | |_) | |_) || || \| | | | \___ \ | | / _ \ | | | _| | |\/| | _| | \| | | |
  244. | __/| _ < | || |\ | | | ___) || |/ ___ \| | | |___| | | | |___| |\ | | |
  245. |_| |_| \_\___|_| \_| |_| |____/ |_/_/ \_\_| |_____|_| |_|_____|_| \_| |_|
  246. '''
  247. ''' Get Pos Session Statemen Confirm Print'''
  248. @api.model
  249. def confirm_pos_get_statement_print(self, id):
  250. _logger.info('Generar resume de cierre de las caja')
  251. posStatement = []
  252. posSession = self.env['pos.session'].search([('id', '=', id),('state', '=', 'closed')])
  253. if (not posSession):
  254. return False
  255. statementIds = map(lambda x: x.id, posSession.statement_ids)
  256. statements = self.env['account.bank.statement'].search([('id', 'in', statementIds),('state', '=', 'confirm')])
  257. if (not statements):
  258. return False
  259. statemntPos = []
  260. for statement in statements:
  261. statementConfirm = []
  262. accountJournal = self.env['account.journal'].browse(statement.journal_id.id)
  263. ''' Verificar si existe cierre '''
  264. confirm = self.env['cashbox.statement.confirm'].search([('statement_id.id', '=', statement.id)])
  265. if (not confirm):
  266. amountStatement = 0.0
  267. for line in statement.line_ids:
  268. amountStatement += line.amount
  269. if (statement.balance_start > 0):
  270. amountStatement += statement.balance_start
  271. statementConfirm.append({
  272. 'amountStatement': amountStatement,
  273. 'amountConfirm': 0,
  274. 'amountDifference': 0,
  275. 'amountOutput': 0,
  276. 'amountTransfer': 0,
  277. 'amountNextOpen': 0,
  278. })
  279. if (confirm):
  280. amountDifference = self.confirm_pos_get_statement_line(confirm.line_difference.id)
  281. amountNextOpen = abs(self.confirm_pos_get_statement_line(confirm.line_next_open.id))
  282. transferID = map(lambda x: x.id, confirm.cashbox_transfer_ids)
  283. amountTransfer = self.confirm_pos_get_casbox_transfer(transferID)
  284. outputIds = map(lambda x: x.id, confirm.cashbox_output_ids)
  285. amountOutput =self.confirm_pos_get_casbox_output(outputIds)
  286. statementConfirm.append({
  287. 'amountStatement': confirm.amount_statement,
  288. 'amountConfirm': confirm.amount_real,
  289. 'amountDifference': amountDifference,
  290. 'amountNextOpen': amountNextOpen,
  291. 'amountTransfer': amountTransfer,
  292. 'amountOutput': amountOutput,
  293. })
  294. statemntPos.append({
  295. 'id': statement.id,
  296. 'name':statement.name,
  297. 'journalType': accountJournal.type,
  298. 'journalName': accountJournal.name,
  299. 'journalId': accountJournal.id,
  300. 'confirm': statementConfirm,
  301. 'currency':[{
  302. 'id': resCurrency.id,
  303. 'name': resCurrency.name,
  304. 'symbol': resCurrency.symbol,
  305. 'localName': resCurrency.local_name,
  306. 'rate': resCurrency.rate,
  307. 'thousandsSeparator': resCurrency.thousands_separator,
  308. 'decimalSeparator': resCurrency.decimal_separator,
  309. 'decimalPlaces': resCurrency.decimal_places,
  310. 'position': resCurrency.position,
  311. }for resCurrency in self.env['res.currency'].browse(statement.currency.id)]
  312. })
  313. posStatement.append({
  314. 'name': posSession.name,
  315. 'startAt': posSession.start_at,
  316. 'stopAt': posSession.stop_at,
  317. 'userName': posSession.user_id.name,
  318. 'statemnt': statemntPos
  319. })
  320. return posStatement
  321. ''' get amount statemnt Line '''
  322. def confirm_pos_get_statement_line(self, id):
  323. _logger.info('Obtener la linea de la caja.')
  324. line = self.env['account.bank.statement.line'].search([('id', '=', id)])
  325. return line.amount if(line) else 0.0
  326. ''' Get cashBox.transfer '''
  327. def confirm_pos_get_casbox_transfer(self,ids):
  328. _logger.info('Obtener la transferencia.')
  329. transfer = self.env['cash.box.transfer'].search([('id', 'in', ids)])
  330. return transfer.amount if(transfer) else 0.0
  331. ''' Get cashBox.output '''
  332. def confirm_pos_get_casbox_output(self,ids):
  333. _logger.info('Obtener los retiro de dinero .')
  334. output = self.env['cash.box.out'].search([('id', 'in', ids)])
  335. return output.amount if(output) else 0.0