Prateeksha Singh | 3aed5c2 | 2018-08-01 17:32:13 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 2 | |
| 3 | import json |
| 4 | |
| 5 | import frappe |
Prateeksha Singh | b614ece | 2018-08-06 17:48:45 +0530 | [diff] [blame] | 6 | from frappe.contacts.doctype.contact.contact import get_default_contact |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 7 | from frappe.frappeclient import FrappeClient |
| 8 | from frappe.utils import nowdate |
| 9 | from frappe.utils.nestedset import get_root_of |
| 10 | |
Prateeksha Singh | b614ece | 2018-08-06 17:48:45 +0530 | [diff] [blame] | 11 | |
| 12 | def get_list(doctype, start, limit, fields, filters, order_by): |
| 13 | pass |
| 14 | |
| 15 | def 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 Singh | 3aed5c2 | 2018-08-01 17:32:13 +0530 | [diff] [blame] | 24 | |
Prateeksha Singh | 3aed5c2 | 2018-08-01 17:32:13 +0530 | [diff] [blame] | 25 | def 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 Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 35 | o.opportunity_from = "Lead" |
Prateeksha Singh | 3aed5c2 | 2018-08-01 17:32:13 +0530 | [diff] [blame] | 36 | 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() |
| 40 | def 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 | |
| 52 | def 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 | |
| 68 | def 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 Singh | 3aed5c2 | 2018-08-01 17:32:13 +0530 | [diff] [blame] | 75 | 'is_primary_contact': 1, |
| 76 | 'links': [ |
| 77 | {'link_doctype': 'Supplier', 'link_name': supplier.supplier_name} |
| 78 | ] |
Himanshu | fffdb6f | 2019-09-02 15:57:45 +0530 | [diff] [blame] | 79 | }) |
Himanshu | 25ab1e4 | 2019-09-30 10:08:15 +0530 | [diff] [blame] | 80 | contact.add_email(supplier.supplier_email, is_primary=True) |
Himanshu | fffdb6f | 2019-09-02 15:57:45 +0530 | [diff] [blame] | 81 | contact.insert() |
Prateeksha Singh | 3aed5c2 | 2018-08-01 17:32:13 +0530 | [diff] [blame] | 82 | else: |
| 83 | contact = frappe.get_doc('Contact', contact_name) |
| 84 | |
| 85 | return contact |
| 86 | |
| 87 | def 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 | |
| 103 | def 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 Ansari | f9a66c1 | 2018-08-31 16:15:06 +0530 | [diff] [blame] | 109 | 'company': frappe.db.get_single_value('Marketplace Settings', 'company'), |
Prateeksha Singh | 3aed5c2 | 2018-08-01 17:32:13 +0530 | [diff] [blame] | 110 | '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 | |
| 130 | def send_opportunity(contact): |
| 131 | # Make Hub Message on Hub with lead data |
| 132 | doc = { |
| 133 | 'doctype': 'Lead', |
Faris Ansari | f9a66c1 | 2018-08-31 16:15:06 +0530 | [diff] [blame] | 134 | 'lead_name': frappe.db.get_single_value('Marketplace Settings', 'company'), |
| 135 | 'email_id': frappe.db.get_single_value('Marketplace Settings', 'user') |
Prateeksha Singh | 3aed5c2 | 2018-08-01 17:32:13 +0530 | [diff] [blame] | 136 | } |
| 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 |