Esempio n. 1
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
Esempio n. 2
0
def make_journal_entry1(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

    target_doc = get_mapped_doc("Cheque Out", source_name, {
    	"Cheque Out": {
    		"doctype": "Journal Entry",
            "field_map": {
                "company": "company",
                "nominal_cheque": "total_debit",
                "party_type": "party_type",
                "party": "party"
            },
            "field_no_map":["cheque_no", "cheque_date", "cheque_due_date", "cheque_bank"],
    	}
    }, target_doc)

    source_doc = frappe.get_doc("Cheque Out", source_name)
    table_map = {
        "doctype": "Journal Entry Account",
        "field_map": {
            "account_paid_from": "account",
            "nominal_cheque" : "debit_in_account_currency",
        },
        "add_if_empty": True,
        "postprocess": _update_links
    }
    map_child_doc(source_doc,target_doc,table_map,source_doc)

    return target_doc
Esempio n. 3
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
Esempio n. 4
0
def make_delivery_note(source_name, target_doc=None):
    def _set_child_fields(source_doc, target_doc, source_parent):
        target_doc.update({
            "qty":
            1,
            "description":
            frappe.db.get_value("Item", source_doc.item_code, "description"),
            "uom":
            frappe.db.get_value("Item", source_doc.item_code, "stock_uom"),
            "serial_no":
            source_doc.serial_no or source_doc.unlinked_serial_no,
            "warehouse":
            frappe.db.get_value("Work Order",
                                {"warranty_claim": source_doc.name},
                                "fg_warehouse")
        })

    target_doc = make_mapped_doc("Delivery Note", source_name, target_doc)
    source_doc = frappe.get_doc("Warranty Claim", source_name)

    # Include the cable and case in the stock receipt, if entered
    if source_doc.cable:
        target_doc.append("items", {"item_code": source_doc.cable, "qty": 1})

    if source_doc.case:
        target_doc.append("items", {"item_code": source_doc.case, "qty": 1})

    if source_doc.get("item_code"):
        table_map = {
            "doctype": "Delivery Note Item",
            "postprocess": _set_child_fields
        }
        map_child_doc(source_doc, target_doc, table_map, source_doc)

    return target_doc
Esempio n. 5
0
def create_delivery_note(source_name, target_doc=None):
	pick_list = frappe.get_doc('Pick List', source_name)
	validate_item_locations(pick_list)

	sales_orders = [d.sales_order for d in pick_list.locations if d.sales_order]
	sales_orders = set(sales_orders)

	delivery_note = None
	for sales_order in sales_orders:
		delivery_note = create_delivery_note_from_sales_order(sales_order,
			delivery_note, skip_item_mapping=True)

	# map rows without sales orders as well
	if not delivery_note:
		delivery_note = frappe.new_doc("Delivery Note")

	item_table_mapper = {
		'doctype': 'Delivery Note Item',
		'field_map': {
			'rate': 'rate',
			'name': 'so_detail',
			'parent': 'against_sales_order',
		},
		'condition': lambda doc: abs(doc.delivered_qty) < abs(doc.qty) and doc.delivered_by_supplier!=1
	}

	item_table_mapper_without_so = {
		'doctype': 'Delivery Note Item',
		'field_map': {
			'rate': 'rate',
			'name': 'name',
			'parent': '',
		}
	}

	for location in pick_list.locations:
		if location.sales_order_item:
			sales_order_item = frappe.get_cached_doc('Sales Order Item', {'name':location.sales_order_item})
		else:
			sales_order_item = None

		source_doc, table_mapper = [sales_order_item, item_table_mapper] if sales_order_item \
			else [location, item_table_mapper_without_so]

		dn_item = map_child_doc(source_doc, delivery_note, table_mapper)

		if dn_item:
			dn_item.warehouse = location.warehouse
			dn_item.qty = location.picked_qty
			dn_item.batch_no = location.batch_no
			dn_item.serial_no = location.serial_no

			update_delivery_note_item(source_doc, dn_item, delivery_note)

	set_delivery_note_missing_values(delivery_note)

	delivery_note.pick_list = pick_list.name
	delivery_note.customer = pick_list.customer if pick_list.customer else None

	return delivery_note
Esempio n. 6
0
def map_pl_locations(pick_list, item_mapper, delivery_note, sales_order=None):

    for location in pick_list.locations:
        if location.sales_order != sales_order or location.product_bundle_item:
            continue

        if location.sales_order_item:
            sales_order_item = frappe.get_doc("Sales Order Item",
                                              location.sales_order_item)
        else:
            sales_order_item = None

        source_doc = sales_order_item or location

        dn_item = map_child_doc(source_doc, delivery_note, item_mapper)

        if dn_item:
            dn_item.pick_list_item = location.name
            dn_item.warehouse = location.warehouse
            dn_item.qty = flt(
                location.picked_qty) / (flt(location.conversion_factor) or 1)
            dn_item.batch_no = location.batch_no
            dn_item.serial_no = location.serial_no

            update_delivery_note_item(source_doc, dn_item, delivery_note)

    add_product_bundles_to_delivery_note(pick_list, delivery_note, item_mapper)
    set_delivery_note_missing_values(delivery_note)

    delivery_note.pick_list = pick_list.name
    delivery_note.company = pick_list.company
    delivery_note.customer = frappe.get_value("Sales Order", sales_order,
                                              "customer")
Esempio n. 7
0
def make_delivery_note(source_name, target_doc=None):
    def _set_child_fields(source_doc, target_doc, source_parent):
        target_doc.update({
            "qty":
            1,
            "uom":
            frappe.db.get_value("Item", source_doc.item_code, "stock_uom"),
            "serial_no":
            source_doc.serial_no or source_doc.unlinked_serial_no,
            "warehouse":
            frappe.db.get_single_value("Repair Settings",
                                       "default_incoming_warehouse"),
            "item_ignore_pricing_rule":
            1,
            "allow_zero_valuation_rate":
            1,
            "rate":
            0,
            "additional_discount_percentage":
            100
        })

    target_doc = make_mapped_doc("Delivery Note", source_name, target_doc)
    source_doc = frappe.get_doc("Warranty Claim", source_name)

    # Include the cable and case in the stock receipt, if entered
    if source_doc.cable:
        target_doc.append("items", {"item_code": source_doc.cable, "qty": 1})

    if source_doc.case:
        target_doc.append("items", {"item_code": source_doc.case, "qty": 1})

    if source_doc.get("item_code"):
        table_map = {
            "doctype": "Delivery Note Item",
            "postprocess": _set_child_fields
        }
        map_child_doc(source_doc, target_doc, table_map, source_doc)

    return target_doc
def make_warranty_management_request(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
        target_doc.issue = source_parent.complaint
        target_doc.stock_uom = frappe.db.sql("""select stock_uom
            from `tabItem`
            where item_code=%s""", target_doc.item_code, as_dict=1)[0].stock_uom or ""
        target_doc.image = frappe.db.sql("""select image
            from `tabItem`
            where item_code=%s""", target_doc.item_code, as_dict=1)[0].image or ""

    visit = frappe.db.sql("""select t1.name
        from `tabWarranty Request` t1, `tabWarranty Request Purposes` t2
        where t2.parent=t1.name and t2.prevdoc_docname=%s
        and t1.docstatus=1""", source_name)

    if not visit:
        target_doc = get_mapped_doc("Warranty Claim", source_name, {
            "Warranty Claim": {
                "doctype": "Warranty Request",
                "field_map": {}
            }
        }, target_doc)

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

        return target_doc
Esempio n. 9
0
def add_product_bundles_to_delivery_note(pick_list: "PickList", delivery_note,
                                         item_mapper) -> None:
    """Add product bundles found in pick list to delivery note.

	When mapping pick list items, the bundle item itself isn't part of the
	locations. Dynamically fetch and add parent bundle item into DN."""
    product_bundles = pick_list._get_product_bundles()
    product_bundle_qty_map = pick_list._get_product_bundle_qty_map(
        product_bundles.values())

    for so_row, item_code in product_bundles.items():
        sales_order_item = frappe.get_doc("Sales Order Item", so_row)
        dn_bundle_item = map_child_doc(sales_order_item, delivery_note,
                                       item_mapper)
        dn_bundle_item.qty = pick_list._compute_picked_qty_for_bundle(
            so_row, product_bundle_qty_map[item_code])
        update_delivery_note_item(sales_order_item, dn_bundle_item,
                                  delivery_note)
Esempio n. 10
0
def create_delivery_note(source_name, target_doc=None):
    pick_list = frappe.get_doc('Pick List', source_name)
    sales_orders = [d.sales_order for d in pick_list.locations]
    sales_orders = set(sales_orders)

    delivery_note = None
    for sales_order in sales_orders:
        delivery_note = create_delivery_note_from_sales_order(
            sales_order, delivery_note, skip_item_mapping=True)

    item_table_mapper = {
        'doctype':
        'Delivery Note Item',
        'field_map': {
            'rate': 'rate',
            'name': 'so_detail',
            'parent': 'against_sales_order',
        },
        'condition':
        lambda doc: abs(doc.delivered_qty) < abs(doc.qty) and doc.
        delivered_by_supplier != 1
    }

    for location in pick_list.locations:
        sales_order_item = frappe.get_cached_doc('Sales Order Item',
                                                 location.sales_order_item)
        dn_item = map_child_doc(sales_order_item, delivery_note,
                                item_table_mapper)

        if dn_item:
            dn_item.warehouse = location.warehouse
            dn_item.qty = location.picked_qty
            dn_item.batch_no = location.batch_no
            dn_item.serial_no = location.serial_no

            update_delivery_note_item(sales_order_item, dn_item, delivery_note)

    set_delivery_note_missing_values(delivery_note)

    delivery_note.pick_list = pick_list.name

    return delivery_note
    def merge_pos_invoice_into(self, invoice, data):
        items, payments, taxes = [], [], []
        loyalty_amount_sum, loyalty_points_sum = 0, 0
        for doc in data:
            doc.customer = invoice.customer
            map_doc(doc, invoice, table_map={"doctype": invoice.doctype})

            if doc.redeem_loyalty_points:
                invoice.loyalty_redemption_account = doc.loyalty_redemption_account
                invoice.loyalty_redemption_cost_center = doc.loyalty_redemption_cost_center
                loyalty_points_sum += doc.loyalty_points
                loyalty_amount_sum += doc.loyalty_amount

            for item in doc.get('items'):
                found = False
                for i in items:
                    if (i.item_code == item.item_code and not i.serial_no and not i.batch_no and
                            i.uom == item.uom and i.net_rate == item.net_rate):
                        found = True
                        i.qty = i.qty + item.qty

                if not found:
                    item.rate = item.net_rate
                    item.price_list_rate = 0
                    si_item = map_child_doc(item, invoice, {"doctype": "Sales Invoice Item"})
                    items.append(si_item)

            for tax in doc.get('taxes'):
                found = False
                for t in taxes:
                    if t.account_head == tax.account_head and t.cost_center == tax.cost_center:
                        t.tax_amount = flt(t.tax_amount) + flt(tax.tax_amount_after_discount_amount)
                        t.base_tax_amount = flt(t.base_tax_amount) + flt(tax.base_tax_amount_after_discount_amount)
                        update_item_wise_tax_detail(t, tax)
                        found = True
                if not found:
                    tax.charge_type = 'Actual'
                    tax.included_in_print_rate = 0
                    tax.tax_amount = tax.tax_amount_after_discount_amount
                    tax.base_tax_amount = tax.base_tax_amount_after_discount_amount
                    tax.item_wise_tax_detail = tax.item_wise_tax_detail
                    taxes.append(tax)

            for payment in doc.get('payments'):
                found = False
                for pay in payments:
                    if pay.account == payment.account and pay.mode_of_payment == payment.mode_of_payment:
                        pay.amount = flt(pay.amount) + flt(payment.amount)
                        pay.base_amount = flt(pay.base_amount) + flt(payment.base_amount)
                        found = True
                if not found:
                    payments.append(payment)

        if loyalty_points_sum:
            invoice.redeem_loyalty_points = 1
            invoice.loyalty_points = loyalty_points_sum
            invoice.loyalty_amount = loyalty_amount_sum

        invoice.set('items', items)
        invoice.set('payments', payments)
        invoice.set('taxes', taxes)
        invoice.additional_discount_percentage = 0
        invoice.discount_amount = 0.0
        invoice.taxes_and_charges = None
        invoice.ignore_pricing_rule = 1

        return invoice
Esempio n. 12
0
def on_submit(self, method):
	pick_list = frappe.get_doc('Pick List', self.name)
	validate_item_locations(pick_list)

	sales_orders = [d.sales_order for d in pick_list.locations if d.sales_order]
	sales_orders = set(sales_orders)

	delivery_note = None
	for sales_order in sales_orders:
		delivery_note = create_delivery_note_from_sales_order(sales_order,
			delivery_note, skip_item_mapping=True)
		delivery_note.update({
			'ignore_pricing_rule': frappe.db.get_value('Sales Order', sales_order, 'ignore_pricing_rule'),
			"disable_rounded_total": 1
		})
		#Add pick list submitted date in sales order
		sales_doc = frappe.get_doc("Sales Order", sales_order)
		sales_doc.update({"pick_list_submitted_date": datetime.datetime.now(timezone('US/Pacific')).strftime("%Y-%m-%d %H:%M:%S")})
		sales_doc.save()

	# map rows without sales orders as well
	if not delivery_note:
		delivery_note = frappe.new_doc("Delivery Note")

	item_table_mapper = {
		'doctype': 'Delivery Note Item',
		'field_map': {
			'rate': 'rate',
			'name': 'so_detail',
			'parent': 'against_sales_order',
		},
		'condition': lambda doc: abs(doc.delivered_qty) < abs(doc.qty) and doc.delivered_by_supplier!=1
	}

	item_table_mapper_without_so = {
		'doctype': 'Delivery Note Item',
		'field_map': {
			'rate': 'rate',
			'name': 'name',
			'parent': '',
		}
	}

	for location in pick_list.locations:
		if location.sales_order_item:
			sales_order_item = frappe.get_cached_doc('Sales Order Item', {'name':location.sales_order_item})
		else:
			sales_order_item = None

		source_doc, table_mapper = [sales_order_item, item_table_mapper] if sales_order_item \
			else [location, item_table_mapper_without_so]

		dn_item = map_child_doc(source_doc, delivery_note, table_mapper)

		if dn_item:
			dn_item.warehouse = location.warehouse
			dn_item.qty = location.picked_qty
			dn_item.batch_no = location.batch_no
			dn_item.serial_no = location.serial_no

			update_delivery_note_item(source_doc, dn_item, delivery_note)

	set_delivery_note_missing_values(delivery_note)

	delivery_note.pick_list = pick_list.name
	delivery_note.customer = pick_list.customer if pick_list.customer else None
	if pick_list.ais_source:
		delivery_note.source = pick_list.ais_source
	delivery_note.save()
Esempio n. 13
0
    def merge_pos_invoice_into(self, invoice, data):
        items, payments, taxes = [], [], []

        loyalty_amount_sum, loyalty_points_sum = 0, 0

        rounding_adjustment, base_rounding_adjustment = 0, 0
        rounded_total, base_rounded_total = 0, 0

        loyalty_amount_sum, loyalty_points_sum, idx = 0, 0, 1

        for doc in data:
            map_doc(doc, invoice, table_map={"doctype": invoice.doctype})

            if doc.redeem_loyalty_points:
                invoice.loyalty_redemption_account = doc.loyalty_redemption_account
                invoice.loyalty_redemption_cost_center = doc.loyalty_redemption_cost_center
                loyalty_points_sum += doc.loyalty_points
                loyalty_amount_sum += doc.loyalty_amount

            for item in doc.get("items"):
                found = False
                for i in items:
                    if (i.item_code == item.item_code and not i.serial_no
                            and not i.batch_no and i.uom == item.uom
                            and i.net_rate == item.net_rate
                            and i.warehouse == item.warehouse):
                        found = True
                        i.qty = i.qty + item.qty
                        i.amount = i.amount + item.net_amount
                        i.net_amount = i.amount
                        i.base_amount = i.base_amount + item.base_net_amount
                        i.base_net_amount = i.base_amount

                if not found:
                    item.rate = item.net_rate
                    item.amount = item.net_amount
                    item.base_amount = item.base_net_amount
                    item.price_list_rate = 0
                    si_item = map_child_doc(item, invoice,
                                            {"doctype": "Sales Invoice Item"})
                    items.append(si_item)

            for tax in doc.get("taxes"):
                found = False
                for t in taxes:
                    if t.account_head == tax.account_head and t.cost_center == tax.cost_center:
                        t.tax_amount = flt(t.tax_amount) + flt(
                            tax.tax_amount_after_discount_amount)
                        t.base_tax_amount = flt(t.base_tax_amount) + flt(
                            tax.base_tax_amount_after_discount_amount)
                        update_item_wise_tax_detail(t, tax)
                        found = True
                if not found:
                    tax.charge_type = "Actual"
                    tax.idx = idx
                    idx += 1
                    tax.included_in_print_rate = 0
                    tax.tax_amount = tax.tax_amount_after_discount_amount
                    tax.base_tax_amount = tax.base_tax_amount_after_discount_amount
                    tax.item_wise_tax_detail = tax.item_wise_tax_detail
                    taxes.append(tax)

            for payment in doc.get("payments"):
                found = False
                for pay in payments:
                    if pay.account == payment.account and pay.mode_of_payment == payment.mode_of_payment:
                        pay.amount = flt(pay.amount) + flt(payment.amount)
                        pay.base_amount = flt(pay.base_amount) + flt(
                            payment.base_amount)
                        found = True
                if not found:
                    payments.append(payment)

            rounding_adjustment += doc.rounding_adjustment
            rounded_total += doc.rounded_total
            base_rounding_adjustment += doc.base_rounding_adjustment
            base_rounded_total += doc.base_rounded_total

        if loyalty_points_sum:
            invoice.redeem_loyalty_points = 1
            invoice.loyalty_points = loyalty_points_sum
            invoice.loyalty_amount = loyalty_amount_sum

        invoice.set("items", items)
        invoice.set("payments", payments)
        invoice.set("taxes", taxes)
        invoice.set("rounding_adjustment", rounding_adjustment)
        invoice.set("base_rounding_adjustment", base_rounding_adjustment)
        invoice.set("rounded_total", rounded_total)
        invoice.set("base_rounded_total", base_rounded_total)
        invoice.additional_discount_percentage = 0
        invoice.discount_amount = 0.0
        invoice.taxes_and_charges = None
        invoice.ignore_pricing_rule = 1
        invoice.customer = self.customer

        if self.merge_invoices_based_on == "Customer Group":
            invoice.flags.ignore_pos_profile = True
            invoice.pos_profile = ""

        return invoice