account_bank_statement.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. # -*- coding: utf-8 -*-
  2. from openerp.http import request
  3. from eiru_logging import make_info_log
  4. _MODEL = 'account.bank.statement'
  5. ''' '''
  6. def search_account_bank_statement(statementId):
  7. make_info_log('GET Account Bank Statement')
  8. casbox= []
  9. statement = request.env['account.bank.statement'].browse(statementId)
  10. if (statement):
  11. casbox = {
  12. 'id': statement.id,
  13. 'name': statement.name,
  14. 'accountId': statement.journal_id.internal_account_id.id,
  15. 'journalId': statement.journal_id.id,
  16. 'balanceEnd': statement.balance_end
  17. }
  18. return casbox
  19. ''' account.bank.statement all'''
  20. def get_account_bank_statement( journalIds=None):
  21. make_info_log('GET Account Bank Statement all ')
  22. if (not journalIds):
  23. return False
  24. domain = [('journal_id.id', 'in', journalIds),('state', '=', 'open')]
  25. # domain = [('journal_id.id', 'in', journalIds), ('user_id', '=', request.env.user.id)]
  26. return [{
  27. 'id': statement.id,
  28. 'name': statement.name,
  29. 'date': statement.date,
  30. 'state': statement.state,
  31. 'balanceEnd': statement.balance_end,
  32. 'user': {
  33. 'id': statement.user_id.id,
  34. 'name': statement.user_id.name,
  35. 'displayName': statement.user_id.display_name
  36. },
  37. 'userSession': request.env.user.id,
  38. 'journal': {
  39. 'id': statement.journal_id.id,
  40. 'name': statement.journal_id.name,
  41. 'displayName': statement.journal_id.display_name,
  42. 'code': statement.journal_id.code,
  43. 'cashControl': statement.journal_id.cash_control,
  44. 'type': statement.journal_id.type,
  45. 'currency': {
  46. 'id': statement.journal_id.currency.id,
  47. 'name': statement.journal_id.currency.name,
  48. 'displayName': statement.journal_id.currency.display_name
  49. }
  50. },
  51. 'line': [{
  52. 'id': line.id,
  53. 'date': line.date,
  54. 'name': line.name,
  55. 'ref': line.ref,
  56. 'amount': line.amount,
  57. 'partnerId': line.partner_id.id,
  58. 'partner':[{
  59. 'id': partner.id,
  60. 'name': partner.name,
  61. 'displayName': partner.display_name
  62. }for partner in request.env['res.partner'].browse(line.partner_id.id)],
  63. } for line in statement.line_ids],
  64. 'typeStatement': {
  65. 'id': statement.type_statement.id,
  66. 'name': statement.type_statement.name,
  67. 'code': statement.type_statement.code
  68. },
  69. 'currency': {
  70. 'id': statement.currency.id,
  71. 'name': statement.currency.display_name,
  72. 'base': statement.currency.base,
  73. 'symbol': statement.currency.symbol,
  74. 'position': statement.currency.position,
  75. 'rateSilent': statement.currency.rate_silent,
  76. 'decimalSeparator': statement.currency.decimal_separator,
  77. 'decimalPlaces': statement.currency.decimal_places,
  78. 'thousandsSeparator': statement.currency.thousands_separator
  79. }
  80. } for statement in request.env['account.bank.statement'].search(domain)]
  81. '''Create Account Bank Statement '''
  82. def create_account_bank_statement(data):
  83. make_info_log('Create Account Bank Statement')
  84. from account_bank_statement_config import get_bank_statement_config
  85. config = get_bank_statement_config()
  86. if (not config):
  87. return {
  88. 'state': False,
  89. 'message': 'No existe configuración de caja'
  90. }
  91. domain = [('journal_id', '=', data['journal_id']),('type_statement', '=', data['type_statement']),('user_id', '=', data['user_id']),('state', '=', 'open')]
  92. statementVerify = request.env[_MODEL].search(domain)
  93. if ((not config.statement_open_config) and (statementVerify)):
  94. return {
  95. 'state': False,
  96. 'message': 'Ya existe una caja abierta con los mismos parámetros'
  97. }
  98. ''' Create CashBox '''
  99. statement = request.env[_MODEL].sudo().create(data)
  100. if(not statement):
  101. return {
  102. 'state': False,
  103. 'message': 'No fue posible crear la caja'
  104. }
  105. ''' Open cashBox '''
  106. openStatement = statement.sudo().button_open()
  107. if (not openStatement):
  108. return {
  109. 'state': False,
  110. 'message': 'No fue posible abrir la caja '
  111. }
  112. return {
  113. 'state': openStatement,
  114. 'message': 'Caja creada con éxito',
  115. 'data' : {
  116. 'id': statement.id,
  117. 'name': statement.name,
  118. 'date': statement.date,
  119. 'state': statement.state,
  120. 'balanceEnd': statement.balance_end,
  121. 'user': {
  122. 'id': statement.user_id.id,
  123. 'name': statement.user_id.name,
  124. 'displayName': statement.user_id.display_name
  125. },
  126. 'userSession': request.env.user.id,
  127. 'journal': {
  128. 'id': statement.journal_id.id,
  129. 'name': statement.journal_id.name,
  130. 'displayName': statement.journal_id.display_name,
  131. 'code': statement.journal_id.code,
  132. 'cashControl': statement.journal_id.cash_control,
  133. 'type': statement.journal_id.type,
  134. 'currency': {
  135. 'id': statement.journal_id.currency.id,
  136. 'name': statement.journal_id.currency.name,
  137. 'displayName': statement.journal_id.currency.display_name
  138. }
  139. },
  140. 'line': [{
  141. 'id': line.id,
  142. 'date': line.date,
  143. 'name': line.name,
  144. 'ref': line.ref,
  145. 'amount': line.amount,
  146. 'patner':{
  147. 'id': line.partner_id.id,
  148. 'name': line.partner_id.name,
  149. 'displayName': line.partner_id.display_name
  150. },
  151. } for line in statement.line_ids],
  152. 'typeStatement': {
  153. 'id': statement.type_statement.id,
  154. 'name': statement.type_statement.name,
  155. 'code': statement.type_statement.code
  156. },
  157. 'currency':{
  158. 'id': statement.currency.id,
  159. 'name': statement.currency.display_name,
  160. 'base': statement.currency.base,
  161. 'symbol': statement.currency.symbol,
  162. 'position': statement.currency.position,
  163. 'rateSilent': statement.currency.rate_silent,
  164. 'decimalSeparator': statement.currency.decimal_separator,
  165. 'decimalPlaces': statement.currency.decimal_places,
  166. 'thousandsSeparator': statement.currency.thousands_separator
  167. }
  168. }
  169. }
  170. ''' confirm bank Statement '''
  171. def closing_statement(data):
  172. make_info_log('Confirm Account Bank Statement')
  173. from account_bank_statement_line import create_statement_line
  174. from cashbox_confirm import get_cashbox_statement_confirm, create_cashbox_statement_confirm, write_cashbox_statement_confirm
  175. from server_datetime import get_date
  176. lineDifference = []
  177. lineclosing = []
  178. statement = request.env[_MODEL].browse(data['statementId'])
  179. if (not statement):
  180. return {
  181. 'state': False,
  182. 'message': 'No fue posible localizar la caja.'
  183. }
  184. amountStatement = statement.balance_end or 0.0
  185. ''' Registrar Diferencia entre saldos '''
  186. if (statement.balance_end != data['amount']):
  187. lineDifference = create_statement_line({
  188. 'statement_id': statement.id,
  189. 'name': "Diferencia entre valor teórico de cierre y valor real de cierre.",
  190. 'amount': data['amount'] - statement.balance_end,
  191. 'ref': 'AJUSTE DE CIERRE',
  192. 'account_id': statement.journal_id.internal_account_id.id,
  193. 'journal_id': statement.journal_id.id,
  194. # 'is_deleted': True
  195. })
  196. if (not lineDifference):
  197. return {
  198. 'state': False,
  199. 'message': 'No fue posible registrar el ajuste de cierre.'
  200. }
  201. ''' Resgitro de saldo '''
  202. if (statement.balance_end != 0):
  203. lineclosing = create_statement_line({
  204. 'statement_id': statement.id,
  205. 'name': "Registro de saldo para próxima apertura.",
  206. 'amount': - statement.balance_end if (statement.balance_end > 0) else abs(statement.balance_end),
  207. 'ref': 'CIERRE DE CAJA',
  208. 'account_id': statement.journal_id.internal_account_id.id,
  209. 'journal_id': statement.journal_id.id,
  210. # 'is_deleted': True
  211. })
  212. if (not lineclosing):
  213. return {
  214. 'state': False,
  215. 'message': 'No fue posible registrar el cierre de caja.'
  216. }
  217. amountNexOpen = abs(lineclosing.amount) if (lineclosing.amount < 0 ) else - (lineclosing.amount)
  218. # import web_pdb; web_pdb.set_trace()
  219. ''' Registro de Cierre de caja '''
  220. statementConfirm = get_cashbox_statement_confirm([('statement_id.id', '=', statement.id)])
  221. cashboxConfirm = {
  222. 'name': "CIERRE DE CAJA /"+str(statement.name),
  223. 'date': get_date(),
  224. 'ref': data['description'],
  225. 'statement_id': statement.id,
  226. 'user_statement': statement.user_id.id,
  227. 'user_confirm': request.env.user.id,
  228. 'journal_id' : statement.journal_id.id,
  229. 'amount_statement': amountStatement,
  230. 'amount_real': data['amount'],
  231. 'line_difference': lineDifference.id if (lineDifference) else '',
  232. 'amount_difference': lineDifference.amount if (lineDifference) else 0.0,
  233. 'line_next_open': lineclosing.id if(lineclosing) else '',
  234. 'amount_next_open': amountNexOpen if(lineclosing) else 0.0,
  235. 'state_avaliable': True if(lineclosing and abs(lineclosing.amount) > 0) else False,
  236. }
  237. confirm = create_cashbox_statement_confirm(cashboxConfirm) if (not statementConfirm) else write_cashbox_statement_confirm(statementConfirm.id, cashboxConfirm)
  238. if (not confirm):
  239. return {
  240. 'state': False,
  241. 'message': 'No fue posible crear el cierre de caja'
  242. }
  243. response = statement.button_confirm_cash()
  244. if (not response):
  245. return {
  246. 'state': False,
  247. 'message': 'No fue posible cerrar la caja'
  248. }
  249. return {
  250. 'state': True,
  251. 'message': 'Cierre de caja, realizada con éxito.'
  252. }