main.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. # -*- coding: utf-8 -*-
  2. from openerp import http
  3. from openerp.http import request
  4. from werkzeug.wrappers import Response
  5. from werkzeug.datastructures import Headers
  6. from datetime import datetime
  7. from dateutil.relativedelta import relativedelta as rd
  8. from dateutil.parser import parse
  9. from pytz import timezone
  10. from gzip import GzipFile
  11. from StringIO import StringIO as IO
  12. import simplejson as json
  13. import gzip
  14. import logging
  15. LOGGER = logging.getLogger(__name__)
  16. DATE_FORMAT = '%Y-%m-%d'
  17. DATETIME_FORMAT = '%Y-%m-%d %H:%m:%S'
  18. GZIP_COMPRESSION_LEVEL = 9
  19. class Statement(http.Controller):
  20. ''' Get timezone '''
  21. def get_timezone(self):
  22. return timezone(request.context['tz'])
  23. ''' Get server date to send '''
  24. def get_server_date(self):
  25. return datetime.now(self.get_timezone()).strftime(DATE_FORMAT)
  26. ''' Get current user information '''
  27. def get_user(self):
  28. user = request.env.user
  29. return {
  30. 'id': user.id,
  31. 'name': user.name,
  32. 'displayName': user.display_name,
  33. 'currency': {
  34. 'id': user.company_id.currency_id.id,
  35. 'name': user.company_id.currency_id.name,
  36. 'displayName': user.company_id.currency_id.display_name,
  37. 'symbol': user.company_id.currency_id.symbol
  38. },
  39. 'company': {
  40. 'id': user.company_id.id,
  41. 'name': user.company_id.name,
  42. 'displayName': user.company_id.display_name
  43. }
  44. }
  45. ''' Get currencies from journals '''
  46. def get_currencies_from_journal(self):
  47. domain = [
  48. ('type', 'in', ['bank', 'cash']),
  49. # ('default_debit_account_id.currency_id', '=', False),
  50. ('active', '=', True)
  51. ]
  52. currencies = []
  53. for j in request.env['account.journal'].search(domain):
  54. c = j.currency or j.company_id.currency_id
  55. currencies.append({
  56. 'id': c.id,
  57. 'name': c.display_name,
  58. 'base': c.base,
  59. 'symbol': c.symbol,
  60. 'position': c.position,
  61. 'rateSilent': c.rate_silent,
  62. 'decimalSeparator': c.decimal_separator,
  63. 'decimalPlaces': c.decimal_places,
  64. 'thousandsSeparator': c.thousands_separator
  65. })
  66. return {c['id']:c for c in currencies}.values()
  67. ''' all active journals '''
  68. def get_journals(self, type=None):
  69. if (not type[0]):
  70. type= ['bank', 'cash']
  71. user = request.env.user
  72. journal = []
  73. for store in user.store_ids:
  74. for journalID in store.journal_ids:
  75. if (journalID.type in ['bank', 'cash']):
  76. journal.append(journalID.id)
  77. # domain = [('type', 'in', type), ('default_debit_account_id.currency_id', '=', False), ('active', '=', True)]
  78. domain = [('type', 'in', type),('active', '=', True)]
  79. if (journal):
  80. domain.append(('id', 'in', journal ))
  81. return [{
  82. 'id': journal.id,
  83. 'name': journal.name,
  84. 'displayName': journal.display_name,
  85. 'code': journal.code,
  86. 'cashControl': journal.cash_control,
  87. 'type': journal.type,
  88. 'storeIds': map(lambda x: x.id, journal.store_ids),
  89. 'currency': {
  90. 'id': journal.currency.id,
  91. 'name': journal.currency.name,
  92. 'displayName': journal.currency.display_name
  93. },
  94. 'defaultDebitAccount': {
  95. 'id': journal.default_debit_account_id.id,
  96. 'name': journal.default_debit_account_id.name,
  97. 'displayName': journal.default_debit_account_id.display_name,
  98. 'code': journal.default_debit_account_id.code,
  99. 'exchange_rate': journal.default_credit_account_id.exchange_rate,
  100. 'foreignBalance': journal.default_credit_account_id.foreign_balance,
  101. 'reconcile': journal.default_credit_account_id.reconcile,
  102. 'debit': journal.default_credit_account_id.debit,
  103. 'credit': journal.default_credit_account_id.credit,
  104. 'currencyMode': journal.default_credit_account_id.currency_mode,
  105. 'companyCurrency': {
  106. 'id': journal.default_credit_account_id.company_currency_id.id,
  107. 'name': journal.default_credit_account_id.company_currency_id.name,
  108. 'displayName': journal.default_credit_account_id.company_currency_id.display_name,
  109. },
  110. 'currency': {
  111. 'id': journal.default_credit_account_id.currency_id.id,
  112. 'name': journal.default_credit_account_id.currency_id.name,
  113. 'displayName': journal.default_credit_account_id.currency_id.display_name
  114. }
  115. }
  116. } for journal in request.env['account.journal'].search(domain, order='id')]
  117. ''' account.bank.statement '''
  118. def get_account_bank_statement(self, journalIds=None):
  119. if (not journalIds):
  120. return False
  121. domain = [('journal_id.id', 'in', journalIds)]
  122. # domain = [('journal_id.id', 'in', journalIds), ('user_id', '=', request.env.user.id)]
  123. return [{
  124. 'id': statement.id,
  125. 'name': statement.name,
  126. 'date': statement.date,
  127. 'state': statement.state,
  128. 'balanceEnd': statement.balance_end,
  129. 'user': {
  130. 'id': statement.user_id.id,
  131. 'name': statement.user_id.name,
  132. 'displayName': statement.user_id.display_name
  133. },
  134. 'userSession': request.env.user.id,
  135. 'journal': {
  136. 'id': statement.journal_id.id,
  137. 'name': statement.journal_id.name,
  138. 'displayName': statement.journal_id.display_name,
  139. 'code': statement.journal_id.code,
  140. 'cashControl': statement.journal_id.cash_control,
  141. 'type': statement.journal_id.type,
  142. 'currency': {
  143. 'id': statement.journal_id.currency.id,
  144. 'name': statement.journal_id.currency.name,
  145. 'displayName': statement.journal_id.currency.display_name
  146. }
  147. },
  148. 'line': [{
  149. 'id': line.id,
  150. 'date': line.date,
  151. 'name': line.name,
  152. 'ref': line.ref,
  153. 'amount': line.amount,
  154. 'patner':{
  155. 'id': line.partner_id.id,
  156. 'name': line.partner_id.name,
  157. 'displayName': line.partner_id.display_name
  158. },
  159. } for line in statement.line_ids],
  160. 'typeStatement': {
  161. 'id': statement.type_statement.id,
  162. 'name': statement.type_statement.name,
  163. 'code': statement.type_statement.code
  164. },
  165. 'currency':{
  166. 'id': statement.currency.id,
  167. 'name': statement.currency.display_name,
  168. 'base': statement.currency.base,
  169. 'symbol': statement.currency.symbol,
  170. 'position': statement.currency.position,
  171. 'rateSilent': statement.currency.rate_silent,
  172. 'decimalSeparator': statement.currency.decimal_separator,
  173. 'decimalPlaces': statement.currency.decimal_places,
  174. 'thousandsSeparator': statement.currency.thousands_separator
  175. }
  176. } for statement in request.env['account.bank.statement'].search(domain)]
  177. ''' Configuracion de Caja '''
  178. def get_account_bank_statement_config(self):
  179. 'account.bank.statement.config'
  180. return [{
  181. 'transfer': {
  182. 'userIds': map(lambda x: x.id, config.transfer_user_ids),
  183. 'statementIds': map(lambda x: x.id, config.transfer_statement_ids) ,
  184. 'negativeAmount': config.transfer_negative_amount
  185. },
  186. 'inputCashBox': {
  187. 'userIds': map(lambda x: x.id, config.input_cash_box_user_id),
  188. 'statementIds': map(lambda x: x.id, config.input_cash_box_statement_ids)
  189. },
  190. 'outputCashBox': {
  191. 'userIds': map(lambda x: x.id, config.output_cash_box_user_id),
  192. 'statementIds': map(lambda x: x.id, config.output_cash_box_statement_ids),
  193. 'negativeAmount': config.output_negative_amount
  194. },
  195. 'delete': {
  196. 'outputUserIds': map(lambda x: x.id, config.delete_output_user_ids),
  197. 'inputUserIds': map(lambda x: x.id, config.delete_input_user_ids),
  198. 'transferUserIds': map(lambda x: x.id, config.delete_transfer_user_ids)
  199. },
  200. 'statementOpen': config.statement_open_config,
  201. 'statementConfirm' :{
  202. 'userIds': map(lambda x: x.id, config.statement_confirm_user),
  203. 'transferUserIds': map(lambda x: x.id , config.statement_confirm_transfer_user),
  204. 'balanceUserIds': map(lambda x: x.id, config.statement_confirm_balance),
  205. 'negativeAmountUserIds': map(lambda x: x.id, config.statement_confirm_negative_amount)
  206. },
  207. 'statementCancelUserIds': map(lambda x: x.id, config.statement_cancel_user),
  208. 'statementUnlinkUserIds': map(lambda x: x.id, config.statement_unlink_user)
  209. } for config in request.env['account.bank.statement.config'].search([('active', '=', True)],order='id')]
  210. ''' Make JSON response to send '''
  211. def make_response(self, data=None, status=200):
  212. return Response(json.dumps(data), status=status, content_type='application/json')
  213. ''' Make GZIP to JSON response '''
  214. def make_gzip_response(self, data=None, status=200):
  215. gzip_buffer = IO()
  216. with GzipFile(mode='wb', compresslevel=GZIP_COMPRESSION_LEVEL, fileobj=gzip_buffer) as gzip_file:
  217. gzip_file.write(json.dumps(data))
  218. contents = gzip_buffer.getvalue()
  219. gzip_buffer.close()
  220. headers = Headers()
  221. headers.add('Content-Encoding', 'gzip')
  222. headers.add('Vary', 'Accept-Encoding')
  223. headers.add('Content-Length', len(contents))
  224. return Response(contents, status=status, headers=headers, content_type='application/json')
  225. ''' Logger info '''
  226. def make_info_log(self, log):
  227. LOGGER.info('\033[1;34m[INFO] --> \033[m{}'.format(log))
  228. '''
  229. ██ ███ ██ ██ ████████
  230. ██ ████ ██ ██ ██
  231. ██ ██ ██ ██ ██ ██
  232. ██ ██ ██ ██ ██ ██
  233. ██ ██ ████ ██ ██
  234. '''
  235. @http.route('/eiru_bank_statement/init', auth='user', methods=['GET'], cors='*')
  236. def init_bank_statement(self,**kw):
  237. from account_bank_statement_type import get_account_bank_statement_type
  238. from res_store import get_res_store
  239. from res_users import get_res_users
  240. self.make_info_log('Preparing data to {}'.format(kw.get('mode')))
  241. store = get_res_store(kw.get('mode'))
  242. return self.make_gzip_response({
  243. 'date': self.get_server_date(),
  244. 'user': self.get_user(),
  245. 'currencies': self.get_currencies_from_journal(),
  246. 'journals': self.get_journals([kw.get('mode')]),
  247. # 'statement': self.get_account_bank_statement(map(lambda x: x['id'], journals[0])),
  248. 'statement': self.get_account_bank_statement(store['journalIds']),
  249. 'statementConfig' : self.get_account_bank_statement_config(),
  250. 'statementType': get_account_bank_statement_type(),
  251. 'resUsers': get_res_users(store['userIds'])
  252. })
  253. '''
  254. ██████ █████ ███████ ██ ██ ██████ ██████ ██ ██ ███ ███ ██████ ██ ██ ███████
  255. ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ████ ██ ██ ██ ██ ██
  256. ██ ███████ ███████ ███████ ██████ ██ ██ ███ ██ ████ ██ ██ ██ ██ ██ █████
  257. ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
  258. ██████ ██ ██ ███████ ██ ██ ██████ ██████ ██ ██ ██ ██ ██████ ████ ███████
  259. * Transfer.
  260. * Input.
  261. * Output.
  262. '''
  263. @http.route('/eiru_bank_statement/create_cashbox_move', type='json', auth='user', methods=['POST'],cors='*')
  264. def _create_cashbox_move(self, **kw):
  265. from account_bank_statement_line import create_statement_line
  266. from cashbox_transfer import create_cashbox_transfer
  267. from cashbox_input import create_cashbox_input
  268. from cashbox_output import create_cashbox_output
  269. data = kw.get('data')
  270. ''' Transferencia '''
  271. if (data['transfer']):
  272. return create_cashbox_transfer(data)
  273. ''' Entrada de Dinero '''
  274. if (data['input']):
  275. return create_cashbox_input(data)
  276. ''' Salida de Dinero '''
  277. if (data['output']):
  278. return create_cashbox_output(data)
  279. '''
  280. ██████ ██████ ███████ █████ ████████ ███████
  281. ██ ██ ██ ██ ██ ██ ██ ██
  282. ██ ██████ █████ ███████ ██ █████
  283. ██ ██ ██ ██ ██ ██ ██ ██
  284. ██████ ██ ██ ███████ ██ ██ ██ ███████ Statement
  285. '''
  286. @http.route('/eiru_bank_statement/create_account_bank_statement', type='json', auth='user', methods=['POST'],cors='*')
  287. def _create_account_bank_statement(self, **kw):
  288. from account_bank_statement import create_account_bank_statement
  289. return create_account_bank_statement(kw.get('data'))
  290. '''
  291. ██████ ██ ██████ ███████ ██ ███ ██ ██████
  292. ██ ██ ██ ██ ██ ██ ████ ██ ██
  293. ██ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ███
  294. ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
  295. ██████ ███████ ██████ ███████ ██ ██ ████ ██████ STATEMENT
  296. '''
  297. @http.route('/eiru_bank_statement/process_closing_statement', type='json', auth='user', methods=['POST'],cors='*')
  298. def _process_closing_statement(self, **kw):
  299. from account_bank_statement import closing_statement
  300. return closing_statement(kw.get('data'))