tracker.py 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Copyright (c) 2013, Fafadia Tech and contributors
  2. # For license information, please see license.txt
  3. from __future__ import unicode_literals
  4. import frappe
  5. from frappe import _
  6. import datetime
  7. def get_po_total(currency, grand_total):
  8. currency_symbol = frappe.db.get_value("Currency", currency, "symbol")
  9. return currency_symbol + " " + str(grand_total)
  10. def get_assigned_to(purchase_order):
  11. q = "SELECT _assign FROM `tabPurchase Order` WHERE name='{0}'".format(purchase_order)
  12. results = frappe.db.sql(q)
  13. if len(results) == 0 or len(results[0]) == 0 or not results[0][0]:
  14. return ""
  15. result = eval(results[0][0])
  16. if result:
  17. email = result[0]
  18. q = "SELECT full_name FROM `tabUser` WHERE email='{0}'".format(email)
  19. results = frappe.db.sql(q)
  20. if len(result) > 0:
  21. return results[0][0]
  22. return ""
  23. def get_comment(comment):
  24. if comment:
  25. return comment.replace("\n","<br>").strip()
  26. return ""
  27. def get_date_of_email(purchase_order):
  28. results = frappe.get_all("Communication",filters={"reference_name":purchase_order})
  29. if len(results) == 0:
  30. return ""
  31. # first time email doctype
  32. doc_name = results[-1]["name"]
  33. comm_date = frappe.get_value("Communication", doc_name, "communication_date")
  34. if comm_date:
  35. return comm_date
  36. def get_so(so_values):
  37. so = None
  38. for sales_order in so_values:
  39. if sales_order[0]:
  40. return sales_order[0]
  41. def execute(filters=None):
  42. columns, data = [], []
  43. columns = [
  44. {"fieldname": "date", "label": _("Date"), "fieldtype": "date",},
  45. {"fieldname": "gtl_so_ref", "label": _("GTL SO Ref"), "fieldtype": "data",},
  46. {"fieldname": "so_value", "label": _("SO Value"), "fieldtype":"data"},
  47. {"fieldname": "rfq_no", "label": _("RFQ/GTL Ref"), "fieldtype":"data"},
  48. {"fieldname": "delivery_date", "label": _("Delivery Date"), "fieldtype":"date"},
  49. {"fieldname": "po_rfq_no", "label": _("GTL PO to supplier/RFQ no"), "fieldtype":"data"},
  50. {"fieldname": "supplier", "label": _("Supplier"), "fieldtype":"data"},
  51. {"fieldname": "contact", "label": _("Product/Contact Person"), "fieldtype":"data"},
  52. {"fieldname": "transaction_date", "label": _("Date - PO sent to Supplier"), "fieldtype":"date"},
  53. {"fieldname": "po_grand_total", "label": _("PO Value to Supplier"), "fieldtype":"Currency/currency"},
  54. {"fieldname": "so_po", "label": _("RDD to Supplier"), "fieldtype":"date"},
  55. {"fieldname": "diff_so", "label": _("Current Delivery status"), "fieldtype":"data"},
  56. {"fieldname": "assigned_user", "label": _("Person Handling the order"), "fieldtype":"data"},
  57. {"fieldname": "gtl_pay_status", "label": _("PAID"), "fieldtype":"data"},
  58. {"fieldname": "workflow_status", "label": _("Category"), "fieldtype":"data"},
  59. {"fieldname": "gtl_status", "label": _("Status"), "fieldtype":"data"},
  60. {"fieldname": "gtl_comment", "label": _("Comments"), "fieldtype":"data"},
  61. ]
  62. for vro in frappe.get_all("Purchase Order", filters={"status":["not in",("Draft", "Cancelled")]}):
  63. so = get_so(frappe.db.sql(f"SELECT sales_order FROM `tabPurchase Order Item` WHERE parent='{vro.name}'"))
  64. if so:
  65. so_q = f"SELECT transaction_date, gtl_so_id, grand_total, rfq_no, delivery_date, status FROM `tabSales Order` WHERE name='{so}'"
  66. so_values = frappe.db.sql(so_q, as_dict=1)[0]
  67. po_q = f"SELECT gtl_po_no, supplier, contact_display, currency, grand_total, glt_status, gtl_po_paid, schedule_date, gtl_po_comment FROM `tabPurchase Order` WHERE name='{vro.name}'"
  68. po_values = frappe.db.sql(po_q, as_dict=1)[0]
  69. data.append({"date": frappe.utils.formatdate(so_values["transaction_date"], "MM-dd-YYYY"),
  70. "gtl_so_ref":so_values["gtl_so_id"],
  71. "so_value":so_values["grand_total"],
  72. "rfq_no":so_values["rfq_no"],
  73. "delivery_date": frappe.utils.formatdate(so_values["delivery_date"], "MM-dd-YYYY"),
  74. "po_rfq_no":po_values["gtl_po_no"],
  75. "supplier":po_values["supplier"],
  76. "contact":po_values["contact_display"],
  77. "transaction_date":frappe.utils.formatdate(get_date_of_email(vro.name), "dd-MMM-YYYY"),
  78. "po_grand_total":get_po_total(po_values["currency"],po_values["grand_total"]),
  79. "workflow_status":so_values["status"],
  80. "gtl_status": po_values["glt_status"],
  81. "gtl_pay_status":po_values["gtl_po_paid"],
  82. "so_po":frappe.utils.formatdate(po_values["schedule_date"], "MM-dd-YYYY"),
  83. "diff_so":(so_values["delivery_date"] - po_values["schedule_date"]).days,
  84. "assigned_user": get_assigned_to(vro.name),
  85. "gtl_comment": get_comment(po_values["gtl_po_comment"])
  86. })
  87. return columns, data