validations.py.bak 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. results = ""
  19. for current in doc.items:
  20. supplier_quotes = []
  21. for quote in get_supplier_quotes(current.item_code):
  22. supplier_quotes.append(quote['parent'])
  23. if len(supplier_quotes) > 0:
  24. results += "Item %s has Supplier Quotes: %s <br/><hr/>" % (current.item_code, ",".join(supplier_quotes))
  25. supplier_quotes = []
  26. manufaturer_part_no = get_manufaturer_part_no(current.item_code)
  27. # we didn't find manufaturer_part_no
  28. # hence no point processing it furthure
  29. if manufaturer_part_no.strip() == "":
  30. continue
  31. for quote in get_supplier_quotes_by_mpn(manufaturer_part_no):
  32. supplier_quotes.append(quote['parent'])
  33. if len(supplier_quotes) > 0:
  34. results += "Item %s{MPN:%s} has Supplier Quotes: %s <br/><hr/>" % (current.item_code, manufaturer_part_no, ",".join(supplier_quotes))
  35. if results.strip() != "":
  36. frappe.msgprint(_(results))
  37. def get_marked_up_prices(opportunity_name):
  38. q = "SELECT name,supplier FROM `tabSupplier Quotation` WHERE opportunity='{0}' AND status='Submitted';".format(opportunity_name)
  39. quotes = frappe.db.sql(q, as_dict=True)
  40. results = {}
  41. suppliers = {}
  42. for current in quotes:
  43. rate_q = "SELECT item_code,rate FROM `tabSupplier Quotation Item` WHERE parent='{0}'".format(current['name'])
  44. rate_results = frappe.db.sql(rate_q, as_dict=True)
  45. for item in rate_results:
  46. results[item['item_code']] = item['rate'] * 1.15
  47. suppliers[item['item_code']] = current["supplier"]
  48. return results, suppliers
  49. def quote_validations(doc, method):
  50. marked_up_prices, suppliers = get_marked_up_prices(doc.opportunity)
  51. if marked_up_prices:
  52. for current in doc.items:
  53. if current.rate == 0.0:
  54. if current.item_code not in marked_up_prices:
  55. current.rate = 0
  56. current.amount = 0
  57. else:
  58. current.rate = marked_up_prices[current.item_code]
  59. current.amount = current.rate * current.qty
  60. if current.item_code in suppliers:
  61. current.supplier = suppliers[current.item_code]
  62. def quote_update(doc, method):
  63. doc.quote_reviewed_by = frappe.session.user
  64. def purchase_order_validation(doc, method):
  65. # fetch unit price from iro to vro
  66. for item in doc.items:
  67. if item.sales_order:
  68. so = frappe.get_doc("Sales Order", item.sales_order)
  69. shipping_address = so.shipping_address.replace("<br>"," ")
  70. doc.gtl_ship_address = shipping_address
  71. for so_item in so.items:
  72. if so_item.supplier == doc.supplier_name:
  73. item.gtl_unit_price_usd = so_item.rate
  74. for item in doc.items:
  75. total_amt = item.qty*item.gtl_unit_price_usd
  76. item.gtl_total_price_usd = total_amt
  77. supplier_name = frappe.get_list("Supplier Quotation",{"rfq_no":doc.rfq_no})
  78. if supplier_name:
  79. for supp_doc_name in supplier_name:
  80. supp_doc = frappe.get_doc("Supplier Quotation",supp_doc_name["name"])
  81. for supp_item in supp_doc.items:
  82. for vro_item in doc.items:
  83. if vro_item.item_name == supp_item.item_name:
  84. vro_item.rate = supp_item.rate
  85. vro_item.amount = supp_item.amount
  86. frappe.logger().debug(vro_item.rate)
  87. frappe.logger().debug(vro_item.amount)
  88. return True
  89. def todo_validation(doc, method):
  90. if doc.reference_type == "Quotation":
  91. quote_doc = frappe.get_doc(doc.reference_type,doc.reference_name)
  92. doc.rfq_no = quote_doc.rfq_no
  93. doc.total = quote_doc.total
  94. if doc.reference_type == "Purchase Order":
  95. po_doc = frappe.get_doc(doc.reference_type,doc.reference_name)
  96. doc.po_no = po_doc.po_no
  97. doc.total = po_doc.total