Example #1
0
def make_delivery_note(source_name, target_doc=None):
	def set_missing_values(source, target):
		target.posting_date = nowdate()
		target.territory = frappe.db.get_value('Customer', source.customer, 'territory')

	def update_item(source, target, source_parent):
		target.image = source.work_order_attach
		target.item_code = source.item_code
		target.item_name = source.item_name
		target.description = source.description 
		target.qty = source.qty - source.delivered_qty
		target.rate = source.rate
		target.against_sales_order = source.prevdoc_docname
		target.so_detail = source.so_detail

	doclist = get_mapped_doc("Work Order", source_name, {
		"Work Order": {
			"doctype": "Delivery Note"
		},
		"Work Order Item": {
			"doctype": "Delivery Note Item",
			"postprocess": update_item,
			"condition": lambda doc: (flt(doc.qty) - flt(doc.delivered_qty)) > 0
		}
	}, target_doc, set_missing_values)

	return doclist
def make_salary_slip(source_name, target_doc=None):
	def postprocess(source, target):
		target.run_method("pull_emp_details")
		target.run_method("get_leave_details")
		target.run_method("calculate_net_pay")

	doc = get_mapped_doc("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_doc, postprocess)

	return doc
Example #3
0
def make_quotation(source_name, target_doc=None):
	def set_missing_values(source, target):
		target.posting_date = nowdate()
		target.territory = frappe.db.get_value('Customer', source.customer, 'territory')
		target.order_type = 'Sales'
		target.quotation_to = source.work_order_to

	def update_item(source, target, source_parent):
		target.image = source.work_order_attach
		target.item_code = source.item_code
		target.item_name = source.item_name
		target.description = source.description 
		target.qty = source.qty
		target.status = ''
		target.prevdoc_doctype = ''
		target.prevdoc_docname = ''

	doclist = get_mapped_doc("Work Order", source_name, {
		"Work Order": {
			"doctype": "Quotation"
		},
		"Work Order Item": {
			"doctype": "Quotation Item",
			"postprocess": update_item
		}
	}, target_doc, set_missing_values)

	return doclist
Example #4
0
def make_stock_entry(source_name, target_doc=None):
	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.purpose = "Material Transfer"
		target.run_method("get_stock_and_rate")

	doclist = get_mapped_doc("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,
			"condition": lambda doc: doc.ordered_qty < doc.qty
		}
	}, target_doc, set_missing_values)

	return doclist
Example #5
0
def _make_customer(source_name, target_doc=None, ignore_permissions=False):
    def set_missing_values(source, target):
        if source.company_name:
            target.customer_type = "Company"
            target.customer_name = source.company_name
        else:
            target.customer_type = "Individual"
            target.customer_name = source.lead_name

        target.customer_group = frappe.db.get_default("Customer Group")

    doclist = get_mapped_doc(
        "Lead",
        source_name,
        {
            "Lead": {
                "doctype": "Customer",
                "field_map": {
                    "name": "lead_name",
                    "company_name": "customer_name",
                    "contact_no": "phone_1",
                    "fax": "fax_1",
                },
            }
        },
        target_doc,
        set_missing_values,
        ignore_permissions=ignore_permissions,
    )

    return doclist
Example #6
0
def get_item_from_so(source_name, target_doc=None):
	est = get_mapped_doc("Sales Order", source_name, {
			"Sales Order": {
				"doctype": "Est Tools",
				"validation": {
					"docstatus": ["=", 1]
				}
			},
			"Sales Order Item": {
				"doctype": "Est Tools Primary Item",
				"field_map": {
					"parent": "prevdoc_docname",
					"qty": "quantity"
				},
			},
		}, target_doc)
	
	return est

# ERRPR SAAT UPDATE BENCH VERSION 2
# error: <class 'xmlrpclib.Fault'>, <Fault 10: 'BAD_NAME: frappe-bench-processes'>: file: /usr/lib/python2.7/xmlrpclib.py line: 794
# mv config config-bak
# mkdir -p config/pids
# sudo supervisorctl stop all
# sudo service nginx stop
# bench setup config
# bench setup redis
# bench setup supervisor
# bench setup nginx
# bench setup procfile
# sudo bench setup sudoers frappe # or the user that you used to install erpnext
# sudo nginx -t  # check if this command shows any error 
# sudo service nginx reload    
# sudo supervisorctl reread
# sudo supervisorctl update
Example #7
0
def make_bom(source_name, target_doc=None):
	doc = get_mapped_doc("Est Tools", source_name, {
		"Est Tools": {
			"doctype": "BOM",
			"field_map": {
				"item_code": "item"
			},
			"validation": {
				"docstatus": ["=", 1]
			}
		},
		"Est Tools Primary Item": {
			"doctype": "BOM Item",
			"field_map": {
				"quantity": "qty",
				"uom": "stock_uom",
				"net_amount": "amount"
			}
		},
		"Est Tools Secondary Item": {
			"doctype": "BOM Item",
			"field_map": {
				"si_qty": "qty",
				"uom": "stock_uom",
				"si_rate":"rate",
				"si_net_amount": "amount"
			}
		},
	}, target_doc)
	
	return doc
Example #8
0
File: api.py Project: umaepoch/pppl
def make_purchase_receipt(source_name, target_doc=None):
	def update_item(obj, target, source_parent):
		target.qty = flt(obj.qty) 
		target.stock_qty = flt(obj.qty)
		target.amount = flt(obj.qty) * flt(obj.rate)
		target.base_amount = flt(obj.qty) * flt(obj.rate) 

	doc = get_mapped_doc("Sales Invoice", source_name,	{
		"Sales Invoice": {
			"doctype": "Purchase Receipt",
			"validation": {
				"docstatus": ["=", 1],
			}
		},
		"Sales Invoice Item": {
			"doctype": "Purchase Receipt Item",
			"field_map": {
				"name": "sales_invoice_item",
				"parent": "sales_invoice",
			},
			"postprocess": update_item,
			"condition": lambda doc: abs(doc.qty) > 0
		},
		"Purchase Taxes and Charges": {
			"doctype": "Purchase Taxes and Charges",
			"add_if_empty": True
		}
	}, target_doc, set_missing_values)

	return doc
Example #9
0
def make_material_request(source_name, target_doc=None):
	def postprocess(source, doc):
		doc.material_request_type = "Purchase"

	def update_item(source, target, source_parent):
		target.project = source_parent.project

	doc = get_mapped_doc("Sales Order", source_name, {
		"Sales Order": {
			"doctype": "Material Request",
			"validation": {
				"docstatus": ["=", 1]
			}
		},
		"Packed Item": {
			"doctype": "Material Request Item",
			"field_map": {
				"parent": "sales_order",
				"stock_uom": "uom"
			},
			"postprocess": update_item
		},
		"Sales Order Item": {
			"doctype": "Material Request Item",
			"field_map": {
				"parent": "sales_order",
				"stock_uom": "uom"
			},
			"condition": lambda doc: not frappe.db.exists('Product Bundle', doc.item_code),
			"postprocess": update_item
		}
	}, target_doc, postprocess)

	return doc
def get_operational_matrix_details(customer=None,project_id=None,operational_matrix=None,target_doc=None,ignore_permissions=False):
	if not frappe.db.get_value("Operation And Project Commercial",{'project_commercial':project_id,'operational_id':operational_matrix,'customer':customer},'name'):
		doclist = get_mapped_doc("Operational Matrix", operational_matrix, {
				"Operational Matrix": {
					"doctype": "Operation And Project Commercial"
				},
				"Operation Details": {
					"doctype": "Operation And Project Details",
				}
			}, target_doc, ignore_permissions=ignore_permissions)

		doclist.project_commercial = project_id
		doclist.customer = customer
		doclist.save(ignore_permissions)

	elif frappe.db.get_value("Operation And Project Commercial",{'project_commercial':project_id,'operational_id':operational_matrix,'customer':customer,'operational_matrix_status':'Deactive'},'name'):
		name = frappe.db.get_value("Operation And Project Commercial",{'project_commercial':project_id,'operational_id':operational_matrix,'customer':customer,'operational_matrix_status':'Deactive'},'name')
		frappe.db.sql("""update `tabOperation And Project Commercial` set operational_matrix_status='Active'
						where name='%s'"""%name)
		frappe.db.commit()
		frappe.msgprint("Specified operation matrix '%s' is get linked for  project id '%s' and customer '%s' please check below records."%(operational_matrix,project_id,customer))
	else:
		frappe.msgprint("Specified operation matrix '%s' is already linked for  project id '%s' and customer '%s'."%(operational_matrix,project_id,customer))

	last_final_data = get_operational_matrix_data(customer)
	return {"final_data": last_final_data['final_data']}
Example #11
0
def make_project(source_name, target_doc=None):
	def postprocess(source, doc):
		doc.project_type = "External"
		doc.project_name = source.name

	doc = get_mapped_doc("Sales Order", source_name, {
		"Sales Order": {
			"doctype": "Project",
			"validation": {
				"docstatus": ["=", 1]
			},
			"field_map":{
				"name" : "sales_order",
				"base_grand_total" : "estimated_costing",
			}
		},
		"Sales Order Item": {
			"doctype": "Project Task",
			"field_map": {
				"description": "title",
			},
		}
	}, target_doc, postprocess)

	return doc
def make_purchase_invoice(source_name, target_doc=None):
	def postprocess(source, target):
		set_missing_values(source, target)
		#Get the advance paid Journal Entries in Purchase Invoice Advance
		target.get_advances()

	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 = target.amount / flt(obj.rate) if (flt(obj.rate) and flt(obj.billed_amt)) else flt(obj.qty)

	doc = get_mapped_doc("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) and doc.delivered_by_supplier!=1
		},
		"Purchase Taxes and Charges": {
			"doctype": "Purchase Taxes and Charges",
			"add_if_empty": True
		}
	}, target_doc, postprocess)

	return doc
Example #13
0
def make_salary_slip(source_name, target_doc=None):
	def postprocess(source, target):
		# copy earnings and deductions table
		for key in ('earnings', 'deductions'):
			for d in source.get(key):
				target.append(key, {
					'amount': d.amount,
					'default_amount': d.amount,
					'depends_on_lwp' : d.depends_on_lwp,
					'salary_component' : d.salary_component
				})

		target.run_method("pull_emp_details")
		target.run_method("get_leave_details")
		target.run_method("calculate_net_pay")
			

	doc = get_mapped_doc("Salary Structure", source_name, {
		"Salary Structure": {
			"doctype": "Salary Slip",
			"field_map": {
				"total_earning": "gross_pay",
				"name": "salary_structure"
			}
		}
	}, target_doc, postprocess, ignore_child_tables=True)

	return doc
def make_purchase_invoice(source_name, target_doc=None):
	def set_missing_values(source, target):
		target.run_method("set_missing_values")
		target.run_method("calculate_taxes_and_totals")

	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)

	doc = get_mapped_doc("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_doc, set_missing_values)

	return doc
Example #15
0
def make_purchase_order(source_name, target_doc=None):
	def postprocess(source, target_doc):
		set_missing_values(source, target_doc)

	doclist = get_mapped_doc("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,
			"condition": lambda doc: doc.ordered_qty < doc.qty
		}
	}, target_doc, postprocess)

	return doclist
def make_maintenance_visit(source_name, target_doc=None):
    from frappe.model.mapper import get_mapped_doc

    def update_status(source, target, parent):
        target.maintenance_type = "Scheduled"

    doclist = get_mapped_doc(
        "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_doc,
    )

    return doclist
