def set_or_create_batch(doc, method): def set_existing_batch(item): if item.os_expiry_date and not item.batch_no: has_batch_no, has_expiry_date = frappe.db.get_value( "Item", item.item_code, ["has_batch_no", "has_expiry_date"]) if has_batch_no and has_expiry_date: batch_no = frappe.db.exists( "Batch", { "item": item.item_code, "expiry_date": item.os_expiry_date }, ) item.batch_no = batch_no get_batch_in_previous_items = compose( lambda x: x.get("batch_no"), excepts(StopIteration, first, lambda _: {}), lambda x: filter( lambda item: item.idx < x.idx and item.item_code == x.item_code and item.pb_expiry_date == x.pb_expiry_date, doc.items, ), ) def create_new_batch(item): warehouse = "t_warehouse" if doc.doctype == "Stock Entry" else "warehouse" if item.get(warehouse) and item.os_expiry_date and not item.batch_no: has_batch_no, create_new_batch, has_expiry_date = frappe.db.get_value( "Item", item.item_code, ["has_batch_no", "create_new_batch", "has_expiry_date"], ) if has_batch_no and create_new_batch and has_expiry_date: batch_in_items = get_batch_in_previous_items(item) if batch_in_items: item.batch_no = batch_in_items return batch = frappe.get_doc({ "doctype": "Batch", "item": item.item_code, "expiry_date": item.os_expiry_date, "supplier": doc.supplier, # "reference_doctype": doc.doctype, # "reference_name": doc.name, }).insert() item.batch_no = batch.name if doc._action == "save": map_resolved(set_existing_batch, doc.items) # TODO: when `before_validate` gets merged into master create_new_batch should # run when doc._action == 'submit'. # also update `hooks.py` to use `before_validate` instead of the current # `before_save` method map_resolved(create_new_batch, doc.items)
def _get_branch_collections(payments, end_date): get_sum_today = compose( sum_by("amount"), lambda x: filter( lambda row: row.branch == x and row.posting_date == end_date, payments ), partial(get, "branch"), ) get_sum_mtd = compose( sum_by("amount"), lambda x: filter(lambda row: row.branch == x, payments), partial(get, "branch"), ) get_percent = excepts(ZeroDivisionError, lambda x, y: x / y * 100, lambda __: 0) def set_amounts(x): monthly_target = get("monthly_target", x, 0) collected_mtd = get_sum_mtd(x) return { "collected_today": get_sum_today(x), "half_monthly_target": monthly_target / 2, "half_monthly_target_percent": get_percent( collected_mtd, monthly_target / 2 ), "collected_mtd": collected_mtd, "monthly_target_remaining": monthly_target - collected_mtd, "monthly_target_percent": get_percent(collected_mtd, monthly_target), } return map_resolved( lambda x: merge(x, set_amounts(x)), frappe.get_all( "Branch", fields=["name AS branch", "os_target AS monthly_target"], filters={"disabled": 0}, ), )