sale_order.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # -*- coding: utf-8 -*-
  2. from openerp.http import request
  3. _MODEL = 'sale.order'
  4. '''
  5. Get sale orders
  6. '''
  7. def get_sale_orders():
  8. domain = [
  9. ('state', '=', 'manual'),
  10. ('from_pos', '=', True)
  11. ]
  12. return [
  13. {
  14. 'id': sale_order.id,
  15. 'name': sale_order.display_name,
  16. 'createDate': sale_order.create_date,
  17. 'partner': {
  18. 'id': sale_order.partner_id.id,
  19. 'name': sale_order.partner_id.display_name,
  20. 'image': sale_order.partner_id.image_medium or None,
  21. 'ruc': sale_order.partner_id.ruc or None,
  22. 'phone': sale_order.partner_id.phone or None,
  23. 'mobile': sale_order.partner_id.mobile or None,
  24. 'email': sale_order.partner_id.email or None
  25. },
  26. 'userId': sale_order.user_id.id,
  27. 'warehouseId': sale_order.warehouse_id.id,
  28. 'amountTotal': sale_order.amount_total,
  29. 'lines': [
  30. {
  31. 'id': line.product_id.id,
  32. 'name': line.product_id.display_name,
  33. 'listPrice': line.product_id.list_price,
  34. 'quantity': line.product_uom_qty,
  35. 'price': line.product_id.list_price,
  36. 'discount': 0
  37. } for line in sale_order.order_line
  38. ]
  39. } for sale_order in request.env[_MODEL].search(domain)
  40. ]
  41. def create_sale_from_cart(partner_id, cart_items, date_confirm, currency_id, payment_term_id=None, warehouse_id=None):
  42. '''
  43. '''
  44. def get_pricelist(currency_id):
  45. domain = [
  46. ('active', '=', True),
  47. ('type', '=', 'sale')
  48. ]
  49. pricelist = request.env['product.pricelist'].search(domain)
  50. if not True in pricelist.mapped(lambda p: p.currency_id.id == currency_id):
  51. pricelist = pricelist[0].copy()
  52. pricelist.write({
  53. 'currency_id': currency_id
  54. })
  55. else:
  56. pricelist = pricelist.filtered(lambda p: p.currency_id.id == currency_id)
  57. return pricelist
  58. '''
  59. '''
  60. def compute_cart_item_price(price, currency_id, partner_id=None):
  61. from_currency = request.env.user.company_id.currency_id
  62. to_currency = request.env['res.currency'].search([('id', '=', currency_id)])
  63. return from_currency.compute(price, to_currency)
  64. '''
  65. '''
  66. def compute_cart_item_discount(price, product_id, currency_id):
  67. from_currency = request.env.user.company_id.currency_id
  68. to_currency = request.env['res.currency'].search([('id', '=', currency_id)])
  69. product = request.env['product.product'].search([('id', '=', product_id)])
  70. computed_price = from_currency.compute(price, to_currency)
  71. return 1.0 - (price / product.list_price)
  72. pricelist = get_pricelist(currency_id)
  73. values = {
  74. 'partner_id': partner_id,
  75. 'order_line': [[0, False, {
  76. 'product_id': int(line.get('id')),
  77. 'product_uom_qty': float(line.get('quantity')),
  78. 'price_unit': compute_cart_item_price(float(line.get('price')), currency_id),
  79. 'discount': compute_cart_item_discount(float(line.get('price')), float(line.get('id')), currency_id)
  80. }] for line in cart_items],
  81. 'picking_policy': 'direct',
  82. 'date_confirm': date_confirm,
  83. 'currency_id': currency_id,
  84. 'pricelist_id': pricelist.id,
  85. 'payment_term': payment_term_id,
  86. 'warehouse_id': warehouse_id,
  87. 'state': 'draft',
  88. 'from_pos': True
  89. }
  90. return request.env[_MODEL].create(values)
  91. '''
  92. '''
  93. def confirm_sale_order(sale_order_id):
  94. sale_order = request.env['sale.order'].browse(sale_order_id)
  95. sale_order.write({
  96. 'state': 'manual'
  97. })
  98. return sale_order.action_button_confirm()
  99. '''
  100. '''
  101. def force_assign_picking(sale_order_id):
  102. sale_order = request.env['sale.order'].browse(sale_order_id)
  103. for picking in sale_order.picking_ids:
  104. picking.force_assign()
  105. '''
  106. '''
  107. def done_picking(sale_order_id):
  108. sale_order = request.env['sale.order'].browse(sale_order_id)
  109. for picking in sale_order.picking_ids:
  110. picking.action_done()