main.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. 'invoiceNumber': invoice.number,
  81. 'amountResidual': line.amount_residual,
  82. 'credit': line.credit,
  83. 'debit': line.debit,
  84. 'dateMaturity': line.date_maturity,
  85. 'amountCurrency': (line.amount_currency * -1) if (line.amount_currency != 0) else line.credit,
  86. 'amountResidualCurrency': line.amount_residual_currency,
  87. 'currencyAmount': [{
  88. 'id': currency.id,
  89. 'displayName': currency.display_name,
  90. 'symbol': currency.symbol,
  91. 'rateSilent': currency.rate_silent,
  92. 'rate': currency.rate,
  93. 'thousandsSeparator': currency.thousands_separator,
  94. 'decimalSeparator': currency.decimal_separator,
  95. 'decimalPlaces': currency.decimal_places,
  96. 'position': currency.position,
  97. 'base': currency.base,
  98. 'accuracy': currency.accuracy,
  99. 'rounding': currency.rounding,
  100. 'amountCurencyResidual': round((line.amount_residual_currency * (currency.rate / invoice.currency_id.rate)), decimal_precision)
  101. } for currency in request.env['res.currency'].search([('active', '=', True)])]
  102. } for line in invoice.move_id.line_id if (line.amount_residual > 0 and line.state != "draft" and line.debit <= 0)],
  103. } for invoice in supplier.invoice_ids if (invoice.state == 'open') ],
  104. } for supplier in request.env['res.partner'].search([('active', '=', True), ('supplier', '=', True), ('debit', '<', 0) ])]
  105. '''
  106. Get Journal
  107. '''
  108. def get_journals(self):
  109. # domain =[('active', '=', True),('type', 'in',['bank', 'cash']), ('default_credit_account_id.currency_id', '=', False)]
  110. domain =[('active', '=', True),('type', 'in',['bank', 'cash'])]
  111. paymentsJournals = []
  112. for journal in request.env['account.journal'].search(domain, order="id"):
  113. if not (journal.store_ids >= request.env.user.store_ids):
  114. continue
  115. paymentsJournals.append({
  116. 'id': journal.id,
  117. 'name': journal.name,
  118. 'displayName': journal.display_name,
  119. 'code': journal.code,
  120. 'cashControl': journal.cash_control,
  121. 'type': journal.type,
  122. 'currency': {
  123. 'id': journal.currency.id,
  124. 'name': journal.currency.name,
  125. 'displayName': journal.currency.display_name,
  126. 'symbol': journal.currency.symbol,
  127. 'rateSilent': journal.currency.rate_silent,
  128. 'thousandsSeparator':journal.currency.thousands_separator,
  129. 'decimalSeparator': journal.currency.decimal_separator,
  130. 'decimalPlaces': journal.currency.decimal_places,
  131. 'position': journal.currency.position
  132. },
  133. 'defaultCreditAccount':{
  134. 'id': journal.default_credit_account_id.id,
  135. 'name': journal.default_credit_account_id.name,
  136. 'displayName': journal.default_credit_account_id.display_name,
  137. 'code': journal.default_credit_account_id.code,
  138. 'exchangeRate': journal.default_credit_account_id.exchange_rate,
  139. 'foreignBalance': journal.default_credit_account_id.foreign_balance,
  140. 'reconcile': journal.default_credit_account_id.reconcile,
  141. 'debit': journal.default_credit_account_id.debit,
  142. 'credit': journal.default_credit_account_id.credit,
  143. 'currencyMode': journal.default_credit_account_id.currency_mode,
  144. 'companyCurrency':{
  145. 'id': journal.default_credit_account_id.company_currency_id.id,
  146. 'name': journal.default_credit_account_id.company_currency_id.name,
  147. 'displayName': journal.default_credit_account_id.company_currency_id.display_name,
  148. 'symbol': journal.default_credit_account_id.company_currency_id.symbol,
  149. 'rateSilent': journal.default_credit_account_id.company_currency_id.rate_silent
  150. },
  151. 'currency':{
  152. 'id': journal.default_credit_account_id.currency_id.id,
  153. 'name': journal.default_credit_account_id.currency_id.name,
  154. 'displayName': journal.default_credit_account_id.currency_id.display_name,
  155. 'symbol': journal.default_credit_account_id.currency_id.symbol,
  156. 'rateSilent': journal.default_credit_account_id.currency_id.rate_silent
  157. },
  158. }
  159. })
  160. return paymentsJournals
  161. '''
  162. Get Currency
  163. '''
  164. def get_currency(self):
  165. return [{
  166. 'id': currency.id,
  167. 'name': currency.name,
  168. 'displayName': currency.display_name,
  169. 'base': currency.base,
  170. 'accuracy': currency.accuracy,
  171. 'rateSilent': currency.rate_silent,
  172. 'rounding': currency.rounding,
  173. 'symbol': currency.symbol,
  174. 'position': currency.position,
  175. 'thousandsSeparator': currency.thousands_separator,
  176. 'decimalSeparator': currency.decimal_separator,
  177. 'decimalPlaces': currency.decimal_places
  178. } for currency in request.env['res.currency'].search([('active', '=', True)])]
  179. '''
  180. Make JSON response
  181. '''
  182. def make_json_response(self, data=None, status=200):
  183. return Response(json.dumps(data), status=status, content_type='application/json')
  184. '''
  185. Make GZIP to JSON response
  186. '''
  187. def make_gzip_response(self, data=None, status=200):
  188. gzip_buffer = IO()
  189. with GzipFile(mode='wb', compresslevel=GZIP_COMPRESSION_LEVEL, fileobj=gzip_buffer) as gzip_file:
  190. gzip_file.write(json.dumps(data))
  191. contents = gzip_buffer.getvalue()
  192. gzip_buffer.close()
  193. headers = Headers()
  194. headers.add('Content-Encoding', 'gzip')
  195. headers.add('Vary', 'Accept-Encoding')
  196. headers.add('Content-Length', len(contents))
  197. return Response(contents, status=status, headers=headers, content_type='application/json')
  198. '''
  199. Logger Info
  200. '''
  201. def make_info_log(self, log):
  202. LOGGER.info(log)
  203. '''
  204. New Payments resource router
  205. '''
  206. @http.route('/eiru_payments_purchases/init', auth='user', methods=['GET'], cors='*')
  207. def init_payments(self, **kw):
  208. self.make_info_log('Sending JSON response')
  209. return self.make_gzip_response({
  210. 'date': self.get_server_date(),
  211. 'user': self.get_user(),
  212. 'supplier': self.get_supplier(),
  213. 'journals': self.get_journals(),
  214. 'currencies': self.get_currency()
  215. })