123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- # -*- 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 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
- },
- }
- } for journal in request.env['account.journal'].search([('type', 'in', ['bank', 'cash']), ('default_credit_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,
- 'quantity': 1,
- 'price': product.list_price,
- 'discount': 0,
- 'variants': [{
- 'id': variant.id,
- 'name': variant.name,
- 'displayName': variant.display_name,
- 'ean13': variant.ean13,
- 'imageMedium': variant.image_medium,
- 'listPrice': variant.list_price,
- 'quantity': 1,
- 'price': variant.list_price,
- 'discount': 0,
- } 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/init', 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()
- })
-
- '''
- Create customer and return data
- '''
- @http.route('/eiru_sales/create_customer', type='json', auth='user', methods=['POST'], cors='*')
- def create_customer(self, **kw):
- self.make_info_log('Creating customer')
- customer = request.env['res.partner'].create({
- 'name': kw.get('name'),
- 'ruc': kw.get('ruc'),
- 'phone': kw.get('phone'),
- 'customer': True
- })
- return {
- 'id': customer.id,
- 'name': customer.name,
- 'displayName': customer.display_name,
- 'imageMedium': customer.image_medium,
- 'phone': customer.phone,
- 'mobile': customer.mobile,
- 'email': customer.email
- }
-
- '''
- Create product and return data
- '''
- @http.route('/eiru_sales/create_product', type='json', auth='user', methods=['POST'], cors='*')
- def create_product(self, **kw):
- self.make_info_log('Creating customer')
- product = request.env['product.template'].create({
- 'name': kw.get('name'),
- 'list_price': float(kw.get('price')),
- 'ean13': kw.get('ean13')
- })
- 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,
- 'quantity': 1,
- 'price': product.list_price,
- 'discount': 0,
- 'variants': [{
- 'id': variant.id,
- 'name': variant.name,
- 'displayName': variant.display_name,
- 'ean13': variant.ean13,
- 'imageMedium': variant.image_medium,
- 'listPrice': variant.list_price,
- 'quantity': 1,
- 'price': variant.list_price,
- 'discount': 0,
- } for variant in product.product_variant_ids if variant.active]
- }
-
- '''
- Create sale
- '''
- @http.route('/eiru_sales/create_sale', type='json', auth='user', methods=['POST'], cors='*')
- def create_sale(self, **kw):
- print(kw)
|