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