eiru_statement_pos_confirm.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. if (amountStatement < 0):
  119. balanceNegative = self.confirm_pos_create_statement_line(bankStatement, "Ajuste saldo negativo PTV", abs(amountStatement), "Perdida")
  120. amountStatement = 0.0
  121. amountConfirm = (statement['amountConfirm']) - (amountStatement)
  122. name = "Ajuste de cierre de caja (POS)"
  123. referencia = "Ganancia" if(amountConfirm >= 0) else "Perdida"
  124. # referencia = "Ganancia" if((amountConfirm >= 0) and (amountStatement >= 0) ) else "Perdida"
  125. # import web_pdb; web_pdb.set_trace()
  126. balanceLine = self.confirm_pos_create_statement_line( bankStatement, name, amountConfirm, referencia)
  127. cashboxConfirm = {
  128. 'name': "CIERRE DE CAJA (%s)" %(bankStatement.name),
  129. 'date': dateServer,
  130. 'ref': "Cierre de caja (TPV)",
  131. 'statement_id': bankStatement.id,
  132. 'user_statement': bankStatement.user_id.id,
  133. 'user_confirm': user.id,
  134. 'journal_id' : bankStatement.journal_id.id,
  135. 'amount_statement': amountStatement,
  136. 'amount_real': statement['amountConfirm'],
  137. 'line_difference': balanceLine.id if (balanceLine) else '',
  138. 'amount_difference': balanceLine.amount if (balanceLine) else 0.0,
  139. 'amount_next_open': 0.0,
  140. 'state_avaliable': False,
  141. }
  142. statementConfirm = self.confirm_pos_create_cashbox_statement_confirm(cashboxConfirm, bankStatement)
  143. '''
  144. Salida de Dinero.
  145. '''
  146. if (statement['OutputPos']):
  147. #Crear Line en caja
  148. name = "Retiro de dinero"
  149. lineOutput = self.confirm_pos_create_statement_line( bankStatement, name, (- statement['amountOutput']), "TPV")
  150. # Crear Salida
  151. cashboxOutput = self.confirm_pos_create_cashbox_output(lineOutput, statement['amountOutput'], statementConfirm)
  152. '''
  153. Transferencia.
  154. '''
  155. if (statement['transferPos']):
  156. statementTransfer = self.confirm_pos_get_statement(statement['statementTransfer'])
  157. if (not statementTransfer):
  158. return {
  159. 'state': False,
  160. 'message': "Erro, no se pudo encontrar la caja destino de la transferencia."
  161. }
  162. ## Salida
  163. name = "TRANSFERENCIA/%s (Salida)" % (statementTransfer.name)
  164. transferOutput = self.confirm_pos_create_statement_line( bankStatement, name, (- statement['amountTransfer']), "TPV")
  165. ## Entrada
  166. name = "TRANSFERENCIA/%s (Entrada)" % (bankStatement.name)
  167. transferinput = self.confirm_pos_create_statement_line( statementTransfer, name, statement['amountTransfer'], "TPV")
  168. ## Transferencia
  169. transferencia = self.confirm_pos_create_cashbox_transfer("TPV", statement['amountTransfer'], transferOutput, transferinput, statementConfirm)
  170. '''
  171. Saldo para próxima apertura de caja.
  172. '''
  173. amountStatement = 0.0
  174. for line in bankStatement.line_ids:
  175. amountStatement += line.amount
  176. if (bankStatement.balance_start > 0):
  177. amountStatement += bankStatement.balance_start
  178. # import web_pdb; web_pdb.set_trace()
  179. if (amountStatement > 0.0):
  180. name="Saldo para próxima apertura de caja"
  181. lineNextOpen = self.confirm_pos_create_statement_line(bankStatement, name, (- amountStatement), "TPV")
  182. if (not lineNextOpen):
  183. return {
  184. 'state' : False,
  185. 'message': "Error, En el registro de saldo."
  186. }
  187. statementConfirm.write({
  188. 'line_next_open': lineNextOpen.id,
  189. 'amount_next_open': abs(amountStatement),
  190. 'state_avaliable': True,
  191. })
  192. return {
  193. 'state': True,
  194. 'message': "Operación exitosa"
  195. }
  196. ''' Get Statement '''
  197. def confirm_pos_get_statement(self,id):
  198. return self.env['account.bank.statement'].browse(id)
  199. ''' Create Line Statemen '''
  200. def confirm_pos_create_statement_line(self, statement, name, amount, ref):
  201. statementLine = {
  202. 'statement_id': statement.id,
  203. 'name': name,
  204. 'amount': amount,
  205. 'ref': ref,
  206. 'account_id': statement.journal_id.internal_account_id.id,
  207. 'journal_id': statement.journal_id.id,
  208. 'is_deleted': True
  209. }
  210. return self.env['account.bank.statement.line'].create(statementLine)
  211. ''' Create cashbox Confirm '''
  212. def confirm_pos_create_cashbox_statement_confirm(self,values, statement):
  213. casbox = self.env['cashbox.statement.confirm'].search([('statement_id.id', '=', statement.id)])
  214. if (not casbox):
  215. confirm = self.env['cashbox.statement.confirm'].create(values)
  216. else:
  217. confirm = casbox.write(values)
  218. if (confirm):
  219. confirm = self.env['cashbox.statement.confirm'].search([('statement_id.id', '=', statement.id)])
  220. return confirm
  221. ''' Create cashBox output '''
  222. def confirm_pos_create_cashbox_output(self, line, amount, confirm):
  223. cash = {
  224. 'name': "Retiro de dinero (%s)" % (line.statement_id.name),
  225. 'amount': amount,
  226. 'ref': 'TPV',
  227. 'date': line.date,
  228. 'line_id': line.id,
  229. 'statement_id': line.statement_id.id,
  230. 'cashbox_confirm_id': confirm.id
  231. }
  232. return self.env['cash.box.out'].create(cash)
  233. ''' Create cash box transfer '''
  234. def confirm_pos_create_cashbox_transfer(self, ref, amount, lineOutput, lineInput, confirm):
  235. cash = {
  236. 'name': "TRANSFERENCIA %s a %s" %((lineOutput.statement_id.name),(lineInput.statement_id.name)),
  237. 'amount': amount,
  238. 'ref': ref,
  239. 'input_line': lineInput.id,
  240. 'output_line': lineOutput.id,
  241. 'input_statement': lineInput.statement_id.id,
  242. 'output_statement': lineOutput.statement_id.id,
  243. 'date': lineOutput.date,
  244. 'cashbox_confirm_id': confirm.id,
  245. }
  246. return self.env['cash.box.transfer'].create(cash)
  247. '''
  248. ____ ____ ___ _ _ _____ ____ _____ _ _____ _____ __ __ _____ _ _ _____
  249. | _ \| _ \|_ _| \ | |_ _| / ___|_ _|/ \|_ _| ____| \/ | ____| \ | |_ _|
  250. | |_) | |_) || || \| | | | \___ \ | | / _ \ | | | _| | |\/| | _| | \| | | |
  251. | __/| _ < | || |\ | | | ___) || |/ ___ \| | | |___| | | | |___| |\ | | |
  252. |_| |_| \_\___|_| \_| |_| |____/ |_/_/ \_\_| |_____|_| |_|_____|_| \_| |_|
  253. '''
  254. ''' Get Pos Session Statemen Confirm Print'''
  255. @api.model
  256. def confirm_pos_get_statement_print(self, id):
  257. _logger.info('Generar resume de cierre de las caja')
  258. posStatement = []
  259. posSession = self.env['pos.session'].search([('id', '=', id),('state', '=', 'closed')])
  260. if (not posSession):
  261. return False
  262. statementIds = map(lambda x: x.id, posSession.statement_ids)
  263. statements = self.env['account.bank.statement'].search([('id', 'in', statementIds),('state', '=', 'confirm')])
  264. if (not statements):
  265. return False
  266. statemntPos = []
  267. for statement in statements:
  268. statementConfirm = []
  269. accountJournal = self.env['account.journal'].browse(statement.journal_id.id)
  270. ''' Verificar si existe cierre '''
  271. confirm = self.env['cashbox.statement.confirm'].search([('statement_id.id', '=', statement.id)])
  272. if (not confirm):
  273. amountStatement = 0.0
  274. for line in statement.line_ids:
  275. amountStatement += line.amount
  276. if (statement.balance_start > 0):
  277. amountStatement += statement.balance_start
  278. statementConfirm.append({
  279. 'amountStatement': amountStatement,
  280. 'amountConfirm': 0,
  281. 'amountDifference': 0,
  282. 'amountOutput': 0,
  283. 'amountTransfer': 0,
  284. 'amountNextOpen': 0,
  285. })
  286. if (confirm):
  287. amountDifference = self.confirm_pos_get_statement_line(confirm.line_difference.id)
  288. amountNextOpen = abs(self.confirm_pos_get_statement_line(confirm.line_next_open.id))
  289. transferID = map(lambda x: x.id, confirm.cashbox_transfer_ids)
  290. amountTransfer = self.confirm_pos_get_casbox_transfer(transferID)
  291. outputIds = map(lambda x: x.id, confirm.cashbox_output_ids)
  292. amountOutput =self.confirm_pos_get_casbox_output(outputIds)
  293. statementConfirm.append({
  294. 'amountStatement': confirm.amount_statement,
  295. 'amountConfirm': confirm.amount_real,
  296. 'amountDifference': amountDifference,
  297. 'amountNextOpen': amountNextOpen,
  298. 'amountTransfer': amountTransfer,
  299. 'amountOutput': amountOutput,
  300. })
  301. statemntPos.append({
  302. 'id': statement.id,
  303. 'name':statement.name,
  304. 'journalType': accountJournal.type,
  305. 'journalName': accountJournal.name,
  306. 'journalId': accountJournal.id,
  307. 'confirm': statementConfirm,
  308. 'currency':[{
  309. 'id': resCurrency.id,
  310. 'name': resCurrency.name,
  311. 'symbol': resCurrency.symbol,
  312. 'localName': resCurrency.local_name,
  313. 'rate': resCurrency.rate,
  314. 'thousandsSeparator': resCurrency.thousands_separator,
  315. 'decimalSeparator': resCurrency.decimal_separator,
  316. 'decimalPlaces': resCurrency.decimal_places,
  317. 'position': resCurrency.position,
  318. }for resCurrency in self.env['res.currency'].browse(statement.currency.id)]
  319. })
  320. posStatement.append({
  321. 'name': posSession.name,
  322. 'startAt': posSession.start_at,
  323. 'stopAt': posSession.stop_at,
  324. 'userName': posSession.user_id.name,
  325. 'statemnt': statemntPos
  326. })
  327. return posStatement
  328. ''' get amount statemnt Line '''
  329. def confirm_pos_get_statement_line(self, id):
  330. _logger.info('Obtener la linea de la caja.')
  331. line = self.env['account.bank.statement.line'].search([('id', '=', id)])
  332. return line.amount if(line) else 0.0
  333. ''' Get cashBox.transfer '''
  334. def confirm_pos_get_casbox_transfer(self,ids):
  335. _logger.info('Obtener la transferencia.')
  336. transfer = self.env['cash.box.transfer'].search([('id', 'in', ids)])
  337. return transfer.amount if(transfer) else 0.0
  338. ''' Get cashBox.output '''
  339. def confirm_pos_get_casbox_output(self,ids):
  340. _logger.info('Obtener los retiro de dinero .')
  341. output = self.env['cash.box.out'].search([('id', 'in', ids)])
  342. return output.amount if(output) else 0.0