purchase_bill.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import fields, models, tools
  4. from odoo.tools import formatLang
  5. class PurchaseBillUnion(models.Model):
  6. _name = 'purchase.bill.union'
  7. _auto = False
  8. _description = 'Purchases & Bills Union'
  9. _order = "date desc, name desc"
  10. _rec_names_search = ['name', 'reference']
  11. name = fields.Char(string='Reference', readonly=True)
  12. reference = fields.Char(string='Source', readonly=True)
  13. partner_id = fields.Many2one('res.partner', string='Vendor', readonly=True)
  14. date = fields.Date(string='Date', readonly=True)
  15. amount = fields.Float(string='Amount', readonly=True)
  16. currency_id = fields.Many2one('res.currency', string='Currency', readonly=True)
  17. company_id = fields.Many2one('res.company', 'Company', readonly=True)
  18. vendor_bill_id = fields.Many2one('account.move', string='Vendor Bill', readonly=True)
  19. purchase_order_id = fields.Many2one('purchase.order', string='Purchase Order', readonly=True)
  20. def init(self):
  21. tools.drop_view_if_exists(self.env.cr, 'purchase_bill_union')
  22. self.env.cr.execute("""
  23. CREATE OR REPLACE VIEW purchase_bill_union AS (
  24. SELECT
  25. id, name, ref as reference, partner_id, date, amount_untaxed as amount, currency_id, company_id,
  26. id as vendor_bill_id, NULL as purchase_order_id
  27. FROM account_move
  28. WHERE
  29. move_type='in_invoice' and state = 'posted'
  30. UNION
  31. SELECT
  32. -id, name, partner_ref as reference, partner_id, date_order::date as date, amount_untaxed as amount, currency_id, company_id,
  33. NULL as vendor_bill_id, id as purchase_order_id
  34. FROM purchase_order
  35. WHERE
  36. state in ('purchase', 'done') AND
  37. invoice_status in ('to invoice', 'no')
  38. )""")
  39. def name_get(self):
  40. result = []
  41. for doc in self:
  42. name = doc.name or ''
  43. if doc.reference:
  44. name += ' - ' + doc.reference
  45. amount = doc.amount
  46. if doc.purchase_order_id and doc.purchase_order_id.invoice_status == 'no':
  47. amount = 0.0
  48. name += ': ' + formatLang(self.env, amount, monetary=True, currency_obj=doc.currency_id)
  49. result.append((doc.id, name))
  50. return result