def set_current_invoice_end(self): """ This sets the date of the end of the current billing period. If the subscription is in trial period, it will be set as the end of the trial period. If is not in a trial period, it will be `x` days from the beginning of the current billing period where `x` is the billing interval from the `Subscription Plan` in the `Subscription`. """ if self.is_trialling() and getdate( self.current_invoice_start) < getdate(self.trial_period_end): self.current_invoice_end = self.trial_period_end else: billing_cycle_info = self.get_billing_cycle_data() if billing_cycle_info: if self.is_new_subscription() and getdate( self.start_date) < getdate(self.current_invoice_start): self.current_invoice_end = add_to_date( self.start_date, **billing_cycle_info) # For cases where trial period is for an entire billing interval if getdate(self.current_invoice_end) < getdate( self.current_invoice_start): self.current_invoice_end = add_to_date( self.current_invoice_start, **billing_cycle_info) else: self.current_invoice_end = add_to_date( self.current_invoice_start, **billing_cycle_info) else: self.current_invoice_end = get_last_day( self.current_invoice_start) if self.follow_calendar_months: billing_info = self.get_billing_cycle_and_interval() billing_interval_count = billing_info[0][ 'billing_interval_count'] calendar_months = get_calendar_months(billing_interval_count) calendar_month = 0 current_invoice_end_month = getdate( self.current_invoice_end).month current_invoice_end_year = getdate( self.current_invoice_end).year for month in calendar_months: if month <= current_invoice_end_month: calendar_month = month if cint(calendar_month - billing_interval_count) <= 0 and \ getdate(self.current_invoice_start).month != 1: calendar_month = 12 current_invoice_end_year -= 1 self.current_invoice_end = get_last_day(cstr(current_invoice_end_year) + '-' \ + cstr(calendar_month) + '-01') if self.end_date and getdate(self.current_invoice_end) > getdate( self.end_date): self.current_invoice_end = self.end_date
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 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 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_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 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 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 _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 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): """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 _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_point_transaction(ref_link, ref_name, inv, tran_type, points, rule_details=None): if points != 0: tran = frappe.new_doc("Point Transaction") tran.ref_link = ref_link tran.ref_name = ref_name tran.date = today() tran.type = tran_type tran.points = cint(points) * 1 if tran_type == 'Earned' else -1 * cint(points) if rule_details: tran.valied_upto = add_months(nowdate(), cint(rule_details.get('valid_upto'))) tran.invoice_number = inv tran.rule_details = cstr(rule_details) tran.docstatus = 1 tran.insert()
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 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 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 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 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 create_point_transaction(ref_link, ref_name, inv, tran_type, points, rule_details=None): if points != 0: tran = frappe.new_doc("Point Transaction") tran.ref_link = ref_link tran.ref_name = ref_name tran.date = today() tran.type = tran_type tran.points = cint(points) * 1 if tran_type == 'Earned' else -1 * cint( points) if rule_details: tran.valied_upto = add_months(nowdate(), cint(rule_details.get('valid_upto'))) tran.invoice_number = inv tran.rule_details = cstr(rule_details) tran.docstatus = 1 tran.insert()
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 read_json(name): file_path = os.path.join(os.path.dirname(__file__), '{name}.json'.format(name=name)) with open(file_path, 'r') as f: return cstr(f.read())
def is_bio_item(item): return cstr(item.get("bio_barcode")) != ""
def is_bio_plant(plant): return cstr(plant.get("bio_barcode")) != ""
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 is_bio_plant(plant): return cstr(plant.get("bio_barcode")) != ""
def is_bio_item(item): return cstr(item.get("bio_barcode")) != ""