# -*- coding: utf-8 -*- from openerp.http import request as r from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT from datetime import datetime from stock_warehouse import get_location_id def get_purchase_orders(): domain = [ ('state', '=', 'approved'), ('from_pop', '=', True) ] return [ { 'id': po.id, 'name': po.display_name, 'createDate': po.create_date, 'partner': { 'id': po.partner_id.id, 'name': po.partner_id.display_name, 'image': po.partner_id.image_medium or None, 'ruc': po.partner_id.ruc or None, 'phone': po.partner_id.phone or None, 'mobile': po.partner_id.mobile or None, 'email': po.partner_id.email or None }, 'locationId': po.location_id.id, 'amountTotal': po.amount_total, 'lines': [ { 'id': l.product_id.id, 'name': l.product_id.display_name, 'listPrice': l.price_unit, 'quantity': l.product_qty, 'price': l.price_unit, 'discount': 0 } for l in po.order_line ] } for po in r.env['purchase.order'].search(domain, order='id') ] def get_purchase_order(order_id): return r.env['purchase.order'].browse(order_id) def create_purchase_order(supplier_id, cart_items, date_order, currency_id, pricelist_id, payment_term_id=None, warehouse_id=None, user_id=None): def get_product_obj(product_id): return r.env['product.product'].browse(product_id) lines = [] for item in cart_items: product = get_product_obj(item.get('id')) lines += [[0, False, { 'name': item.get('name'), 'date_planned': date_order, 'product_id': product.id, 'product_qty': float(item.get('quantity')), 'price_unit': float(item.get('price')), 'product_uom': product.uom_id.id }]] if warehouse_id == None: warehouse_id = r.env['stock.warehouse'].browse([1]).id values = { 'name': '/', 'partner_id': supplier_id, 'order_line': lines, 'date_order': datetime.utcnow().strftime(DEFAULT_SERVER_DATETIME_FORMAT), 'currency_id': currency_id, 'pricelist_id': pricelist_id, 'payment_term_id': payment_term_id, 'location_id': get_location_id(warehouse_id), 'validator': user_id, 'invoice_method': 'order', 'from_pop': True } return r.env['purchase.order'].create(values) def create_picking(purchase_order_id): po = r.env['purchase.order'].browse(purchase_order_id) picking_id = po.action_picking_create() return picking_id def confirm_purchase_order(purchase_order_id): po = r.env['purchase.order'].browse(purchase_order_id) po.write({ 'state': 'approved' }) # return po.action_purchase_confirm() def done_purchase_order(purchase_order_id): po = r.env['purchase.order'].browse(purchase_order_id) po.write({ 'state': 'done' }) def force_assign_picking(purchase_order_id): po = r.env['purchase.order'].browse(purchase_order_id) for p in po.picking_ids: p.force_assing() def done_picking(purchase_order_id): po = r.env['purchase.order'].browse(purchase_order_id) for p in po.picking_ids: p.action_done()