from odoo import models, fields, api import re from odoo.exceptions import ValidationError from datetime import datetime, timedelta class SosInsideSalesActivities(models.Model): _name = 'sos_inside_sales_activities' _description = 'Inside Sales executive activities log' _rec_name="company_name" company_name = fields.Char(string="Company Name") entry_date = fields.Date(string="Date", default=fields.Date.today) contact_person = fields.Char(string="Contact Person") designation = fields.Char(string="Designation") email = fields.Char(string="Email Id") mobile_number = fields.Char(string="Contact Number") location = fields.Char(string="Location") website_url = fields.Char(string="Website URL") vertical_domain = fields.Selection( [ ('battery_mfg', 'Battery Mfg'), ('cements', 'Cements'), ('data_centre', 'Data Centre'), ('ess', 'ESS'), ('ev', 'EV'), ('hospital', 'Hospital'), ('hotels', 'Hotels'), ('inverter_mfg', 'Inverter Mfg'), ('metal_mfg', 'Metal Mfg'), ('o_and_g', 'O & G'), ('power_distribution', 'Power Distribution'), ('power_generation', 'Power Generation'), ('processing', 'Processing'), ], string="Domain / Industry" ) linkedin_profile = fields.Char(string="Linkedin profile") products = fields.Selection( [ ('BHMS 1.2V', 'BHMS 1.2V'), ('BHMS 2V', 'BHMS 2V'), ('BHMS 12V', 'BHMS 12V'), ('BMS-HV', 'BMS-HV'), ('BMS-LV 100A', 'BMS-LV 100A'), ('BMS-LV 40A', 'BMS-LV 40A'), ('SBMS 55A', 'SBMS 55A'), ('MC 250W', 'MC 250W'), ('HeartTarang', 'HeartTarang') ], string="Product Interested In",required=True) source = fields.Selection( [ ('website', 'Website'), ('expo', 'Expo'), ('cold_calls', 'Cold Calls'), ('linkedin', 'Linkedin') ], string="Source") expo_name = fields.Char(string="Expo Name") sales_person = fields.Many2one( 'res.users', string='Sales Executive', default=lambda self: self.env.user, readonly=True ) line_ids = fields.One2many('sos_inside_sales_activities_line', 'ref_id', string="Action") week = fields.Char(string="Week", compute="_compute_week", store=True) reporting_to = fields.Many2one('res.users', string='Reporting To') @api.depends('entry_date') def _compute_week(self): for record in self: if record.entry_date: entry_date = record.entry_date start_of_week = entry_date - timedelta(days=entry_date.weekday()) # Monday end_of_week = start_of_week + timedelta(days=6) # Sunday # Determine week number first_day_of_month = entry_date.replace(day=1) week_number = ((entry_date - first_day_of_month).days // 7) + 1 # Exclude Saturdays for 2nd and 4th week if week_number in [2, 4]: end_of_week = start_of_week + timedelta(days=4) # Friday record.week = f"{start_of_week.strftime('%d/%m/%Y')} to {end_of_week.strftime('%d/%m/%Y')}" TARGET_PER_WEEK = 4 @api.model def create(self, vals): create_uid = vals.get('create_uid', self.env.uid) create_user = self.env['res.users'].browse(create_uid) vals['reporting_to'] = create_user.reporting_to.id if 'sales_person' not in vals: vals['sales_person'] = self.env.user.id # Set the current user if 'entry_date' in vals: entry_date = fields.Date.from_string(vals['entry_date']) start_of_week = entry_date - timedelta(days=entry_date.weekday()) # Monday end_of_week = start_of_week + timedelta(days=6) # Sunday first_day_of_month = entry_date.replace(day=1) week_number = ((entry_date - first_day_of_month).days // 7) + 1 if week_number in [2, 4]: end_of_week = start_of_week + timedelta(days=4) # Friday week_str = f"{start_of_week.strftime('%d/%m/%Y')} to {end_of_week.strftime('%d/%m/%Y')}" vals['week'] = week_str # Assign computed week to vals before record creation sos_lines = self.env['sos_target_tracker'].search([ ('week', '=', week_str), ('inside_sales_executive', '=', vals.get('sales_person')) ]) new_week_entry = sos_lines.weekly_entries_count + 1 if new_week_entry < self.TARGET_PER_WEEK: remaining = self.TARGET_PER_WEEK - new_week_entry weekly_status = f"{remaining} more to achieve this week's target." elif new_week_entry == self.TARGET_PER_WEEK: weekly_status = "Target achieved for this week!" else: weekly_status = "Well performed! Exceeded weekly target!" if sos_lines: sos_lines.write({'weekly_entries_count': new_week_entry,'entry_date':self.entry_date, 'weekly_status':weekly_status}) else: self.env['sos_target_tracker'].create({ 'entry_date': vals['entry_date'], 'inside_sales_executive': vals['sales_person'], 'week': week_str, 'weekly_entries_count':1 }) record = super(SosInsideSalesActivities, self).create(vals) return record class sos_inside_sales_activities_lines(models.Model): _name = 'sos_inside_sales_activities_line' _description = 'Inside Sales executive activities log lines' ref_id = fields.Many2one('sos_inside_sales_activities', ondelete="cascade") action_date = fields.Date(string="Action Date") remarks = fields.Text(string="Remarks")