main.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 gzip import GzipFile
  8. from StringIO import StringIO as IO
  9. import simplejson as json
  10. import gzip
  11. import logging
  12. LOGGER = logging.getLogger(__name__)
  13. DATE_FORMAT = '%Y-%m-%d'
  14. GZIP_COMPRESSION_LEVEL = 9
  15. class PaymentsSales(http.Controller):
  16. '''
  17. Get server date
  18. '''
  19. def get_server_date(self):
  20. return datetime.now().strftime(DATE_FORMAT)
  21. '''
  22. Get partner customer
  23. '''
  24. def get_user(self):
  25. user = request.env.user
  26. return {
  27. 'id': user.id,
  28. 'name': user.name,
  29. 'displayName': user.display_name,
  30. 'company': {
  31. 'id': user.company_id.id,
  32. 'name': user.company_id.name
  33. },
  34. 'currency': {
  35. 'id': user.company_id.currency_id.id,
  36. 'name': user.company_id.currency_id.name,
  37. 'displayName': user.company_id.currency_id.display_name,
  38. 'symbol': user.company_id.currency_id.symbol,
  39. 'rateSilent': user.company_id.currency_id.rate_silent,
  40. 'thousandsSeparator': user.company_id.currency_id.thousands_separator,
  41. 'decimalSeparator': user.company_id.currency_id.decimal_separator,
  42. 'decimalPlaces': user.company_id.currency_id.decimal_places,
  43. 'position': user.company_id.currency_id.position
  44. }
  45. }
  46. '''
  47. Get Supplier
  48. '''
  49. def get_supplier(self):
  50. decimal_precision = request.env['decimal.precision'].precision_get('Account')
  51. return [{
  52. 'id': supplier.id,
  53. 'name': supplier.name,
  54. 'displayName': supplier.display_name,
  55. 'ruc': supplier.ruc,
  56. 'imageMedium': supplier.image_medium,
  57. 'phone': supplier.phone,
  58. 'mobile': supplier.mobile,
  59. 'email':supplier.email,
  60. 'debit': supplier.debit,
  61. 'invoices': [{
  62. 'id': invoice.id,
  63. 'number': invoice.number ,
  64. 'dateInvoice': invoice.date_invoice,
  65. 'amountTotal': invoice.amount_total,
  66. 'residual': invoice.residual,
  67. 'currencyInvoice': {
  68. 'id': invoice.currency_id.id,
  69. 'displayName': invoice.currency_id.display_name,
  70. 'symbol': invoice.currency_id.symbol,
  71. 'rateSilent': invoice.currency_id.rate_silent,
  72. 'rate': invoice.currency_id.rate,
  73. 'thousandsSeparator': invoice.currency_id.thousands_separator,
  74. 'decimalSeparator': invoice.currency_id.decimal_separator,
  75. 'decimalPlaces': invoice.currency_id.decimal_places,
  76. 'position': invoice.currency_id.position
  77. },
  78. 'moveLines' :[{
  79. 'id': line.id,
  80. 'amountResidual': line.amount_residual,
  81. 'credit': line.credit,
  82. 'debit': line.debit,
  83. 'dateMaturity': line.date_maturity,
  84. 'amountCurrency': (line.amount_currency * -1) if (line.amount_currency != 0) else line.credit,
  85. 'amountResidualCurrency': line.amount_residual_currency,
  86. 'currencyAmount': [{
  87. 'id': currency.id,
  88. 'displayName': currency.display_name,
  89. 'symbol': currency.symbol,
  90. 'rateSilent': currency.rate_silent,
  91. 'rate': currency.rate,
  92. 'thousandsSeparator': currency.thousands_separator,
  93. 'decimalSeparator': currency.decimal_separator,
  94. 'decimalPlaces': currency.decimal_places,
  95. 'position': currency.position,
  96. 'base': currency.base,
  97. 'accuracy': currency.accuracy,
  98. 'rounding': currency.rounding,
  99. 'amountCurencyResidual': round((line.amount_residual_currency * (currency.rate / invoice.currency_id.rate)), decimal_precision)
  100. } for currency in request.env['res.currency'].search([('active', '=', True)])]
  101. } for line in invoice.move_id.line_id if (line.amount_residual > 0 and line.state != "draft" and line.debit <= 0)],
  102. } for invoice in supplier.invoice_ids if (invoice.state == 'open') ],
  103. } for supplier in request.env['res.partner'].search([('active', '=', True), ('supplier', '=', True), ('debit', '<', 0) ])]
  104. '''
  105. Get Journal
  106. '''
  107. def get_journals(self):
  108. # domain =[('active', '=', True),('type', 'in',['bank', 'cash']), ('default_credit_account_id.currency_id', '=', False)]
  109. domain =[('active', '=', True),('type', 'in',['bank', 'cash'])]
  110. paymentsJournals = []
  111. for journal in request.env['account.journal'].search(domain, order="id"):
  112. if not (journal.store_ids >= request.env.user.store_ids):
  113. continue
  114. paymentsJournals.append({
  115. 'id': journal.id,
  116. 'name': journal.name,
  117. 'displayName': journal.display_name,
  118. 'code': journal.code,
  119. 'cashControl': journal.cash_control,
  120. 'type': journal.type,
  121. 'currency': {
  122. 'id': journal.currency.id,
  123. 'name': journal.currency.name,
  124. 'displayName': journal.currency.display_name,
  125. 'symbol': journal.currency.symbol,
  126. 'rateSilent': journal.currency.rate_silent,
  127. 'thousandsSeparator':journal.currency.thousands_separator,
  128. 'decimalSeparator': journal.currency.decimal_separator,
  129. 'decimalPlaces': journal.currency.decimal_places,
  130. 'position': journal.currency.position
  131. },
  132. 'defaultCreditAccount':{
  133. 'id': journal.default_credit_account_id.id,
  134. 'name': journal.default_credit_account_id.name,
  135. 'displayName': journal.default_credit_account_id.display_name,
  136. 'code': journal.default_credit_account_id.code,
  137. 'exchangeRate': journal.default_credit_account_id.exchange_rate,
  138. 'foreignBalance': journal.default_credit_account_id.foreign_balance,
  139. 'reconcile': journal.default_credit_account_id.reconcile,
  140. 'debit': journal.default_credit_account_id.debit,
  141. 'credit': journal.default_credit_account_id.credit,
  142. 'currencyMode': journal.default_credit_account_id.currency_mode,
  143. 'companyCurrency':{
  144. 'id': journal.default_credit_account_id.company_currency_id.id,
  145. 'name': journal.default_credit_account_id.company_currency_id.name,
  146. 'displayName': journal.default_credit_account_id.company_currency_id.display_name,
  147. 'symbol': journal.default_credit_account_id.company_currency_id.symbol,
  148. 'rateSilent': journal.default_credit_account_id.company_currency_id.rate_silent
  149. },
  150. 'currency':{
  151. 'id': journal.default_credit_account_id.currency_id.id,
  152. 'name': journal.default_credit_account_id.currency_id.name,
  153. 'displayName': journal.default_credit_account_id.currency_id.display_name,
  154. 'symbol': journal.default_credit_account_id.currency_id.symbol,
  155. 'rateSilent': journal.default_credit_account_id.currency_id.rate_silent
  156. },
  157. }
  158. })
  159. return paymentsJournals
  160. '''
  161. Get Currency
  162. '''
  163. def get_currency(self):
  164. return [{
  165. 'id': currency.id,
  166. 'name': currency.name,
  167. 'displayName': currency.display_name,
  168. 'base': currency.base,
  169. 'accuracy': currency.accuracy,
  170. 'rateSilent': currency.rate_silent,
  171. 'rounding': currency.rounding,
  172. 'symbol': currency.symbol,
  173. 'position': currency.position,
  174. 'thousandsSeparator': currency.thousands_separator,
  175. 'decimalSeparator': currency.decimal_separator,
  176. 'decimalPlaces': currency.decimal_places
  177. } for currency in request.env['res.currency'].search([('active', '=', True)])]
  178. '''
  179. Make JSON response
  180. '''
  181. def make_json_response(self, data=None, status=200):
  182. return Response(json.dumps(data), status=status, content_type='application/json')
  183. '''
  184. Make GZIP to JSON response
  185. '''
  186. def make_gzip_response(self, data=None, status=200):
  187. gzip_buffer = IO()
  188. with GzipFile(mode='wb', compresslevel=GZIP_COMPRESSION_LEVEL, fileobj=gzip_buffer) as gzip_file:
  189. gzip_file.write(json.dumps(data))
  190. contents = gzip_buffer.getvalue()
  191. gzip_buffer.close()
  192. headers = Headers()
  193. headers.add('Content-Encoding', 'gzip')
  194. headers.add('Vary', 'Accept-Encoding')
  195. headers.add('Content-Length', len(contents))
  196. return Response(contents, status=status, headers=headers, content_type='application/json')
  197. '''
  198. Logger Info
  199. '''
  200. def make_info_log(self, log):
  201. LOGGER.info(log)
  202. '''
  203. New Payments resource router
  204. '''
  205. @http.route('/eiru_payments_purchases/init', auth='user', methods=['GET'], cors='*')
  206. def init_payments(self, **kw):
  207. self.make_info_log('Sending JSON response')
  208. return self.make_gzip_response({
  209. 'date': self.get_server_date(),
  210. 'user': self.get_user(),
  211. 'supplier': self.get_supplier(),
  212. 'journals': self.get_journals(),
  213. 'currencies': self.get_currency()
  214. })