Prateeksha Singh | 3aed5c2 | 2018-08-01 17:32:13 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
| 2 | import frappe, requests, json |
| 3 | from frappe.utils import now, nowdate |
| 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 | |
| 21 | @frappe.whitelist() |
| 22 | def get_item_favourites(start=0, limit=20, fields=["*"], order_by=None): |
| 23 | doctype = 'Hub Item' |
| 24 | hub_settings = frappe.get_doc('Hub Settings') |
| 25 | item_names_str = hub_settings.get('custom_data') or '[]' |
| 26 | item_names = json.loads(item_names_str) |
| 27 | filters = json.dumps({ |
| 28 | 'hub_item_code': ['in', item_names] |
| 29 | }) |
| 30 | return get_list(doctype, start, limit, fields, filters, order_by) |
| 31 | |
| 32 | @frappe.whitelist() |
| 33 | def update_wishlist_item(item_name, remove=0): |
| 34 | remove = int(remove) |
| 35 | hub_settings = frappe.get_doc('Hub Settings') |
| 36 | data = hub_settings.get('custom_data') |
| 37 | if not data or not json.loads(data): |
| 38 | data = '[]' |
| 39 | hub_settings.custom_data = data |
| 40 | hub_settings.save() |
| 41 | |
| 42 | item_names_str = data |
| 43 | item_names = json.loads(item_names_str) |
| 44 | if not remove and item_name not in item_names: |
| 45 | item_names.append(item_name) |
| 46 | if remove and item_name in item_names: |
| 47 | item_names.remove(item_name) |
| 48 | |
| 49 | item_names_str = json.dumps(item_names) |
| 50 | |
| 51 | hub_settings.custom_data = item_names_str |
| 52 | hub_settings.save() |
| 53 | |
| 54 | @frappe.whitelist() |
| 55 | def update_category(hub_item_code, category): |
| 56 | connection = get_hub_connection() |
| 57 | |
| 58 | # args = frappe._dict(dict( |
| 59 | # doctype='Hub Category', |
| 60 | # hub_category_name=category |
| 61 | # )) |
| 62 | # response = connection.insert('Hub Category', args) |
| 63 | |
| 64 | response = connection.update('Hub Item', frappe._dict(dict( |
| 65 | doctype='Hub Item', |
| 66 | hub_category = category |
| 67 | )), hub_item_code) |
| 68 | |
| 69 | return response |
| 70 | |
| 71 | def make_opportunity(buyer_name, email_id): |
| 72 | buyer_name = "HUB-" + buyer_name |
| 73 | |
| 74 | if not frappe.db.exists('Lead', {'email_id': email_id}): |
| 75 | lead = frappe.new_doc("Lead") |
| 76 | lead.lead_name = buyer_name |
| 77 | lead.email_id = email_id |
| 78 | lead.save(ignore_permissions=True) |
| 79 | |
| 80 | o = frappe.new_doc("Opportunity") |
| 81 | o.enquiry_from = "Lead" |
| 82 | o.lead = frappe.get_all("Lead", filters={"email_id": email_id}, fields = ["name"])[0]["name"] |
| 83 | o.save(ignore_permissions=True) |
| 84 | |
| 85 | @frappe.whitelist() |
| 86 | def make_rfq_and_send_opportunity(item, supplier): |
| 87 | supplier = make_supplier(supplier) |
| 88 | contact = make_contact(supplier) |
| 89 | item = make_item(item) |
| 90 | rfq = make_rfq(item, supplier, contact) |
| 91 | status = send_opportunity(contact) |
| 92 | |
| 93 | return { |
| 94 | 'rfq': rfq, |
| 95 | 'hub_document_created': status |
| 96 | } |
| 97 | |
| 98 | def make_supplier(supplier): |
| 99 | # make supplier if not already exists |
| 100 | supplier = frappe._dict(json.loads(supplier)) |
| 101 | |
| 102 | if not frappe.db.exists('Supplier', {'supplier_name': supplier.supplier_name}): |
| 103 | supplier_doc = frappe.get_doc({ |
| 104 | 'doctype': 'Supplier', |
| 105 | 'supplier_name': supplier.supplier_name, |
| 106 | 'supplier_group': supplier.supplier_group, |
| 107 | 'supplier_email': supplier.supplier_email |
| 108 | }).insert() |
| 109 | else: |
| 110 | supplier_doc = frappe.get_doc('Supplier', supplier.supplier_name) |
| 111 | |
| 112 | return supplier_doc |
| 113 | |
| 114 | def make_contact(supplier): |
| 115 | contact_name = get_default_contact('Supplier', supplier.supplier_name) |
| 116 | # make contact if not already exists |
| 117 | if not contact_name: |
| 118 | contact = frappe.get_doc({ |
| 119 | 'doctype': 'Contact', |
| 120 | 'first_name': supplier.supplier_name, |
| 121 | 'email_id': supplier.supplier_email, |
| 122 | 'is_primary_contact': 1, |
| 123 | 'links': [ |
| 124 | {'link_doctype': 'Supplier', 'link_name': supplier.supplier_name} |
| 125 | ] |
| 126 | }).insert() |
| 127 | else: |
| 128 | contact = frappe.get_doc('Contact', contact_name) |
| 129 | |
| 130 | return contact |
| 131 | |
| 132 | def make_item(item): |
| 133 | # make item if not already exists |
| 134 | item = frappe._dict(json.loads(item)) |
| 135 | |
| 136 | if not frappe.db.exists('Item', {'item_code': item.item_code}): |
| 137 | item_doc = frappe.get_doc({ |
| 138 | 'doctype': 'Item', |
| 139 | 'item_code': item.item_code, |
| 140 | 'item_group': item.item_group, |
| 141 | 'is_item_from_hub': 1 |
| 142 | }).insert() |
| 143 | else: |
| 144 | item_doc = frappe.get_doc('Item', item.item_code) |
| 145 | |
| 146 | return item_doc |
| 147 | |
| 148 | def make_rfq(item, supplier, contact): |
| 149 | # make rfq |
| 150 | rfq = frappe.get_doc({ |
| 151 | 'doctype': 'Request for Quotation', |
| 152 | 'transaction_date': nowdate(), |
| 153 | 'status': 'Draft', |
| 154 | 'company': frappe.db.get_single_value('Hub Settings', 'company'), |
| 155 | 'message_for_supplier': 'Please supply the specified items at the best possible rates', |
| 156 | 'suppliers': [ |
| 157 | { 'supplier': supplier.name, 'contact': contact.name } |
| 158 | ], |
| 159 | 'items': [ |
| 160 | { |
| 161 | 'item_code': item.item_code, |
| 162 | 'qty': 1, |
| 163 | 'schedule_date': nowdate(), |
| 164 | 'warehouse': item.default_warehouse or get_root_of("Warehouse"), |
| 165 | 'description': item.description, |
| 166 | 'uom': item.stock_uom |
| 167 | } |
| 168 | ] |
| 169 | }).insert() |
| 170 | |
| 171 | rfq.save() |
| 172 | rfq.submit() |
| 173 | return rfq |
| 174 | |
| 175 | def send_opportunity(contact): |
| 176 | # Make Hub Message on Hub with lead data |
| 177 | doc = { |
| 178 | 'doctype': 'Lead', |
| 179 | 'lead_name': frappe.db.get_single_value('Hub Settings', 'company'), |
| 180 | 'email_id': frappe.db.get_single_value('Hub Settings', 'user') |
| 181 | } |
| 182 | |
| 183 | args = frappe._dict(dict( |
| 184 | doctype='Hub Message', |
| 185 | reference_doctype='Lead', |
| 186 | data=json.dumps(doc), |
| 187 | user=contact.email_id |
| 188 | )) |
| 189 | |
| 190 | connection = get_hub_connection() |
| 191 | response = connection.insert('Hub Message', args) |
| 192 | |
| 193 | return response.ok |