main.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 StringIO import StringIO
  8. from gzip import GzipFile
  9. from StringIO import StringIO as IO
  10. import simplejson as json
  11. import gzip
  12. import logging
  13. LOGGER = logging.getLogger(__name__)
  14. class Sales(http.Controller):
  15. '''
  16. Get server date
  17. '''
  18. def get_server_date(self):
  19. return datetime.now().strftime('%Y-%m-%d')
  20. '''
  21. Get current user information
  22. '''
  23. def get_user(self):
  24. user = request.env.user
  25. return {
  26. 'id': user.id,
  27. 'name': user.name,
  28. 'displayName': user.display_name,
  29. 'currency': {
  30. 'id': user.company_id.currency_id.id,
  31. 'name': user.company_id.currency_id.name,
  32. 'displayName': user.company_id.currency_id.display_name,
  33. 'symbol': user.company_id.currency_id.symbol
  34. }
  35. }
  36. '''
  37. Get currencies
  38. '''
  39. def get_currencies(self):
  40. return [{
  41. 'id': currency.id,
  42. 'name': currency.name,
  43. 'displayName': currency.display_name,
  44. 'base': currency.base,
  45. 'accuracy': currency.accuracy,
  46. 'rateSilent': currency.rate_silent,
  47. 'rounding': currency.rounding,
  48. 'symbol': currency.symbol,
  49. 'position': currency.position,
  50. 'decimalSeparator': currency.decimal_separator,
  51. 'decimalPlaces': currency.decimal_places,
  52. 'thousandsSeparator': currency.thousands_separator
  53. } for currency in request.env['res.currency'].search([('active', '=', True)])]
  54. '''
  55. Get all active journals
  56. '''
  57. def get_journals(self):
  58. return [{
  59. 'id': journal.id,
  60. 'name': journal.name,
  61. 'displayName': journal.display_name,
  62. 'code': journal.code,
  63. 'cashControl': journal.cash_control,
  64. 'type': journal.type,
  65. 'currency': {
  66. 'id': journal.currency.id,
  67. 'name': journal.currency.name,
  68. 'displayName': journal.currency.display_name
  69. },
  70. 'defaultCreditAccount': {
  71. 'id': journal.default_credit_account_id.id,
  72. 'name': journal.default_credit_account_id.name,
  73. 'displayName': journal.default_credit_account_id.display_name,
  74. 'code': journal.default_credit_account_id.code,
  75. 'exchangeRate': journal.default_credit_account_id.exchange_rate,
  76. 'foreignBalance': journal.default_credit_account_id.foreign_balance,
  77. 'reconcile': journal.default_credit_account_id.reconcile,
  78. 'debit': journal.default_credit_account_id.debit,
  79. 'credit': journal.default_credit_account_id.credit,
  80. 'currencyMode': journal.default_credit_account_id.currency_mode,
  81. 'companyCurrency': {
  82. 'id': journal.default_credit_account_id.company_currency_id.id,
  83. 'name': journal.default_credit_account_id.company_currency_id.name,
  84. 'displayName': journal.default_credit_account_id.company_currency_id.display_name,
  85. },
  86. 'currency': {
  87. 'id': journal.default_credit_account_id.currency_id.id,
  88. 'name': journal.default_credit_account_id.currency_id.name,
  89. 'displayName': journal.default_credit_account_id.currency_id.display_name
  90. },
  91. }
  92. } for journal in request.env['account.journal'].search([('type', 'in', ['bank', 'cash']), ('default_credit_account_id.currency_id', '=', False), ('active', '=', True)], order='id')]
  93. '''
  94. Get all active customers
  95. '''
  96. def get_customers(self):
  97. return [{
  98. 'id': customer.id,
  99. 'name': customer.name,
  100. 'displayName': customer.display_name,
  101. 'imageMedium': customer.image_medium,
  102. 'phone': customer.phone,
  103. 'mobile': customer.mobile,
  104. 'email': customer.email
  105. } for customer in request.env['res.partner'].search([('customer', '=', True), ('active', '=', True)])]
  106. '''
  107. Get all saleable and active products
  108. '''
  109. def get_products(self):
  110. return [{
  111. 'id': product.id,
  112. 'name': product.name,
  113. 'displayName': product.display_name,
  114. 'ean13': product.ean13,
  115. 'imageMedium': product.image_medium,
  116. 'listPrice': product.list_price,
  117. 'variantCount': product.product_variant_count,
  118. 'variants': [{
  119. 'id': variant.id,
  120. 'name': variant.name,
  121. 'displayName': variant.display_name,
  122. 'ean13': variant.ean13,
  123. 'imageMedium': variant.image_medium,
  124. 'listPrice': variant.list_price
  125. } for variant in product.product_variant_ids if variant.active]
  126. } for product in request.env['product.template'].search([('sale_ok', '=', True), ('list_price', '>', 0), ('active', '=', True)])]
  127. '''
  128. Get all active payment terms
  129. '''
  130. def get_payment_terms(self):
  131. return [{
  132. 'id': payment_term.id,
  133. 'name': payment_term.name,
  134. 'displayName': payment_term.display_name,
  135. 'lines': [{
  136. 'id': line.id,
  137. 'days': line.days,
  138. 'days2': line.days2,
  139. 'value': line.value,
  140. 'value_amount': line.value_amount
  141. } for line in payment_term.line_ids]
  142. } for payment_term in request.env['account.payment.term'].search([('active', '=', True)])]
  143. '''
  144. Make JSON response
  145. '''
  146. def make_json_response(self, data=None, status=200):
  147. return Response(json.dumps(data), status=status, content_type='application/json')
  148. '''
  149. Make GZIP to JSON response
  150. '''
  151. def make_gzip_response(self, data=None, status=200):
  152. gzip_buffer = IO()
  153. with GzipFile(mode='wb', compresslevel=9, fileobj=gzip_buffer) as gzip_file:
  154. gzip_file.write(json.dumps(data))
  155. contents = gzip_buffer.getvalue()
  156. gzip_buffer.close()
  157. headers = Headers()
  158. headers.add('Content-Encoding', 'gzip')
  159. headers.add('Vary', 'Accept-Encoding')
  160. headers.add('Content-Length', len(contents))
  161. return Response(contents, status=status, headers=headers, content_type='application/json')
  162. '''
  163. '''
  164. def make_info_log(self, log):
  165. LOGGER.info(log)
  166. '''
  167. New purchase resource route
  168. '''
  169. @http.route('/eiru_sales/init', auth='user', methods=['GET'], cors='*')
  170. def init_sale(self, **kw):
  171. self.make_info_log('Sending JSON response')
  172. return self.make_gzip_response({
  173. 'date': self.get_server_date(),
  174. 'user': self.get_user(),
  175. 'currencies': self.get_currencies(),
  176. 'journals': self.get_journals(),
  177. 'customers': self.get_customers(),
  178. 'products': self.get_products(),
  179. 'paymentTerms': self.get_payment_terms()
  180. })
  181. '''
  182. Create customer and return data
  183. '''
  184. @http.route('/eiru_sales/create_customer', type='json', auth='user', methods=['POST'], cors='*')
  185. def create_customer(self, **kw):
  186. self.make_info_log('Creating customer')
  187. customer = request.env['res.partner'].create({
  188. 'name': kw.get('name'),
  189. 'ruc': kw.get('ruc'),
  190. 'phone': kw.get('phone'),
  191. 'customer': True
  192. })
  193. return {
  194. 'id': customer.id,
  195. 'name': customer.name,
  196. 'displayName': customer.display_name,
  197. 'imageMedium': customer.image_medium,
  198. 'phone': customer.phone,
  199. 'mobile': customer.mobile,
  200. 'email': customer.email
  201. }
  202. '''
  203. Create product and return data
  204. '''
  205. @http.route('/eiru_sales/create_product', type='json', auth='user', methods=['POST'], cors='*')
  206. def create_product(self, **kw):
  207. self.make_info_log('Creating customer')
  208. product = request.env['product.template'].create({
  209. 'name': kw.get('name'),
  210. 'list_price': float(kw.get('price')),
  211. 'ean13': kw.get('ean13')
  212. })
  213. return {
  214. 'id': product.id,
  215. 'name': product.name,
  216. 'displayName': product.display_name,
  217. 'ean13': product.ean13,
  218. 'imageMedium': product.image_medium,
  219. 'listPrice': product.list_price,
  220. 'variantCount': product.product_variant_count,
  221. 'variants': [{
  222. 'id': variant.id,
  223. 'name': variant.name,
  224. 'displayName': variant.display_name,
  225. 'ean13': variant.ean13,
  226. 'imageMedium': variant.image_medium,
  227. 'listPrice': variant.list_price
  228. } for variant in product.product_variant_ids if variant.active]
  229. }