blob: 2e4c26684365b56117c7141a8127d2af7d75154a [file] [log] [blame]
Prateeksha Singh3aed5c22018-08-01 17:32:13 +05301from __future__ import unicode_literals
Chillar Anand915b3432021-09-02 16:44:59 +05302
3import json
4
5import frappe
Prateeksha Singhb614ece2018-08-06 17:48:45 +05306from frappe.contacts.doctype.contact.contact import get_default_contact
Chillar Anand915b3432021-09-02 16:44:59 +05307from frappe.frappeclient import FrappeClient
8from frappe.utils import nowdate
9from frappe.utils.nestedset import get_root_of
10
Prateeksha Singhb614ece2018-08-06 17:48:45 +053011
12def get_list(doctype, start, limit, fields, filters, order_by):
13 pass
14
15def get_hub_connection():
16 if frappe.db.exists('Data Migration Connector', 'Hub Connector'):
17 hub_connector = frappe.get_doc('Data Migration Connector', 'Hub Connector')
18 hub_connection = hub_connector.get_connection()
19 return hub_connection.connection
20
21 # read-only connection
22 hub_connection = FrappeClient(frappe.conf.hub_url)
23 return hub_connection
Prateeksha Singh3aed5c22018-08-01 17:32:13 +053024
Prateeksha Singh3aed5c22018-08-01 17:32:13 +053025def make_opportunity(buyer_name, email_id):
26 buyer_name = "HUB-" + buyer_name
27
28 if not frappe.db.exists('Lead', {'email_id': email_id}):
29 lead = frappe.new_doc("Lead")
30 lead.lead_name = buyer_name
31 lead.email_id = email_id
32 lead.save(ignore_permissions=True)
33
34 o = frappe.new_doc("Opportunity")
Nabin Hait34c551d2019-07-03 10:34:31 +053035 o.opportunity_from = "Lead"
Prateeksha Singh3aed5c22018-08-01 17:32:13 +053036 o.lead = frappe.get_all("Lead", filters={"email_id": email_id}, fields = ["name"])[0]["name"]
37 o.save(ignore_permissions=True)
38
39@frappe.whitelist()
40def make_rfq_and_send_opportunity(item, supplier):
41 supplier = make_supplier(supplier)
42 contact = make_contact(supplier)
43 item = make_item(item)
44 rfq = make_rfq(item, supplier, contact)
45 status = send_opportunity(contact)
46
47 return {
48 'rfq': rfq,
49 'hub_document_created': status
50 }
51
52def make_supplier(supplier):
53 # make supplier if not already exists
54 supplier = frappe._dict(json.loads(supplier))
55
56 if not frappe.db.exists('Supplier', {'supplier_name': supplier.supplier_name}):
57 supplier_doc = frappe.get_doc({
58 'doctype': 'Supplier',
59 'supplier_name': supplier.supplier_name,
60 'supplier_group': supplier.supplier_group,
61 'supplier_email': supplier.supplier_email
62 }).insert()
63 else:
64 supplier_doc = frappe.get_doc('Supplier', supplier.supplier_name)
65
66 return supplier_doc
67
68def make_contact(supplier):
69 contact_name = get_default_contact('Supplier', supplier.supplier_name)
70 # make contact if not already exists
71 if not contact_name:
72 contact = frappe.get_doc({
73 'doctype': 'Contact',
74 'first_name': supplier.supplier_name,
Prateeksha Singh3aed5c22018-08-01 17:32:13 +053075 'is_primary_contact': 1,
76 'links': [
77 {'link_doctype': 'Supplier', 'link_name': supplier.supplier_name}
78 ]
Himanshufffdb6f2019-09-02 15:57:45 +053079 })
Himanshu25ab1e42019-09-30 10:08:15 +053080 contact.add_email(supplier.supplier_email, is_primary=True)
Himanshufffdb6f2019-09-02 15:57:45 +053081 contact.insert()
Prateeksha Singh3aed5c22018-08-01 17:32:13 +053082 else:
83 contact = frappe.get_doc('Contact', contact_name)
84
85 return contact
86
87def make_item(item):
88 # make item if not already exists
89 item = frappe._dict(json.loads(item))
90
91 if not frappe.db.exists('Item', {'item_code': item.item_code}):
92 item_doc = frappe.get_doc({
93 'doctype': 'Item',
94 'item_code': item.item_code,
95 'item_group': item.item_group,
96 'is_item_from_hub': 1
97 }).insert()
98 else:
99 item_doc = frappe.get_doc('Item', item.item_code)
100
101 return item_doc
102
103def make_rfq(item, supplier, contact):
104 # make rfq
105 rfq = frappe.get_doc({
106 'doctype': 'Request for Quotation',
107 'transaction_date': nowdate(),
108 'status': 'Draft',
Faris Ansarif9a66c12018-08-31 16:15:06 +0530109 'company': frappe.db.get_single_value('Marketplace Settings', 'company'),
Prateeksha Singh3aed5c22018-08-01 17:32:13 +0530110 'message_for_supplier': 'Please supply the specified items at the best possible rates',
111 'suppliers': [
112 { 'supplier': supplier.name, 'contact': contact.name }
113 ],
114 'items': [
115 {
116 'item_code': item.item_code,
117 'qty': 1,
118 'schedule_date': nowdate(),
119 'warehouse': item.default_warehouse or get_root_of("Warehouse"),
120 'description': item.description,
121 'uom': item.stock_uom
122 }
123 ]
124 }).insert()
125
126 rfq.save()
127 rfq.submit()
128 return rfq
129
130def send_opportunity(contact):
131 # Make Hub Message on Hub with lead data
132 doc = {
133 'doctype': 'Lead',
Faris Ansarif9a66c12018-08-31 16:15:06 +0530134 'lead_name': frappe.db.get_single_value('Marketplace Settings', 'company'),
135 'email_id': frappe.db.get_single_value('Marketplace Settings', 'user')
Prateeksha Singh3aed5c22018-08-01 17:32:13 +0530136 }
137
138 args = frappe._dict(dict(
139 doctype='Hub Message',
140 reference_doctype='Lead',
141 data=json.dumps(doc),
142 user=contact.email_id
143 ))
144
145 connection = get_hub_connection()
146 response = connection.insert('Hub Message', args)
147
148 return response.ok