validations.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import frappe
  2. from frappe import _
  3. def get_supplier_quotes(item_code):
  4. q = "SELECT parent FROM `tabSupplier Quotation Item` WHERE item_code='{0}' ORDER BY modified DESC LIMIT 3;".format(item_code)
  5. results = frappe.db.sql(q, as_dict=True)
  6. return results
  7. def get_manufaturer_part_no(item_code):
  8. q = "SELECT manufacturer_part_no FROM `tabItem Manufacturer` WHERE parent='{0}' LIMIT 1;".format(item_code)
  9. results = frappe.db.sql(q, as_dict=True)
  10. if len(results) > 0:
  11. return results[0]['manufacturer_part_no']
  12. return ""
  13. def get_supplier_quotes_by_mpn(manufacturer_part_no):
  14. q = "SELECT parent FROM `tabSupplier Quotation Item` WHERE manufacturer_part_no='{0}' ORDER BY modified DESC LIMIT 3;".format(manufacturer_part_no)
  15. results = frappe.db.sql(q, as_dict=True)
  16. return results
  17. def rfq_validations(doc, method):
  18. """
  19. Message print of values of supplier quotation id manufacturer
  20. part no associated with the item code
  21. """
  22. results = ""
  23. for current in doc.items:
  24. supplier_quotes = []
  25. for quote in get_supplier_quotes(current.item_code):
  26. supplier_quotes.append(quote['parent'])
  27. if len(supplier_quotes) > 0:
  28. results += "Item %s has Supplier Quotes: %s <br/><hr/>" % (current.item_code, ",".join(supplier_quotes))
  29. supplier_quotes = []
  30. manufaturer_part_no = get_manufaturer_part_no(current.item_code)
  31. # we didn't find manufaturer_part_no
  32. # hence no point processing it furthure
  33. if manufaturer_part_no.strip() == "":
  34. continue
  35. for quote in get_supplier_quotes_by_mpn(manufaturer_part_no):
  36. supplier_quotes.append(quote['parent'])
  37. if len(supplier_quotes) > 0:
  38. results += "Item %s{MPN:%s} has Supplier Quotes: %s <br/><hr/>" % (current.item_code, manufaturer_part_no, ",".join(supplier_quotes))
  39. if results.strip() != "":
  40. frappe.msgprint(_(results))
  41. def get_marked_up_prices(opportunity_name):
  42. q = "SELECT name,supplier FROM `tabSupplier Quotation` WHERE opportunity='{0}' AND status='Submitted';".format(opportunity_name)
  43. quotes = frappe.db.sql(q, as_dict=True)
  44. results = {}
  45. suppliers = {}
  46. for current in quotes:
  47. rate_q = "SELECT item_code,rate FROM `tabSupplier Quotation Item` WHERE parent='{0}'".format(current['name'])
  48. rate_results = frappe.db.sql(rate_q, as_dict=True)
  49. for item in rate_results:
  50. results[item['item_code']] = item['rate'] * 1.15
  51. suppliers[item['item_code']] = current["supplier"]
  52. return results, suppliers
  53. def quote_validations(doc, method):
  54. """
  55. Setting up rate, amount, and supplier in item table from previous supplier quotation
  56. based on opporunity.
  57. """
  58. marked_up_prices, suppliers = get_marked_up_prices(doc.opportunity)
  59. if marked_up_prices:
  60. for current in doc.items:
  61. if current.rate == 0.0:
  62. if current.item_code not in marked_up_prices:
  63. current.rate = 0
  64. current.amount = 0
  65. else:
  66. current.rate = marked_up_prices[current.item_code]
  67. current.amount = current.rate * current.qty
  68. if current.item_code in suppliers:
  69. current.supplier = suppliers[current.item_code]
  70. def quote_update(doc, method):
  71. doc.quote_reviewed_by = frappe.session.user
  72. def purchase_order_validation(doc, method):
  73. # fetch unit price from iro to vro
  74. for item in doc.items:
  75. if item.sales_order:
  76. so = frappe.get_doc("Sales Order", item.sales_order)
  77. if so.shipping_address:
  78. shipping_address = so.shipping_address.replace("<br>"," ")
  79. shipping_address = so.shipping_address
  80. doc.gtl_ship_address = shipping_address
  81. for so_item in so.items:
  82. if so_item.supplier == doc.supplier_name:
  83. item.gtl_unit_price_usd = so_item.rate
  84. for item in doc.items:
  85. total_amt = item.qty*item.gtl_unit_price_usd
  86. item.gtl_total_price_usd = total_amt
  87. supplier_name = frappe.get_list("Supplier Quotation",{"rfq_no":doc.rfq_no})
  88. if supplier_name:
  89. for supp_doc_name in supplier_name:
  90. supp_doc = frappe.get_doc("Supplier Quotation",supp_doc_name["name"])
  91. for supp_item in supp_doc.items:
  92. for vro_item in doc.items:
  93. if vro_item.item_name == supp_item.item_name:
  94. if vro_item.rate not in [0,supp_item.rate] or vro_item.qty != supp_item.qty:
  95. return True
  96. vro_item.rate = supp_item.rate
  97. vro_item.amount = supp_item.amount
  98. return True
  99. def todo_validation(doc, method):
  100. """
  101. Fetch Values from different References.
  102. """
  103. if doc.reference_type == "Quotation":
  104. quote_doc = frappe.get_doc(doc.reference_type,doc.reference_name)
  105. doc.rfq_no = quote_doc.rfq_no
  106. doc.total = quote_doc.total
  107. if doc.reference_type == "Purchase Order":
  108. po_doc = frappe.get_doc(doc.reference_type,doc.reference_name)
  109. doc.po_no = po_doc.po_no
  110. doc.total = po_doc.total