product_template.py 2.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
# -*- encoding: utf-8 -*-
##############################################################################
#
#    Product - Average Consumption Module for Odoo
#    Copyright (C) 2013-Today GRAP (http://www.grap.coop)
#    @author Julien WESTE
#    @author Sylvain LE GAL (https://twitter.com/legalsylvain)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp import models, fields, api


class ProductTemplate(models.Model):
    _inherit = "product.template"

    total_sales_qty = fields.Float(string="Total POS", compute='_compute_total_sold_qty')
    
    @api.multi
    def _compute_total_sold_qty(self):
        for product_t in self:
            tsq = -1
            try:
                sql = """
                      SELECT sum(qty) as tsq
                      FROM pos_order_line
                      WHERE product_id = (SELECT id FROM product_product WHERE product_tmpl_id = %s)
                      """
                self.env.cr.execute(sql , [product_t.id])
                req_res = self.env.cr.dictfetchall()
                if len(req_res) > 0:
                    tsq = req_res[0]['tsq']
            except Exception as e:
                print(str(e))
            product_t.total_sales_qty = tsq

    @api.multi
    def view_sales_client_action(self):
        return {
            'res_model': 'lacagette.pos_product_sales',
            'type': 'ir.actions.client',
            'tag': 'custom_pos_sales',
            'target': 'new',
        }