# -*- coding: utf-8 -*- from openerp.http import request _MODEL = 'sale.order' ''' Create sale from pos cart ''' def create_sale_from_cart(partner_id, cart_items, date_confirm, currency_id, payment_term_id=None): ''' ''' def get_pricelist(currency_id): domain = [ ('active', '=', True), ('type', '=', 'sale') ] pricelist = request.env['product.pricelist'].search(domain) 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 ''' ''' def compute_cart_item_price(price, currency_id, partner_id=None): from_currency = request.env.user.company_id.currency_id to_currency = request.env['res.currency'].search([('id', '=', currency_id)]) return from_currency.compute(price, to_currency) ''' ''' def compute_cart_item_discount(price, product_id, currency_id): from_currency = request.env.user.company_id.currency_id to_currency = request.env['res.currency'].search([('id', '=', currency_id)]) product = request.env['product.product'].search([('id', '=', product_id)]) computed_price = from_currency.compute(price, to_currency) return 1.0 - (price / product.list_price) pricelist = get_pricelist(currency_id) values = { 'partner_id': partner_id, 'order_line': [[0, False, { 'product_id': int(line.get('id')), 'product_uom_qty': float(line.get('quantity')), 'price_unit': compute_cart_item_price(float(line.get('price')), currency_id), 'discount': compute_cart_item_discount(float(line.get('price')), float(line.get('id')), currency_id) }] for line in cart_items], 'picking_policy': 'direct', 'date_confirm': date_confirm, 'currency_id': currency_id, 'pricelist_id': pricelist.id, 'payment_term': payment_term_id, 'state': 'draft', } return request.env[_MODEL].create(values) ''' ''' def confirm_sale_order(sale_order_id): sale_order = request.env['sale.order'].browse(sale_order_id) sale_order.write({ 'state': 'manual' }) return sale_order.action_button_confirm()