# -*- coding: utf-8 -*- from openerp.http import request from openerp.tools import float_round _MODEL = 'sale.order' ''' Get sale orders ''' def get_sale_orders(): domain = [ ('state', '=', 'manual'), ('from_pos', '=', True) ] return [ { 'id': sale_order.id, 'name': sale_order.display_name, 'createDate': sale_order.create_date, 'partner': { 'id': sale_order.partner_id.id, 'name': sale_order.partner_id.display_name, 'image': sale_order.partner_id.image_medium or None, 'ruc': sale_order.partner_id.ruc or None, 'phone': sale_order.partner_id.phone or None, 'mobile': sale_order.partner_id.mobile or None, 'email': sale_order.partner_id.email or None }, 'userId': sale_order.user_id.id, 'warehouseId': sale_order.warehouse_id.id, 'amountTotal': sale_order.amount_total, 'lines': [ { 'id': line.product_id.id, 'name': line.product_id.display_name, 'listPrice': line.product_id.list_price, 'quantity': line.product_uom_qty, 'price': line.product_id.list_price, 'discount': 0 } for line in sale_order.order_line ] } for sale_order in request.env[_MODEL].search(domain) ] def create_sale_from_cart(partner_id, cart_items, date_confirm, currency_id, payment_term_id=None, warehouse_id=None, customer_pricelist_id=None, user_id=None): ''' ''' def get_pricelist(currency_id, customer_pricelist_id=None): if customer_pricelist_id: return request.env['product.pricelist'].browse([customer_pricelist_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) discount = 1.0 - (computed_price / product.list_price) precision = request.env['decimal.precision'].precision_get('Discount') # return float_round(discount, precision) return float_round(discount, precision) * 100 pricelist = get_pricelist(currency_id) if len(pricelist) > 1: pricelist = pricelist[0] 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('listPrice')), 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, 'warehouse_id': warehouse_id, 'user_id': user_id, 'state': 'draft', 'from_pos': True } 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() def done_sale_order(sale_order_id): sale_order = request.env['sale.order'].browse(sale_order_id) sale_order.write({ 'state': 'done' }) ''' ''' def force_assign_picking(sale_order_id): sale_order = request.env['sale.order'].browse(sale_order_id) for picking in sale_order.picking_ids: picking.force_assign() ''' ''' def done_picking(sale_order_id): sale_order = request.env['sale.order'].browse(sale_order_id) for picking in sale_order.picking_ids: picking.action_done()