|
@@ -125,7 +125,18 @@ class Purchases(http.Controller):
|
|
|
'''
|
|
|
Get all purchasable and active products
|
|
|
'''
|
|
|
- def get_products(self):
|
|
|
+ def get_products(self, type=None):
|
|
|
+ product_obj = request.env['product.template']
|
|
|
+ domain = [('standard_price', '>=', 0), ('active', '=', True)]
|
|
|
+
|
|
|
+ if type == 'purchase':
|
|
|
+ domain.append(('purchase_ok', '=', True))
|
|
|
+ else:
|
|
|
+ if product_obj.fields_get('hr_expense_ok'):
|
|
|
+ domain.append(('hr_expense_ok', '=', True))
|
|
|
+ else:
|
|
|
+ return []
|
|
|
+
|
|
|
return [{
|
|
|
'id': product.id,
|
|
|
'name': product.name,
|
|
@@ -146,7 +157,7 @@ class Purchases(http.Controller):
|
|
|
'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)])]
|
|
|
+ } for product in product_obj.search(domain)]
|
|
|
|
|
|
'''
|
|
|
Get all incoming and active picking types
|
|
@@ -209,7 +220,7 @@ class Purchases(http.Controller):
|
|
|
'''
|
|
|
@http.route('/eiru_purchases/init', auth='user', methods=['GET'], cors='*')
|
|
|
def init_purchase(self, **kw):
|
|
|
- self.make_info_log('Sending JSON response')
|
|
|
+ self.make_info_log('Preparing data to {}'.format(kw.get('mode')))
|
|
|
|
|
|
return self.make_gzip_response({
|
|
|
'date': self.get_server_date(),
|
|
@@ -217,7 +228,7 @@ class Purchases(http.Controller):
|
|
|
'currencies': self.get_currencies(),
|
|
|
'journals': self.get_journals(),
|
|
|
'suppliers': self.get_suppliers(),
|
|
|
- 'products': self.get_products(),
|
|
|
+ 'products': self.get_products(kw.get('mode')),
|
|
|
'pickingTypes': self.get_picking_types(),
|
|
|
'paymentTerms': self.get_payment_terms()
|
|
|
})
|
|
@@ -347,6 +358,28 @@ class Purchases(http.Controller):
|
|
|
'state': 'done'
|
|
|
})
|
|
|
|
|
|
+ '''
|
|
|
+ Create invoice
|
|
|
+ '''
|
|
|
+ def create_invoice(self, supplier_id, cart_items, currency_id, payment_term_id=None):
|
|
|
+ partner = request.env['res.partner'].browse(supplier_id)
|
|
|
+ journal = request.env['account.journal'].search([('type', 'in', ['purchase']), ('company_id', '=', partner.company_id.id)])
|
|
|
+
|
|
|
+ return request.env['account.invoice'].create({
|
|
|
+ 'name': '/',
|
|
|
+ 'partner_id': partner.id,
|
|
|
+ 'invoice_line': [[0, False, {
|
|
|
+ 'name': line.get('name'),
|
|
|
+ 'product_id': int(line.get('id')),
|
|
|
+ 'quantity': float(line.get('quantity')),
|
|
|
+ 'price_unit': float(line.get('price'))
|
|
|
+ }] for line in cart_items],
|
|
|
+ 'account_id': journal.id,
|
|
|
+ 'currency_id': currency_id,
|
|
|
+ 'payment_term': payment_term_id,
|
|
|
+ 'type': 'in_invoice'
|
|
|
+ })
|
|
|
+
|
|
|
'''
|
|
|
Validate invoice
|
|
|
'''
|
|
@@ -615,16 +648,25 @@ class Purchases(http.Controller):
|
|
|
pricelist_id = self.get_pricelist_id(currency_id)
|
|
|
self.make_info_log('Product pricelist checked')
|
|
|
|
|
|
- # Create purchase order
|
|
|
- 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('Purchase order created')
|
|
|
+ invoice = None
|
|
|
+
|
|
|
+ if kw.get('mode') == 'purchase':
|
|
|
+ # Create purchase order
|
|
|
+ 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('Purchase order created')
|
|
|
+
|
|
|
+ # Confirm purchase
|
|
|
+ self.confirm_purchase_order(purchase_order.id)
|
|
|
+ self.make_info_log('Purchase order confirmed')
|
|
|
+
|
|
|
+ invoice = purchase_order.invoice_ids
|
|
|
+ else:
|
|
|
+ invoice = self.create_invoice(kw.get('supplierId'), kw.get('items'), currency_id, kw.get('paymentTermId'))
|
|
|
+ self.make_info_log('Invoice created')
|
|
|
|
|
|
- # Confirm purchase
|
|
|
- self.confirm_purchase_order(purchase_order.id)
|
|
|
- self.make_info_log('Purchase order confirmed')
|
|
|
+ invoice_ids = invoice.mapped(lambda x: x.id)
|
|
|
|
|
|
# Validate invoice
|
|
|
- invoice_ids = purchase_order.invoice_ids.mapped(lambda x: x.id)
|
|
|
self.prepare_invoice(invoice_ids, currency_id, date_now)
|
|
|
self.make_info_log('Invoice prepared')
|
|
|
|