<commit_before># -*- coding: utf-8 -*-
# -*- encoding: utf-8 -*-

#############################################################################
#
#    Copyright (c) 2007 <NAME> <<EMAIL>>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU 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 General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import fields, osv
from openerp.tools.translate import _

class commission_line(osv.osv):
    
    def _update_bonus(self, cr, uid, salesman_ids, period_ids, context=None):
        if not salesman_ids or not period_ids:
            return 

        period_id = None
        partner_id = None
        team_id = None
        team = None
        
        team_obj = self.pool.get("crm.case.section")
        commission_line_obj = self.pool.get("commission.line")
        
        cr.execute("SELECT s.id team_id, cl.period_id, cl.partner_id, SUM(cl.price_sub) "
                   " FROM commission_line cl "
                   " INNER JOIN res_users u ON u.id = cl.salesman_id "
                   " INNER JOIN crm_case_section s ON s.id = u.default_section_id "
                   " WHERE cl.salesman_id IN %s "
                   "   AND cl.period_id IN %s "
                   " GROUP BY 1,2,3 "
                   " ORDER BY 1,2,3 "
                   ,(tuple(salesman_ids),tuple(period_ids)))
                                 
        for row in cr.fetchall():            
            # grouped by section
            if team_id != row[0]:                
                team_id = row[0]
                team = team_obj.browse(cr,uid,team_id,context)                
            period_id = row[1]
            partner_id = row[2]
            total = row[3]
            #
            sale_bonus = team.sales_bonus_id
            if not sale_bonus:
                continue
            #            
            bonus = None
            for bonus_line in sale_bonus.line_ids:
                if bonus_line.volume_of_sales <= total:
                    bonus = bonus_line
                    continue
                break
                        
            if not bonus:
                line_ids = commission_line_obj.search(cr,uid,[("period_id","=",period_id),
                                                              ("partner_id","=",partner_id),
                                                              ("invoiced_id","=",False),
                                                              ("sales_bonus_line_id","!=",False)])
                
                for cl in commission_line_obj.browse(cr,uid,line_ids,context):
                    amount = self._get_sale_commission(cr, uid, cl.price_sub, cl.base_commission, context=context)
                    commission_line_obj.write(cr,uid,[cl.id], {  
                        "total_commission" : cl.base_commission,                                                            
                        "amount" : amount,
                        "sales_bonus_line_id" : None                             
                    },context)
            elif bonus:
                line_ids = commission_line_obj.search(cr,uid,[("period_id","=",period_id),
                                                              ("partner_id","=",partner_id),
                                                              ("invoiced_id","=",False),
                                                              '|',
                                                              ("sales_bonus_line_id","!=",bonus.id),
                                                              ("sales_bonus_line_id","=",False)])
                
                for cl in commission_line_obj.browse(cr,uid,line_ids,context):
                    total_commission=cl.base_commission+bonus.bonus
                    amount = self._get_sale_commission(cr, uid, cl.price_sub, total_commission, context=context)
                    commission_line_obj.write(cr,uid,[cl.id], {
                        "total_commission" : total_commission,
                        "amount" : amount,
                        "sales_bonus_line_id" : bonus.id                             
                    },context)
                    
    def _get_sale_commission(self, cr, uid, name, user, customer, product, qty, netto, date, pricelist=None, defaults=None, period=None, context=None):
        res = []
        
        commission_obj = self.pool["commission_product.commission"]
        period_obj = self.pool["account.period"]        
        pricelist_obj = self.pool.get("product.pricelist")
        pricelist_item_obj = self.pool.get("product.pricelist.item")
        rule_obj = self.pool.get("commission_sale.rule")
        
        team = user.default_section_id
        partner = user.partner_id
        
        #check partner and team
        if not partner or not team:
            return res
        
        # get global commission
        percent = product.commission_percent or product.categ_id.commission_percent or team.sales_commission
        # search for rule                                                               
        rule = rule_obj._get_rule(cr, uid, team, product, context=context)
        if rule:
            percent = rule.get("commission",0.0) or 0.0
        elif pricelist:<commit_msg>[FIX] commission<commit_after># -*- coding: utf-8 -*-
# -*- encoding: utf-8 -*-

