purchase_report1.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. #
  22. # Please note that these reports are not multi-currency !!!
  23. #
  24. from openerp.osv import fields,osv
  25. from openerp import tools
  26. class purchase_report1(osv.osv):
  27. _name = "purchase.report1"
  28. _description = "Ordenes de Compra"
  29. _auto = False
  30. _columns = {
  31. 'date': fields.datetime('Fecha Compra', readonly=True, help="Fecha en la que se ha creado este documento"), # TDE FIXME master: rename into date_order
  32. 'state': fields.selection([('draft', 'Borrador'),
  33. ('confirmed', 'Confirmado'),
  34. ('approved', 'Aprobado'),
  35. ('except_picking', 'Excepción de envío'),
  36. ('except_invoice', 'Excepción factura'),
  37. ('done', 'Realizado'),
  38. ('cancel', 'Cancelado')],'Estado del pedido', readonly=True),
  39. 'product_id':fields.many2one('product.product', 'Producto', readonly=True),
  40. 'product_brand_id':fields.many2one('product.brands', 'Marca', readonly=True),
  41. 'picking_type_id': fields.many2one('stock.warehouse', 'Almacén', readonly=True),
  42. 'location_id': fields.many2one('stock.location', 'Destino', readonly=True),
  43. 'partner_id':fields.many2one('res.partner', 'Proveedor', readonly=True),
  44. 'pricelist_id':fields.many2one('product.pricelist', 'Lista Precio', readonly=True),
  45. 'date_approve':fields.date('Fecha aprovado', readonly=True),
  46. 'expected_date':fields.date('Fecha esperada', readonly=True),
  47. 'validator' : fields.many2one('res.users', 'Por validado', readonly=True),
  48. 'product_uom' : fields.many2one('product.uom', 'Unidad de Medida', required=True),
  49. 'company_id':fields.many2one('res.company', 'Compania', readonly=True),
  50. 'user_id':fields.many2one('res.users', 'Responsable', readonly=True),
  51. 'delay':fields.float('Días para validar', digits=(16,2), readonly=True),
  52. 'delay_pass':fields.float('Días para entregar', digits=(16,2), readonly=True),
  53. 'quantity': fields.integer('Cantidad', readonly=True), # TDE FIXME master: rename into unit_quantity
  54. 'price_total': fields.float('Total Precio', readonly=True),
  55. 'price_average': fields.float('Precio promedio', readonly=True, group_operator="avg"),
  56. 'negociation': fields.float('Precio Standard Compra', readonly=True, group_operator="avg"),
  57. 'price_standard': fields.float('Products Value', readonly=True, group_operator="sum"),
  58. 'nbr': fields.integer('# de Lineas', readonly=True), # TDE FIXME master: rename into nbr_lines
  59. 'category_id': fields.many2one('product.category', 'Categoria', readonly=True)
  60. }
  61. _order = 'date desc, price_total desc'
  62. def init(self, cr):
  63. tools.sql.drop_view_if_exists(cr, 'purchase_report1')
  64. cr.execute("""
  65. create or replace view purchase_report1 as (
  66. select
  67. min(l.id) as id,
  68. s.date_order as date,
  69. s.state,
  70. s.date_approve,
  71. s.minimum_planned_date as expected_date,
  72. s.dest_address_id,
  73. s.pricelist_id,
  74. s.validator,
  75. s.picking_type_id as picking_type_id,
  76. s.partner_id as partner_id,
  77. s.create_uid as user_id,
  78. s.company_id as company_id,
  79. l.product_id,
  80. t.categ_id as category_id,
  81. t.product_brand_id as product_brand_id,
  82. t.uom_id as product_uom,
  83. s.location_id as location_id,
  84. sum(l.product_qty/u.factor*u2.factor) as quantity,
  85. extract(epoch from age(s.date_approve,s.date_order))/(24*60*60)::decimal(16,2) as delay,
  86. extract(epoch from age(l.date_planned,s.date_order))/(24*60*60)::decimal(16,2) as delay_pass,
  87. count(*) as nbr,
  88. sum(l.price_unit*l.product_qty)::decimal(16,2) as price_total,
  89. avg(100.0 * (l.price_unit*l.product_qty) / NULLIF(ip.value_float*l.product_qty/u.factor*u2.factor, 0.0))::decimal(16,2) as negociation,
  90. sum(ip.value_float*l.product_qty/u.factor*u2.factor)::decimal(16,2) as price_standard,
  91. (sum(l.product_qty*l.price_unit)/NULLIF(sum(l.product_qty/u.factor*u2.factor),0.0))::decimal(16,2) as price_average
  92. from purchase_order_line l
  93. join purchase_order s on (l.order_id=s.id)
  94. left join product_product p on (l.product_id=p.id)
  95. left join product_template t on (p.product_tmpl_id=t.id)
  96. left join product_brands r on (r.id=t.product_brand_id)
  97. LEFT JOIN ir_property ip ON (ip.name='standard_price' AND ip.res_id=CONCAT('product.template,',t.id) AND ip.company_id=s.company_id)
  98. left join product_uom u on (u.id=l.product_uom)
  99. left join product_uom u2 on (u2.id=t.uom_id)
  100. group by
  101. s.company_id,
  102. s.create_uid,
  103. s.partner_id,
  104. u.factor,
  105. s.location_id,
  106. l.price_unit,
  107. s.date_approve,
  108. l.date_planned,
  109. t.product_brand_id,
  110. l.product_uom,
  111. s.minimum_planned_date,
  112. s.pricelist_id,
  113. s.validator,
  114. s.dest_address_id,
  115. l.product_id,
  116. t.categ_id,
  117. s.date_order,
  118. s.state,
  119. s.picking_type_id,
  120. u.uom_type,
  121. u.category_id,
  122. t.uom_id,
  123. u.id,
  124. u2.factor
  125. )
  126. """)
  127. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: