def on_destroy_schedule(plants, reason_type=None, reason=None, override=None, undo=False): barcodeid = [] if not isinstance(plants, list): plants = [plants] for plant in plants: if is_bio_plant(plant): barcodeid.append(plant.get("bio_barcode")) if len(barcodeid): if not undo and not reason_type: frappe.throw("Reason type is required") try: if undo: call("plant_destroy_schedule_undo", { "barcodeid": barcodeid, }) else: call( "plant_destroy_schedule", { "barcodeid": barcodeid, "reason_extended": reason_type, "reason": reason, "override": override or 0 }) except BioTrackClientError as e: # ignore error pass
def on_harvest_schedule(plants, undo=False): barcodeid = [] if not isinstance(plants, list): plants = [plants] for plant in plants: if is_bio_plant(plant): barcodeid.append(plant.get("bio_barcode")) if len(barcodeid): # figure out err: # Barcode 9160883599199700 is no longer in a state where it can be harvested if undo: try: call("plant_harvest_schedule_undo", { "barcodeid": barcodeid, }) except BioTrackClientError as e: # ignore error pass else: try: call("plant_harvest_schedule", { "barcodeid": barcodeid, }) except BioTrackClientError as e: frappe.throw(cstr(e.message), title="BioTrackTHC sync up failed")
def on_destroy_schedule(plants, reason_type=None, reason=None, override=None, undo=False): barcodeid = [] if not isinstance(plants, list): plants = [plants] for plant in plants: if is_bio_plant(plant): barcodeid.append(plant.get("bio_barcode")) if len(barcodeid): if not undo and not reason_type: frappe.throw("Reason type is required") try: if undo: call("plant_destroy_schedule_undo", { "barcodeid": barcodeid, }) else: call("plant_destroy_schedule", { "barcodeid": barcodeid, "reason_extended": reason_type, "reason": reason, "override": override or 0 }) except BioTrackClientError as e: # ignore error pass
def on_trash(doc): if not is_bio_plant_room(doc): return try: call("plant_room_remove", { "id": doc.bio_id, }) except BioTrackClientError as e: frappe.throw(cstr(e.message), title="BioTrackTHC sync up failed")
def on_trash(plant): if not is_bio_plant(plant): return try: call("plant_destroy", { "barcodeid": [plant.bio_barcode], }) except BioTrackClientError as e: frappe.throw(cstr(e.message), title="BioTrackTHC sync up failed")
def before_cancel(plant): """Call plant_new_undo api""" if not is_bio_plant(plant) or plant.state != "Growing": return # Barcode 9160883599199700 is no longer in a state where it can be un-done. try: call("plant_new_undo", { "barcodeid": [plant.bio_barcode], }) except BioTrackClientError as e: frappe.throw(cstr(e.message), title="BioTrackTHC sync up failed")
def before_cancel(plant_entry): if plant_entry.purpose == "Convert" or not plant_entry.bio_transaction: return if plant_entry.purpose == "Harvest": action = "plant_harvest_undo" else: action = "plant_cure_undo" try: call(action, {"transactionid": plant_entry.bio_transaction}) except BioTrackClientError as e: frappe.throw(cstr(e.message), title="BioTrackTHC sync up failed") plant_entry.bio_transaction = ""
def _inventory_new(item, qty): item_group = frappe.get_doc("Item Group", item.item_group) if not item_group.external_id: frappe.throw("Invalid inventory type") if not item.strain: frappe.throw("strain is missing for item {0}".format(item.item_code)) call_data = { "invtype": item_group.external_id, "quantity": qty, "strain": item.strain, } if item.plant: call_data["source_id"] = item.plant res = call("inventory_new", data={ "data": call_data, "location": get_location() }) item.update({ "bio_barcode": res.get("barcode_id")[0], "transaction_id": res.get("transactionid"), "bio_remaining_quantity": qty }) item.save()
def before_submit(plant_entry): """BioTrack sync up: inventory_new or inventory_adjust""" barcodeid = [] for ple_detail in plant_entry.get("plants"): plant = frappe.get_doc("Plant", ple_detail.plant_code) if is_bio_plant(plant): barcodeid.append(plant.get("bio_barcode")) if len(barcodeid) == 0: return if plant_entry.purpose == "Convert": convert_on_submit(plant_entry, barcodeid) return action = "plant_harvest" if plant_entry.purpose == "Harvest" else "plant_cure" res = None try: res = call(action, { "barcodeid": barcodeid, "location": get_location(), "weights": make_weights_data(plant_entry.flower, plant_entry.other_material, plant_entry.waste), "collectadditional": cint(plant_entry.additional_collections), }) except BioTrackClientError as e: frappe.throw(cstr(e.message), title="BioTrackTHC sync up failed") if res: plant_entry.bio_transaction = res.get("transactionid") items = plant_entry.items or {} map_item_derivatives(items, res.get("derivatives", []))
def before_cancel(plant_entry): if plant_entry.purpose == "Convert" or not plant_entry.bio_transaction: return if plant_entry.purpose == "Harvest": action = "plant_harvest_undo" else: action = "plant_cure_undo" try: call(action, { "transactionid": plant_entry.bio_transaction }) except BioTrackClientError as e: frappe.throw(cstr(e.message), title="BioTrackTHC sync up failed") plant_entry.bio_transaction = ""
def on_plant_move(plants, plant_room): if not plant_room.external_id: return barcodeid = [] for plant in plants: if is_bio_plant(plant): barcodeid.append(plant.get("bio_barcode")) if len(barcodeid): try: call("plant_move", { "room": plant_room.external_id, "barcodeid": barcodeid, }) except BioTrackClientError as e: frappe.throw(cstr(e.message), title="BioTrackTHC sync up failed")
def _create_lot(stock_entry, data): try: res = call("inventory_create_lot", {"data": data}) frappe.db.set_value("Item", stock_entry.lot_item, { "bio_barcode": res.get("barcode_id"), "disabled": 0 }, update_modified=False) except BioTrackClientError as e: frappe.throw(cstr(e.message), title="BioTrack sync-up failed")
def _inventory_adjust(item, additional_quantity=None, remove_quantity=None): data = { "barcodeid": item.bio_barcode, "reason": "Client Adjustment", "type": 1, "remove_quantity_uom": "g", } if additional_quantity: item.bio_remaining_quantity = item.bio_remaining_quantity + additional_quantity elif remove_quantity: item.bio_remaining_quantity = item.bio_remaining_quantity - remove_quantity data["quantity"] = item.bio_remaining_quantity call("inventory_adjust", data={ "data": data, }) item.save()
def _create_product(stock_entry, data): derivative_type = frappe.get_value("Item Group", stock_entry.product_group, "external_id") request_data = {} if not derivative_type: frappe.throw("Invalid Inventory type on '{0}'".format( stock_entry.product_group)) request_data["data"] = data request_data["derivative_type"] = cint(derivative_type) request_data["derivative_quantity"] = flt(stock_entry.product_qty) request_data["derivative_quantity_uom"] = "g" request_data["waste"] = flt(stock_entry.product_waste) request_data["waste_uom"] = "g" product_usable = flt(stock_entry.product_usable) if product_usable: request_data["derivative_usable"] = product_usable if stock_entry.product_name: request_data["derivative_product"] = stock_entry.product_name response = {} try: response = call("inventory_convert", request_data) except BioTrackClientError as ex: frappe.throw(cstr(ex.message), title="BioTrack sync-up failed") derivatives = response.get("derivatives", []) for derivative in derivatives: item_type = derivative.get("barcode_type") barcode = derivative.get("barcode_id") if item_type == 27 and stock_entry.waste_item: frappe.db.set_value("Item", stock_entry.waste_item, { "bio_barcode": barcode, "disabled": 0 }, update_modified=False) elif item_type == derivative_type and stock_entry.product_item: frappe.db.set_value("Item", stock_entry.product_item, { "bio_barcode": barcode, "disabled": 0 }, update_modified=False)
def after_insert(doc): bio_id = generate_id(doc) try: res = call("plant_room_add", { "id": bio_id, "name": doc.plant_room_name, "location": get_location(), }) except BioTrackClientError as e: frappe.throw(cstr(e.message), title="BioTrackTHC sync up failed") else: frappe.db.set_value(doc.doctype, doc.name, { "bio_id": bio_id, "bio_name": doc.plant_room_name, "bio_transactionid": res.get("transactionid") }, None, update_modified=False)
def on_update(doc): if is_bio_plant_room(doc) and doc.plant_room_name != doc.bio_name: frappe.log(doc.plant_room_name) frappe.log(doc.bio_name) try: res = call("plant_room_modify", { "id": doc.bio_id, "name": doc.plant_room_name, "location": get_location(), }) except BioTrackClientError as e: frappe.throw(cstr(e.message), title="BioTrackTHC sync up failed") else: frappe.db.set_value(doc.doctype, doc.name, { "bio_name": doc.plant_room_name, "bio_transactionid": res.get("transactionid") }, None, update_modified=False)
def before_submit(plant): # only root plant get handled if plant.brother_plant: return plants = plant.flags.bulk_plants or [] plants.append(plant) if len(plants) != plant.get("qty"): frappe.throw("Bulk adding qty mismatch") plant_room = frappe.get_doc("Plant Room", plant.get("plant_room")) if not is_bio_plant_room(plant_room): return source = None if plant.get("item_code"): source = frappe.get_value("Item", plant.item_code, "bio_barcode") elif plant.get("source_plant"): source = frappe.get_value("Plant", plant.source_plant, "bio_barcode") if not source: return try: result = call( "plant_new", { "room": plant_room.bio_id, "quantity": plant.get("qty"), "strain": plant.get("strain"), "source": source, "mother": cint(plant.get("is_mother")), "location": get_location() }) except BioTrackClientError as e: plant.revert_on_failure() frappe.throw(cstr(e.message), title="BioTrackTHC sync up failed") else: for idx, barcode in enumerate(result.get("barcode_id")): doc = plants[idx] frappe.db.set_value("Plant", doc.name, "bio_barcode", barcode, update_modified=False)
def convert_on_submit(plant_entry, barcodeid): res = None try: res = call("plant_convert_to_inventory", {"barcodeid": barcodeid}) except BioTrackClientError as e: frappe.throw(cstr(e.message), title="BioTrackTHC sync up failed") if res: plant_entry.bio_transaction = res.get("transactionid") items = plant_entry.items or {} for name in items: item = items[name] if not item.barcode: plant = frappe.get_doc("Plant", name) if is_bio_plant(plant): item.barcode = plant.get("bio_barcode") item.bio_barcode = plant.get("bio_barcode") item.save()
def convert_on_submit(plant_entry, barcodeid): res = None try: res = call("plant_convert_to_inventory", { "barcodeid": barcodeid }) except BioTrackClientError as e: frappe.throw(cstr(e.message), title="BioTrackTHC sync up failed") if res: plant_entry.bio_transaction = res.get("transactionid") items = plant_entry.items or {} for name in items: item = items[name] if not item.barcode: plant = frappe.get_doc("Plant", name) if is_bio_plant(plant): item.barcode = plant.get("bio_barcode") item.bio_barcode = plant.get("bio_barcode") item.save()
def before_submit(plant): # only root plant get handled if plant.brother_plant: return plants = plant.flags.bulk_plants or [] plants.append(plant) if len(plants) != plant.get("qty"): frappe.throw("Bulk adding qty mismatch") plant_room = frappe.get_doc("Plant Room", plant.get("plant_room")) if not is_bio_plant_room(plant_room): return source = None if plant.get("item_code"): source = frappe.get_value("Item", plant.item_code, "bio_barcode") elif plant.get("source_plant"): source = frappe.get_value("Plant", plant.source_plant, "bio_barcode") if not source: return try: result = call("plant_new", { "room": plant_room.bio_id, "quantity": plant.get("qty"), "strain": plant.get("strain"), "source": source, "mother": cint(plant.get("is_mother")), "location": get_location() }) except BioTrackClientError as e: plant.revert_on_failure() frappe.throw(cstr(e.message), title="BioTrackTHC sync up failed") else: for idx, barcode in enumerate(result.get("barcode_id")): doc = plants[idx] frappe.db.set_value("Plant", doc.name, "bio_barcode", barcode, update_modified=False)
def before_submit(plant_entry): """BioTrack sync up: inventory_new or inventory_adjust""" barcodeid = [] for ple_detail in plant_entry.get("plants"): plant = frappe.get_doc("Plant", ple_detail.plant_code) if is_bio_plant(plant): barcodeid.append(plant.get("bio_barcode")) if len(barcodeid) == 0: return if plant_entry.purpose == "Convert": convert_on_submit(plant_entry, barcodeid) return action = "plant_harvest" if plant_entry.purpose == "Harvest" else "plant_cure" res = None try: res = call( action, { "barcodeid": barcodeid, "location": get_location(), "weights": make_weights_data(plant_entry.flower, plant_entry.other_material, plant_entry.waste), "collectadditional": cint(plant_entry.additional_collections), }) except BioTrackClientError as e: frappe.throw(cstr(e.message), title="BioTrackTHC sync up failed") if res: plant_entry.bio_transaction = res.get("transactionid") items = plant_entry.items or {} map_item_derivatives(items, res.get("derivatives", []))
def _create_product(stock_entry, data): derivative_type = frappe.get_value("Item Group", stock_entry.product_group, "external_id") request_data = {} if not derivative_type: frappe.throw("Invalid Inventory type on '{0}'".format(stock_entry.product_group)) request_data["data"] = data request_data["derivative_type"] = cint(derivative_type) request_data["derivative_quantity"] = flt(stock_entry.product_qty) request_data["derivative_quantity_uom"] = "g" request_data["waste"] = flt(stock_entry.product_waste) request_data["waste_uom"] = "g" product_usable = flt(stock_entry.product_usable) if product_usable: request_data["derivative_usable"] = product_usable if stock_entry.product_name: request_data["derivative_product"] = stock_entry.product_name response = {} try: response = call("inventory_convert", request_data) except BioTrackClientError as ex: frappe.throw(cstr(ex.message), title="BioTrack sync-up failed") derivatives = response.get("derivatives", []) for derivative in derivatives: item_type = derivative.get("barcode_type") barcode = derivative.get("barcode_id") if item_type == 27 and stock_entry.waste_item: frappe.db.set_value("Item", stock_entry.waste_item, {"bio_barcode": barcode, "disabled": 0}, update_modified=False) elif item_type == derivative_type and stock_entry.product_item: frappe.db.set_value("Item", stock_entry.product_item, {"bio_barcode": barcode, "disabled": 0}, update_modified=False)
def _create_lot(stock_entry, data): try: res = call("inventory_create_lot", {"data": data}) frappe.db.set_value("Item", stock_entry.lot_item, {"bio_barcode": res.get("barcode_id"), "disabled": 0}, update_modified=False) except BioTrackClientError as e: frappe.throw(cstr(e.message), title="BioTrack sync-up failed")