summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiqdad <ahmadmiqdad27@gmail.com>2025-06-14 09:10:12 +0700
committerMiqdad <ahmadmiqdad27@gmail.com>2025-06-14 09:10:12 +0700
commit136fc0acd87bceb21b89fc9f040bffc49c93e9f9 (patch)
treeb454434e7f48350e5fa60b7892ab2616e588f240
parent764db5308084192d38bf346a0e9882d405a90a2e (diff)
<miqdad> restore wati
-rw-r--r--indoteknik_custom/models/wati.py166
1 files changed, 1 insertions, 165 deletions
diff --git a/indoteknik_custom/models/wati.py b/indoteknik_custom/models/wati.py
index 18517502..a0619f83 100644
--- a/indoteknik_custom/models/wati.py
+++ b/indoteknik_custom/models/wati.py
@@ -201,170 +201,6 @@ class WatiNotification(models.Model):
wati.is_lead = True
wati.lead_id = current_lead.id
- # FINAL CODE - Sesuai dengan mapping table Anda
-
- def check_wati_tags_leads(self):
- """Check tags 'leads' di WATI dan create leads di Odoo - Final Version"""
- _logger.info('=== Starting WATI Tags Check (Final) ===')
-
- wati_api = self.env['wati.api']
- total_leads_created = 0
-
- try:
- # Get WATI contacts
- wati_contacts = wati_api.http_get('/api/v1/getContacts', {'pageSize': 100, 'pageNumber': 1})
-
- if isinstance(wati_contacts, dict) and wati_contacts.get('result') == 'success':
- contact_list = wati_contacts.get('contact_list', [])
-
- for contact in contact_list:
- if self._create_lead_if_tagged(contact):
- total_leads_created += 1
-
- _logger.info('WATI check completed: %s leads created' % total_leads_created)
- return {'leads_created': total_leads_created}
-
- except Exception as e:
- _logger.error('Error in WATI tags check: %s' % str(e))
- return {'leads_created': 0, 'error': str(e)}
-
- def _create_lead_if_tagged(self, contact):
- """Create lead jika contact punya tags=leads - Sesuai Mapping Table"""
- try:
- # Check tags leads
- if not self._has_tags_leads(contact):
- return False
-
- phone = contact.get('phone', '')
- if not phone:
- return False
-
- # Check existing lead by phone
- existing_lead = self.env['crm.lead'].search([('phone', '=', phone)], limit=1)
- if existing_lead:
- _logger.info('Lead already exists for phone %s' % phone)
- return False
-
- # Extract data dari customParams sesuai mapping table
- custom_params = contact.get('customParams', [])
- contact_data = self._extract_contact_data(custom_params)
-
- # Create lead dengan field mapping yang sesuai
- lead_vals = {
- 'name': self._generate_lead_name(contact_data, contact),
- 'phone': phone, # Phone Number → Mobile
- 'contact_name': contact_data.get('name', ''), # Name → Contact Name
- 'partner_name': contact_data.get('perusahaan', ''), # Perusahaan → Company Name
- 'email_from': contact_data.get('email', ''), # Email → Email
- 'description': contact_data.get('notes', ''), # Notes → Internal Notes
- 'type': 'lead',
- 'user_id': self._get_salesperson_id(contact_data.get('sales', '')), # Sales → Salesperson
- }
-
- new_lead = self.env['crm.lead'].create(lead_vals)
- _logger.info('Created WATI lead %s for %s (%s)' % (new_lead.id, contact_data.get('name', 'Unknown'), phone))
- return True
-
- except Exception as e:
- _logger.error('Error creating lead: %s' % str(e))
- return False
-
- def _extract_contact_data(self, custom_params):
- """Extract data dari customParams sesuai mapping table"""
- contact_data = {}
-
- for param in custom_params:
- param_name = param.get('name', '').lower()
- param_value = param.get('value', '').strip()
-
- # Mapping sesuai table:
- if param_name == 'perusahaan': # Perusahaan → Company Name
- contact_data['perusahaan'] = param_value
- elif param_name == 'name': # Name → Contact Name
- contact_data['name'] = param_value
- elif param_name == 'email': # Email → Email
- contact_data['email'] = param_value
- elif param_name == 'sales': # Sales → Salesperson
- contact_data['sales'] = param_value
- elif param_name == 'notes': # Notes → Internal Notes
- contact_data['notes'] = param_value
- # Phone Number sudah diambil dari contact.phone
-
- return contact_data
-
- def _generate_lead_name(self, contact_data, contact):
- """Generate lead name sesuai mapping: Judul Leads berdasarkan company/contact"""
- company_name = contact_data.get('perusahaan', '')
- contact_name = contact_data.get('name', '') or contact.get('name', '')
-
- if company_name:
- return 'WATI Lead - %s' % company_name
- elif contact_name:
- return 'WATI Lead - %s' % contact_name
- else:
- return 'WATI Lead - %s' % contact.get('phone', 'Unknown')
-
- def _get_salesperson_id(self, sales_name):
- """Get salesperson ID dari nama - Sales → Salesperson"""
- if not sales_name:
- return 2 # Default Sales (ID 2)
-
- # Try find user by name
- user = self.env['res.users'].search([
- ('name', 'ilike', sales_name)
- ], limit=1)
-
- if user:
- return user.id
- else:
- # Fallback ke default Sales
- return 2
-
- def _has_tags_leads(self, contact):
- """Check apakah ada tags untuk tajik ke odoo => Leads"""
- custom_params = contact.get('customParams', [])
-
- for param in custom_params:
- param_name = param.get('name', '').lower()
- param_value = param.get('value', '').lower()
-
- # Check: "Judul Tags untuk tajik ke odoo => Leads"
- if param_name == 'tags' and param_value == 'leads':
- return True
-
- return False
-
- def manual_check_tags(self):
- """Manual trigger untuk testing"""
- result = self.check_wati_tags_leads()
-
- message = 'WATI Tags Check Completed!\n\n'
- message += 'Leads Created: %s\n\n' % result.get('leads_created', 0)
- message += 'Field Mapping:\n'
- message += '• Perusahaan → Company Name\n'
- message += '• Name → Contact Name\n'
- message += '• Email → Email\n'
- message += '• Sales → Salesperson\n'
- message += '• Phone Number → Mobile\n'
- message += '• Notes → Internal Notes\n'
- message += '• Tags=leads → Trigger Lead Creation'
-
- if result.get('error'):
- message += '\n\nError: %s' % result['error']
- message_type = 'warning'
- else:
- message_type = 'success'
-
- return {
- 'type': 'ir.actions.client',
- 'tag': 'display_notification',
- 'params': {
- 'title': 'WATI Tags Check',
- 'message': message,
- 'type': message_type,
- 'sticky': True,
- }
- }
class WatiHistory(models.Model):
_name = 'wati.history'
@@ -483,4 +319,4 @@ class WatiHistoryLine(models.Model):
ticket_id = fields.Char(string='Ticket ID')
type = fields.Char(string='Type')
wa_id = fields.Char(string='WA ID')
- date_wati = fields.Datetime(string='Date WATI') \ No newline at end of file
+ date_wati = fields.Datetime(string='Date WATI')