cashbox_statement_cancel.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. # -*- coding: utf-8 -*-
  2. from openerp import models, fields, tools, api
  3. from datetime import datetime
  4. from pytz import timezone
  5. import openerp.addons.decimal_precision as dp
  6. DATE_FORMAT = '%Y-%m-%d'
  7. class eiru_account_statement_cancel(models.Model):
  8. _inherit = 'account.bank.statement'
  9. @api.model
  10. def eiru_get_bank_statement_cancel(self, id):
  11. return [{
  12. 'id': statement.id,
  13. 'name': statement.name,
  14. 'state': statement.state,
  15. 'date': statement.date,
  16. 'currencystatement': {
  17. 'id': statement.currency.id,
  18. 'symbol': statement.currency.symbol,
  19. 'rate': statement.currency.rate,
  20. 'thousandsSeparator': statement.currency.thousands_separator,
  21. 'decimalSeparator': statement.currency.decimal_separator,
  22. 'decimalPlaces': statement.currency.decimal_places,
  23. 'position': statement.currency.position,
  24. },
  25. 'user': {
  26. 'id': statement.user_id.id,
  27. 'name': statement.user_id.name
  28. },
  29. 'statementTransfer': [{
  30. 'id': transfer.id,
  31. 'name': transfer.name,
  32. 'amount': transfer.amount,
  33. 'ref': transfer.ref,
  34. 'date': transfer.date,
  35. 'outputStatement':{
  36. 'id': transfer.output_statement.id,
  37. 'name': transfer.output_statement.name,
  38. 'state': transfer.output_statement.state,
  39. 'statementUser': "%s/%s" % ((transfer.output_statement.name), (transfer.output_statement.user_id.name))
  40. },
  41. 'inputStatement':{
  42. 'id': transfer.input_statement.id,
  43. 'name': transfer.input_statement.name,
  44. 'state': transfer.input_statement.state,
  45. 'statementUser': "%s/%s" % ((transfer.input_statement.name), (transfer.input_statement.user_id.name))
  46. },
  47. 'outputLineId': transfer.output_line.id,
  48. 'inputLineId': transfer.input_line.id,
  49. 'type': 'in' if (transfer.input_statement.id == statement.id) else 'out'
  50. } for transfer in self.env['cash.box.transfer'].search(['|', ('output_statement.id','=',statement.id), ('input_statement.id','=',statement.id)])],
  51. 'statementConfirm': [{
  52. 'id': casBox.id,
  53. 'name': casBox.name,
  54. 'amountStatement': casBox.amount_statement,
  55. 'amountReal': casBox.amount_real,
  56. 'amountDifference': casBox.amount_difference,
  57. 'amountNextOpen': casBox.amount_next_open,
  58. 'type': 'open' if (casBox.statement_open.id == statement.id) else 'confirm',
  59. 'statementConfirm':{
  60. 'id': casBox.statement_id.id if (casBox.statement_id) else '',
  61. 'name': casBox.statement_id.name if (casBox.statement_id) else '',
  62. 'state': casBox.statement_id.state if (casBox.statement_id) else '',
  63. 'statementUser': "%s/%s" % ((casBox.statement_id.name), (casBox.statement_id.user_id.name)) if (casBox.statement_id) else ''
  64. },
  65. 'statementOpen':{
  66. 'id': casBox.statement_open.id if (casBox.statement_open) else '',
  67. 'name': casBox.statement_open.name if (casBox.statement_open) else '',
  68. 'state': casBox.statement_open.state if (casBox.statement_open) else '',
  69. 'statementUser': "%s/%s" % ((casBox.statement_open.name), (casBox.statement_open.user_id.name)) if (casBox.statement_open) else ''
  70. },
  71. 'lineDifference': [{
  72. 'id': lineDifference.id,
  73. 'date': lineDifference.date,
  74. 'name': lineDifference.name ,
  75. 'amount': lineDifference.amount,
  76. 'state': lineDifference.statement_id.state,
  77. 'type': 'balance'
  78. } for lineDifference in self.env['account.bank.statement.line'].browse(casBox.line_difference.id)],
  79. 'lineNextOpen': [{
  80. 'id': lineNextOpen.id,
  81. 'date': lineNextOpen.date,
  82. 'name': lineNextOpen.name ,
  83. 'amount': lineNextOpen.amount,
  84. 'state': casBox.statement_open.state if (casBox.statement_open) else '',
  85. 'type': 'reserve'
  86. } for lineNextOpen in self.env['account.bank.statement.line'].browse(casBox.line_next_open.id)]
  87. } for casBox in self.env['cashbox.statement.confirm'].search([('statement_id.id','=',statement.id)])],
  88. } for statement in self.env['account.bank.statement'].search([('id','=',id)])]
  89. '''
  90. ____ _____ _ _____ _____ __ __ _____ _ _ _____ ____ _ _ _ ____ _____ _
  91. / ___|_ _|/ \|_ _| ____| \/ | ____| \ | |_ _| / ___| / \ | \ | |/ ___| ____| |
  92. \___ \ | | / _ \ | | | _| | |\/| | _| | \| | | | | | / _ \ | \| | | | _| | |
  93. ___) || |/ ___ \| | | |___| | | | |___| |\ | | | | |___ / ___ \| |\ | |___| |___| |___
  94. |____/ |_/_/ \_\_| |_____|_| |_|_____|_| \_| |_| \____/_/ \_\_| \_|\____|_____|_____|
  95. '''
  96. '''
  97. Get Statement
  98. '''
  99. def _eiru_get_account_bank_statement(self, id):
  100. return self.env['account.bank.statement'].browse(id)
  101. '''
  102. Get Statement Line
  103. '''
  104. def _eiru_get_account_bank_statement_line(self, id):
  105. return self.env['account.bank.statement.line'].browse(id)
  106. '''
  107. Get CashBox statement Confirm
  108. '''
  109. def _eiru_account_get_cashbox_confirm(self, statementId):
  110. return self.env['cashbox.statement.confirm'].search([('statement_id', '=', statementId)])
  111. '''
  112. statement Cancel
  113. '''
  114. @api.model
  115. def eiru_statement_cancel(self, values):
  116. ### date server
  117. dateServer = self.get_server_datetime()
  118. ### Statement
  119. statement = self._eiru_get_account_bank_statement(values['statementId'])
  120. if (not statement):
  121. return {
  122. 'state': False,
  123. 'messege': "Error en obtener el registro de caja ref(account.bank.statement)."
  124. }
  125. ### user
  126. users = self.env.user
  127. if (not users):
  128. return {
  129. 'state': False,
  130. 'messege':"Error en obtener el usuario. ref(res.users) "
  131. }
  132. # Creatre Line cancel
  133. line_ids = []
  134. for line in statement.line_ids:
  135. line_ids.append([0, False, {
  136. 'statement_line': line.id,
  137. 'date': line.date,
  138. 'name': line.name,
  139. 'ref': line.ref,
  140. 'partner_id': line.partner_id.id,
  141. 'amount': line.amount,
  142. 'currency_id': line.currency_id.id
  143. }])
  144. # create cancel
  145. cancel = {
  146. 'name': "Cancelación de caja "+str(statement.name),
  147. 'ref': values['messege'],
  148. 'date': dateServer,
  149. 'message': values['messege'],
  150. 'statement_id': statement.id,
  151. 'journal_id': statement.journal_id.id,
  152. 'user_statement': statement.user_id.id,
  153. 'user_cancel': users.id,
  154. 'line_ids': line_ids
  155. }
  156. statementCancel = self.env['cashbox.statement.cancel'].create(cancel)
  157. if (not statementCancel):
  158. return {
  159. 'state': False,
  160. 'messege': 'Error en el registro de cancelación de caja. ref(cashbox.statement.cancel /cashbox.statement.cancel.line).'
  161. }
  162. '''
  163. Confirm
  164. '''
  165. statementOpen = []
  166. stateOpen = []
  167. lineDifference = []
  168. lineNextOpen = []
  169. lineOpen = []
  170. cashBoxConfirm = self._eiru_account_get_cashbox_confirm(statement.id)
  171. if (cashBoxConfirm):
  172. statementOpen = self._eiru_get_account_bank_statement(cashBoxConfirm.statement_open.id)
  173. if (statementOpen and statementOpen.state =='confirm' and cashBoxConfirm.statement_open.id != cashBoxConfirm.statement_id.id ):
  174. return {
  175. 'state': False,
  176. 'messege': "No se puede reabrir esta caja porque tiene relación a una caja que ya esta cerrada."
  177. }
  178. stateOpen = statementOpen.state
  179. # Line Diferencia
  180. lineDifference = self._eiru_get_account_bank_statement_line(cashBoxConfirm.line_difference.id)
  181. # line Next Open
  182. lineNextOpen = self._eiru_get_account_bank_statement_line(cashBoxConfirm.line_next_open.id)
  183. # Line Open
  184. lineOpen = self._eiru_get_account_bank_statement_line(cashBoxConfirm.line_open.id)
  185. '''
  186. Cancel statement
  187. '''
  188. cancel = self.eiru_button_cancel(statement)
  189. if (not cancel):
  190. return {
  191. 'state': False,
  192. 'messege': "Error al cancelar la caja"
  193. }
  194. if (lineDifference):
  195. self._write_statement_line_transfer(lineDifference)
  196. self._unlink_statement_line_transfer(lineDifference)
  197. if (lineNextOpen):
  198. self._write_statement_line_transfer(lineNextOpen)
  199. self._unlink_statement_line_transfer(lineNextOpen)
  200. if (lineOpen):
  201. statementOpenCancel = self.eiru_button_cancel(statementOpen)
  202. if (not statementOpenCancel):
  203. return {
  204. 'state': False,
  205. 'messege': "Error al cancelar la caja"
  206. }
  207. self._write_statement_line_transfer(lineOpen)
  208. self._unlink_statement_line_transfer(lineOpen)
  209. if (stateOpen == 'open'):
  210. statementOpen.button_open()
  211. '''
  212. Update Confirm
  213. '''
  214. confirm = {
  215. 'state_avaliable': True,
  216. 'statement_open': '',
  217. 'amount_real': 0.0,
  218. 'amount_difference': 0.0,
  219. 'amount_next_open': 0.0
  220. }
  221. confirmCasBox = self._writer_cashbox_statement_confirm(cashBoxConfirm,confirm)
  222. if (not confirmCasBox):
  223. return {
  224. 'state': False,
  225. 'messege': 'Error en actualizar cierre de caja ref(cashbox.statement.confirm)'
  226. }
  227. open = statement.button_open()
  228. if (not open):
  229. return {
  230. 'state': False,
  231. 'messege': 'Error en reabrir la caja ref('+statement+')'
  232. }
  233. return {
  234. 'state': True,
  235. 'messege': 'Caja reabierta'
  236. }
  237. '''
  238. Cancelar Caja
  239. '''
  240. def eiru_button_cancel(self,statement):
  241. return super(eiru_account_statement_cancel, statement.with_context(cancel_from_statement=True)).button_cancel()
  242. '''
  243. Histórico de Cajas Canceladas.
  244. '''
  245. class cashbox_statement_cancel(models.Model):
  246. _name = "cashbox.statement.cancel"
  247. ### fields Basic
  248. name = fields.Char('name')
  249. ref = fields.Char('Ref')
  250. date = fields.Date()
  251. message = fields.Text('Message Cancel')
  252. active = fields.Boolean('Active', default=True)
  253. message_deleted = fields.Text('Message Deleted')
  254. ### statement, journal, users
  255. statement_id = fields.Many2one('account.bank.statement', 'Bank Statement Cancel')
  256. journal_id = fields.Many2one('account.journal', 'Journal')
  257. user_statement = fields.Many2one('res.users', 'User Statement')
  258. user_cancel = fields.Many2one('res.users', 'Usaer Cancel')
  259. line_ids = fields.One2many('cashbox.statement.cancel.line', 'cancel_id', 'Cancel Lines')
  260. class cashbox_statement_cancel_line(models.Model):
  261. _name = "cashbox.statement.cancel.line"
  262. ### cashbox.statement.cancel
  263. cancel_id = fields.Many2one('cashbox.statement.cancel', 'Statement Cancel')
  264. statement_line = fields.Many2one('account.bank.statement.line', 'statement Line')
  265. date = fields.Date()
  266. name = fields.Char('name')
  267. ref = fields.Char('Ref')
  268. partner_id = fields.Many2one('res.partner', 'partner')
  269. amount = fields.Float('Amount Line', digits_compute=dp.get_precision('Account'))
  270. currency_id = fields.Many2one('res.currency', string="Currency", help="Moneda de la operación")