sale_order.py 4.8 KB

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