|
@@ -2,11 +2,19 @@
|
|
|
from openerp import http
|
|
|
from openerp.http import request
|
|
|
from werkzeug.wrappers import Response
|
|
|
+from werkzeug.datastructures import Headers
|
|
|
from datetime import datetime
|
|
|
+from dateutil.relativedelta import relativedelta as rd
|
|
|
+from dateutil.parser import parse
|
|
|
+from gzip import GzipFile
|
|
|
+from StringIO import StringIO as IO
|
|
|
import simplejson as json
|
|
|
+import gzip
|
|
|
import logging
|
|
|
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
+DATE_FORMAT = '%Y-%m-%d'
|
|
|
+GZIP_COMPRESSION_LEVEL = 9
|
|
|
|
|
|
class Purchases(http.Controller):
|
|
|
|
|
@@ -14,7 +22,7 @@ class Purchases(http.Controller):
|
|
|
Get server date to send
|
|
|
'''
|
|
|
def get_server_date(self):
|
|
|
- return datetime.now().strftime('%Y-%m-%d')
|
|
|
+ return datetime.now().strftime(DATE_FORMAT)
|
|
|
|
|
|
'''
|
|
|
Get current user information
|
|
@@ -31,9 +39,17 @@ class Purchases(http.Controller):
|
|
|
'name': user.company_id.currency_id.name,
|
|
|
'displayName': user.company_id.currency_id.display_name,
|
|
|
'symbol': user.company_id.currency_id.symbol
|
|
|
+ },
|
|
|
+ 'company': {
|
|
|
+ 'id': user.company_id.id,
|
|
|
+ 'name': user.company_id.name,
|
|
|
+ 'displayName': user.company_id.display_name
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ '''
|
|
|
+ Get currencies
|
|
|
+ '''
|
|
|
def get_currencies(self):
|
|
|
return [{
|
|
|
'id': currency.id,
|
|
@@ -41,10 +57,13 @@ class Purchases(http.Controller):
|
|
|
'displayName': currency.display_name,
|
|
|
'base': currency.base,
|
|
|
'accuracy': currency.accuracy,
|
|
|
- 'position': currency.position,
|
|
|
- 'rate': currency.rate,
|
|
|
+ 'rateSilent': currency.rate_silent,
|
|
|
'rounding': currency.rounding,
|
|
|
- 'symbol': currency.symbol
|
|
|
+ '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)])]
|
|
|
|
|
|
'''
|
|
@@ -63,28 +82,6 @@ class Purchases(http.Controller):
|
|
|
'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,
|
|
@@ -136,16 +133,20 @@ class Purchases(http.Controller):
|
|
|
'imageMedium': product.image_medium,
|
|
|
'standardPrice': product.standard_price,
|
|
|
'variantCount': product.product_variant_count,
|
|
|
+ 'quantity': 1,
|
|
|
+ 'price': product.standard_price,
|
|
|
'variants': [{
|
|
|
'id': variant.id,
|
|
|
'name': variant.name,
|
|
|
'displayName': variant.display_name,
|
|
|
'ean13': variant.ean13,
|
|
|
'imageMedium': variant.image_medium,
|
|
|
- 'standardPrice': variant.standard_price
|
|
|
+ 'standardPrice': variant.standard_price,
|
|
|
+ 'quantity': 1,
|
|
|
+ 'price': variant.standard_price
|
|
|
} for variant in product.product_variant_ids if variant.active]
|
|
|
} for product in request.env['product.template'].search([('purchase_ok', '=', True), ('standard_price', '>', 0), ('active', '=', True)])]
|
|
|
-
|
|
|
+
|
|
|
'''
|
|
|
Get all incoming and active picking types
|
|
|
'''
|
|
@@ -169,7 +170,7 @@ class Purchases(http.Controller):
|
|
|
'days': line.days,
|
|
|
'days2': line.days2,
|
|
|
'value': line.value,
|
|
|
- 'value_amount': line.value_amount
|
|
|
+ 'valueAmount': line.value_amount
|
|
|
} for line in payment_term.line_ids]
|
|
|
} for payment_term in request.env['account.payment.term'].search([('active', '=', True)])]
|
|
|
|
|
@@ -179,16 +180,34 @@ class Purchases(http.Controller):
|
|
|
def make_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=GZIP_COMPRESSION_LEVEL, 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)
|
|
|
+ LOGGER.info('[INFO]' + log)
|
|
|
|
|
|
'''
|
|
|
New purchase resource route
|
|
|
'''
|
|
|
- @http.route('/eiru_purchases/new', auth='user', methods=['GET'], cors='*')
|
|
|
- def new_purchase(self, **kw):
|
|
|
+ @http.route('/eiru_purchases/init', auth='user', methods=['GET'], cors='*')
|
|
|
+ def init_purchase(self, **kw):
|
|
|
self.make_info_log('Sending JSON response')
|
|
|
|
|
|
return self.make_response({
|
|
@@ -254,29 +273,10 @@ class Purchases(http.Controller):
|
|
|
'displayName': variant.display_name,
|
|
|
'ean13': variant.ean13,
|
|
|
'imageMedium': variant.image_medium,
|
|
|
- 'listPrice': variant.list_price
|
|
|
+ 'standardPrice': variant.standard_price
|
|
|
} for variant in product.product_variant_ids if variant.active]
|
|
|
}
|
|
|
|
|
|
- '''
|
|
|
- Purchase processing resource route
|
|
|
- '''
|
|
|
- @http.route('/eiru_purchases/create', type='json', auth='user', methods=['POST'], cors='*')
|
|
|
- def process_purchase(self, **kw):
|
|
|
- self.make_info_log('Creating purchase')
|
|
|
-
|
|
|
- # Step 1: Select currency
|
|
|
- currency_id = self.get_currency_id(int(kw.get('journal')))
|
|
|
-
|
|
|
- # Step 2: Save purchase
|
|
|
-
|
|
|
- print(currency_id)
|
|
|
-
|
|
|
- return {
|
|
|
- 'status': 'ok'
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
'''
|
|
|
Get currency id based on journal
|
|
|
'''
|
|
@@ -284,19 +284,63 @@ class Purchases(http.Controller):
|
|
|
journal = request.env['account.journal'].browse(journal_id)
|
|
|
return journal.default_debit_account_id.currency_id.id or journal.default_debit_account_id.company_currency_id.id
|
|
|
|
|
|
+
|
|
|
'''
|
|
|
- Save purchase and return it
|
|
|
+ Check currency in pricelist and return it
|
|
|
'''
|
|
|
- def create_purchase(self, partner_id, order_lines, currency_id=None, payment_term_id=None):
|
|
|
- purchase_order = request.env['purchase.order'].create({
|
|
|
- 'partner_id': partner_id,
|
|
|
+ def get_pricelist_id(self, currency_id):
|
|
|
+ pricelist = request.env['product.pricelist'].search([('active', '=', True), ('type', '=', 'sale')])
|
|
|
+
|
|
|
+ if not True in pricelist.mapped(lambda p: p.currency_id.id == currency_id):
|
|
|
+ pricelist = pricelist[0].copy()
|
|
|
+ pricelist.write({
|
|
|
+ 'currency_id': currency_id
|
|
|
+ })
|
|
|
+ else:
|
|
|
+ pricelist = pricelist.filtered(lambda p: p.currency_id.id == currency_id)
|
|
|
+
|
|
|
+ return pricelist.id
|
|
|
+
|
|
|
+ '''
|
|
|
+ Create purchase order from cart and return id
|
|
|
+ '''
|
|
|
+ def create_purchase_order(self, supplier_id, cart_items, date_aprove, currency_id, pricelist_id, payment_term_id=None):
|
|
|
+ return request.env['purchase.order'].create({
|
|
|
+ 'partner_id': supplier_id,
|
|
|
'order_line': [[0, False, {
|
|
|
- 'product_id': line.id,
|
|
|
- 'product_qty': line.qty,
|
|
|
- 'price_unit': line.price
|
|
|
- }] for line in order_lines],
|
|
|
- 'date_order': datetime.now().strftime('%Y-%m-%d'),
|
|
|
- 'currency_id': currency_id
|
|
|
+ 'product_id': int(line.get('id')),
|
|
|
+ 'product_uom_qty': float(line.get('quantity')),
|
|
|
+ 'price_unit': float(line.get('price'))
|
|
|
+ }] for line in cart_items],
|
|
|
+ 'date_aprove': date_aprove,
|
|
|
+ 'currency_id': currency_id,
|
|
|
+ 'pricelist_id': pricelist_id,
|
|
|
+ 'payment_term_id': payment_term_id,
|
|
|
+ 'state': 'draft'
|
|
|
})
|
|
|
|
|
|
- return purchase_order
|
|
|
+ '''
|
|
|
+ Purchase processing resource route
|
|
|
+ '''
|
|
|
+ @http.route('/eiru_purchases/process', type='json', auth='user', methods=['POST'], cors='*')
|
|
|
+ def process_purchase(self, **kw):
|
|
|
+ self.make_info_log('Processing purchase...')
|
|
|
+
|
|
|
+ # Get date
|
|
|
+ date_now = datetime.now().strftime(DATE_FORMAT)
|
|
|
+ self.make_info_log('Getting date')
|
|
|
+
|
|
|
+ # Get currency
|
|
|
+ currency_id = self.get_currency_id(kw.get('journalId'))
|
|
|
+ self.make_info_log('Getting currency')
|
|
|
+
|
|
|
+ # Get pricelist
|
|
|
+ pricelist_id = self.get_pricelist_id(currency_id)
|
|
|
+ self.make_info_log('Getting product pricelist')
|
|
|
+
|
|
|
+ purchase_order = self.create_purchase_order(kw.get('supplierId'), kw.get('items'), date_now, currency_id, pricelist_id, kw.get('paymentTermId'))
|
|
|
+ self.make_info_log('Created purchase order')
|
|
|
+
|
|
|
+ return {
|
|
|
+ 'status': 'ok'
|
|
|
+ }
|