main.py 9.6 KB

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