#############################################################################
#
#    Copyright (c) 2007 <NAME> <<EMAIL>>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU 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 General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import fields, osv
from openerp.tools.translate import _

class commission_line(osv.osv):
    
    def _update_bonus(self, cr, uid, salesman_ids, period_ids, context=None):
        if not salesman_ids or not period_ids:
            return 

        period_id = None
        partner_id = None
        team_id = None
        team = None
        
        team_obj = self.pool.get("crm.case.section")
        commission_line_obj = self.pool.get("commission.line")
        
        cr.execute("SELECT s.id team_id, cl.period_id, cl.partner_id, SUM(cl.price_sub) "
                   " FROM commission_line cl "
                   " INNER JOIN res_users u ON u.id = cl.salesman_id "
                   " INNER JOIN crm_case_section s ON s.id = u.default_section_id "
                   " WHERE cl.salesman_id IN %s "
                   "   AND cl.period_id IN %s "
                   " GROUP BY 1,2,3 "
                   " ORDER BY 1,2,3 "
                   ,(tuple(salesman_ids),tuple(period_ids)))
                                 
        for row in cr.fetchall():            
            # grouped by section
            if team_id != row[0]:                
                team_id = row[0]
                team = team_obj.browse(cr,uid,team_id,context)                
            period_id = row[1]
            partner_id = row[2]
            total = row[3]
            #
            sale_bonus = team.sales_bonus_id
            if not sale_bonus:
                continue
            #            
            bonus = None
            for bonus_line in sale_bonus.line_ids:
                if bonus_line.volume_of_sales <= total:
                    bonus = bonus_line
                    continue
                break
                        
            if not bonus:
                line_ids = commission_line_obj.search(cr,uid,[("period_id","=",period_id),
                                                              ("partner_id","=",partner_id),
                                                              ("invoiced_id","=",False),
                                                              ("sales_bonus_line_id","!=",False)])
                
                for cl in commission_line_obj.browse(cr,uid,line_ids,context):
                    amount = self._get_sale_commission(cr, uid, cl.price_sub, cl.base_commission, context=context)
                    commission_line_obj.write(cr,uid,[cl.id], {  
                        "total_commission" : cl.base_commission,                                                            
                        "amount" : amount,
                        "sales_bonus_line_id" : None                             
                    },context)
            elif bonus:
                line_ids = commission_line_obj.search(cr,uid,[("period_id","=",period_id),
                                                              ("partner_id","=",partner_id),
                                                              ("invoiced_id","=",False),
                                                              '|',
                                                              ("sales_bonus_line_id","!=",bonus.id),
                                                              ("sales_bonus_line_id","=",False)])
                
                for cl in commission_line_obj.browse(cr,uid,line_ids,context):
                    total_commission=cl.base_commission+bonus.bonus
                    amount = self._get_sale_commission(cr, uid, cl.price_sub, total_commission, context=context)
                    commission_line_obj.write(cr,uid,[cl.id], {
                        "total_commission" : total_commission,
                        "amount" : amount,
                        "sales_bonus_line_id" : bonus.id                             
                    },context)
                    
    def _get_sale_commission(self, cr, uid, name, user, customer, product, qty, netto, date, pricelist=None, defaults=None, period=None, context=None):
        res = []
        
        period_obj = self.pool["account.period"]        
        pricelist_obj = self.pool.get("product.pricelist")
        pricelist_item_obj = self.pool.get("product.pricelist.item")
        rule_obj = self.pool.get("commission_sale.rule")
        
        team = user.default_section_id
        partner = user.partner_id
        
        #check partner and team
        if not partner or not team:
            return res
        
        # get percent
        percent = product.commission_percent
        if not percent:
            percent = product.categ_id.commission_percent
        if not percent:
            percent = team.sales_commission
            
        # search for rule                                                               
        rule = rule_obj._get_rule(cr, uid, team, product, context=context)
        if rule:
            percent = rule.get("commission",0.0) or 0.0
        elif pricelist:
{'fraction_non_alphanumeric': 0.10974784227449653, 'fraction_numerical': 0.003723134202064647, 'mean_word_length': 1.3427155599603569, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4143566', 'n_tokens_mistral': 3075, 'n_tokens_neox': 2900, 'n_words': 689}