diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2024-10-15 11:33:36 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2024-10-15 11:33:36 +0700 |
| commit | 387fe06b8d8d212172a1ef0a61ad2657f618ec1b (patch) | |
| tree | ed24bf1cf8d275d1113754e79eeb5f2dcb4c3cdd | |
| parent | 1833d42fe880ca3c63630c46d1b2e4f19e89c9ab (diff) | |
scheduled action for pipeline
| -rwxr-xr-x | indoteknik_custom/models/crm_lead.py | 71 |
1 files changed, 70 insertions, 1 deletions
diff --git a/indoteknik_custom/models/crm_lead.py b/indoteknik_custom/models/crm_lead.py index 9ffd607c..b0d3430c 100755 --- a/indoteknik_custom/models/crm_lead.py +++ b/indoteknik_custom/models/crm_lead.py @@ -2,6 +2,7 @@ from odoo import fields, models, api import logging import random from odoo.exceptions import AccessError, UserError, ValidationError +from datetime import datetime, timedelta _logger = logging.getLogger(__name__) @@ -97,4 +98,72 @@ class CrmLead(models.Model): lead.user_id = salesperson_id - + def _cancel_pipeline(self, delta=48): + # Get the current time + current_time = datetime.now() + + # Calculate the time 24 hours ago + time_48_hours_ago = current_time - timedelta(hours=delta) + + # Define the allowed states + allowed_states = ['cancel'] + + # Search for sale orders with date_order greater than 24 hours ago and opportunity_id is null + orders = self.env['sale.order'].search([ + ('write_date', '>=', time_48_hours_ago), + ('opportunity_id', '!=', False), + ('state', 'in', allowed_states) + ]) + for order in orders: + order.opportunity_id.stage_id = 7 + _logger.info('cancel order stage pipeline %s' % order.id) + + def _convert_to_pipeline(self, delta=48): + # Get the current time + current_time = datetime.now() + + # Calculate the time 24 hours ago + time_48_hours_ago = current_time - timedelta(hours=delta) + + # Define the allowed states + allowed_states = ['draft', 'done', 'sale', 'sent', 'cancel'] + + # Search for sale orders with date_order greater than 24 hours ago and opportunity_id is null + orders = self.env['sale.order'].search([ + ('write_date', '>=', time_48_hours_ago), + ('opportunity_id', '=', False), + ('state', 'in', allowed_states) + ]) + # stage + # 1 potensi baru, 2 proses quotation, 3 proses lain visit, 4 proses berhasil, 5 proses negosiasi, 7 tidak terpakai / gagal + for order in orders: + # stage_id = 2 + if order.state == 'sale' or order.state == 'done': + stage_id = 4 + elif order.state == 'sent': + stage_id = 5 + elif order.state == 'cancel': + stage_id = 7 + else: + stage_id = 2 + crm_lead = self.env['crm.lead'].create([{ + 'email_normalized': order.email, + 'name': order.name, + 'user_id': order.user_id.id, + 'company_id': 1, + 'type': 'opportunity', + 'priority': 0, + 'team_id': order.team_id.id, + 'stage_id': stage_id, + 'expected_revenue': order.amount_untaxed, + 'partner_id': order.partner_id.parent_id.id or order.partner_id.id, + 'contact_name': order.partner_id.name, + 'partner_name': order.partner_id.parent_id.name or order.partner_id.name, + 'phone': order.partner_id.mobile, + 'street': order.partner_id.street, + 'street2': order.partner_id.street2, + 'zip': order.partner_id.zip, + 'order_id': order.id + }]) + order.opportunity_id = crm_lead.id + _logger.info('convert order to opportunity %s' % crm_lead.id) |
