blob: b61b88bf078db6c77fe0bfd0e7c3f808fabd7f66 [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")
Nabin Hait34c551d2019-07-03 10:34:31 +053031 o.opportunity_from = "Lead"
Prateeksha Singh3aed5c22018-08-01 17:32:13 +053032 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,
Prateeksha Singh3aed5c22018-08-01 17:32:13 +053071 'is_primary_contact': 1,
72 'links': [
73 {'link_doctype': 'Supplier', 'link_name': supplier.supplier_name}
74 ]
Himanshufffdb6f2019-09-02 15:57:45 +053075 })
Himanshu25ab1e42019-09-30 10:08:15 +053076 contact.add_email(supplier.supplier_email, is_primary=True)
Himanshufffdb6f2019-09-02 15:57:45 +053077 contact.insert()
Prateeksha Singh3aed5c22018-08-01 17:32:13 +053078 else:
79 contact = frappe.get_doc('Contact', contact_name)
80
81 return contact
82
83def make_item(item):
84 # make item if not already exists
85 item = frappe._dict(json.loads(item))
86
87 if not frappe.db.exists('Item', {'item_code': item.item_code}):
88 item_doc = frappe.get_doc({
89 'doctype': 'Item',
90 'item_code': item.item_code,
91 'item_group': item.item_group,
92 'is_item_from_hub': 1
93 }).insert()
94 else:
95 item_doc = frappe.get_doc('Item', item.item_code)
96
97 return item_doc
98
99def make_rfq(item, supplier, contact):
100 # make rfq
101 rfq = frappe.get_doc({
102 'doctype': 'Request for Quotation',
103 'transaction_date': nowdate(),
104 'status': 'Draft',
Faris Ansarif9a66c12018-08-31 16:15:06 +0530105 'company': frappe.db.get_single_value('Marketplace Settings', 'company'),
Prateeksha Singh3aed5c22018-08-01 17:32:13 +0530106 'message_for_supplier': 'Please supply the specified items at the best possible rates',
107 'suppliers': [
108 { 'supplier': supplier.name, 'contact': contact.name }
109 ],
110 'items': [
111 {
112 'item_code': item.item_code,
113 'qty': 1,
114 'schedule_date': nowdate(),
115 'warehouse': item.default_warehouse or get_root_of("Warehouse"),
116 'description': item.description,
117 'uom': item.stock_uom
118 }
119 ]
120 }).insert()
121
122 rfq.save()
123 rfq.submit()
124 return rfq
125
126def send_opportunity(contact):
127 # Make Hub Message on Hub with lead data
128 doc = {
129 'doctype': 'Lead',
Faris Ansarif9a66c12018-08-31 16:15:06 +0530130 'lead_name': frappe.db.get_single_value('Marketplace Settings', 'company'),
131 'email_id': frappe.db.get_single_value('Marketplace Settings', 'user')
Prateeksha Singh3aed5c22018-08-01 17:32:13 +0530132 }
133
134 args = frappe._dict(dict(
135 doctype='Hub Message',
136 reference_doctype='Lead',
137 data=json.dumps(doc),
138 user=contact.email_id
139 ))
140
141 connection = get_hub_connection()
142 response = connection.insert('Hub Message', args)
143
144 return response.ok