main.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. 'quantity': 1,
  119. 'price': product.list_price,
  120. 'discount': 0,
  121. 'variants': [{
  122. 'id': variant.id,
  123. 'name': variant.name,
  124. 'displayName': variant.display_name,
  125. 'ean13': variant.ean13,
  126. 'imageMedium': variant.image_medium,
  127. 'listPrice': variant.list_price
  128. } for variant in product.product_variant_ids if variant.active]
  129. } for product in request.env['product.template'].search([('sale_ok', '=', True), ('list_price', '>', 0), ('active', '=', True)])]
  130. '''
  131. Get all active payment terms
  132. '''
  133. def get_payment_terms(self):
  134. return [{
  135. 'id': payment_term.id,
  136. 'name': payment_term.name,
  137. 'displayName': payment_term.display_name,
  138. 'lines': [{
  139. 'id': line.id,
  140. 'days': line.days,
  141. 'days2': line.days2,
  142. 'value': line.value,
  143. 'value_amount': line.value_amount
  144. } for line in payment_term.line_ids]
  145. } for payment_term in request.env['account.payment.term'].search([('active', '=', True)])]
  146. '''
  147. Make JSON response
  148. '''
  149. def make_json_response(self, data=None, status=200):
  150. return Response(json.dumps(data), status=status, content_type='application/json')
  151. '''
  152. Make GZIP to JSON response
  153. '''
  154. def make_gzip_response(self, data=None, status=200):
  155. gzip_buffer = IO()
  156. with GzipFile(mode='wb', compresslevel=9, fileobj=gzip_buffer) as gzip_file:
  157. gzip_file.write(json.dumps(data))
  158. contents = gzip_buffer.getvalue()
  159. gzip_buffer.close()
  160. headers = Headers()
  161. headers.add('Content-Encoding', 'gzip')
  162. headers.add('Vary', 'Accept-Encoding')
  163. headers.add('Content-Length', len(contents))
  164. return Response(contents, status=status, headers=headers, content_type='application/json')
  165. '''
  166. '''
  167. def make_info_log(self, log):
  168. LOGGER.info(log)
  169. '''
  170. New purchase resource route
  171. '''
  172. @http.route('/eiru_sales/init', auth='user', methods=['GET'], cors='*')
  173. def init_sale(self, **kw):
  174. self.make_info_log('Sending JSON response')
  175. return self.make_gzip_response({
  176. 'date': self.get_server_date(),
  177. 'user': self.get_user(),
  178. 'currencies': self.get_currencies(),
  179. 'journals': self.get_journals(),
  180. 'customers': self.get_customers(),
  181. 'products': self.get_products(),
  182. 'paymentTerms': self.get_payment_terms()
  183. })
  184. '''
  185. Create customer and return data
  186. '''
  187. @http.route('/eiru_sales/create_customer', type='json', auth='user', methods=['POST'], cors='*')
  188. def create_customer(self, **kw):
  189. self.make_info_log('Creating customer')
  190. customer = request.env['res.partner'].create({
  191. 'name': kw.get('name'),
  192. 'ruc': kw.get('ruc'),
  193. 'phone': kw.get('phone'),
  194. 'customer': True
  195. })
  196. return {
  197. 'id': customer.id,
  198. 'name': customer.name,
  199. 'displayName': customer.display_name,
  200. 'imageMedium': customer.image_medium,
  201. 'phone': customer.phone,
  202. 'mobile': customer.mobile,
  203. 'email': customer.email
  204. }
  205. '''
  206. Create product and return data
  207. '''
  208. @http.route('/eiru_sales/create_product', type='json', auth='user', methods=['POST'], cors='*')
  209. def create_product(self, **kw):
  210. self.make_info_log('Creating customer')
  211. product = request.env['product.template'].create({
  212. 'name': kw.get('name'),
  213. 'list_price': float(kw.get('price')),
  214. 'ean13': kw.get('ean13')
  215. })
  216. return {
  217. 'id': product.id,
  218. 'name': product.name,
  219. 'displayName': product.display_name,
  220. 'ean13': product.ean13,
  221. 'imageMedium': product.image_medium,
  222. 'listPrice': product.list_price,
  223. 'variantCount': product.product_variant_count,
  224. 'quantity': 1,
  225. 'price': product.list_price,
  226. 'discount': 0,
  227. 'variants': [{
  228. 'id': variant.id,
  229. 'name': variant.name,
  230. 'displayName': variant.display_name,
  231. 'ean13': variant.ean13,
  232. 'imageMedium': variant.image_medium,
  233. 'listPrice': variant.list_price
  234. } for variant in product.product_variant_ids if variant.active]
  235. }