def make_purchase_order(source_name, target_doclist=None): from frappe.model.mapper import get_mapped_doclist doclist = get_mapped_doclist( "Material Request", source_name, { "Material Request": { "doctype": "Purchase Order", "validation": { "docstatus": ["=", 1], "material_request_type": ["=", "Purchase"] } }, "Material Request Item": { "doctype": "Purchase Order Item", "field_map": [["name", "prevdoc_detail_docname"], ["parent", "prevdoc_docname"], ["parenttype", "prevdoc_doctype"], ["uom", "stock_uom"], ["uom", "uom"]], "postprocess": update_item } }, target_doclist, set_missing_values) return [d.fields for d in doclist]
def make_installation_note(source_name, target_doclist=None): def update_item(obj, target, source_parent): target.qty = flt(obj.qty) - flt(obj.installed_qty) target.serial_no = obj.serial_no doclist = get_mapped_doclist("Delivery Note", source_name, { "Delivery Note": { "doctype": "Installation Note", "validation": { "docstatus": ["=", 1] } }, "Delivery Note Item": { "doctype": "Installation Note Item", "field_map": { "name": "prevdoc_detail_docname", "parent": "prevdoc_docname", "parenttype": "prevdoc_doctype", }, "postprocess": update_item, "condition": lambda doc: doc.installed_qty < doc.qty } }, target_doclist) return [d.fields for d in doclist]
def make_purchase_invoice(source_name, target_doclist=None): from frappe.model.mapper import get_mapped_doclist def set_missing_values(source, target): bean = frappe.bean(target) bean.run_method("set_missing_values") doclist = get_mapped_doclist( "Purchase Receipt", source_name, { "Purchase Receipt": { "doctype": "Purchase Invoice", "validation": { "docstatus": ["=", 1], } }, "Purchase Receipt Item": { "doctype": "Purchase Invoice Item", "field_map": { "name": "pr_detail", "parent": "purchase_receipt", "prevdoc_detail_docname": "po_detail", "prevdoc_docname": "purchase_order", }, }, "Purchase Taxes and Charges": { "doctype": "Purchase Taxes and Charges", "add_if_empty": True } }, target_doclist, set_missing_values) return [d.fields for d in doclist]
def make_stock_entry(source_name, target_doclist=None): from frappe.model.mapper import get_mapped_doclist def update_item(obj, target, source_parent): target.conversion_factor = 1 target.qty = flt(obj.qty) - flt(obj.ordered_qty) target.transfer_qty = flt(obj.qty) - flt(obj.ordered_qty) def set_missing_values(source, target): target[0].purpose = "Material Transfer" se = frappe.bean(target) se.run_method("get_stock_and_rate") doclist = get_mapped_doclist( "Material Request", source_name, { "Material Request": { "doctype": "Stock Entry", "validation": { "docstatus": ["=", 1], "material_request_type": ["=", "Transfer"] } }, "Material Request Item": { "doctype": "Stock Entry Detail", "field_map": { "name": "material_request_item", "parent": "material_request", "uom": "stock_uom", "warehouse": "t_warehouse" }, "postprocess": update_item } }, target_doclist, set_missing_values) return [d.fields for d in doclist]
def make_quotation(source_name, target_doclist=None): from frappe.model.mapper import get_mapped_doclist def set_missing_values(source, target): quotation = frappe.bean(target) quotation.run_method("onload_post_render") quotation.run_method("calculate_taxes_and_totals") doclist = get_mapped_doclist( "Opportunity", source_name, { "Opportunity": { "doctype": "Quotation", "field_map": { "enquiry_from": "quotation_to", "enquiry_type": "order_type", "name": "enq_no", }, "validation": { "docstatus": ["=", 1] } }, "Opportunity Item": { "doctype": "Quotation Item", "field_map": { "parent": "prevdoc_docname", "parenttype": "prevdoc_doctype", "uom": "stock_uom" }, "add_if_empty": True } }, target_doclist, set_missing_values) return [d.fields for d in doclist]
def make_maintenance_visit(source_name, target_doclist=None): from frappe.model.mapper import get_mapped_doclist def update_status(source, target, parent): target.maintenance_type = "Scheduled" doclist = get_mapped_doclist( "Maintenance Schedule", source_name, { "Maintenance Schedule": { "doctype": "Maintenance Visit", "field_map": { "name": "maintenance_schedule" }, "validation": { "docstatus": ["=", 1] }, "postprocess": update_status }, "Maintenance Schedule Item": { "doctype": "Maintenance Visit Purpose", "field_map": { "parent": "prevdoc_docname", "parenttype": "prevdoc_doctype", "sales_person": "service_person" } } }, target_doclist) return [d.fields for d in doclist]
def _make_customer(source_name, target_doclist=None, ignore_permissions=False): from frappe.model.mapper import get_mapped_doclist def set_missing_values(source, target): if source.doc.company_name: target[0].customer_type = "Company" target[0].customer_name = source.doc.company_name else: target[0].customer_type = "Individual" target[0].customer_name = source.doc.lead_name target[0].customer_group = frappe.db.get_default("customer_group") doclist = get_mapped_doclist("Lead", source_name, {"Lead": { "doctype": "Customer", "field_map": { "name": "lead_name", "company_name": "customer_name", "contact_no": "phone_1", "fax": "fax_1" } }}, target_doclist, set_missing_values, ignore_permissions=ignore_permissions) return [d.fields for d in doclist]
def make_maintenance_schedule(source_name, target_doclist=None): maint_schedule = frappe.db.sql("""select t1.name from `tabMaintenance Schedule` t1, `tabMaintenance Schedule Item` t2 where t2.parent=t1.name and t2.prevdoc_docname=%s and t1.docstatus=1""", source_name) if not maint_schedule: doclist = get_mapped_doclist("Sales Order", source_name, { "Sales Order": { "doctype": "Maintenance Schedule", "field_map": { "name": "sales_order_no" }, "validation": { "docstatus": ["=", 1] } }, "Sales Order Item": { "doctype": "Maintenance Schedule Item", "field_map": { "parent": "prevdoc_docname" }, "add_if_empty": True } }, target_doclist) return [d.fields for d in doclist]
def make_maintenance_visit(source_name, target_doclist=None): visit = frappe.db.sql("""select t1.name from `tabMaintenance Visit` t1, `tabMaintenance Visit Purpose` t2 where t2.parent=t1.name and t2.prevdoc_docname=%s and t1.docstatus=1 and t1.completion_status='Fully Completed'""", source_name) if not visit: doclist = get_mapped_doclist("Sales Order", source_name, { "Sales Order": { "doctype": "Maintenance Visit", "field_map": { "name": "sales_order_no" }, "validation": { "docstatus": ["=", 1] } }, "Sales Order Item": { "doctype": "Maintenance Visit Purpose", "field_map": { "parent": "prevdoc_docname", "parenttype": "prevdoc_doctype" }, "add_if_empty": True } }, target_doclist) return [d.fields for d in doclist]
def make_sales_invoice(source_name, target_doclist=None): invoiced_qty_map = get_invoiced_qty_map(source_name) def update_accounts(source, target): si = frappe.bean(target) si.doc.is_pos = 0 si.run_method("onload_post_render") si.set_doclist(si.doclist.get({"parentfield": ["!=", "entries"]}) + si.doclist.get({"parentfield": "entries", "qty": [">", 0]})) if len(si.doclist.get({"parentfield": "entries"})) == 0: frappe.msgprint(_("Hey! All these items have already been invoiced."), raise_exception=True) return si.doclist def update_item(source_doc, target_doc, source_parent): target_doc.qty = source_doc.qty - invoiced_qty_map.get(source_doc.name, 0) doclist = get_mapped_doclist("Delivery Note", source_name, { "Delivery Note": { "doctype": "Sales Invoice", "validation": { "docstatus": ["=", 1] } }, "Delivery Note Item": { "doctype": "Sales Invoice Item", "field_map": { "name": "dn_detail", "parent": "delivery_note", "prevdoc_detail_docname": "so_detail", "against_sales_order": "sales_order", "serial_no": "serial_no" }, "postprocess": update_item }, "Sales Taxes and Charges": { "doctype": "Sales Taxes and Charges", "add_if_empty": True }, "Sales Team": { "doctype": "Sales Team", "field_map": { "incentives": "incentives" }, "add_if_empty": True } }, target_doclist, update_accounts) return [d.fields for d in doclist]
def fetch_appraisal_template(source_name, target_doclist=None): from frappe.model.mapper import get_mapped_doclist doclist = get_mapped_doclist( "Appraisal Template", source_name, { "Appraisal Template": { "doctype": "Appraisal", }, "Appraisal Template Goal": { "doctype": "Appraisal Goal", } }, target_doclist) return [d.fields for d in doclist]
def _make_sales_order(source_name, target_doclist=None, ignore_permissions=False): from frappe.model.mapper import get_mapped_doclist customer = _make_customer(source_name, ignore_permissions) def set_missing_values(source, target): if customer: target[0].customer = customer.doc.name target[0].customer_name = customer.doc.customer_name si = frappe.bean(target) si.ignore_permissions = ignore_permissions si.run_method("onload_post_render") doclist = get_mapped_doclist("Quotation", source_name, { "Quotation": { "doctype": "Sales Order", "validation": { "docstatus": ["=", 1] } }, "Quotation Item": { "doctype": "Sales Order Item", "field_map": { "parent": "prevdoc_docname" } }, "Sales Taxes and Charges": { "doctype": "Sales Taxes and Charges", "add_if_empty": True }, "Sales Team": { "doctype": "Sales Team", "add_if_empty": True } }, target_doclist, set_missing_values, ignore_permissions=ignore_permissions) # postprocess: fetch shipping address, set missing values return [d.fields for d in doclist]
def make_delivery_note(source_name, target_doclist=None): from frappe.model.mapper import get_mapped_doclist def set_missing_values(source, target): bean = frappe.bean(target) bean.run_method("onload_post_render") def update_item(source_doc, target_doc, source_parent): target_doc.base_amount = (flt(source_doc.qty) - flt(source_doc.delivered_qty)) * \ flt(source_doc.base_rate) target_doc.amount = (flt(source_doc.qty) - flt(source_doc.delivered_qty)) * \ flt(source_doc.rate) target_doc.qty = flt(source_doc.qty) - flt(source_doc.delivered_qty) doclist = get_mapped_doclist( "Sales Invoice", source_name, { "Sales Invoice": { "doctype": "Delivery Note", "validation": { "docstatus": ["=", 1] } }, "Sales Invoice Item": { "doctype": "Delivery Note Item", "field_map": { "name": "prevdoc_detail_docname", "parent": "against_sales_invoice", "serial_no": "serial_no" }, "postprocess": update_item }, "Sales Taxes and Charges": { "doctype": "Sales Taxes and Charges", "add_if_empty": True }, "Sales Team": { "doctype": "Sales Team", "field_map": { "incentives": "incentives" }, "add_if_empty": True } }, target_doclist, set_missing_values) return [d.fields for d in doclist]
def make_opportunity(source_name, target_doclist=None): from frappe.model.mapper import get_mapped_doclist doclist = get_mapped_doclist("Lead", source_name, {"Lead": { "doctype": "Opportunity", "field_map": { "campaign_name": "campaign", "doctype": "enquiry_from", "name": "lead", "lead_name": "contact_display", "company_name": "customer_name", "email_id": "contact_email", "mobile_no": "contact_mobile" } }}, target_doclist) return [d if isinstance(d, dict) else d.fields for d in doclist]
def make_purchase_order_based_on_supplier(source_name, target_doclist=None): from frappe.model.mapper import get_mapped_doclist if target_doclist: if isinstance(target_doclist, basestring): import json target_doclist = frappe.doclist(json.loads(target_doclist)) target_doclist = target_doclist.get( {"parentfield": ["!=", "po_details"]}) material_requests, supplier_items = get_material_requests_based_on_supplier( source_name) def postprocess(source, target_doclist): target_doclist[0].supplier = source_name set_missing_values(source, target_doclist) po_items = target_doclist.get({"parentfield": "po_details"}) target_doclist = target_doclist.get({"parentfield": ["!=", "po_details"]}) + \ [d for d in po_items if d.fields.get("item_code") in supplier_items and d.fields.get("qty") > 0] return target_doclist for mr in material_requests: target_doclist = get_mapped_doclist( "Material Request", mr, { "Material Request": { "doctype": "Purchase Order", }, "Material Request Item": { "doctype": "Purchase Order Item", "field_map": [["name", "prevdoc_detail_docname"], ["parent", "prevdoc_docname"], ["parenttype", "prevdoc_doctype"], ["uom", "stock_uom"], ["uom", "uom"]], "postprocess": update_item } }, target_doclist, postprocess) return [d.fields for d in target_doclist]
def make_material_request(source_name, target_doclist=None): def postprocess(source, doclist): doclist[0].material_request_type = "Purchase" doclist = get_mapped_doclist("Sales Order", source_name, { "Sales Order": { "doctype": "Material Request", "validation": { "docstatus": ["=", 1] } }, "Sales Order Item": { "doctype": "Material Request Item", "field_map": { "parent": "sales_order_no", "stock_uom": "uom" } } }, target_doclist, postprocess) return [(d if isinstance(d, dict) else d.fields) for d in doclist]
def make_purchase_receipt(source_name, target_doclist=None): from frappe.model.mapper import get_mapped_doclist def set_missing_values(source, target): bean = frappe.bean(target) bean.run_method("set_missing_values") def update_item(obj, target, source_parent): target.qty = flt(obj.qty) - flt(obj.received_qty) target.stock_qty = (flt(obj.qty) - flt(obj.received_qty)) * flt( obj.conversion_factor) target.amount = (flt(obj.qty) - flt(obj.received_qty)) * flt(obj.rate) target.base_amount = (flt(obj.qty) - flt(obj.received_qty)) * flt( obj.base_rate) doclist = get_mapped_doclist( "Purchase Order", source_name, { "Purchase Order": { "doctype": "Purchase Receipt", "validation": { "docstatus": ["=", 1], } }, "Purchase Order Item": { "doctype": "Purchase Receipt Item", "field_map": { "name": "prevdoc_detail_docname", "parent": "prevdoc_docname", "parenttype": "prevdoc_doctype", }, "postprocess": update_item, "condition": lambda doc: doc.received_qty < doc.qty }, "Purchase Taxes and Charges": { "doctype": "Purchase Taxes and Charges", "add_if_empty": True } }, target_doclist, set_missing_values) return [d.fields for d in doclist]
def make_purchase_invoice(source_name, target_doclist=None): from frappe.model.mapper import get_mapped_doclist def set_missing_values(source, target): bean = frappe.bean(target) bean.run_method("set_missing_values") def update_item(obj, target, source_parent): target.amount = flt(obj.amount) - flt(obj.billed_amt) target.base_amount = target.amount * flt(source_parent.conversion_rate) if flt(obj.base_rate): target.qty = target.base_amount / flt(obj.base_rate) doclist = get_mapped_doclist( "Purchase Order", source_name, { "Purchase Order": { "doctype": "Purchase Invoice", "validation": { "docstatus": ["=", 1], } }, "Purchase Order Item": { "doctype": "Purchase Invoice Item", "field_map": { "name": "po_detail", "parent": "purchase_order", }, "postprocess": update_item, "condition": lambda doc: doc.base_amount == 0 or doc.billed_amt < doc.amount }, "Purchase Taxes and Charges": { "doctype": "Purchase Taxes and Charges", "add_if_empty": True } }, target_doclist, set_missing_values) return [d.fields for d in doclist]
def make_purchase_order(source_name, target_doclist=None): from frappe.model.mapper import get_mapped_doclist def set_missing_values(source, target): bean = frappe.bean(target) bean.run_method("set_missing_values") bean.run_method("get_schedule_dates") def update_item(obj, target, source_parent): target.conversion_factor = 1 doclist = get_mapped_doclist( "Supplier Quotation", source_name, { "Supplier Quotation": { "doctype": "Purchase Order", "validation": { "docstatus": ["=", 1], } }, "Supplier Quotation Item": { "doctype": "Purchase Order Item", "field_map": [["name", "supplier_quotation_item"], ["parent", "supplier_quotation"], ["uom", "stock_uom"], ["uom", "uom"], ["prevdoc_detail_docname", "prevdoc_detail_docname"], ["prevdoc_doctype", "prevdoc_doctype"], ["prevdoc_docname", "prevdoc_docname"]], "postprocess": update_item }, "Purchase Taxes and Charges": { "doctype": "Purchase Taxes and Charges", "add_if_empty": True }, }, target_doclist, set_missing_values) return [d.fields for d in doclist]
def get_mapped_doclist(source_name, target_doclist=None): from frappe.model.mapper import get_mapped_doclist def postprocess(source, target): sal_slip = frappe.bean(target) sal_slip.run_method("pull_emp_details") sal_slip.run_method("get_leave_details") sal_slip.run_method("calculate_net_pay") doclist = get_mapped_doclist( "Salary Structure", source_name, { "Salary Structure": { "doctype": "Salary Slip", "field_map": { "total_earning": "gross_pay" } }, "Salary Structure Deduction": { "doctype": "Salary Slip Deduction", "field_map": [["depend_on_lwp", "d_depends_on_lwp"], ["d_modified_amt", "d_amount"], ["d_modified_amt", "d_modified_amount"]], "add_if_empty": True }, "Salary Structure Earning": { "doctype": "Salary Slip Earning", "field_map": [["depend_on_lwp", "e_depends_on_lwp"], ["modified_value", "e_modified_amount"], ["modified_value", "e_amount"]], "add_if_empty": True } }, target_doclist, postprocess) return doclist
def make_delivery_note(source_name, target_doclist=None): def update_item(obj, target, source_parent): target.base_amount = (flt(obj.qty) - flt(obj.delivered_qty)) * flt(obj.base_rate) target.amount = (flt(obj.qty) - flt(obj.delivered_qty)) * flt(obj.rate) target.qty = flt(obj.qty) - flt(obj.delivered_qty) doclist = get_mapped_doclist("Sales Order", source_name, { "Sales Order": { "doctype": "Delivery Note", "field_map": { "shipping_address": "address_display", "shipping_address_name": "customer_address", }, "validation": { "docstatus": ["=", 1] } }, "Sales Order Item": { "doctype": "Delivery Note Item", "field_map": { "rate": "rate", "name": "prevdoc_detail_docname", "parent": "against_sales_order", }, "postprocess": update_item, "condition": lambda doc: doc.delivered_qty < doc.qty }, "Sales Taxes and Charges": { "doctype": "Sales Taxes and Charges", "add_if_empty": True }, "Sales Team": { "doctype": "Sales Team", "add_if_empty": True } }, target_doclist, set_missing_values) return [d.fields for d in doclist]
def make_sales_invoice(source_name, target_doclist=None): def set_missing_values(source, target): bean = frappe.bean(target) bean.doc.is_pos = 0 bean.run_method("onload_post_render") def update_item(obj, target, source_parent): target.amount = flt(obj.amount) - flt(obj.billed_amt) target.base_amount = target.amount * flt(source_parent.conversion_rate) target.qty = obj.rate and target.amount / flt(obj.rate) or obj.qty doclist = get_mapped_doclist("Sales Order", source_name, { "Sales Order": { "doctype": "Sales Invoice", "validation": { "docstatus": ["=", 1] } }, "Sales Order Item": { "doctype": "Sales Invoice Item", "field_map": { "name": "so_detail", "parent": "sales_order", }, "postprocess": update_item, "condition": lambda doc: doc.base_amount==0 or doc.billed_amt < doc.amount }, "Sales Taxes and Charges": { "doctype": "Sales Taxes and Charges", "add_if_empty": True }, "Sales Team": { "doctype": "Sales Team", "add_if_empty": True } }, target_doclist, set_missing_values) return [d.fields for d in doclist]
def make_supplier_quotation(source_name, target_doclist=None): from frappe.model.mapper import get_mapped_doclist doclist = get_mapped_doclist( "Material Request", source_name, { "Material Request": { "doctype": "Supplier Quotation", "validation": { "docstatus": ["=", 1], "material_request_type": ["=", "Purchase"] } }, "Material Request Item": { "doctype": "Supplier Quotation Item", "field_map": { "name": "prevdoc_detail_docname", "parent": "prevdoc_docname", "parenttype": "prevdoc_doctype" } } }, target_doclist, set_missing_values) return [d.fields for d in doclist]
def make_maintenance_visit(source_name, target_doclist=None): from frappe.model.mapper import get_mapped_doclist visit = frappe.db.sql( """select t1.name from `tabMaintenance Visit` t1, `tabMaintenance Visit Purpose` t2 where t2.parent=t1.name and t2.prevdoc_docname=%s and t1.docstatus=1 and t1.completion_status='Fully Completed'""", source_name) if not visit: doclist = get_mapped_doclist( "Customer Issue", source_name, { "Customer Issue": { "doctype": "Maintenance Visit", "field_map": { "complaint": "description", "doctype": "prevdoc_doctype", "name": "prevdoc_docname" } } }, target_doclist) return [d.fields for d in doclist]
def make_salary_slip(source_name, target_doclist=None): return [d.fields for d in get_mapped_doclist(source_name, target_doclist)]