report_posgral.py 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. from openerp import tools
  22. from openerp.osv import fields, osv
  23. class pos_reportgral(osv.osv):
  24. _name = "pos.reportgral"
  25. _description = "Listado General de Punto de Ventas"
  26. _auto = False
  27. _rec_name = 'date'
  28. _columns = {
  29. 'date': fields.datetime('Date Order', readonly=True),
  30. 'partner_id':fields.many2one('res.partner', 'Partner', readonly=True),
  31. 'city_id': fields.many2one('res.country.state', 'City', readonly=True),
  32. 'city': fields.char('Ciudad', readonly=True),
  33. 'product_id':fields.many2one('product.product', 'Product', readonly=True),
  34. 'product_brand_id':fields.many2one('product.brand', 'Brand', readonly=True),
  35. 'state': fields.selection([('draft', 'New'), ('paid', 'Closed'), ('done', 'Synchronized'), ('invoiced', 'Invoiced'), ('cancel', 'Cancelled')],
  36. 'Status'),
  37. 'user_id':fields.many2one('res.users', 'Salesperson', readonly=True),
  38. 'price_unit':fields.float('Total Unit.', readonly=True),
  39. 'price_total':fields.float('Total Price', readonly=True),
  40. 'total_discount':fields.float('Total Discount', readonly=True),
  41. 'average_price': fields.float('Average Price', readonly=True,group_operator="avg"),
  42. 'location_id':fields.many2one('stock.location', 'Location', readonly=True),
  43. 'company_id':fields.many2one('res.company', 'Company', readonly=True),
  44. 'nbr':fields.integer('# of Lines', readonly=True), # TDE FIXME master: rename into nbr_lines
  45. 'product_qty':fields.integer('Product Quantity', readonly=True),
  46. 'journal_id': fields.many2one('account.journal', 'Journal'),
  47. 'delay_validation': fields.integer('Delay Validation'),
  48. 'origen': fields.many2one('product.attribute.value' ,string='Origen', readonly=True),
  49. 'product_categ_id': fields.many2one('product.category', 'Product Category', readonly=True),
  50. }
  51. _order = 'date desc'
  52. def init(self, cr):
  53. tools.drop_view_if_exists(cr, 'pos_reportgral')
  54. cr.execute("""
  55. create or replace view pos_reportgral as (
  56. select
  57. min(l.id) as id,
  58. count(*) as nbr,
  59. s.date_order as date,
  60. l.price_unit as price_unit,
  61. sum(l.qty * u.factor) as product_qty,
  62. sum(l.qty * l.price_unit) as price_total,
  63. sum((l.qty * l.price_unit) * (l.discount / 100)) as total_discount,
  64. (sum(l.qty*l.price_unit)/sum(l.qty * u.factor))::decimal as average_price,
  65. sum(cast(to_char(date_trunc('day',s.date_order) - date_trunc('day',s.create_date),'DD') as int)) as delay_validation,
  66. s.partner_id as partner_id,
  67. s.state as state,
  68. q.id as city_id,
  69. h.name as city,
  70. s.user_id as user_id,
  71. s.location_id as location_id,
  72. s.company_id as company_id,
  73. s.sale_journal as journal_id,
  74. l.product_id as product_id,
  75. pt.product_brand_id as product_brand_id,
  76. pt.categ_id as product_categ_id
  77. from pos_order_line as l
  78. left join pos_order s on (s.id=l.order_id)
  79. left join product_product p on (p.id=l.product_id)
  80. left join product_template pt on (pt.id=p.product_tmpl_id)
  81. left join product_brand r on (r.id=pt.product_brand_id)
  82. left join product_uom u on (u.id=pt.uom_id)
  83. left join res_partner q on (q.id=s.partner_id)
  84. left join res_country_state h on (h.id=q.state_id)
  85. group by
  86. s.date_order,q.id,h.name,s.partner_id,s.state,q.city,l.price_unit, pt.categ_id, pt.product_brand_id,
  87. s.user_id,s.location_id,s.company_id,s.sale_journal,l.product_id,s.create_date
  88. having
  89. sum(l.qty * u.factor) != 0)""")
  90. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: