<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Addons modules by CLEARCORP S.A.
#    Copyright (C) 2009-TODAY CLEARCORP S.A. (<http://clearcorp.co.cr>).
#
#    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.osv import osv, fields
from openerp import tools
from openerp.tools.translate import _
from openerp.addons.decimal_precision import decimal_precision as dp
from datetime import datetime, timedelta
from openerp.exceptions import Warning

class account_analytic_account(osv.osv):
    _inherit = "account.analytic.account"
    def _is_holiday_pricelist(self,cr,uid,date,account,context={}):
        holidays=account.holidays_calendar_id.holiday_ids
        if holidays:
            for holiday in holidays:
                if holiday.date==date:
                    return True
        return False
    
    def _is_extra_hour_pricelist(self,cr,uid,date,account,start_time,end_time,context={}):
        is_extra=False
        date_number=datetime.strptime(date, '%Y-%m-%d').weekday()
        schedules=account.regular_schedule_id.attendance_ids
        if schedules:
            for schedule in schedules:
                if str(date_number)==schedule.dayofweek:
                    if start_time>=schedule.hour_from and end_time<=schedule.hour_to:
                        is_extra=False
                        break
                    elif start_time>=schedule.hour_to and end_time>schedule.hour_to:
                        is_extra=True
                        break
                    elif start_time<schedule.hour_from and end_time<=schedule.hour_from:
                        is_extra=True
                        break
            else:
                is_extra=False
        else:
            is_extra=False
        return is_extra
            
    def _get_invoice_price(self, cr, uid, account, date,start_time,end_time, product_id,categ_id,qty,service_type,employee_id,factor_id,context = {}):
        timesheet_obj=self.pool.get('hr.analytic.timesheet')
        pricelist_obj=self.pool.get('contract.pricelist')
        issue_obj=self.pool.get('project.issue')
        product_ids=[]
        categ_ids=[]
        amount=0.0
        count_limit=0.0
        cont_timesheets=0.0
        count_extra=0.0
        origin_start_time=start_time
        origin_end_time=end_time
        original_quantity=qty

        pricelist_product_ids=pricelist_obj.search(cr,uid,[('contract_id','=', account.id),('product_id','=', product_id)])
        pricelist_category_ids=pricelist_obj.search(cr,uid,[('contract_id','=', account.id),('categ_id','=', categ_id)])
        if pricelist_product_ids or pricelist_category_ids:
            if pricelist_product_ids and pricelist_category_ids:
                pricelist=pricelist_obj.browse(cr, uid, pricelist_product_ids, context=context)
            elif not pricelist_product_ids and pricelist_category_ids:
                pricelist=pricelist_obj.browse(cr, uid, pricelist_category_ids, context=context)
            elif  pricelist_product_ids and not pricelist_category_ids:
                pricelist=pricelist_obj.browse(cr, uid, pricelist_product_ids, context=context)
            for list in pricelist:
                minimun_time_ids=pricelist_obj.search(cr,uid,[('contract_id','=', account.id),('minimum_time','=', list.minimum_time),('technical_rate','=', list.technical_rate),('assistant_rate','=', list.assistant_rate)])
                pricelist_times=pricelist_obj.browse(cr,uid,minimun_time_ids,context=context)
                for pricelist in pricelist_times:
                    if pricelist.pricelist_line_type=='category':
                        categ_ids.append(pricelist.categ_id.id)
                    elif pricelist.pricelist_line_type=='product':
                        product_ids.append(pricelist.product_id.id)
                issues_ids=issue_obj.search(cr,uid,['|',('product_id','in',product_ids),('categ_id','in',categ_ids),('analytic_account_id','=',account.id)])
                timesheets_before_ids=timesheet_obj.search(cr,uid,[('issue_id','in',issues_ids),('date','=',date),('end_time','<=',start_time),('service_type','=',service_type),('employee_id','=',employee_id)],order='end_time desc')
                timesheets_before=timesheet_obj.browse(cr,uid,timesheets_before_ids,context=context)
                timesheets_after_ids=timesheet_obj.search(cr,uid,[('issue_id','in',issues_ids),('date','=',date),('start_time','>=',end_time),('service_type','=',service_type),('employee_id','=',employee_id)],order='start_time asc')
                timesheets_after=timesheet_obj.browse(cr,uid,timesheets_after_ids,context=context)
        
                count_limit+=qty
                if timesheets_before or timesheets_after:
                    for timesheet in timesheets_before:
                        if timesheet.end_time==start_time:
                            count_limit+=timesheet.end_time-timesheet.start_time
                            start_time=timesheet.start_time
                            cont_timesheets+=1
                    for timesheet in timesheets_after:
                        if timesheet.start_time==end_time:
                            count_limit+=timesheet.end_time-timesheet.start_time
                            end_time=timesheet.end_time
                            cont_timesheets+=1
                    if count_limit<list.minimum_time:
                        if count_limit!=0:
                            if cont_timesheets>0:
                                qty=qty/count_limit
                            else:
                                qty=list.minimum_time
                        else:
                            qty=0
                else:
                    if qty<list.minimum_time:
                        qty=list.minimum_time
                holiday_state=self._is_holiday_pricelist(cr,uid,date,account,context={})
                if holiday_state==True:
                    if service_type=='expert':
                        amount=(list.technical_rate*list.holiday_multiplier)
                    elif service_type=='assistant':
                        amount=list.assistant_rate*list.holiday_multiplier
                    elif service_type=='basic':
                        amount=0.0
                    
                else:
                    if service_type=='expert':
                        rate=list.technical_rate
                    elif service_type=='assistant':
                        rate=list.assistant_rate
                    elif service_type=='basic':
                        rate=0.0
                    is_extra=self._is_extra_hour_pricelist(cr,uid,date,account,origin_start_time,origin_end_time,context={})
                    if is_extra==True:
                        count_extra+=original_quantity
                        if timesheets_before or timesheets_after:
                            for timesheet in timesheets_before:
                                if timesheet.end_time==origin_start_time:
                                    if self._is_extra_hour_pricelist(cr,uid,timesheet.date,account,timesheet.start_time,timesheet.end_time,context={})==True:
                                        count_extra+=timesheet.end_time-timesheet.start_time
                                        origin_start_time=timesheet.start_time
                                    else:
                                        is_extra=False
                            for timesheet in timesheets_after:
                                if timesheet.start_time==origin_end_time:
                                    if self._is_extra_hour_pricelist(cr,uid,timesheet.date,account,timesheet.start_time,timesheet.end_time,context={})==True:
                                        count_extra+=timesheet.end_time-timesheet.start_time
                                        origin_end_time=timesheet.end_time
                            if count_extra<1 and is_extra==False:
                                amount=rate
                            elif count_extra>=1:
                                amount=rate*list.overtime_multiplier
                            else:
                                amount=rate*list.overtime_multiplier
                        else:
                            amount=rate*list.overtime_multiplier
                    else:
                        amount=rate
        return qty,amount
    def _analysis_all(self, cr, uid, ids, fields, arg, context=None):
        total=0.0
        res=super(account_analytic_account,self)._analysis_all(cr, uid, ids, fields, arg, context)
        accounts = self.browse(cr, uid, ids, context=context)
        for f in fields:
            if f == 'ca_to_invoice':
                for account in accounts:
                    if account.pricelist_ids:<commit_msg>[FIX]-project_issue_helpdesk_invoicing-Fix method of compute pricelist for issues invoicing<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Addons modules by CLEARCORP S.A.
#    Copyright (C) 2009-TODAY CLEARCORP S.A. (<http://clearcorp.co.cr>).
#
#    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.osv import osv, fields
from openerp import tools
from openerp.tools.translate import _
from openerp.addons.decimal_precision import decimal_precision as dp
from datetime import datetime, timedelta
from openerp.exceptions import Warning

class account_analytic_account(osv.osv):
    _inherit = "account.analytic.account"
    def _is_holiday_pricelist(self,cr,uid,date,account,context={}):
        holidays=account.holidays_calendar_id.holiday_ids
        if holidays:
            for holiday in holidays:
                if holiday.date==date:
                    return True
        return False
    
    def _is_extra_hour_pricelist(self,cr,uid,date,account,start_time,end_time,context={}):
        is_extra=False
        date_number=datetime.strptime(date, '%Y-%m-%d').weekday()
        schedules=account.regular_schedule_id.attendance_ids
        if schedules:
            for schedule in schedules:
                if str(date_number)==schedule.dayofweek:
                    if start_time>=schedule.hour_from and end_time<=schedule.hour_to:
                        is_extra=False
                        break
                    elif start_time>=schedule.hour_to and end_time>schedule.hour_to:
                        is_extra=True
                        break
                    elif start_time<schedule.hour_from and end_time<=schedule.hour_from:
                        is_extra=True
                        break
            else:
                is_extra=False
        else:
            is_extra=False
        return is_extra
            
    def _get_invoice_price(self, cr, uid, account, date,start_time,end_time, product_id,categ_id,qty,service_type,employee_id,factor_id,context = {}):
        timesheet_obj=self.pool.get('hr.analytic.timesheet')
        pricelist_obj=self.pool.get('contract.pricelist')
        issue_obj=self.pool.get('project.issue')
        multiple=1.0
        product_ids=[]
        categ_ids=[]
        amount=0.0
        count_limit=0.0
        cont_timesheets=0.0
        count_extra=0.0
        origin_start_time=start_time
        origin_end_time=end_time
        original_quantity=qty

        pricelist_product_ids=pricelist_obj.search(cr,uid,[('contract_id','=', account.id),('product_id','=', product_id)])
        pricelist_category_ids=pricelist_obj.search(cr,uid,[('contract_id','=', account.id),('categ_id','=', categ_id)])
        if pricelist_product_ids or pricelist_category_ids:
            if pricelist_product_ids and pricelist_category_ids:
                pricelist=pricelist_obj.browse(cr, uid, pricelist_product_ids, context=context)
            elif not pricelist_product_ids and pricelist_category_ids:
                pricelist=pricelist_obj.browse(cr, uid, pricelist_category_ids, context=context)
            elif  pricelist_product_ids and not pricelist_category_ids:
                pricelist=pricelist_obj.browse(cr, uid, pricelist_product_ids, context=context)
            for list in pricelist:
                minimun_time_ids=pricelist_obj.search(cr,uid,[('contract_id','=', account.id),('minimum_time','=', list.minimum_time),('technical_rate','=', list.technical_rate),('assistant_rate','=', list.assistant_rate)])
                pricelist_times=pricelist_obj.browse(cr,uid,minimun_time_ids,context=context)
                for pricelist in pricelist_times:
                    if pricelist.pricelist_line_type=='category':
                        categ_ids.append(pricelist.categ_id.id)
                    elif pricelist.pricelist_line_type=='product':
                        product_ids.append(pricelist.product_id.id)
                issues_ids=issue_obj.search(cr,uid,['|',('product_id','in',product_ids),('categ_id','in',categ_ids),('analytic_account_id','=',account.id)])
                timesheets_before_ids=timesheet_obj.search(cr,uid,[('issue_id','in',issues_ids),('date','=',date),('end_time','<=',start_time),('service_type','=',service_type),('employee_id','=',employee_id)],order='end_time desc')
                timesheets_before=timesheet_obj.browse(cr,uid,timesheets_before_ids,context=context)
                timesheets_after_ids=timesheet_obj.search(cr,uid,[('issue_id','in',issues_ids),('date','=',date),('start_time','>=',end_time),('service_type','=',service_type),('employee_id','=',employee_id)],order='start_time asc')
                timesheets_after=timesheet_obj.browse(cr,uid,timesheets_after_ids,context=context)
        
                count_limit+=qty
                if timesheets_before or timesheets_after:
                    for timesheet in timesheets_before:
                        if timesheet.end_time==start_time:
                            count_limit+=timesheet.end_time-timesheet.start_time
                            start_time=timesheet.start_time
                            cont_timesheets+=1
                    for timesheet in timesheets_after:
                        if timesheet.start_time==end_time:
                            count_limit+=timesheet.end_time-timesheet.start_time
                            end_time=timesheet.end_time
                            cont_timesheets+=1
                    if count_limit<=list.minimum_time:
                        if count_limit!=0:
                            if cont_timesheets>0:
                                qty=qty/count_limit
                            else:
                                qty=list.minimum_time
                        else:
                            qty=0
                else:
                    if qty<list.minimum_time:
                        if list.minimum_time<1:
                            multiple=1/list.minimum_time
                        qty=list.minimum_time
                    elif qty==list.minimum_time:
                        if list.minimum_time<1:
                            multiple=1/list.minimum_time
                holiday_state=self._is_holiday_pricelist(cr,uid,date,account,context={})
                if holiday_state==True:
                    if service_type=='expert':
                        amount=(list.technical_rate*list.holiday_multiplier)*multiple
                    elif service_type=='assistant':
                        amount=list.assistant_rate*list.holiday_multiplier*multiple
                    elif service_type=='basic':
                        amount=0.0
                    
                else:
                    if service_type=='expert':
                        rate=list.technical_rate
                    elif service_type=='assistant':
                        rate=list.assistant_rate
                    elif service_type=='basic':
                        rate=0.0
                    is_extra=self._is_extra_hour_pricelist(cr,uid,date,account,origin_start_time,origin_end_time,context={})
                    if is_extra==True:
                        count_extra+=original_quantity
                        if timesheets_before or timesheets_after:
                            for timesheet in timesheets_before:
                                if timesheet.end_time==origin_start_time:
                                    if self._is_extra_hour_pricelist(cr,uid,timesheet.date,account,timesheet.start_time,timesheet.end_time,context={})==True:
                                        count_extra+=timesheet.end_time-timesheet.start_time
                                        origin_start_time=timesheet.start_time
                                    else:
                                        is_extra=False
                            for timesheet in timesheets_after:
                                if timesheet.start_time==origin_end_time:
                                    if self._is_extra_hour_pricelist(cr,uid,timesheet.date,account,timesheet.start_time,timesheet.end_time,context={})==True:
                                        count_extra+=timesheet.end_time-timesheet.start_time
                                        origin_end_time=timesheet.end_time
                            if count_extra<1 and is_extra==False:
                                amount=rate*multiple
                            elif count_extra>=1:
                                amount=rate*list.overtime_multiplier*multiple
                            else:
                                amount=rate*list.overtime_multiplier*multiple
                        else:
                            amount=rate*list.overtime_multiplier*multiple
                    else:
                        amount=rate*multiple
        return qty,amount
    def _analysis_all(self, cr, uid, ids, fields, arg, context=None):
        total=0.0
        res=super(account_analytic_account,self)._analysis_all(cr, uid, ids, fields, arg, context)
        accounts = self.browse(cr, uid, ids, context=context)
        for f in fields:
            if f == 'ca_to_invoice':
                for account in accounts:
                    if account.pricelist_ids:
{'fraction_non_alphanumeric': 0.10244446698448119, 'fraction_numerical': 0.003042904959935085, 'mean_word_length': 1.9278396436525613, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '935406', 'n_tokens_mistral': 5599, 'n_tokens_neox': 5101, 'n_words': 867}