Ver código fonte

[ADD] http controllers

Gogs 7 anos atrás
pai
commit
b8e37bcc1b
3 arquivos alterados com 224 adições e 2 exclusões
  1. 1 2
      .gitignore
  2. 2 0
      controllers/__init__.py
  3. 221 0
      controllers/main.py

+ 1 - 2
.gitignore

@@ -1,4 +1,3 @@
 *.pyc
 node_modules
-static/src
-controllers
+static/src

+ 2 - 0
controllers/__init__.py

@@ -0,0 +1,2 @@
+# -*- coding: utf-8 -*-
+import main

+ 221 - 0
controllers/main.py

@@ -0,0 +1,221 @@
+# -*- coding: utf-8 -*-
+from openerp import http
+from openerp.http import request
+from werkzeug.wrappers import Response
+from werkzeug.datastructures import Headers
+from datetime import datetime
+from StringIO import StringIO
+from gzip import GzipFile
+from StringIO import StringIO as IO
+import simplejson as json
+import gzip
+import logging
+
+LOGGER = logging.getLogger(__name__)
+
+class Sales(http.Controller):
+
+    '''
+        Get server date
+    '''
+    def get_server_date(self):
+        return datetime.now().strftime('%Y-%m-%d')
+
+    '''
+        Get current user information
+    '''
+    def get_user(self):
+        user = request.env.user
+
+        return {
+            'id': user.id,
+            'name': user.name,
+            'displayName': user.display_name,
+            'currency': {
+                'id': user.company_id.currency_id.id,
+                'name': user.company_id.currency_id.name,
+                'displayName': user.company_id.currency_id.display_name,
+                'symbol': user.company_id.currency_id.symbol
+            }
+        }
+
+    '''
+        Get currencies
+    '''
+    def get_currencies(self):
+        return [{
+            'id': currency.id,
+            'name': currency.name,
+            'displayName': currency.display_name,
+            'base': currency.base,
+            'accuracy': currency.accuracy,
+            'rateSilent': currency.rate_silent,
+            'rounding': currency.rounding,
+            'symbol': currency.symbol,
+            'position': currency.position,
+            'decimalSeparator': currency.decimal_separator,
+            'decimalPlaces': currency.decimal_places,
+            'thousandsSeparator': currency.thousands_separator
+        } for currency in request.env['res.currency'].search([('active', '=', True)])]
+
+    '''
+        Get all active journals
+    '''
+    def get_journals(self):
+        return [{
+            'id': journal.id,
+            'name': journal.name,
+            'displayName': journal.display_name,
+            'code': journal.code,
+            'cashControl': journal.cash_control,
+            'type': journal.type,
+            'currency': {
+                'id': journal.currency.id,
+                'name': journal.currency.name,
+                'displayName': journal.currency.display_name
+            },
+            'defaultCreditAccount': {
+                'id': journal.default_credit_account_id.id,
+                'name': journal.default_credit_account_id.name,
+                'displayName': journal.default_credit_account_id.display_name,
+                'code': journal.default_credit_account_id.code,
+                'exchangeRate': journal.default_credit_account_id.exchange_rate,
+                'foreignBalance': journal.default_credit_account_id.foreign_balance,
+                'reconcile': journal.default_credit_account_id.reconcile,
+                'debit': journal.default_credit_account_id.debit,
+                'credit': journal.default_credit_account_id.credit,
+                'currencyMode': journal.default_credit_account_id.currency_mode,
+                'companyCurrency': {
+                    'id': journal.default_credit_account_id.company_currency_id.id,
+                    'name': journal.default_credit_account_id.company_currency_id.name,
+                    'displayName': journal.default_credit_account_id.company_currency_id.display_name,
+                },
+                'currency': {
+                    'id': journal.default_credit_account_id.currency_id.id,
+                    'name': journal.default_credit_account_id.currency_id.name,
+                    'displayName': journal.default_credit_account_id.currency_id.display_name
+                },
+            },
+            'defaultDebitAccount': {
+                'id': journal.default_debit_account_id.id,
+                'name': journal.default_debit_account_id.name,
+                'displayName': journal.default_debit_account_id.display_name,
+                'code': journal.default_debit_account_id.code,
+                'exchange_rate': journal.default_credit_account_id.exchange_rate,
+                'foreignBalance': journal.default_credit_account_id.foreign_balance,
+                'reconcile': journal.default_credit_account_id.reconcile,
+                'debit': journal.default_credit_account_id.debit,
+                'credit': journal.default_credit_account_id.credit,
+                'currencyMode': journal.default_credit_account_id.currency_mode,
+                'companyCurrency': {
+                    'id': journal.default_credit_account_id.company_currency_id.id,
+                    'name': journal.default_credit_account_id.company_currency_id.name,
+                    'displayName': journal.default_credit_account_id.company_currency_id.display_name,
+                },
+                'currency': {
+                    'id': journal.default_credit_account_id.currency_id.id,
+                    'name': journal.default_credit_account_id.currency_id.name,
+                    'displayName': journal.default_credit_account_id.currency_id.display_name
+                }
+
+            }
+        } for journal in request.env['account.journal'].search([('type', 'in', ['bank', 'cash']), ('default_debit_account_id.currency_id', '=', False), ('active', '=', True)], order='id')]
+    
+    '''
+        Get all active customers
+    '''
+    def get_customers(self):
+        return [{
+            'id': customer.id,
+            'name': customer.name,
+            'displayName': customer.display_name,
+            'imageMedium': customer.image_medium,
+            'phone': customer.phone,
+            'mobile': customer.mobile,
+            'email': customer.email
+        } for customer in request.env['res.partner'].search([('customer', '=', True), ('active', '=', True)])]
+
+    '''
+        Get all saleable and active products
+    '''
+    def get_products(self):
+        return [{
+            'id': product.id,
+            'name': product.name,
+            'displayName': product.display_name,
+            'ean13': product.ean13,
+            'imageMedium': product.image_medium,
+            'listPrice': product.list_price,
+            'variantCount': product.product_variant_count,
+            'variants': [{
+                'id': variant.id,
+                'name': variant.name,
+                'displayName': variant.display_name,
+                'ean13': variant.ean13,
+                'imageMedium': variant.image_medium,
+                'listPrice': variant.list_price
+            } for variant in product.product_variant_ids if variant.active]
+        } for product in request.env['product.template'].search([('sale_ok', '=', True), ('list_price', '>', 0), ('active', '=', True)])]
+
+    '''
+        Get all active payment terms
+    '''
+    def get_payment_terms(self):
+        return [{
+            'id': payment_term.id,
+            'name': payment_term.name,
+            'displayName': payment_term.display_name,
+            'lines': [{
+                'id': line.id,
+                'days': line.days,
+                'days2': line.days2,
+                'value': line.value,
+                'value_amount': line.value_amount
+            } for line in payment_term.line_ids]
+        } for payment_term in request.env['account.payment.term'].search([('active', '=', True)])]
+
+    '''
+        Make JSON response
+    '''
+    def make_json_response(self, data=None, status=200):
+        return Response(json.dumps(data), status=status, content_type='application/json')
+
+    '''
+        Make GZIP to JSON response
+    '''
+    def make_gzip_response(self, data=None, status=200):
+        gzip_buffer = IO()
+        with GzipFile(mode='wb', compresslevel=9, fileobj=gzip_buffer) as gzip_file:
+            gzip_file.write(json.dumps(data))
+        
+        contents = gzip_buffer.getvalue()
+        gzip_buffer.close()
+
+        headers = Headers()
+        headers.add('Content-Encoding', 'gzip')
+        headers.add('Vary', 'Accept-Encoding')
+        headers.add('Content-Length', len(contents))
+
+        return Response(contents, status=status, headers=headers, content_type='application/json')
+
+    '''
+    '''
+    def make_info_log(self, log):
+        LOGGER.info(log)
+
+    '''
+        New purchase resource route
+    '''
+    @http.route('/eiru_sales/new', auth='user', methods=['GET'], cors='*')
+    def init_sale(self, **kw):
+        self.make_info_log('Sending JSON response')
+    
+        return self.make_gzip_response({
+            'date': self.get_server_date(),
+            'user': self.get_user(),
+            'currencies': self.get_currencies(),
+            'journals': self.get_journals(),
+            'customers': self.get_customers(),
+            'products': self.get_products(),
+            'paymentTerms': self.get_payment_terms()
+        })