purchase.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as
  6. # published by the Free Software Foundation, either version 3 of the
  7. # License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #
  17. ##############################################################################
  18. from openerp.osv import osv, orm
  19. from openerp import SUPERUSER_ID, workflow, models, fields
  20. class purchase_order(orm.Model):
  21. _inherit = 'purchase.order'
  22. def _update_product_cost_price(self, cr, uid, ids, context=None):
  23. purchase_order_line_obj = self.pool.get('purchase.order.line')
  24. product_template_obj = self.pool.get('product.template')
  25. ## update product cost with unit price of respective po line
  26. current_po = self.browse(cr, uid, ids, context=context)
  27. for po in current_po:
  28. for po_line in po.order_line:
  29. product_tmpl = po_line.product_id.product_tmpl_id
  30. update_price = po_line.price_unit
  31. ## check if user chose to update cost price & current cost price is same as po line unit cost (update_price)
  32. if po_line.update_cost_price and (product_tmpl.standard_price != update_price):
  33. vals = {
  34. 'standard_price' : update_price
  35. }
  36. product_template_obj.write(cr,uid,[product_tmpl.id],vals,context=context)
  37. class purchase_order_line(models.Model):
  38. _inherit = 'purchase.order.line'
  39. update_cost_price = fields.Boolean(string="Update Cost Price?", default= True, help="Select to update cost price of product after confirming invoice")
  40. class account_invoice(osv.Model):
  41. _inherit = 'account.invoice'
  42. def invoice_validate(self, cr, uid, ids, context=None):
  43. res = super(account_invoice, self).invoice_validate(cr, uid, ids, context=context)
  44. purchase_order_obj = self.pool.get('purchase.order')
  45. # read access on purchase.order object is not required
  46. if not purchase_order_obj.check_access_rights(cr, uid, 'read', raise_exception=False):
  47. user_id = SUPERUSER_ID
  48. else:
  49. user_id = uid
  50. po_ids = purchase_order_obj.search(cr, user_id, [('invoice_ids', 'in', ids)], context=context)
  51. for po_id in po_ids:
  52. purchase_order_obj._update_product_cost_price(cr, user_id, [po_id],context=context)
  53. return res