purchase_order.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # -*- coding: utf-8 -*-
  2. from openerp.http import request as r
  3. from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
  4. from datetime import datetime
  5. from stock_warehouse import get_location_id
  6. def get_purchase_orders():
  7. domain = [
  8. ('state', '=', 'approved'),
  9. ('from_pop', '=', True)
  10. ]
  11. return [
  12. {
  13. 'id': po.id,
  14. 'name': po.display_name,
  15. 'createDate': po.create_date,
  16. 'partner': {
  17. 'id': po.partner_id.id,
  18. 'name': po.partner_id.display_name,
  19. 'image': po.partner_id.image_medium or None,
  20. 'ruc': po.partner_id.ruc or None,
  21. 'phone': po.partner_id.phone or None,
  22. 'mobile': po.partner_id.mobile or None,
  23. 'email': po.partner_id.email or None
  24. },
  25. 'locationId': po.location_id.id,
  26. 'amountTotal': po.amount_total,
  27. 'lines': [
  28. {
  29. 'id': l.product_id.id,
  30. 'name': l.product_id.display_name,
  31. 'listPrice': l.price_unit,
  32. 'quantity': l.product_qty,
  33. 'price': l.price_unit,
  34. 'discount': 0
  35. } for l in po.order_line
  36. ]
  37. } for po in r.env['purchase.order'].search(domain, order='id')
  38. ]
  39. def get_purchase_order(order_id):
  40. return r.env['purchase.order'].browse(order_id)
  41. def create_purchase_order(supplier_id, cart_items, date_order, currency_id, pricelist_id, payment_term_id=None, warehouse_id=None, user_id=None):
  42. def get_product_obj(product_id):
  43. return r.env['product.product'].browse(product_id)
  44. lines = []
  45. for item in cart_items:
  46. product = get_product_obj(item.get('id'))
  47. lines += [[0, False, {
  48. 'name': item.get('name'),
  49. 'date_planned': date_order,
  50. 'product_id': product.id,
  51. 'product_qty': float(item.get('quantity')),
  52. 'price_unit': float(item.get('price')),
  53. 'product_uom': product.uom_id.id
  54. }]]
  55. if warehouse_id == None:
  56. warehouse_id = r.env['stock.warehouse'].browse([1]).id
  57. values = {
  58. 'name': '/',
  59. 'partner_id': supplier_id,
  60. 'order_line': lines,
  61. 'date_order': datetime.utcnow().strftime(DEFAULT_SERVER_DATETIME_FORMAT),
  62. 'currency_id': currency_id,
  63. 'pricelist_id': pricelist_id,
  64. 'payment_term_id': payment_term_id,
  65. 'location_id': get_location_id(warehouse_id),
  66. 'validator': user_id,
  67. 'invoice_method': 'order',
  68. 'from_pop': True
  69. }
  70. return r.env['purchase.order'].create(values)
  71. def create_picking(purchase_order_id):
  72. po = r.env['purchase.order'].browse(purchase_order_id)
  73. picking_id = po.action_picking_create()
  74. return picking_id
  75. def confirm_purchase_order(purchase_order_id):
  76. po = r.env['purchase.order'].browse(purchase_order_id)
  77. po.write({
  78. 'state': 'approved'
  79. })
  80. # return po.action_purchase_confirm()
  81. def done_purchase_order(purchase_order_id):
  82. po = r.env['purchase.order'].browse(purchase_order_id)
  83. po.write({
  84. 'state': 'done'
  85. })
  86. def force_assign_picking(purchase_order_id):
  87. po = r.env['purchase.order'].browse(purchase_order_id)
  88. for p in po.picking_ids:
  89. p.force_assing()
  90. def done_picking(purchase_order_id):
  91. po = r.env['purchase.order'].browse(purchase_order_id)
  92. for p in po.picking_ids:
  93. p.action_done()