Guest User

Untitled

a guest
Oct 16th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. from openerp import models, api
  2. from openerp.osv import fields, osv
  3. import openerp.addons.decimal_precision as dp
  4.  
  5.  
  6. class PurchaseOrder(models.Model):
  7. _inherit = "purchase.order"
  8.  
  9. def _get_order(self, cr, uid, ids, context=None):
  10. result = {}
  11. for line in self.pool.get('purchase.order.line').browse(cr, uid, ids, context=context):
  12. result[line.order_id.id] = True
  13. return result.keys()
  14.  
  15. def _amount_all(self, cr, uid, ids, field_name, arg, context=None):
  16. res = {}
  17. cur_obj=self.pool.get('res.currency')
  18. line_obj = self.pool['purchase.order.line']
  19. for order in self.browse(cr, uid, ids, context=context):
  20. res[order.id] = {
  21. 'amount_untaxed': 0.0,
  22. 'amount_tax': 0.0,
  23. 'amount_total': 0.0,
  24. }
  25. val = val1 = 0.0
  26. cur = order.pricelist_id.currency_id
  27. for line in order.order_line:
  28. val1 += line.price_subtotal
  29. line_price = line_obj._calc_line_base_price(cr, uid, line,
  30. context=context)
  31. line_qty = line_obj._calc_line_quantity(cr, uid, line,
  32. context=context)
  33. for c in self.pool['account.tax'].compute_all(
  34. cr, uid, line.taxes_id, line_price, line_qty,
  35. line.product_id, order.partner_id)['taxes']:
  36. val += c.get('amount', 0.0)
  37. res[order.id]['amount_tax']=cur_obj.round(cr, uid, cur, val)
  38. if order.discount:
  39. res[order.id]['amount_untaxed']=cur_obj.round(cr, uid, cur, val1) * (1 - (order.discount or 0.0) / 100.0)
  40. else:
  41. res[order.id]['amount_untaxed']=cur_obj.round(cr, uid, cur, val1)
  42. res[order.id]['amount_total']=res[order.id]['amount_untaxed'] + res[order.id]['amount_tax']
  43. return res
  44.  
  45. _columns = {
  46. 'discount': fields.float(
  47. string='Discount (%)',
  48. digits_compute=dp.get_precision('Discount')
  49. ),
  50. 'amount_untaxed': fields.function(
  51. _amount_all,
  52. digits_compute=dp.get_precision('Account'),
  53. string='Untaxed Amount',
  54. store={
  55. 'purchase.order': (lambda s, c, u, i, x: i, ['discount'], 10),
  56. 'purchase.order.line': (_get_order, None, 10),
  57. },
  58. multi="sums",
  59. help="The amount without tax",
  60. track_visibility='always'
  61. ),
  62. 'amount_tax': fields.function(
  63. _amount_all, digits_compute=dp.get_precision('Account'), string='Taxes',
  64. store={
  65. 'purchase.order': (lambda s, c, u, i, x: i, ['discount'], 10),
  66. 'purchase.order.line': (_get_order, None, 10),
  67. },
  68. multi="sums",
  69. help="The tax amount"
  70. ),
  71. 'amount_total': fields.function(
  72. _amount_all,
  73. digits_compute=dp.get_precision('Account'),
  74. string='Total',
  75. store={
  76. 'purchase.order': (lambda s, c, u, i, x: i, ['discount'], 10),
  77. 'purchase.order.line': (_get_order, None, 10),
  78. },
  79. multi="sums",
  80. help="The total amount"
  81. ),
  82. }
  83.  
  84. _sql_constraints = [
  85. ('order_discount_limit', 'CHECK (discount <= 100.0)',
  86. 'Discount must be lower than 100%.'),
  87. ]
Add Comment
Please, Sign In to add comment