Example #17
0
def make_maintenance_schedule(source_name, target_doc=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_doc("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_doc)

		return doclist
Example #18
0
def make_maintenance_visit(source_name, target_doc=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_doc("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_doc)

		return doclist
def make_supplier_quotation(source_name, for_supplier, target_doc=None):
	def postprocess(source, target_doc):
		target_doc.supplier = for_supplier
		args = get_party_details(for_supplier, party_type="Supplier", ignore_permissions=True)
		target_doc.currency = args.currency or get_party_account_currency('Supplier', for_supplier, source.company)
		target_doc.buying_price_list = args.buying_price_list or frappe.db.get_value('Buying Settings', None, 'buying_price_list')
		set_missing_values(source, target_doc)

	doclist = get_mapped_doc("Request for Quotation", source_name, {
		"Request for Quotation": {
			"doctype": "Supplier Quotation",
			"validation": {
				"docstatus": ["=", 1]
			}
		},
		"Request for Quotation Item": {
			"doctype": "Supplier Quotation Item",
			"field_map": {
				"name": "request_for_quotation_item",
				"parent": "request_for_quotation"
			},
		}
	}, target_doc, postprocess)

	return doclist
Example #20
0
def make_material_request(source_name, target_doc=None):
	def postprocess(source, doc):
		doc.material_request_type = "Purchase"

	so = frappe.get_doc("Sales Order", source_name)

	item_table = "Packed Item" if so.packed_items else "Sales Order Item"

	doc = get_mapped_doc("Sales Order", source_name, {
		"Sales Order": {
			"doctype": "Material Request",
			"validation": {
				"docstatus": ["=", 1]
			}
		},
		item_table: {
			"doctype": "Material Request Item",
			"field_map": {
				"parent": "sales_order_no",
				"stock_uom": "uom"
			}
		}
	}, target_doc, postprocess)

	return doc
Example #21
0
def make_installation_note(source_name, target_doc=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_doc("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_doc)

	return doclist
Example #22
0
def make_oppurtunity(source_name, target_doc=None):
	def set_missing_values(source, target):
		target.ignore_pricing_rule = 1
		target.run_method("set_missing_values")


	target_doc = get_mapped_doc("Sales Order", source_name, {
		"Sales Order": {
			"doctype": "Opportunity",
			"validation": {
				"docstatus": ["=", 1]
			}
		},
		"Sales Order Item": {
			"doctype": "Opportunity Item",
			"field_map": {
				"rate": "rate",
				"name": "prevdoc_detail_docname",
				"parent": "against_sales_order",
			},

		}
	}, target_doc, set_missing_values)

	return target_doc
Example #23
0
def make_sales_invoice(source_name, target_doc=None):
	attended_by = frappe.get_value("Appointment",source_name,"employee")
	def postprocess(source, target):
		set_missing_values(source, target)

	def set_missing_values(source, target):
		target.is_pos = 1
		target.ignore_pricing_rule = 1
		[d.update({"emp":attended_by}) for d in target.get("items")]
		target.run_method("set_missing_values")

	doclist = get_mapped_doc("Appointment", source_name, {
		"Appointment": {
			"doctype": "Sales Invoice",
			"field_map": {
				"starts_on":"posting_date"
			},
			"validation": {
				"status": ["=", "Confirm"]
			}
		},
		"Services": {
			"doctype": "Sales Invoice Item",
			"field_map": {
				"item": "item_code"
			},
			"add_if_empty": True
		}

	}, target_doc, postprocess)

	return doclist
Example #24
0
def make_purchase_invoice(source_name, target_doc=None):
	def postprocess(source, target):
		target.sales_order = source.name
		target.supplier = source.technician
		target.credit_to = frappe.db.get_value("Company", frappe.db.get_default("company"), "default_payable_account")
		target.taxes_and_charges = ''

	def update_item(source, target, source_parent):
		target.amount = flt(source.amount) - flt(source.billed_amt)
		target.base_amount = target.amount * flt(source_parent.conversion_rate)
		target.qty = source.qty

	doclist = get_mapped_doc("Sales Order", source_name, {
		"Sales Order": {
			"doctype": "Purchase Invoice",
			"validation": {
				"docstatus": ["=", 1]
			}
		},
		"Sales Order Item": {
			"doctype": "Purchase Invoice Item",
			"field_map": {
				# "name": "pr_detail",
				"parent": "purchase_invoice"
			},
			"postprocess": update_item,
			"condition": lambda doc: frappe.db.get_value("Item",doc.item_code,"is_service_item") == 1
		},
	}, target_doc, postprocess)

	return doclist
Example #25
0
def make_maintenance_visit(source_name, target_doc=None):
	from frappe.model.mapper import get_mapped_doc, map_child_doc

	def _update_links(source_doc, target_doc, source_parent):
		target_doc.prevdoc_doctype = source_parent.doctype
		target_doc.prevdoc_docname = source_parent.name

	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:
		target_doc = get_mapped_doc("Warranty Claim", source_name, {
			"Warranty Claim": {
				"doctype": "Maintenance Visit",
				"field_map": {}
			}
		}, target_doc)

		source_doc = frappe.get_doc("Warranty Claim", source_name)
		if source_doc.get("item_code"):
			table_map = {
				"doctype": "Maintenance Visit Purpose",
				"postprocess": _update_links
			}
			map_child_doc(source_doc, target_doc, table_map, source_doc)

		return target_doc
Example #26
0
def make_delivery_note(source_name, target_doc=None):
    def set_missing_values(source, target):
        target.ignore_pricing_rule = 1
        target.run_method("set_missing_values")
        target.run_method("calculate_taxes_and_totals")

    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_doc(
        "Sales Invoice",
        source_name,
        {
            "Sales Invoice": {"doctype": "Delivery Note", "validation": {"docstatus": ["=", 1]}},
            "Sales Invoice Item": {
                "doctype": "Delivery Note Item",
                "field_map": {
                    "name": "si_detail",
                    "parent": "against_sales_invoice",
                    "serial_no": "serial_no",
                    "sales_order": "against_sales_order",
                    "so_detail": "so_detail",
                },
                "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_doc,
        set_missing_values,
    )

    return doclist
Example #27
0
def make_PO(source_name, target_doc=None):
	def postprocess(source, target):
		target.taxes_and_charges = frappe.db.get_value('Purchase Taxes and Charges Template', {'is_default':1}, 'name') or None

	def update_item(source, target, source_parent):
		target.uom = source.stock_uom
		target.conversion_factor = 1
		target.qty = target.amount / flt(source.rate) if (source.rate and source.billed_amt) else source.qty
		target.price_list_rate=0
		target.rate=0
	target_doc = get_mapped_doc("Sales Order", source_name, {
		"Sales Order": {
			"doctype": "Purchase Order",
			"validation": {
				"docstatus": ["=", 1]
			}
		},
		"Sales Order Item": {
			"doctype": "Purchase Order Item",
			"field_map": {
				"parent": "purchase_order",
			},
			"postprocess": update_item,
			"condition": lambda doc: frappe.db.get_value("Item",doc.item_code,"is_service_item") == 0
		}
	}, target_doc, postprocess)
	return target_doc
def make_purchase_order(source_name, target_doc=None):
	def set_missing_values(source, target):
		target.ignore_pricing_rule = 1
		target.run_method("set_missing_values")
		target.run_method("get_schedule_dates")
		target.run_method("calculate_taxes_and_totals")

	def update_item(obj, target, source_parent):
		target.stock_qty = flt(obj.qty) * flt(obj.conversion_factor)

	doclist = get_mapped_doc("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"],
				["material_request", "material_request"],
				["material_request_item", "material_request_item"]
			],
			"postprocess": update_item
		},
		"Purchase Taxes and Charges": {
			"doctype": "Purchase Taxes and Charges",
		},
	}, target_doc, set_missing_values)

	return doclist
Example #29
0
def make_salary_slip(source_name, target_doc = None, employee = None, as_print = False, print_format = None):
	def postprocess(source, target):
		if employee:
			employee_details = frappe.db.get_value("Employee", employee,
				["employee_name", "branch", "designation", "department"], as_dict=1)
			target.employee = employee
			target.employee_name = employee_details.employee_name
			target.branch = employee_details.branch
			target.designation = employee_details.designation
			target.department = employee_details.department
		target.run_method('process_salary_structure')

	doc = get_mapped_doc("Salary Structure", source_name, {
		"Salary Structure": {
			"doctype": "Salary Slip",
			"field_map": {
				"total_earning": "gross_pay",
				"name": "salary_structure"
			}
		}
	}, target_doc, postprocess, ignore_child_tables=True)

	if cint(as_print):
		doc.name = 'Preview for {0}'.format(employee)
		return frappe.get_print(doc.doctype, doc.name, doc = doc, print_format = print_format)
	else:
		return doc
Example #30
0
def make_purchase_order(source_name,
                        for_supplier=None,
                        selected_items=[],
                        target_doc=None):
    if isinstance(selected_items, string_types):
        selected_items = json.loads(selected_items)

    def set_missing_values(source, target):
        target.supplier = supplier
        target.apply_discount_on = ""
        target.additional_discount_percentage = 0.0
        target.discount_amount = 0.0
        target.inter_company_order_reference = ""

        default_price_list = frappe.get_value("Supplier", supplier,
                                              "default_price_list")
        if default_price_list:
            target.buying_price_list = default_price_list

        if any(item.delivered_by_supplier == 1 for item in source.items):
            if source.shipping_address_name:
                target.shipping_address = source.shipping_address_name
                target.shipping_address_display = source.shipping_address
            else:
                target.shipping_address = source.customer_address
                target.shipping_address_display = source.address_display

            target.customer_contact_person = source.contact_person
            target.customer_contact_display = source.contact_display
            target.customer_contact_mobile = source.contact_mobile
            target.customer_contact_email = source.contact_email

        else:
            target.customer = ""
            target.customer_name = ""

        target.run_method("set_missing_values")
        target.run_method("calculate_taxes_and_totals")

    def update_item(source, target, source_parent):
        target.schedule_date = source.delivery_date
        target.qty = flt(source.qty) - flt(source.ordered_qty)
        target.stock_qty = (flt(source.qty) - flt(source.ordered_qty)) * flt(
            source.conversion_factor)
        target.project = source_parent.project

    suppliers = []
    if for_supplier:
        suppliers.append(for_supplier)
    else:
        sales_order = frappe.get_doc("Sales Order", source_name)
        for item in sales_order.items:
            if item.supplier and item.supplier not in suppliers:
                suppliers.append(item.supplier)
    for supplier in suppliers:
        po = frappe.get_list("Purchase Order",
                             filters={
                                 "sales_order": source_name,
                                 "supplier": supplier,
                                 "docstatus": ("<", "2")
                             })
        if len(po) == 0:
            doc = get_mapped_doc(
                "Sales Order", source_name, {
                    "Sales Order": {
                        "doctype":
                        "Purchase Order",
                        "field_no_map": [
                            "address_display", "contact_display",
                            "contact_mobile", "contact_email",
                            "contact_person", "taxes_and_charges"
                        ],
                        "validation": {
                            "docstatus": ["=", 1]
                        }
                    },
                    "Sales Order Item": {
                        "doctype":
                        "Purchase Order Item",
                        "field_map":
                        [["name", "sales_order_item"], [
                            "parent", "sales_order"
                        ], ["stock_uom", "stock_uom"], ["uom", "uom"],
                         ["conversion_factor", "conversion_factor"],
                         ["delivery_date", "schedule_date"]],
                        "field_no_map": ["rate", "price_list_rate"],
                        "postprocess":
                        update_item,
                        "condition":
                        lambda doc: doc.ordered_qty < doc.qty and doc.supplier
                        == supplier and doc.item_code in selected_items
                    }
                }, target_doc, set_missing_values)
            if not for_supplier:
                doc.insert()
        else:
            suppliers = []
    if suppliers:
        if not for_supplier:
            frappe.db.commit()
        return doc
    else:
        frappe.msgprint(_("PO already created for all sales order items"))
Example #31
0
def make_delivery_note(source_name, target_doc=None, skip_item_mapping=False):
    def set_missing_values(source, target):
        target.ignore_pricing_rule = 1
        target.run_method("set_missing_values")
        target.run_method("set_po_nos")
        target.run_method("calculate_taxes_and_totals")

        # set company address
        target.update(get_company_address(target.company))
        if target.company_address:
            target.update(
                get_fetch_values("Delivery Note", 'company_address',
                                 target.company_address))

    def update_item(source, target, source_parent):
        target.base_amount = (flt(source.qty) - flt(
            source.delivered_qty)) * flt(source.base_rate)
        target.amount = (flt(source.qty) - flt(source.delivered_qty)) * flt(
            source.rate)
        target.qty = flt(source.qty) - flt(source.delivered_qty)

        item = get_item_defaults(target.item_code, source_parent.company)
        item_group = get_item_group_defaults(target.item_code,
                                             source_parent.company)

        if item:
            target.cost_center = frappe.db.get_value("Project", source_parent.project, "cost_center") \
             or item.get("buying_cost_center") \
             or item_group.get("buying_cost_center")

    mapper = {
        "Sales Order": {
            "doctype": "Delivery Note",
            "validation": {
                "docstatus": ["=", 1]
            }
        },
        "Sales Taxes and Charges": {
            "doctype": "Sales Taxes and Charges",
            "add_if_empty": True
        },
        "Sales Team": {
            "doctype": "Sales Team",
            "add_if_empty": True
        }
    }

    if not skip_item_mapping:
        mapper["Sales Order Item"] = {
            "doctype":
            "Delivery Note Item",
            "field_map": {
                "rate": "rate",
                "name": "so_detail",
                "parent": "against_sales_order",
            },
            "postprocess":
            update_item,
            "condition":
            lambda doc: abs(doc.delivered_qty) < abs(doc.qty) and doc.
            delivered_by_supplier != 1
        }

    target_doc = get_mapped_doc("Sales Order", source_name, mapper, target_doc,
                                set_missing_values)

    return target_doc
Example #32
0
def make_shipment(source_name, target_doc=None):
    def postprocess(source, target):
        user = frappe.db.get_value(
            "User",
            frappe.session.user, ['email', 'full_name', 'phone', 'mobile_no'],
            as_dict=1)
        target.pickup_contact_email = user.email
        pickup_contact_display = '{}'.format(user.full_name)
        if user:
            if user.email:
                pickup_contact_display += '<br>' + user.email
            if user.phone:
                pickup_contact_display += '<br>' + user.phone
            if user.mobile_no and not user.phone:
                pickup_contact_display += '<br>' + user.mobile_no
        target.pickup_contact = pickup_contact_display

        # As we are using session user details in the pickup_contact then pickup_contact_person will be session user
        target.pickup_contact_person = frappe.session.user

        contact = frappe.db.get_value("Contact",
                                      source.contact_person,
                                      ['email_id', 'phone', 'mobile_no'],
                                      as_dict=1)
        delivery_contact_display = '{}'.format(source.contact_display)
        if contact:
            if contact.email_id:
                delivery_contact_display += '<br>' + contact.email_id
            if contact.phone:
                delivery_contact_display += '<br>' + contact.phone
            if contact.mobile_no and not contact.phone:
                delivery_contact_display += '<br>' + contact.mobile_no
        target.delivery_contact = delivery_contact_display

        if source.shipping_address_name:
            target.delivery_address_name = source.shipping_address_name
            target.delivery_address = source.shipping_address
        elif source.customer_address:
            target.delivery_address_name = source.customer_address
            target.delivery_address = source.address_display

    doclist = get_mapped_doc(
        "Delivery Note", source_name, {
            "Delivery Note": {
                "doctype": "Shipment",
                "field_map": {
                    "grand_total": "value_of_goods",
                    "company": "pickup_company",
                    "company_address": "pickup_address_name",
                    "company_address_display": "pickup_address",
                    "customer": "delivery_customer",
                    "contact_person": "delivery_contact_name",
                    "contact_email": "delivery_contact_email"
                },
                "validation": {
                    "docstatus": ["=", 1]
                }
            },
            "Delivery Note Item": {
                "doctype": "Shipment Delivery Note",
                "field_map": {
                    "name": "prevdoc_detail_docname",
                    "parent": "prevdoc_docname",
                    "parenttype": "prevdoc_doctype",
                    "base_amount": "grand_total"
                }
            }
        }, target_doc, postprocess)

    return doclist
Example #33
0
def make_purchase_invoice(source_name, target_doc=None):
    from frappe.model.mapper import get_mapped_doc
    from erpnext.accounts.party import get_payment_terms_template

    doc = frappe.get_doc('Purchase Receipt', source_name)
    returned_qty_map = get_returned_qty_map(source_name)
    invoiced_qty_map = get_invoiced_qty_map(source_name)

    def set_missing_values(source, target):
        if len(target.get("items")) == 0:
            frappe.throw(_("All items have already been Invoiced/Returned"))

        doc = frappe.get_doc(target)
        doc.ignore_pricing_rule = 1
        doc.payment_terms_template = get_payment_terms_template(
            source.supplier, "Supplier", source.company)
        doc.run_method("onload")
        doc.run_method("set_missing_values")
        doc.run_method("calculate_taxes_and_totals")

    def update_item(source_doc, target_doc, source_parent):
        target_doc.qty, returned_qty = get_pending_qty(source_doc)
        returned_qty_map[source_doc.name] = returned_qty

    def get_pending_qty(item_row):
        pending_qty = item_row.qty - invoiced_qty_map.get(item_row.name, 0)
        returned_qty = flt(returned_qty_map.get(item_row.name, 0))
        if returned_qty:
            if returned_qty >= pending_qty:
                pending_qty = 0
                returned_qty -= pending_qty
            else:
                pending_qty -= returned_qty
                returned_qty = 0
        return pending_qty, returned_qty

    doclist = get_mapped_doc(
        "Purchase Receipt", source_name, {
            "Purchase Receipt": {
                "doctype": "Purchase Invoice",
                "field_map": {
                    "supplier_warehouse": "supplier_warehouse",
                    "is_return": "is_return"
                },
                "validation": {
                    "docstatus": ["=", 1],
                },
            },
            "Purchase Receipt Item": {
                "doctype":
                "Purchase Invoice Item",
                "field_map": {
                    "name": "pr_detail",
                    "parent": "purchase_receipt",
                    "purchase_order_item": "po_detail",
                    "purchase_order": "purchase_order",
                    "is_fixed_asset": "is_fixed_asset",
                    "asset_location": "asset_location",
                    "asset_category": 'asset_category'
                },
                "postprocess":
                update_item,
                "filter":
                lambda d: get_pending_qty(d)[0] <= 0
                if not doc.get("is_return") else get_pending_qty(d)[0] > 0
            },
            "Purchase Taxes and Charges": {
                "doctype": "Purchase Taxes and Charges",
                "add_if_empty": True
            }
        }, target_doc, set_missing_values)

    return doclist
def make_purchase_invoice(source_name, target_doc=None):
    from frappe.model.mapper import get_mapped_doc
    doc = frappe.get_doc('Purchase Receipt', source_name)
    purchase_orders = [d.purchase_order for d in doc.items]
    returned_qty_map_against_po = get_returned_qty_map_against_po(
        purchase_orders)
    returned_qty_map_against_pr = get_returned_qty_map_against_pr(source_name)

    invoiced_qty_map = get_invoiced_qty_map(source_name)

    def set_missing_values(source, target):
        if len(target.get("items")) == 0:
            frappe.throw(_("All items have already been invoiced"))

        doc = frappe.get_doc(target)
        doc.ignore_pricing_rule = 1
        doc.update_stock = 0
        doc.from_pr = 1
        sdoc = frappe.get_doc(source)
        doc.received_date = sdoc.posting_date
        doc.run_method("set_missing_values")
        doc.run_method("calculate_taxes_and_totals")

    def update_item(source_doc, target_doc, source_parent):
        target_doc.qty, returned_qty = get_pending_qty(source_doc)
        if source_doc.boxes_pallet_for_purchase == None:
            target_doc.pallets_ordered = 0
        elif source_doc.boxes_pallet_for_purchase == 0:
            target_doc.pallets_ordered = 0
        else:
            boxes_pallet_for_purchase = source_doc.boxes_pallet_for_purchase
            target_doc.pallets_ordered = flt(
                source_doc.box) / flt(boxes_pallet_for_purchase)

        target_doc.boxes_ordered = flt(source_doc.box)
        target_doc.received_box = flt(source_doc.received_boxed)
        if source_doc.uom == "Pallet":
            target_doc.received_qty = flt(
                source_doc.received_boxed) / flt(boxes_pallet_for_purchase)
        else:
            target_doc.received_qty = flt(source_doc.received_boxed)
        target_doc.box_rate = flt(source_doc.box_unit_rate)
        target_doc.rate = flt(source_doc.box_unit_rate)
        if source_doc.uom == "Pallet":
            target_doc.qty = flt(source_doc.received_boxed) / flt(
                source_doc.boxes_pallet_for_purchase)
        else:
            target_doc.qty = flt(source_doc.received_boxed)
        if not source_doc.purchase_order_item:
            returned_qty_map_against_pr[source_doc.item_code] = returned_qty

    def get_pending_qty(item_row):
        pending_qty = item_row.qty - invoiced_qty_map.get(item_row.name, 0) \
         - returned_qty_map_against_po.get(item_row.purchase_order_item, 0)
        returned_qty = flt(
            returned_qty_map_against_pr.get(item_row.item_code, 0))
        if not item_row.purchase_order_item:
            if returned_qty >= pending_qty:
                pending_qty = 0
                returned_qty -= pending_qty
            else:
                pending_qty -= returned_qty
                returned_qty = 0
        return pending_qty, returned_qty

    doclist = get_mapped_doc(
        "Purchase Receipt",
        source_name,
        {
            "Purchase Receipt": {
                "doctype": "Purchase Invoice",
                "field_map": {
                    "supplier_warehouse": "supplier_warehouse"
                },
                "validation": {
                    "docstatus": ["=", 1],
                },
            },
            "Purchase Receipt Item": {
                "doctype":
                "Purchase Invoice Item",
                "field_map": {
                    "name": "pr_detail",
                    "parent": "purchase_receipt",
                    "purchase_order_item": "po_detail",
                    "purchase_order": "purchase_order",
                    "is_fixed_asset": "is_fixed_asset",
                    "asset": "asset",
                },
                "postprocess":
                update_item,
                #"filter": lambda d: get_pending_qty(d)[0]<=0
                "filter":
                lambda d: abs(d.qty) - abs(invoiced_qty_map.get(d.name, 0)) <=
                0
            },
            "Purchase Taxes and Charges": {
                "doctype": "Purchase Taxes and Charges",
                "add_if_empty": True
            }
        },
        target_doc,
        set_missing_values)

    return doclist
def make_return_doc(doctype, source_name, target_doc=None):
	from frappe.model.mapper import get_mapped_doc
	def set_missing_values(source, target):
		doc = frappe.get_doc(target)
		doc.is_return = 1
		doc.return_against = source.name
		doc.ignore_pricing_rule = 1
		if doctype == "Sales Invoice":
			doc.is_pos = 0

			# look for Print Heading "Credit Note"
			if not doc.select_print_heading:
				doc.select_print_heading = frappe.db.get_value("Print Heading", _("Credit Note"))

		elif doctype == "Purchase Invoice":
			# look for Print Heading "Debit Note"
			doc.select_print_heading = frappe.db.get_value("Print Heading", _("Debit Note"))

		for tax in doc.get("taxes"):
			if tax.charge_type == "Actual":
				tax.tax_amount = -1 * tax.tax_amount

		doc.run_method("calculate_taxes_and_totals")

	def update_item(source_doc, target_doc, source_parent):
		target_doc.qty = -1* source_doc.qty
		if doctype == "Purchase Receipt":
			target_doc.received_qty = -1* source_doc.qty
			target_doc.prevdoc_doctype = source_doc.prevdoc_doctype
			target_doc.prevdoc_docname = source_doc.prevdoc_docname
			target_doc.prevdoc_detail_docname = source_doc.prevdoc_detail_docname
		elif doctype == "Purchase Invoice":
			target_doc.purchase_order = source_doc.purchase_order
			target_doc.purchase_receipt = source_doc.purchase_receipt
			target_doc.po_detail = source_doc.po_detail
			target_doc.pr_detail = source_doc.pr_detail
		elif doctype == "Delivery Note":
			target_doc.against_sales_order = source_doc.against_sales_order
			target_doc.against_sales_invoice = source_doc.against_sales_invoice
			target_doc.so_detail = source_doc.so_detail
			target_doc.si_detail = source_doc.si_detail
			target_doc.expense_account = source_doc.expense_account
		elif doctype == "Sales Invoice":
			target_doc.sales_order = source_doc.sales_order
			target_doc.delivery_note = source_doc.delivery_note
			target_doc.so_detail = source_doc.so_detail
			target_doc.dn_detail = source_doc.dn_detail
			target_doc.expense_account = source_doc.expense_account

	doclist = get_mapped_doc(doctype, source_name,	{
		doctype: {
			"doctype": doctype,

			"validation": {
				"docstatus": ["=", 1],
			}
		},
		doctype +" Item": {
			"doctype": doctype + " Item",
			"field_map": {
				"serial_no": "serial_no",
				"batch_no": "batch_no"
			},
			"postprocess": update_item
		},
	}, target_doc, set_missing_values)

	return doclist
Example #36
0
def make_return_doc(doctype, source_name, target_doc=None):
	from frappe.model.mapper import get_mapped_doc
	def set_missing_values(source, target):
		doc = frappe.get_doc(target)
		doc.is_return = 1
		doc.return_against = source.name
		doc.ignore_pricing_rule = 1
		if doctype == "Sales Invoice":
			doc.is_pos = source.is_pos

			# look for Print Heading "Credit Note"
			if not doc.select_print_heading:
				doc.select_print_heading = frappe.db.get_value("Print Heading", _("Credit Note"))

		elif doctype == "Purchase Invoice":
			# look for Print Heading "Debit Note"
			doc.select_print_heading = frappe.db.get_value("Print Heading", _("Debit Note"))

		for tax in doc.get("taxes"):
			if tax.charge_type == "Actual":
				tax.tax_amount = -1 * tax.tax_amount

		if doc.get("is_return"):
			if doc.doctype == 'Sales Invoice':
				doc.set('payments', [])
				for data in source.payments:
					doc.append('payments', {
						'mode_of_payment': data.mode_of_payment,
						'type': data.type,
						'amount': -1 * data.amount,
						'base_amount': -1 * data.base_amount
					})
			elif doc.doctype == 'Purchase Invoice':
				doc.paid_amount = -1 * source.paid_amount
				doc.base_paid_amount = -1 * source.base_paid_amount

		doc.discount_amount = -1 * source.discount_amount
		doc.run_method("calculate_taxes_and_totals")

	def update_item(source_doc, target_doc, source_parent):
		target_doc.qty = -1* source_doc.qty
		if doctype == "Purchase Receipt":
			target_doc.received_qty = -1* source_doc.received_qty
			target_doc.rejected_qty = -1* source_doc.rejected_qty
			target_doc.qty = -1* source_doc.qty
			target_doc.stock_qty = -1 * source_doc.stock_qty
			target_doc.purchase_order = source_doc.purchase_order
			target_doc.purchase_order_item = source_doc.purchase_order_item
			target_doc.rejected_warehouse = source_doc.rejected_warehouse
		elif doctype == "Purchase Invoice":
			target_doc.received_qty = -1* source_doc.received_qty
			target_doc.rejected_qty = -1* source_doc.rejected_qty
			target_doc.qty = -1* source_doc.qty
			target_doc.stock_qty = -1 * source_doc.stock_qty
			target_doc.purchase_order = source_doc.purchase_order
			target_doc.purchase_receipt = source_doc.purchase_receipt
			target_doc.rejected_warehouse = source_doc.rejected_warehouse
			target_doc.po_detail = source_doc.po_detail
			target_doc.pr_detail = source_doc.pr_detail
		elif doctype == "Delivery Note":
			target_doc.against_sales_order = source_doc.against_sales_order
			target_doc.against_sales_invoice = source_doc.against_sales_invoice
			target_doc.so_detail = source_doc.so_detail
			target_doc.si_detail = source_doc.si_detail
			target_doc.expense_account = source_doc.expense_account
		elif doctype == "Sales Invoice":
			target_doc.sales_order = source_doc.sales_order
			target_doc.delivery_note = source_doc.delivery_note
			target_doc.so_detail = source_doc.so_detail
			target_doc.dn_detail = source_doc.dn_detail
			target_doc.expense_account = source_doc.expense_account

	def update_terms(source_doc, target_doc, source_parent):
		target_doc.payment_amount = -source_doc.payment_amount

	doclist = get_mapped_doc(doctype, source_name,	{
		doctype: {
			"doctype": doctype,

			"validation": {
				"docstatus": ["=", 1],
			}
		},
		doctype +" Item": {
			"doctype": doctype + " Item",
			"field_map": {
				"serial_no": "serial_no",
				"batch_no": "batch_no"
			},
			"postprocess": update_item
		},
		"Payment Schedule": {
			"doctype": "Payment Schedule",
			"postprocess": update_terms
		}
	}, target_doc, set_missing_values)

	return doclist
Example #37
0
def make_stock_entry(source_name, target_doc=None):
    def update_item(obj, target, source_parent):
        qty = (flt(flt(obj.stock_qty) - flt(obj.ordered_qty)) /
               target.conversion_factor
               if flt(obj.stock_qty) > flt(obj.ordered_qty) else 0)
        target.qty = qty
        target.transfer_qty = qty * obj.conversion_factor
        target.conversion_factor = obj.conversion_factor

        if (source_parent.material_request_type == "Material Transfer"
                or source_parent.material_request_type == "Customer Provided"):
            target.t_warehouse = obj.warehouse
        else:
            target.s_warehouse = obj.warehouse

        if source_parent.material_request_type == "Customer Provided":
            target.allow_zero_valuation_rate = 1

        if source_parent.material_request_type == "Material Transfer":
            target.s_warehouse = obj.from_warehouse

    def set_missing_values(source, target):
        target.purpose = source.material_request_type
        if source.job_card:
            target.purpose = "Material Transfer for Manufacture"

        if source.material_request_type == "Customer Provided":
            target.purpose = "Material Receipt"

        target.set_missing_values()
        target.set_stock_entry_type()
        target.set_job_card_data()

    doclist = get_mapped_doc(
        "Material Request",
        source_name,
        {
            "Material Request": {
                "doctype": "Stock Entry",
                "validation": {
                    "docstatus": ["=", 1],
                    "material_request_type": [
                        "in",
                        [
                            "Material Transfer", "Material Issue",
                            "Customer Provided"
                        ]
                    ],
                },
            },
            "Material Request Item": {
                "doctype": "Stock Entry Detail",
                "field_map": {
                    "name": "material_request_item",
                    "parent": "material_request",
                    "uom": "stock_uom",
                    "job_card_item": "job_card_item",
                },
                "postprocess": update_item,
                "condition": lambda doc: doc.ordered_qty < doc.stock_qty,
            },
        },
        target_doc,
        set_missing_values,
    )

    return doclist
Example #38
0
def make_purchase_invoice(source_name, target_doc=None):
    def postprocess(source, target):
        set_missing_values(source, target)
        #Get the advance paid Journal Entries in Purchase Invoice Advance

        if target.get("allocate_advances_automatically"):
            target.set_advances()

    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 = target.amount / flt(obj.rate) if (
            flt(obj.rate) and flt(obj.billed_amt)) else flt(obj.qty)

        item = get_item_defaults(target.item_code, source_parent.company)
        item_group = get_item_group_defaults(target.item_code,
                                             source_parent.company)
        target.cost_center = (obj.cost_center or frappe.db.get_value(
            "Project", obj.project, "cost_center")
                              or item.get("buying_cost_center")
                              or item_group.get("buying_cost_center"))

    fields = {
        "Purchase Order": {
            "doctype": "Purchase Invoice",
            "field_map": {
                "party_account_currency": "party_account_currency",
                "supplier_warehouse": "supplier_warehouse"
            },
            "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 abs(doc.billed_amt) < abs(doc.amount))
        },
        "Purchase Taxes and Charges": {
            "doctype": "Purchase Taxes and Charges",
            "add_if_empty": True
        }
    }

    if frappe.get_single(
            "Accounts Settings").automatically_fetch_payment_terms == 1:
        fields["Payment Schedule"] = {
            "doctype": "Payment Schedule",
            "add_if_empty": True
        }

    doc = get_mapped_doc("Purchase Order", source_name, fields, target_doc,
                         postprocess)

    return doc
Example #39
0
def estudante_enroll(source_name):
	"""Creates a Student Record and returns a Program Enrollment.

	:param source_name: Student Applicant.
	"""
	frappe.publish_realtime('enroll_student_progress', {"progress": [1, 4]}, user=frappe.session.user)
	student = get_mapped_doc("Student Applicant", source_name,
		{"Student Applicant": {
			"doctype": "Student",
			"field_map": {
				"name": "student_applicant"
			}
		}}, ignore_permissions=True)

	student.save()

	frappe.db.set_value('Student',student.name,'_user_tags',student.title[0])
	frappe.db.commit()


	#Cria Customer	
	cliente = get_mapped_doc("Student Applicant", source_name,
		{"Student Applicant": {
			"doctype": "Customer",
			"field_map": {
				"name": "student_applicant"
			}
		}}, ignore_permissions=True)

	cliente.customer_name = student.title
	cliente.customer_group = 'Individual'
	cliente.territory = 'Angola'
	cliente.language = 'pt'

	cliente.save()

	frappe.db.set_value('Customer',cliente.name,'_user_tags',student.title[0])
	frappe.db.commit()
	

	contacto = frappe.new_doc("Contact")
	contacto.name = student.title
	contacto.first_name = student.first_name	
	contacto.middle_name = student.middle_name	
	contacto.last_name = student.last_name	
	contacto.gender = student.gender
	contacto.email_id = student.student_email_id
	contacto.mobile_no = student.student_mobile_number
	#contacto.parent

	contacto.status = 'Passive'
	contacto.save()


	contacto_link = frappe.new_doc('Dynamic Link')
	contacto_link.parent = contacto.name
	contacto_link.parentfield ='links'
	contacto_link.parenttype ='Contact'
	contacto_link.link_title = student.title
	contacto_link.link_doctype ='Customer'
	contacto_link.link_name = student.title

	contacto_link.save()


	program_enrollment = frappe.new_doc("Program Enrollment")
	program_enrollment.student = student.name
	program_enrollment.student_name = student.title
	program_enrollment.program = frappe.db.get_value("Student Applicant", source_name, "program")
	frappe.publish_realtime('enroll_student_progress', {"progress": [4, 4]}, user=frappe.session.user)	
	return program_enrollment
Example #40
0
def make_inter_company_transaction(doctype, source_name, target_doc=None):
	from erpnext.accounts.doctype.sales_invoice.sales_invoice import validate_inter_company_transaction, get_inter_company_details

	if doctype == 'Delivery Note':
		source_doc = frappe.get_doc(doctype, source_name)
		target_doctype = "Purchase Receipt"
		source_document_warehouse_field = 'target_warehouse'
		target_document_warehouse_field = 'from_warehouse'
	else:
		source_doc = frappe.get_doc(doctype, source_name)
		target_doctype = 'Delivery Note'
		source_document_warehouse_field = 'from_warehouse'
		target_document_warehouse_field = 'target_warehouse'

	validate_inter_company_transaction(source_doc, doctype)
	details = get_inter_company_details(source_doc, doctype)

	def set_missing_values(source, target):
		target.run_method("set_missing_values")

		if target.doctype == 'Purchase Receipt':
			master_doctype = 'Purchase Taxes and Charges Template'
		else:
			master_doctype = 'Sales Taxes and Charges Template'

		if not target.get('taxes') and target.get('taxes_and_charges'):
			for tax in get_taxes_and_charges(master_doctype, target.get('taxes_and_charges')):
				target.append('taxes', tax)

	def update_details(source_doc, target_doc, source_parent):
		target_doc.inter_company_invoice_reference = source_doc.name
		if target_doc.doctype == 'Purchase Receipt':
			target_doc.company = details.get("company")
			target_doc.supplier = details.get("party")
			target_doc.supplier_address = source_doc.company_address
			target_doc.shipping_address = source_doc.shipping_address_name or source_doc.customer_address
			target_doc.buying_price_list = source_doc.selling_price_list
			target_doc.is_internal_supplier = 1
			target_doc.inter_company_reference = source_doc.name
		else:
			target_doc.company = details.get("company")
			target_doc.customer = details.get("party")
			target_doc.company_address = source_doc.supplier_address
			target_doc.shipping_address_name = source_doc.shipping_address
			target_doc.selling_price_list = source_doc.buying_price_list
			target_doc.is_internal_customer = 1
			target_doc.inter_company_reference = source_doc.name

	doclist = get_mapped_doc(doctype, source_name,	{
		doctype: {
			"doctype": target_doctype,
			"postprocess": update_details,
			"field_no_map": [
				"taxes_and_charges"
			]
		},
		doctype +" Item": {
			"doctype": target_doctype + " Item",
			"field_map": {
				source_document_warehouse_field: target_document_warehouse_field
			},
			"field_no_map": [
				"warehouse"
			]
		}

	}, target_doc, set_missing_values)

	return doclist
Example #41
0
def make_appointment(frm):
    appointment = get_mapped_doc("Registration", frm,
                                 {"Registration": {
                                     "doctype": "Appointments"
                                 }})
    return appointment
Example #42
0
def make_purchase_order(source_name, selected_items=[], target_doc=None):
	if isinstance(selected_items, string_types):
		selected_items = json.loads(selected_items)

	items_to_map = [item.get('item_code') for item in selected_items if item.get('item_code') and item.get('item_code')]
	items_to_map = list(set(items_to_map))

	def set_missing_values(source, target):
		target.supplier = ""
		target.apply_discount_on = ""
		target.additional_discount_percentage = 0.0
		target.discount_amount = 0.0
		target.inter_company_order_reference = ""
		target.customer = ""
		target.customer_name = ""
		target.run_method("set_missing_values")
		target.run_method("calculate_taxes_and_totals")

	def update_item(source, target, source_parent):
		target.schedule_date = source.delivery_date
		target.qty = flt(source.qty) - (flt(source.ordered_qty) / flt(source.conversion_factor))
		target.stock_qty = (flt(source.stock_qty) - flt(source.ordered_qty))
		target.project = source_parent.project

	# po = frappe.get_list("Purchase Order", filters={"sales_order":source_name, "supplier":supplier, "docstatus": ("<", "2")})
	doc = get_mapped_doc("Sales Order", source_name, {
		"Sales Order": {
			"doctype": "Purchase Order",
			"field_no_map": [
				"address_display",
				"contact_display",
				"contact_mobile",
				"contact_email",
				"contact_person",
				"taxes_and_charges",
				"shipping_address"
			],
			"validation": {
				"docstatus": ["=", 1]
			}
		},
		"Sales Order Item": {
			"doctype": "Purchase Order Item",
			"field_map":  [
				["name", "sales_order_item"],
				["parent", "sales_order"],
				["stock_uom", "stock_uom"],
				["uom", "uom"],
				["conversion_factor", "conversion_factor"],
				["delivery_date", "schedule_date"]
			],
			"field_no_map": [
				"rate",
				"price_list_rate",
				"item_tax_template",
				"supplier"
			],
			"postprocess": update_item,
			"condition": lambda doc: doc.ordered_qty < doc.stock_qty and doc.item_code in items_to_map
		}
	}, target_doc, set_missing_values)
	return doc
def make_delivery_note_cs(source_name=None,
                          target_doc=None,
                          item_code=None,
                          test_list=None):
    test_list_json = json.loads(test_list)
    for source_name in test_list_json:

        def set_missing_values(source, target):
            if source.po_no:
                if target.po_no:
                    target_po_no = target.po_no.split(", ")
                    target_po_no.append(source.po_no)
                    target.po_no = ", ".join(list(set(target_po_no))) if len(
                        target_po_no) > 1 else target_po_no[0]
                else:
                    target.po_no = source.po_no

            target.ignore_pricing_rule = 1
            target.run_method("set_missing_values")
            target.run_method("calculate_taxes_and_totals")

        def update_item(source, target, source_parent):
            target.base_amount = (flt(source.qty) - flt(
                source.delivered_qty)) * flt(source.base_rate)
            target.amount = (flt(source.qty) -
                             flt(source.delivered_qty)) * flt(source.rate)
            target.qty = flt(source.qty) - flt(source.delivered_qty)

        target_doc = get_mapped_doc(
            "Sales Order", source_name, {
                "Sales Order": {
                    "doctype": "Delivery Note",
                    "validation": {
                        "docstatus": ["=", 1]
                    }
                },
                "Sales Order Item": {
                    "doctype":
                    "Delivery Note Item",
                    "field_map": {
                        "rate": "rate",
                        "name": "so_detail",
                        "parent": "against_sales_order",
                    },
                    "postprocess":
                    update_item,
                    "condition":
                    lambda doc: abs(doc.delivered_qty) < abs(doc.qty) and doc.
                    delivered_by_supplier != 1 and doc.item_code == item_code
                },
                "Sales Taxes and Charges": {
                    "doctype": "Sales Taxes and Charges",
                    "add_if_empty": True
                },
                "Sales Team": {
                    "doctype": "Sales Team",
                    "add_if_empty": True
                },
            }, target_doc, set_missing_values)

    return target_doc
Example #44
0
def _make_sales_order(source_name, target_doc=None, ignore_permissions=False):
    customer = _make_customer(source_name, ignore_permissions)

    def set_missing_values(source, target):
        if customer:
            target.customer = customer.name
            target.customer_name = customer.customer_name
        if source.referral_sales_partner:
            target.sales_partner = source.referral_sales_partner
            target.commission_rate = frappe.get_value(
                'Sales Partner', source.referral_sales_partner,
                'commission_rate')
        target.ignore_pricing_rule = 1
        target.flags.ignore_permissions = ignore_permissions
        target.run_method("set_missing_values")
        target.run_method("calculate_taxes_and_totals")

    def update_item(obj, target, source_parent):
        target.stock_qty = flt(obj.qty) * flt(obj.conversion_factor)

    def update_payment_schedule(obj, target, source_parent):
        if getdate(target.due_date) < getdate(nowdate()):
            target.due_date = getdate(nowdate())

    doclist = get_mapped_doc(
        "Quotation",
        source_name, {
            "Quotation": {
                "doctype": "Sales Order",
                "validation": {
                    "docstatus": ["=", 1]
                },
                "field_map": {
                    "requested_delivery_date": "delivery_date"
                }
            },
            "Quotation Item": {
                "doctype": "Sales Order Item",
                "field_map": {
                    "parent": "prevdoc_docname",
                    "requested_delivery_date": "delivery_date"
                },
                "postprocess": update_item
            },
            "Sales Taxes and Charges": {
                "doctype": "Sales Taxes and Charges",
                "add_if_empty": True
            },
            "Sales Team": {
                "doctype": "Sales Team",
                "add_if_empty": True
            },
            "Payment Schedule": {
                "doctype": "Payment Schedule",
                "add_if_empty": True,
                "postprocess": update_payment_schedule
            }
        },
        target_doc,
        set_missing_values,
        ignore_permissions=ignore_permissions)

    # postprocess: fetch shipping address, set missing values

    return doclist
Example #45
0
def make_sales_invoice(source_name, target_doc=None, ignore_permissions=False):
    def postprocess(source, target):
        set_missing_values(source, target)
        #Get the advance paid Journal Entries in Sales Invoice Advance
        target.set_advances()

    def set_missing_values(source, target):
        target.is_pos = 0
        target.ignore_pricing_rule = 1
        target.flags.ignore_permissions = True
        target.run_method("set_missing_values")
        target.run_method("calculate_taxes_and_totals")

        # set company address
        target.update(get_company_address(target.company))
        if target.company_address:
            target.update(
                get_fetch_values("Sales Invoice", 'company_address',
                                 target.company_address))

    def update_item(source, target, source_parent):
        target.amount = flt(source.amount) - flt(source.billed_amt)
        target.base_amount = target.amount * flt(source_parent.conversion_rate)
        target.qty = target.amount / flt(source.rate) if (
            source.rate and source.billed_amt) else source.qty

        item = frappe.db.get_value("Item",
                                   target.item_code,
                                   ["item_group", "selling_cost_center"],
                                   as_dict=1)
        target.cost_center = frappe.db.get_value("Project", source_parent.project, "cost_center") \
         or item.selling_cost_center \
         or frappe.db.get_value("Item Group", item.item_group, "default_cost_center")

    doclist = get_mapped_doc(
        "Sales Order",
        source_name, {
            "Sales Order": {
                "doctype": "Sales Invoice",
                "field_map": {
                    "party_account_currency": "party_account_currency"
                },
                "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.qty and
                (doc.base_amount == 0 or abs(doc.billed_amt) < abs(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_doc,
        postprocess,
        ignore_permissions=ignore_permissions)

    return doclist
def make_return_doc(doctype, source_name, target_doc=None):
    from frappe.model.mapper import get_mapped_doc
    from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos
    company = frappe.db.get_value("Delivery Note", source_name, "company")
    default_warehouse_for_sales_return = frappe.db.get_value(
        "Company", company, "default_warehouse_for_sales_return")

    def set_missing_values(source, target):
        doc = frappe.get_doc(target)
        doc.is_return = 1
        doc.return_against = source.name
        doc.ignore_pricing_rule = 1
        doc.set_warehouse = ""
        if doctype == "Sales Invoice" or doctype == "POS Invoice":
            doc.is_pos = source.is_pos

            # look for Print Heading "Credit Note"
            if not doc.select_print_heading:
                doc.select_print_heading = frappe.db.get_value(
                    "Print Heading", _("Credit Note"))

        elif doctype == "Purchase Invoice":
            # look for Print Heading "Debit Note"
            doc.select_print_heading = frappe.db.get_value(
                "Print Heading", _("Debit Note"))

        for tax in doc.get("taxes"):
            if tax.charge_type == "Actual":
                tax.tax_amount = -1 * tax.tax_amount

        if doc.get("is_return"):
            if doc.doctype == 'Sales Invoice' or doc.doctype == 'POS Invoice':
                doc.consolidated_invoice = ""
                doc.set('payments', [])
                for data in source.payments:
                    paid_amount = 0.00
                    base_paid_amount = 0.00
                    data.base_amount = flt(
                        data.amount * source.conversion_rate,
                        source.precision("base_paid_amount"))
                    paid_amount += data.amount
                    base_paid_amount += data.base_amount
                    doc.append(
                        'payments', {
                            'mode_of_payment': data.mode_of_payment,
                            'type': data.type,
                            'amount': -1 * paid_amount,
                            'base_amount': -1 * base_paid_amount,
                            'account': data.account,
                            'default': data.default
                        })
                if doc.is_pos:
                    doc.paid_amount = -1 * source.paid_amount
            elif doc.doctype == 'Purchase Invoice':
                doc.paid_amount = -1 * source.paid_amount
                doc.base_paid_amount = -1 * source.base_paid_amount
                doc.payment_terms_template = ''
                doc.payment_schedule = []

        if doc.get("is_return") and hasattr(doc, "packed_items"):
            for d in doc.get("packed_items"):
                d.qty = d.qty * -1

        doc.discount_amount = -1 * source.discount_amount
        doc.run_method("calculate_taxes_and_totals")

    def update_item(source_doc, target_doc, source_parent):
        target_doc.qty = -1 * source_doc.qty

        if source_doc.serial_no:
            returned_serial_nos = get_returned_serial_nos(
                source_doc, source_parent)
            serial_nos = list(
                set(get_serial_nos(source_doc.serial_no)) -
                set(returned_serial_nos))
            if serial_nos:
                target_doc.serial_no = '\n'.join(serial_nos)

        if doctype == "Purchase Receipt":
            returned_qty_map = get_returned_qty_map_for_row(
                source_doc.name, doctype)
            target_doc.received_qty = -1 * \
                flt(source_doc.received_qty -
                    (returned_qty_map.get('received_qty') or 0))
            target_doc.rejected_qty = -1 * \
                flt(source_doc.rejected_qty -
                    (returned_qty_map.get('rejected_qty') or 0))
            target_doc.qty = -1 * \
                flt(source_doc.qty - (returned_qty_map.get('qty') or 0))

            target_doc.stock_qty = -1 * \
                flt(source_doc.stock_qty - (returned_qty_map.get('stock_qty') or 0))
            target_doc.received_stock_qty = -1 * \
                flt(source_doc.received_stock_qty -
                    (returned_qty_map.get('received_stock_qty') or 0))

            target_doc.purchase_order = source_doc.purchase_order
            target_doc.purchase_order_item = source_doc.purchase_order_item
            target_doc.rejected_warehouse = source_doc.rejected_warehouse
            target_doc.purchase_receipt_item = source_doc.name

        elif doctype == "Purchase Invoice":
            returned_qty_map = get_returned_qty_map_for_row(
                source_doc.name, doctype)
            target_doc.received_qty = -1 * \
                flt(source_doc.received_qty -
                    (returned_qty_map.get('received_qty') or 0))
            target_doc.rejected_qty = -1 * \
                flt(source_doc.rejected_qty -
                    (returned_qty_map.get('rejected_qty') or 0))
            target_doc.qty = -1 * \
                flt(source_doc.qty - (returned_qty_map.get('qty') or 0))

            target_doc.stock_qty = -1 * \
                flt(source_doc.stock_qty - (returned_qty_map.get('stock_qty') or 0))
            target_doc.purchase_order = source_doc.purchase_order
            target_doc.purchase_receipt = source_doc.purchase_receipt
            target_doc.rejected_warehouse = source_doc.rejected_warehouse
            target_doc.po_detail = source_doc.po_detail
            target_doc.pr_detail = source_doc.pr_detail
            target_doc.purchase_invoice_item = source_doc.name
            target_doc.price_list_rate = 0

        elif doctype == "Delivery Note":
            returned_qty_map = get_returned_qty_map_for_row(
                source_doc.name, doctype)
            target_doc.qty = -1 * \
                flt(source_doc.qty - (returned_qty_map.get('qty') or 0))
            target_doc.stock_qty = -1 * \
                flt(source_doc.stock_qty - (returned_qty_map.get('stock_qty') or 0))

            target_doc.against_sales_order = source_doc.against_sales_order
            target_doc.against_sales_invoice = source_doc.against_sales_invoice
            target_doc.so_detail = source_doc.so_detail
            target_doc.si_detail = source_doc.si_detail
            target_doc.expense_account = source_doc.expense_account
            target_doc.dn_detail = source_doc.name
            if default_warehouse_for_sales_return:
                target_doc.warehouse = default_warehouse_for_sales_return
        elif doctype == "Sales Invoice" or doctype == "POS Invoice":
            returned_qty_map = get_returned_qty_map_for_row(
                source_doc.name, doctype)
            target_doc.qty = -1 * \
                flt(source_doc.qty - (returned_qty_map.get('qty') or 0))
            target_doc.stock_qty = -1 * \
                flt(source_doc.stock_qty - (returned_qty_map.get('stock_qty') or 0))

            target_doc.sales_order = source_doc.sales_order
            target_doc.delivery_note = source_doc.delivery_note
            target_doc.so_detail = source_doc.so_detail
            target_doc.dn_detail = source_doc.dn_detail
            target_doc.expense_account = source_doc.expense_account
            target_doc.sales_invoice_item = source_doc.name
            target_doc.price_list_rate = 0
            if default_warehouse_for_sales_return:
                target_doc.warehouse = default_warehouse_for_sales_return

    def update_terms(source_doc, target_doc, source_parent):
        target_doc.payment_amount = -source_doc.payment_amount

    doclist = get_mapped_doc(
        doctype, source_name, {
            doctype: {
                "doctype": doctype,
                "validation": {
                    "docstatus": ["=", 1],
                }
            },
            doctype + " Item": {
                "doctype": doctype + " Item",
                "field_map": {
                    "serial_no": "serial_no",
                    "batch_no": "batch_no"
                },
                "postprocess": update_item
            },
            "Payment Schedule": {
                "doctype": "Payment Schedule",
                "postprocess": update_terms
            }
        }, target_doc, set_missing_values)

    return doclist
Example #47
0
def make_inter_company_transaction(self, target_doc=None):
    source_doc = frappe.get_doc("Sales Invoice", self.name)

    validate_inter_company_transaction(source_doc, "Sales Invoice")
    details = get_inter_company_details(source_doc, "Sales Invoice")

    def set_missing_values(source, target):
        if self.amended_from:
            name = frappe.db.get_value("Purchase Invoice",
                                       {'si_ref': self.amended_from}, "name")
            target.amended_from = name

        target.company = source.customer
        target.supplier = source.company
        # target.buying_price_list = source.selling_price_list
        target.posting_date = source.posting_date

        abbr = frappe.db.get_value("Company", target.company, 'abbr')

        target.set_warehouse = self.set_target_warehouse

        if source.taxes_and_charges:
            target_company_abbr = frappe.db.get_value("Company",
                                                      target.company, "abbr")
            source_company_abbr = frappe.db.get_value("Company",
                                                      source.company, "abbr")
            taxes_and_charges = source.taxes_and_charges.replace(
                source_company_abbr, target_company_abbr)

            if frappe.db.exists("Purchase Taxes and Charges Template",
                                taxes_and_charges):
                target.taxes_and_charges = taxes_and_charges
            else:
                frappe.throw(
                    "Please Create Purchase Taxes and Charges Template Like Sales Taxes and Charges Template {}"
                    .format(frappe.bold(source.taxes_and_charges)))
            target.taxes = source.taxes

            for index, item in enumerate(source.taxes):
                target.taxes[index].account_head = item.account_head.replace(
                    source_company_abbr, target_company_abbr)
                target.taxes[index].cost_center = item.cost_center.replace(
                    source_company_abbr, target_company_abbr)

        target.run_method("set_missing_values")

    def update_accounts(source_doc, target_doc, source_parent):
        target_company = source_parent.customer
        doc = frappe.get_doc("Company", target_company)

        target_company_abbr = frappe.db.get_value("Company", target_company,
                                                  "abbr")
        source_company_abbr = frappe.db.get_value("Company",
                                                  source_parent.company,
                                                  "abbr")
        if source_doc.pr_detail:
            target_doc.purchase_receipt = frappe.db.get_value(
                "Purchase Receipt Item", source_doc.pr_detail, 'parent')
        if source_doc.purchase_order_item:
            target_doc.purchase_order = frappe.db.get_value(
                "Purchase Order Item", source_doc.purchase_order_item,
                'parent')

        target_doc.income_account = doc.default_income_account
        target_doc.expense_account = doc.default_expense_account
        target_doc.cost_center = source_doc.cost_center.replace(
            source_company_abbr, target_company_abbr)

    def post_process(source, target):
        target_company_abbr = frappe.db.get_value("Company", source.company,
                                                  "abbr")
        source_company_abbr = frappe.db.get_value("Company", target.company,
                                                  "abbr")
        target.taxes_and_charges = source.taxes_and_charges.replace(
            source_company_abbr, target_company_abbr)
        target.account_head = source.account_head.replace(
            source_company_abbr, target_company_abbr)
        target.cost_center = source.cost_center.replace(
            source_company_abbr, target_company_abbr)

    doclist = get_mapped_doc(
        "Sales Invoice",
        self.name,
        {
            "Sales Invoice": {
                "doctype":
                "Purchase Invoice",
                "field_map": {
                    "name": "bill_no",
                    "posting_date": "bill_date",
                    "set_target_warehouse": "set_warehouse",
                    "shipping_address_name": "shipping_address",
                    "shipping_address": "shipping_address_display",
                },
                "field_no_map": [
                    "series_value", "update_stock", "real_difference_amount",
                    "cost_center"
                ]
            },
            "Sales Invoice Item": {
                "doctype":
                "Purchase Invoice Item",
                "field_map": {
                    "pr_detail": "pr_detail",
                    "purchase_order_item": "po_detail",
                },
                "field_no_map": [
                    "income_account",
                    "expense_account",
                    "warehouse",
                    "proforma_invoice",
                ],
                "postprocess":
                update_accounts,
            },
            # "Sales Taxes and Charges":{
            # 	"doctype":"Purchase Taxes and Charges",
            # 	"field_no_map": ["dont_recompute_tax"]
            # }
        },
        target_doc,
        set_missing_values)

    return doclist
Example #48
0
def make_delivery_note(source_name, target_doc=None):
    def set_missing_values(source, target):
        if source.po_no:
            if target.po_no:
                target_po_no = target.po_no.split(", ")
                target_po_no.append(source.po_no)
                target.po_no = ", ".join(list(set(target_po_no))) if len(
                    target_po_no) > 1 else target_po_no[0]
            else:
                target.po_no = source.po_no

        target.ignore_pricing_rule = 1
        target.run_method("set_missing_values")
        target.run_method("calculate_taxes_and_totals")

        # set company address
        target.update(get_company_address(target.company))
        if target.company_address:
            target.update(
                get_fetch_values("Delivery Note", 'company_address',
                                 target.company_address))

    def update_item(source, target, source_parent):
        target.base_amount = (flt(source.qty) - flt(
            source.delivered_qty)) * flt(source.base_rate)
        target.amount = (flt(source.qty) - flt(source.delivered_qty)) * flt(
            source.rate)
        target.qty = flt(source.qty) - flt(source.delivered_qty)

        item = frappe.db.get_value("Item",
                                   target.item_code,
                                   ["item_group", "selling_cost_center"],
                                   as_dict=1)

        if item:
            target.cost_center = frappe.db.get_value("Project", source_parent.project, "cost_center") \
             or item.selling_cost_center \
             or frappe.db.get_value("Item Group", item.item_group, "default_cost_center")

    target_doc = get_mapped_doc(
        "Sales Order", source_name, {
            "Sales Order": {
                "doctype": "Delivery Note",
                "validation": {
                    "docstatus": ["=", 1]
                }
            },
            "Sales Order Item": {
                "doctype":
                "Delivery Note Item",
                "field_map": {
                    "rate": "rate",
                    "name": "so_detail",
                    "parent": "against_sales_order",
                },
                "postprocess":
                update_item,
                "condition":
                lambda doc: abs(doc.delivered_qty) < abs(doc.qty) and doc.
                delivered_by_supplier != 1
            },
            "Sales Taxes and Charges": {
                "doctype": "Sales Taxes and Charges",
                "add_if_empty": True
            },
            "Sales Team": {
                "doctype": "Sales Team",
                "add_if_empty": True
            }
        }, target_doc, set_missing_values)

    return target_doc
def make_delivery_note(source_name, target_doc=None, set_warehouse=None):
    def warehouse_condition(doc):
        if set_warehouse:
            return doc.warehouse == set_warehouse
        else:
            return True
    def set_missing_values(source, target):
        target.ignore_pricing_rule = 1
        target.run_method("set_missing_values")
        target.run_method("calculate_taxes_and_totals")
    
    def get_qty(source_doc):
        delivery_note_item_count = get_delivery_note_item_count(source_doc.name, source_doc.parent)
        return flt(source_doc.stock_qty) - delivery_note_item_count
        

    def update_item(source_doc, target_doc, source_parent):
        target_doc.stock_qty = get_qty(source_doc)
        target_doc.qty = target_doc.stock_qty / flt(source_doc.conversion_factor)
        target_doc.base_amount = target_doc.qty * flt(source_doc.base_rate)
        target_doc.amount = target_doc.qty * flt(source_doc.rate)


    doclist = get_mapped_doc("Sales Invoice", source_name,     {
        "Sales Invoice": {
            "doctype": "Delivery Note",
            "validation": {
                "docstatus": ["=", 1]
            }
        },
        "Sales Invoice Item": {
            "doctype": "Delivery Note Item",
            "field_map": {
                "name": "si_detail",
                "parent": "against_sales_invoice",
                "serial_no": "serial_no",
                "sales_order": "against_sales_order",
                "so_detail": "so_detail",
                "cost_center": "cost_center",
                "Warehouse":"warehouse",
            },
            "postprocess": update_item,
            "condition": lambda doc: check_item_is_maintain(doc.item_code),
            "condition": lambda doc: warehouse_condition(doc),
        },
        "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_doc, set_missing_values, ignore_permissions=True)

    items_list = []
    for it in doclist.items:
        if float(it.qty) != 0.0 and check_item_is_maintain(it.item_code):
            items_list.append(it)
    doclist.items = items_list

    return doclist
Example #50
0
def make_purchase_order_for_drop_shipment(source_name,
                                          for_supplier,
                                          target_doc=None):
    def set_missing_values(source, target):
        target.supplier = for_supplier
        target.apply_discount_on = ""
        target.additional_discount_percentage = 0.0
        target.discount_amount = 0.0

        default_price_list = frappe.get_value("Supplier", for_supplier,
                                              "default_price_list")
        if default_price_list:
            target.buying_price_list = default_price_list

        if any(item.delivered_by_supplier == 1 for item in source.items):
            if source.shipping_address_name:
                target.shipping_address = source.shipping_address_name
                target.shipping_address_display = source.shipping_address
            else:
                target.shipping_address = source.customer_address
                target.shipping_address_display = source.address_display

            target.customer_contact_person = source.contact_person
            target.customer_contact_display = source.contact_display
            target.customer_contact_mobile = source.contact_mobile
            target.customer_contact_email = source.contact_email

        else:
            target.customer = ""
            target.customer_name = ""

        target.run_method("set_missing_values")
        target.run_method("calculate_taxes_and_totals")

    def update_item(source, target, source_parent):
        target.schedule_date = source.delivery_date
        target.qty = flt(source.qty) - flt(source.ordered_qty)
        target.stock_qty = (flt(source.qty) - flt(source.ordered_qty)) * flt(
            source.conversion_factor)

    doclist = get_mapped_doc(
        "Sales Order", source_name, {
            "Sales Order": {
                "doctype":
                "Purchase Order",
                "field_no_map": [
                    "address_display", "contact_display", "contact_mobile",
                    "contact_email", "contact_person", "taxes_and_charges"
                ],
                "validation": {
                    "docstatus": ["=", 1]
                }
            },
            "Sales Order Item": {
                "doctype":
                "Purchase Order Item",
                "field_map": [["name", "sales_order_item"],
                              ["parent", "sales_order"],
                              ["stock_uom", "stock_uom"], ["uom", "uom"],
                              ["conversion_factor", "conversion_factor"],
                              ["delivery_date", "schedule_date"]],
                "field_no_map": ["rate", "price_list_rate"],
                "postprocess":
                update_item,
                "condition":
                lambda doc: doc.ordered_qty < doc.qty and doc.supplier ==
                for_supplier
            }
        }, target_doc, set_missing_values)

    return doclist
Example #51
0
def make_purchase_receipt(source_name, target_doc=None):
    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.rate) * flt(source_parent.conversion_rate)
        target.project = source_parent.project

    def update_target_doc(obj, target, source_parent):
        pass

    def set_missing_values(source, target):
        target.supplier = source.company
        target.supplier_delivery_note = source.name

        target.company = source.customer

        target.shipping_address = source.shipping_address_name
        target.title = source.title

        target.ignore_pricing_rule = 1
        target.run_method("set_missing_values")
        target.run_method("calculate_taxes_and_totals")

        if not target.company == source.company:
            from erpnext.stock.utils import get_default_warehouse

            default_warehouse = get_default_warehouse(
                company=target.company).get("source_warehouse")

            for d in target.get("items"):
                d.warehouse = default_warehouse

        target.flags.ignore_permissions = True
        target.insert()

    doc = get_mapped_doc(
        "Delivery Note",
        source_name,
        {
            "Delivery Note": {
                "doctype": "Purchase Receipt",
                "field_map": {
                    # "per_billed": "per_billed"
                },
                "validation": {
                    "docstatus": ["=", 1],
                }
            },
            "Delivery Note Item": {
                "doctype": "Purchase Receipt Item",
                # "field_map": {
                # "name": "delivery_note_item",
                # "parent": "delivery_note"
                # },
                "postprocess": update_item
            },
            "Sales Taxes and Charges": {
                "doctype": "Purchase Taxes and Charges",
                "add_if_empty": True
            }
        },
        target_doc,
        set_missing_values)

    return doc
Example #52
0
def make_sales_invoice(source_name, target_doc=None):
    doc = frappe.get_doc('Delivery Note', source_name)

    to_make_invoice_qty_map = {}
    returned_qty_map = get_returned_qty_map(source_name)
    invoiced_qty_map = get_invoiced_qty_map(source_name)

    def set_missing_values(source, target):
        target.ignore_pricing_rule = 1
        target.run_method("set_missing_values")
        target.run_method("set_po_nos")

        if len(target.get("items")) == 0:
            frappe.throw(
                _("All these items have already been Invoiced/Returned"))

        target.run_method("calculate_taxes_and_totals")

        # set company address
        if source.company_address:
            target.update({'company_address': source.company_address})
        else:
            # set company address
            target.update(get_company_address(target.company))

        if target.company_address:
            target.update(
                get_fetch_values("Sales Invoice", 'company_address',
                                 target.company_address))

    def update_item(source_doc, target_doc, source_parent):
        target_doc.qty = to_make_invoice_qty_map[source_doc.name]

        if source_doc.serial_no and source_parent.per_billed > 0 and not source_parent.is_return:
            target_doc.serial_no = get_delivery_note_serial_no(
                source_doc.item_code, target_doc.qty, source_parent.name)

    def get_pending_qty(item_row):
        pending_qty = item_row.qty - invoiced_qty_map.get(item_row.name, 0)

        returned_qty = 0
        if returned_qty_map.get(item_row.name, 0) > 0:
            returned_qty = flt(returned_qty_map.get(item_row.name, 0))
            returned_qty_map[item_row.name] -= pending_qty

        if returned_qty:
            if returned_qty >= pending_qty:
                pending_qty = 0
                returned_qty -= pending_qty
            else:
                pending_qty -= returned_qty
                returned_qty = 0

        to_make_invoice_qty_map[item_row.name] = pending_qty

        return pending_qty

    doc = get_mapped_doc(
        "Delivery Note", source_name, {
            "Delivery Note": {
                "doctype": "Sales Invoice",
                "field_map": {
                    "is_return": "is_return"
                },
                "validation": {
                    "docstatus": ["=", 1]
                }
            },
            "Delivery Note Item": {
                "doctype":
                "Sales Invoice Item",
                "field_map": {
                    "name": "dn_detail",
                    "parent": "delivery_note",
                    "so_detail": "so_detail",
                    "against_sales_order": "sales_order",
                    "serial_no": "serial_no",
                    "cost_center": "cost_center"
                },
                "postprocess":
                update_item,
                "filter":
                lambda d: get_pending_qty(d) <= 0
                if not doc.get("is_return") else get_pending_qty(d) > 0
            },
            "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_doc, set_missing_values)

    return doc
Example #53
0
def get_mapped_purchase_invoice(source_name,
                                target_doc=None,
                                ignore_permissions=False):
    def postprocess(source, target):
        target.flags.ignore_permissions = ignore_permissions
        set_missing_values(source, target)
        # Get the advance paid Journal Entries in Purchase Invoice Advance
        if target.get("allocate_advances_automatically"):
            target.set_advances()

        target.set_payment_schedule()

    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 = (target.amount / flt(obj.rate) if
                      (flt(obj.rate) and flt(obj.billed_amt)) else flt(
                          obj.qty))

        item = get_item_defaults(target.item_code, source_parent.company)
        item_group = get_item_group_defaults(target.item_code,
                                             source_parent.company)
        target.cost_center = (obj.cost_center or frappe.db.get_value(
            "Project", obj.project, "cost_center")
                              or item.get("buying_cost_center")
                              or item_group.get("buying_cost_center"))

    fields = {
        "Purchase Order": {
            "doctype": "Purchase Invoice",
            "field_map": {
                "party_account_currency": "party_account_currency",
                "supplier_warehouse": "supplier_warehouse",
            },
            "field_no_map": ["payment_terms_template"],
            "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 abs(doc.billed_amt) < abs(doc.amount)),
        },
        "Purchase Taxes and Charges": {
            "doctype": "Purchase Taxes and Charges",
            "add_if_empty": True
        },
    }

    doc = get_mapped_doc(
        "Purchase Order",
        source_name,
        fields,
        target_doc,
        postprocess,
        ignore_permissions=ignore_permissions,
    )
    doc.set_onload("ignore_price_list", True)

    return doc
Example #54
0
def make_inter_company_transaction(doctype, source_name, target_doc=None):
    from erpnext.accounts.doctype.sales_invoice.sales_invoice import (
        validate_inter_company_transaction, get_inter_company_details,
        update_address, update_taxes, set_purchase_references)

    if doctype == 'Delivery Note':
        source_doc = frappe.get_doc(doctype, source_name)
        target_doctype = "Purchase Receipt"
        source_document_warehouse_field = 'target_warehouse'
        target_document_warehouse_field = 'from_warehouse'
    else:
        source_doc = frappe.get_doc(doctype, source_name)
        target_doctype = 'Delivery Note'
        source_document_warehouse_field = 'from_warehouse'
        target_document_warehouse_field = 'target_warehouse'

    validate_inter_company_transaction(source_doc, doctype)
    details = get_inter_company_details(source_doc, doctype)

    def set_missing_values(source, target):
        target.run_method("set_missing_values")
        set_purchase_references(target)

        if target.doctype == 'Purchase Receipt':
            master_doctype = 'Purchase Taxes and Charges Template'
        else:
            master_doctype = 'Sales Taxes and Charges Template'

        if not target.get('taxes') and target.get('taxes_and_charges'):
            for tax in get_taxes_and_charges(master_doctype,
                                             target.get('taxes_and_charges')):
                target.append('taxes', tax)

    def update_details(source_doc, target_doc, source_parent):
        target_doc.inter_company_invoice_reference = source_doc.name
        if target_doc.doctype == 'Purchase Receipt':
            target_doc.company = details.get("company")
            target_doc.supplier = details.get("party")
            target_doc.buying_price_list = source_doc.selling_price_list
            target_doc.is_internal_supplier = 1
            target_doc.inter_company_reference = source_doc.name

            # Invert the address on target doc creation
            update_address(target_doc, 'supplier_address', 'address_display',
                           source_doc.company_address)
            update_address(target_doc, 'shipping_address',
                           'shipping_address_display',
                           source_doc.customer_address)

            update_taxes(target_doc,
                         party=target_doc.supplier,
                         party_type='Supplier',
                         company=target_doc.company,
                         doctype=target_doc.doctype,
                         party_address=target_doc.supplier_address,
                         company_address=target_doc.shipping_address)
        else:
            target_doc.company = details.get("company")
            target_doc.customer = details.get("party")
            target_doc.company_address = source_doc.supplier_address
            target_doc.selling_price_list = source_doc.buying_price_list
            target_doc.is_internal_customer = 1
            target_doc.inter_company_reference = source_doc.name

            # Invert the address on target doc creation
            update_address(target_doc, 'company_address',
                           'company_address_display',
                           source_doc.supplier_address)
            update_address(target_doc, 'shipping_address_name',
                           'shipping_address', source_doc.shipping_address)
            update_address(target_doc, 'customer_address', 'address_display',
                           source_doc.shipping_address)

            update_taxes(
                target_doc,
                party=target_doc.customer,
                party_type='Customer',
                company=target_doc.company,
                doctype=target_doc.doctype,
                party_address=target_doc.customer_address,
                company_address=target_doc.company_address,
                shipping_address_name=target_doc.shipping_address_name)

    doclist = get_mapped_doc(
        doctype, source_name, {
            doctype: {
                "doctype": target_doctype,
                "postprocess": update_details,
                "field_no_map": ["taxes_and_charges"]
            },
            doctype + " Item": {
                "doctype": target_doctype + " Item",
                "field_map": {
                    source_document_warehouse_field:
                    target_document_warehouse_field,
                    'name': 'delivery_note_item',
                    'batch_no': 'batch_no',
                    'serial_no': 'serial_no'
                },
                "field_no_map": ["warehouse"]
            }
        }, target_doc, set_missing_values)

    return doclist
Example #55
0
def make_sales_invoice(source_name, target_doc=None):
    invoiced_qty_map = get_invoiced_qty_map(source_name)

    def set_missing_values(source, target):
        target.is_pos = 0
        target.ignore_pricing_rule = 1
        target.run_method("set_missing_values")
        target.run_method("set_po_nos")

        if len(target.get("items")) == 0:
            frappe.throw(_("All these items have already been invoiced"))

        target.run_method("calculate_taxes_and_totals")

        # set company address
        target.update(get_company_address(target.company))
        if target.company_address:
            target.update(
                get_fetch_values("Sales Invoice", 'company_address',
                                 target.company_address))

    def update_item(source_doc, target_doc, source_parent):
        target_doc.qty = source_doc.qty - invoiced_qty_map.get(
            source_doc.name, 0)
        if source_doc.serial_no and source_parent.per_billed > 0:
            target_doc.serial_no = get_delivery_note_serial_no(
                source_doc.item_code, target_doc.qty, source_parent.name)

    doc = get_mapped_doc(
        "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",
                    "so_detail": "so_detail",
                    "against_sales_order": "sales_order",
                    "serial_no": "serial_no",
                    "cost_center": "cost_center"
                },
                "postprocess":
                update_item,
                "filter":
                lambda d: abs(d.qty) - abs(invoiced_qty_map.get(d.name, 0)) <=
                0
            },
            "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_doc, set_missing_values)

    return doc
Example #56
0
def make_sales_invoice(source_name, target_doc=None, ignore_permissions=False):
    def postprocess(source, target):
        set_missing_values(source, target)
        #Get the advance paid Journal Entries in Sales Invoice Advance
        if target.get("allocate_advances_automatically"):
            target.set_advances()

    def set_missing_values(source, target):
        target.is_pos = 0
        target.ignore_pricing_rule = 1
        target.flags.ignore_permissions = True
        target.run_method("set_missing_values")
        target.run_method("set_po_nos")
        target.run_method("calculate_taxes_and_totals")

        # set company address
        target.update(get_company_address(target.company))
        if target.company_address:
            target.update(
                get_fetch_values("Sales Invoice", 'company_address',
                                 target.company_address))

        # set the redeem loyalty points if provided via shopping cart
        if source.loyalty_points and source.order_type == "Shopping Cart":
            target.redeem_loyalty_points = 1

    def update_item(source, target, source_parent):
        target.amount = flt(source.amount) - flt(source.billed_amt)
        target.base_amount = target.amount * flt(source_parent.conversion_rate)
        target.qty = target.amount / flt(source.rate) if (
            source.rate
            and source.billed_amt) else source.qty - source.returned_qty

        if source_parent.project:
            target.cost_center = frappe.db.get_value("Project",
                                                     source_parent.project,
                                                     "cost_center")
        if not target.cost_center and target.item_code:
            item = get_item_defaults(target.item_code, source_parent.company)
            item_group = get_item_group_defaults(target.item_code,
                                                 source_parent.company)
            target.cost_center = item.get("selling_cost_center") \
             or item_group.get("selling_cost_center")

    doclist = get_mapped_doc(
        "Sales Order",
        source_name, {
            "Sales Order": {
                "doctype": "Sales Invoice",
                "field_map": {
                    "party_account_currency": "party_account_currency",
                    "payment_terms_template": "payment_terms_template"
                },
                "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.qty and
                (doc.base_amount == 0 or abs(doc.billed_amt) < abs(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_doc,
        postprocess,
        ignore_permissions=ignore_permissions)

    return doclist
Example #57
0
def make_deposit_invoice(source_name,
                         target_doc=None,
                         ignore_permissions=False):
    def postprocess(source, target):
        set_missing_values(source, target)
        #Get the advance paid Journal Entries in Sales Invoice Advance
        if target.get("allocate_advances_automatically"):
            target.set_advances()

    def set_missing_values(source, target):
        target.ignore_pricing_rule = 1
        target.flags.ignore_permissions = True
        target.run_method("set_missing_values")
        target.run_method("set_po_nos")
        target.run_method("calculate_taxes_and_totals")

        if source.company_address:
            target.update({'company_address': source.company_address})
        else:
            # set company address
            target.update(get_company_address(target.company))

        if target.company_address:
            target.update(
                get_fetch_values("Sales Invoice", 'company_address',
                                 target.company_address))

        # set print heading
        target.select_print_heading = frappe.db.get_single_value(
            'ERPNext France Settings', 'deposit_print_heading')

    def update_item(source, target, source_parent):

        if frappe.flags.args and frappe.flags.args.payment_term:
            for term in source_parent.payment_schedule:
                if frappe.flags.args.payment_term == term.payment_term:
                    invoice_portion = term.invoice_portion
                    target.qty = source.qty * (invoice_portion / 100)
                    target.description = source.description \
                     + "<br>Acompte de {0} % sur la commande n° {1}".format(invoice_portion, source_parent.name)

    doclist = get_mapped_doc(
        "Sales Order",
        source_name, {
            "Sales Order": {
                "doctype": "Sales Invoice",
                "field_map": {
                    "party_account_currency": "party_account_currency"
                },
                "validation": {
                    "docstatus": ["=", 1]
                }
            },
            "Sales Order Item": {
                "doctype": "Sales Invoice Item",
                "field_map": {
                    "name": "so_detail",
                    "parent": "sales_order",
                },
                "postprocess": update_item
            },
            "Sales Taxes and Charges": {
                "doctype": "Sales Taxes and Charges",
                "add_if_empty": True
            },
            "Sales Team": {
                "doctype": "Sales Team",
                "add_if_empty": True
            }
        },
        target_doc,
        postprocess,
        ignore_permissions=ignore_permissions)

    return doclist
Example #58
0
def make_task(source_name, target_doc=None):
    return get_mapped_doc("Issue", source_name, {"Issue": {
        "doctype": "Task"
    }}, target_doc)
Example #59
0
def make_sales_invoice(source_name, target_doc=None):
    from erpnext.utilities.doctype.address.address import get_address_display

    def postprocess(source, target):

        set_missing_values(source, target)
        target.get_advances()
        if source.doctype == 'Sales Order':
            set_missing_values(source, target)
            target.get_advances()
            #update_item(source,target,source_parent)

    def get_shelf_service_details(source, source_name, target):
        process = frappe.db.sql(
            """ select name from `tabProcess` where get_sales_order='%s'
				and docstatus=1 and sales_invoice_status='Not Done'""" % source_name,
            as_list=1)
        if process:
            for [name] in process:
                create_sales_invoice_item_entry(name, target)
        update_sales_order_process_status(source_name)

    def update_process_entry(name):
        frappe.db.sql(
            """update `tabProcess` set sales_invoice_status='Done' where
				name='%s'""" % name)
        frappe.db.commit()

    def update_sales_order_process_status(source_name):
        frappe.db.sql(
            """update `tabSales Order` set process_status='Completed' where
				name='%s'""" % source_name)
        frappe.db.commit()

    def create_sales_invoice_item_entry(name, target):
        service_details = frappe.db.sql(
            """select s.process,ifnull(s.qty,0),s.file_name from `tabShelf Ready Service Details` s
			inner join `tabProcess` p on s.parent=p.name where s.parent='%s' """ % name,
            as_list=1)
        if service_details:
            for i in service_details:
                si = target.append('entries', {})
                si.item_code = i[0]
                si.item_name = i[0]
                si.description = i[0]
                si.qty = i[1]
                if i[2] != None:
                    si.marcfile_name = i[2]
                else:
                    si.marcfile_name = ""
                si.sales_order = source_name
                si.process_id = name
                #update_process_entry(name)

    def set_missing_values(source, target):
        target.is_pos = 0
        target.ignore_pricing_rule = 1
        target.run_method("set_missing_values")

        # set billing address
        if target.customer_address:
            target.bill_to_address = get_address_display(
                target.customer_address)

        target.run_method("calculate_taxes_and_totals")
        if cint(source.is_recurring) == 1:
            target.is_recurring = source.is_recurring
            target.recurring_type = source.recurring_type
            target.from_date = source.from_date
            target.to_date = source.to_date
            target.repeat_on_day_of_month = source.repeat_on_day_of_month
            target.end_date = source.end_date
            target.notification_email_address = source.notification_email_address

    def update_item(source, target, source_parent):
        target.amount = flt(source.amount) - flt(source.billed_amt)
        target.base_amount = target.amount * flt(source_parent.conversion_rate)
        target.qty = target.amount / flt(source.rate) if (
            source.rate and source.billed_amt) else source.qty
        target.so_detail = source.name
        target.artist = source.artist or frappe.db.get_value(
            'Item', {'name': source.item_code}, 'artist')

    doclist = get_mapped_doc(
        "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) and doc.stop_status != "Yes"
            },
            "Sales Taxes and Charges": {
                "doctype": "Sales Taxes and Charges",
                "add_if_empty": True
            },
            "Sales Team": {
                "doctype": "Sales Team",
                "add_if_empty": True
            }
        }, target_doc, postprocess)

    def set_advance_vouchers(source, target):
        advance_voucher_list = []

        advance_voucher = frappe.db.sql("""
			select
				t1.name as voucher_no, t1.posting_date, t1.remark, t2.account,
				t2.name as voucher_detail_no, {amount_query} as payment_amount, t2.is_advance
			from
				`tabJournal Voucher` t1, `tabJournal Voucher Detail` t2
			""")

    return doclist
def make_purchase_receipt(source_name, target_doc=None):
	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.rate) * flt(source_parent.conversion_rate)

	doc = get_mapped_doc("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 and doc.delivered_by_supplier!=1
		},
		"Purchase Taxes and Charges": {
			"doctype": "Purchase Taxes and Charges",
			"add_if_empty": True
		}
	}, target_doc, set_missing_values)

	return doc