Prateeksha Singh | 3aed5c2 | 2018-08-01 17:32:13 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
Prateeksha Singh | 02c176c | 2018-08-29 14:27:47 +0530 | [diff] [blame] | 2 | import frappe, json |
| 3 | from frappe.utils import nowdate |
Prateeksha Singh | 3aed5c2 | 2018-08-01 17:32:13 +0530 | [diff] [blame] | 4 | from frappe.frappeclient import FrappeClient |
Prateeksha Singh | b614ece | 2018-08-06 17:48:45 +0530 | [diff] [blame] | 5 | from frappe.utils.nestedset import get_root_of |
| 6 | from frappe.contacts.doctype.contact.contact import get_default_contact |
| 7 | |
| 8 | def get_list(doctype, start, limit, fields, filters, order_by): |
| 9 | pass |
| 10 | |
| 11 | def 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 Singh | 3aed5c2 | 2018-08-01 17:32:13 +0530 | [diff] [blame] | 20 | |
Prateeksha Singh | 3aed5c2 | 2018-08-01 17:32:13 +0530 | [diff] [blame] | 21 | def 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 Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 31 | o.opportunity_from = "Lead" |
Prateeksha Singh | 3aed5c2 | 2018-08-01 17:32:13 +0530 | [diff] [blame] | 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() |
| 36 | def 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 | |
| 48 | def 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 | |
| 64 | def 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 Singh | 3aed5c2 | 2018-08-01 17:32:13 +0530 | [diff] [blame] | 71 | 'is_primary_contact': 1, |
| 72 | 'links': [ |
| 73 | {'link_doctype': 'Supplier', 'link_name': supplier.supplier_name} |
| 74 | ] |
Himanshu | fffdb6f | 2019-09-02 15:57:45 +0530 | [diff] [blame] | 75 | }) |
Himanshu | 25ab1e4 | 2019-09-30 10:08:15 +0530 | [diff] [blame] | 76 | contact.add_email(supplier.supplier_email, is_primary=True) |
Himanshu | fffdb6f | 2019-09-02 15:57:45 +0530 | [diff] [blame] | 77 | contact.insert() |
Prateeksha Singh | 3aed5c2 | 2018-08-01 17:32:13 +0530 | [diff] [blame] | 78 | else: |
| 79 | contact = frappe.get_doc('Contact', contact_name) |
| 80 | |
| 81 | return contact |
| 82 | |
| 83 | def 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 | |
| 99 | def 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 Ansari | f9a66c1 | 2018-08-31 16:15:06 +0530 | [diff] [blame] | 105 | 'company': frappe.db.get_single_value('Marketplace Settings', 'company'), |
Prateeksha Singh | 3aed5c2 | 2018-08-01 17:32:13 +0530 | [diff] [blame] | 106 | '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 | |
| 126 | def send_opportunity(contact): |
| 127 | # Make Hub Message on Hub with lead data |
| 128 | doc = { |
| 129 | 'doctype': 'Lead', |
Faris Ansari | f9a66c1 | 2018-08-31 16:15:06 +0530 | [diff] [blame] | 130 | 'lead_name': frappe.db.get_single_value('Marketplace Settings', 'company'), |
| 131 | 'email_id': frappe.db.get_single_value('Marketplace Settings', 'user') |
Prateeksha Singh | 3aed5c2 | 2018-08-01 17:32:13 +0530 | [diff] [blame] | 132 | } |
| 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 |