sale_order.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # -*- coding: utf-8 -*-
  2. from openerp.http import request
  3. _MODEL = 'sale.order'
  4. '''
  5. Create sale from pos cart
  6. '''
  7. def create_sale_from_cart(partner_id, cart_items, date_confirm, currency_id, payment_term_id=None):
  8. '''
  9. '''
  10. def get_pricelist(currency_id):
  11. domain = [
  12. ('active', '=', True),
  13. ('type', '=', 'sale')
  14. ]
  15. pricelist = request.env['product.pricelist'].search(domain)
  16. if not True in pricelist.mapped(lambda p: p.currency_id.id == currency_id):
  17. pricelist = pricelist[0].copy()
  18. pricelist.write({
  19. 'currency_id': currency_id
  20. })
  21. else:
  22. pricelist = pricelist.filtered(lambda p: p.currency_id.id == currency_id)
  23. return pricelist
  24. '''
  25. '''
  26. def compute_cart_item_price(price, currency_id, partner_id=None):
  27. from_currency = request.env.user.company_id.currency_id
  28. to_currency = request.env['res.currency'].search([('id', '=', currency_id)])
  29. return from_currency.compute(price, to_currency)
  30. '''
  31. '''
  32. def compute_cart_item_discount(price, product_id, currency_id):
  33. from_currency = request.env.user.company_id.currency_id
  34. to_currency = request.env['res.currency'].search([('id', '=', currency_id)])
  35. product = request.env['product.product'].search([('id', '=', product_id)])
  36. computed_price = from_currency.compute(price, to_currency)
  37. return 1.0 - (price / product.list_price)
  38. pricelist = get_pricelist(currency_id)
  39. values = {
  40. 'partner_id': partner_id,
  41. 'order_line': [[0, False, {
  42. 'product_id': int(line.get('id')),
  43. 'product_uom_qty': float(line.get('quantity')),
  44. 'price_unit': compute_cart_item_price(float(line.get('price')), currency_id),
  45. 'discount': compute_cart_item_discount(float(line.get('price')), float(line.get('id')), currency_id)
  46. }] for line in cart_items],
  47. 'picking_policy': 'direct',
  48. 'date_confirm': date_confirm,
  49. 'currency_id': currency_id,
  50. 'pricelist_id': pricelist.id,
  51. 'payment_term': payment_term_id,
  52. 'state': 'draft',
  53. }
  54. return request.env[_MODEL].create(values)
  55. '''
  56. '''
  57. def confirm_sale_order(sale_order_id):
  58. sale_order = request.env['sale.order'].browse(sale_order_id)
  59. sale_order.write({
  60. 'state': 'manual'
  61. })
  62. return sale_order.action_button_confirm()