product_template.py 5.13 KB
Newer Older
1 2 3 4
# -*- encoding: utf-8 -*-
##############################################################################
#
#    Product - Average Consumption Module for Odoo
5 6
#    Copyright (C) 2021-Today GRAP (http://www.grap.coop)
#    @author Damien Moulard
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
#
#    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_losses_qty = fields.Float(string="Total Pertes", compute='_compute_total_losses_qty')
    total_autoconso_qty = fields.Float(string="Total Autoconsommation", compute='_compute_total_autoconso_qty')
    total_meals_qty = fields.Float(string="Total Repas Salariés", compute='_compute_total_meals_qty')
32
    total_discounts_qty = fields.Float(string="Total Remises", compute='_compute_total_discounts_qty')
33 34 35 36 37 38 39 40 41 42 43 44

    sql_stock_move = """
          SELECT sum(product_uom_qty) as qty
          FROM stock_move
          WHERE product_id = (SELECT id FROM product_product WHERE product_tmpl_id = %s)
          AND state = 'done'
          AND location_dest_id = %s
          """

    @api.multi
    def _compute_total_losses_qty(self):
        for product_t in self:
45
            qty = 0
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
            try:
                conf = self.env['ir.config_parameter']
                losses_loc_id = conf.get_param("lacagette_sales_helper.losses_location_id")

                self.env.cr.execute(self.sql_stock_move , [product_t.id, losses_loc_id])
                req_res = self.env.cr.dictfetchall()
                if len(req_res) > 0:
                    qty = req_res[0]['qty']
            except Exception as e:
                print(str(e))
            product_t.total_losses_qty = qty

    @api.multi
    def _compute_total_autoconso_qty(self):
        for product_t in self:
61
            qty = 0
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
            try:
                conf = self.env['ir.config_parameter']
                autoconso_loc_id = conf.get_param("lacagette_sales_helper.autoconso_location_id")

                self.env.cr.execute(self.sql_stock_move , [product_t.id, autoconso_loc_id])
                req_res = self.env.cr.dictfetchall()
                if len(req_res) > 0:
                    qty = req_res[0]['qty']
            except Exception as e:
                print(str(e))
            product_t.total_autoconso_qty = qty

    @api.multi
    def _compute_total_meals_qty(self):
        for product_t in self:
77
            qty = 0
78 79 80 81 82 83 84 85 86 87 88 89
            try:
                conf = self.env['ir.config_parameter']
                meals_loc_id = conf.get_param("lacagette_sales_helper.meals_location_id")

                self.env.cr.execute(self.sql_stock_move , [product_t.id, meals_loc_id])
                req_res = self.env.cr.dictfetchall()
                if len(req_res) > 0:
                    qty = req_res[0]['qty']
            except Exception as e:
                print(str(e))
            product_t.total_meals_qty = qty

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    @api.multi
    def _compute_total_discounts_qty(self):
        for product_t in self:
            qty = 0
            try:
                sql = """
                       SELECT qty
                       FROM pos_order_line
                       WHERE product_id = (SELECT id FROM product_product WHERE product_tmpl_id = %s)
                       AND discount > 0
                      """

                self.env.cr.execute(sql , [product_t.id])
                req_res = self.env.cr.dictfetchall()

                if len(req_res) > 0:
                    for i in req_res:
                        qty += req_res[0]['qty']
                    qty = round(qty, 3)
            except Exception as e:
                print(str(e))
            product_t.total_discounts_qty = qty

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    @api.multi
    def view_losses_client_action(self):
        return {
            'res_model': 'lacagette.sales_helper',
            'type': 'ir.actions.client',
            'tag': 'custom_product_losses',
            'target': 'new',
        }

    @api.multi
    def view_autoconso_client_action(self):
        return {
            'res_model': 'lacagette.sales_helper',
            'type': 'ir.actions.client',
            'tag': 'custom_product_autoconso',
            'target': 'new',
        }

    @api.multi
    def view_meals_client_action(self):
        return {
            'res_model': 'lacagette.sales_helper',
            'type': 'ir.actions.client',
            'tag': 'custom_product_meals',
            'target': 'new',
        }
139 140 141 142

    @api.multi
    def view_discounts_client_action(self):
        return 0;