def create_timesheet(user, doctype, reference, time, bemerkung, date): # check if first timesheet entry of reference existing_ts = frappe.db.sql( """SELECT `name` FROM `tabTimesheet` WHERE `docstatus` != 2 AND `name` IN ( SELECT `parent` FROM `tabTimesheet Detail` WHERE `spo_referenz` = '{reference}')""" .format(reference=reference), as_dict=True) if not len(existing_ts) > 0: default_time = get_default_time(doctype) if time < default_time: time = default_time start = date + " 00:00:00" type = 'Mandatsarbeit' if doctype == 'Anfrage': type = 'Beratung' ts = frappe.get_doc({ "doctype": "Timesheet", "employee": user, "time_logs": [{ "activity_type": type, "hours": time, "spo_dokument": doctype, "spo_referenz": reference, "from_time": get_datetime(get_datetime_str(start)), "spo_remark": bemerkung }] }) ts.insert(ignore_permissions=True)
def create_zeiten_uebersicht(dt, name): alle_zeiten = get_zeiten_uebersicht(dt, name) if alle_zeiten: html = '<div style="width: 50%;"><table style="width: 100%;" class="table-striped"><tr><th>Datum</th><th>Stunden</th><th>Timesheet</th><th>Bearbeiten</th></tr>' for zeit in alle_zeiten: html += '<tr><td>' + get_datetime_str(zeit.from_time).split(" ")[0] + '</td><td>' + str(zeit.hours) + '</td><td>' + zeit.parent + '</td><td><a data-referenz="' + zeit.parent + '" data-funktion="open_ts"><i class="fa fa-edit"></i></a></td></tr>' html += '</table></div>' return html else: return False
def after_insert(self): subject = "Share device {0} to {1}".format(self.device, self.share_to) doc = frappe.get_doc("IOT Device", self.device) add_device_owner_log(subject, self.device, doc.company, doc.owner_type, doc.owner_id, message={ "action": "DeleteShare", "device": self.device, "share_to": self.share_to, "end_time": get_datetime_str(self.end_time) })
def update_timesheet(ts, time, doctype, reference, user, bemerkung, date=None): #********************************************************** #overwrite the time_log overlap validation of timesheet overwrite_ts_validation() #********************************************************** # check if first timesheet entry of reference existing_ts = frappe.db.sql( """SELECT `name` FROM `tabTimesheet` WHERE `docstatus` != 2 AND `name` IN ( SELECT `parent` FROM `tabTimesheet Detail` WHERE `spo_referenz` = '{reference}')""" .format(reference=reference), as_dict=True) if not len(existing_ts) > 0: default_time = get_default_time(doctype) if time < default_time: time = default_time ts = frappe.get_doc("Timesheet", ts) type = 'Mandatsarbeit' if doctype == 'Anfrage': type = 'Beratung' if not date: start = nowdate() + " 00:00:00" else: start = date + " 00:00:00" row = {} row["activity_type"] = type row["hours"] = time row["from_time"] = get_datetime(get_datetime_str(start)) row["to_time"] = add_to_date(get_datetime(get_datetime_str(start)), hours=time) row["spo_dokument"] = doctype row["spo_referenz"] = reference row['spo_remark'] = bemerkung ts.append('time_logs', row) ts.save(ignore_permissions=True)
def on_trash(self): subject = "Device share {0} to {1} deleted".format( self.device, self.share_to) doc = frappe.get_doc("IOT Device", self.device) add_device_owner_log(subject, self.device, doc.company, doc.owner_type, doc.owner_id, message={ "action": "AddShare", "device": self.device, "share_to": self.share_to, "end_time": get_datetime_str(self.end_time) })
def before_save(self): if self.is_new(): return org_end_time = frappe.get_value("IOT Device Share", self.name, "end_time") if org_end_time != self.end_time: subject = "Share device {0} to {1} updated".format( self.device, self.share_to) doc = frappe.get_doc("IOT Device", self.device) add_device_owner_log(subject, self.device, doc.company, doc.owner_type, doc.owner_id, message={ "action": "UpdateShare", "device": self.device, "share_to": self.share_to, "end_time": get_datetime_str(self.end_time) })
def sync_manifest(data): barcode = data.get("manifestid") docs = [] for stop in data.get("stops"): customer_name = frappe.get_value("Customer", {"license_no": stop.get("license_number")}) if not customer_name: continue else: customer = frappe.get_doc("Customer", customer_name) if not frappe.db.exists("Delivery Note", {"external_id": barcode, "customer": customer.name}): posting_datetime = datetime.datetime.fromtimestamp(int(stop.get("sessiontime"))) posting_date, posting_time = posting_datetime.strftime("%Y-%m-%d %H:%M:%S").split(" ") doc = frappe.get_doc( { "doctype": "Delivery Note", "external_id": barcode, "creation": get_datetime_str(posting_datetime), "posting_date": posting_date, "posting_time": posting_time, "arrive_datetime": get_datetime_str( datetime.datetime.fromtimestamp(int(stop.get("arrive_time")))), "depart_datetime": get_datetime_str( datetime.datetime.fromtimestamp(int(stop.get("depart_time")))), "customer": customer.name, "instructions": stop.get("travel_route"), "transporter_name": data.get("transporter_name"), "lr_no": data.get("transporter_vehicle_identification"), } ) for item_data in stop.get("items"): if int(item_data.get("deleted")): continue item_barcode = item_data.get("inventoryid") item = frappe.db.exists("Item", item_barcode) if not item: # lookup again at sample inspection = frappe.db.exists("Quality Inspection", {"barcode": item_barcode}) if not inspection: continue else: item_code = frappe.get_value("Quality Inspection", inspection, "item_code") else: item_code = frappe.get_value("Item", item, "item_code") dn_item = frappe.get_doc({ "doctype": "Delivery Note Item", "item_code": item_code, "qty": item_data.get("quantity"), "parentfield": "items", }) doc.get("items").append(dn_item) if len(doc.get("items")) > 0: address = map_address(customer, stop) if address: doc.set("shipping_address_name", address.name) try: doc.save() docs.append(doc) except ValidationError as e: pass doc_len = len(docs) if doc_len: frappe.db.commit() return doc_len
def create_ts_entry(user, doctype, record, datum, time, bemerkung='', nicht_verrechnen=0): #********************************************************** #overwrite the time_log overlap validation of timesheet overwrite_ts_validation() #********************************************************** time = float(time) datum = getdate(datum) latest_date = getdate(add_days(nowdate(), -7)) if datum < latest_date: frappe.throw( "Die Erfassung der standardzeit in Ihrem Timesheet konnte nicht erfasst werden, da das Datum weiter zurück als 7 Tage liegt." ) user = frappe.db.sql( """SELECT `name` FROM `tabEmployee` WHERE `user_id` = '{user}'""". format(user=user), as_list=True) if not user: frappe.throw("Es wurde kein Mitarbeiterstamm gefunden!") else: user = user[0][0] #time = get_default_time(doctype) ts = frappe.db.sql( """SELECT `name` FROM `tabTimesheet` WHERE `docstatus` = 1 AND `employee` = '{user}' AND `start_date` = '{nowdate}'""" .format(user=user, nowdate=datum.strftime("%Y-%m-%d")), as_dict=True) if len(ts) > 0: frappe.throw("Das Timesheet vom {datum} ist bereits verbucht.".format( datum=datum)) else: ts = frappe.db.sql( """SELECT `name` FROM `tabTimesheet` WHERE `docstatus` = 0 AND `employee` = '{user}' AND `start_date` = '{nowdate}'""" .format(user=user, nowdate=datum.strftime("%Y-%m-%d")), as_dict=True) if len(ts) > 0: ts = frappe.get_doc("Timesheet", ts[0].name) type = 'Mandatsarbeit' if doctype == 'Anfrage': type = 'Beratung' start = datum.strftime("%Y-%m-%d") + " 00:00:00" row = {} row["activity_type"] = type row["hours"] = time row["from_time"] = get_datetime(get_datetime_str(start)) row["to_time"] = add_to_date(get_datetime(get_datetime_str(start)), hours=time) row["spo_dokument"] = doctype row["spo_referenz"] = record row['spo_remark'] = bemerkung row['nicht_verrechnen'] = nicht_verrechnen ts.append('time_logs', row) ts.save(ignore_permissions=True) else: start = datum.strftime("%Y-%m-%d") + " 00:00:00" type = 'Mandatsarbeit' if doctype == 'Anfrage': type = 'Beratung' ts = frappe.get_doc({ "doctype": "Timesheet", "employee": user, "time_logs": [{ "activity_type": type, "hours": time, "spo_dokument": doctype, "spo_referenz": record, "from_time": get_datetime(get_datetime_str(start)), "spo_remark": bemerkung, "nicht_verrechnen": nicht_verrechnen }] }) ts.insert(ignore_permissions=True) frappe.db.commit()
def update_existing_ts(ts, employee, date, start, ende, working_hours, pausen, beratungen_mandate, diverses): #********************************************************** #overwrite the time_log overlap validation of timesheet overwrite_ts_validation() #********************************************************** try: _datum = date #Arbeitszeit start_datum = _datum + " " + start end_datum = _datum + " " + ende ts = frappe.get_doc("Timesheet", ts) ts.employee = employee ts.start_date = date ts.end_date = date ts.twh = working_hours ts.time_logs = {} row = {} row["activity_type"] = _("Arbeitszeit") row["hours"] = time_diff_in_hours( get_datetime(get_datetime_str(end_datum)), get_datetime(get_datetime_str(start_datum))) row["from_time"] = get_datetime(get_datetime_str(start_datum)) row["to_time"] = get_datetime(get_datetime_str(end_datum)) ts.append('time_logs', row) #ts.save(ignore_permissions=True) #/Arbeitszeit #Pausen pausen = json.loads(pausen) for pause in pausen: datum = _datum + " " + pause["from"] row = {} row["activity_type"] = 'Pause' row["hours"] = pause['dauer'] row["from_time"] = get_datetime(get_datetime_str(datum)) ts.append('time_logs', row) #/Pausen #Beratungen/Mandate beratungen_mandate = json.loads(beratungen_mandate) datum = _datum + " 00:00:00" for beratung in beratungen_mandate: row = {} if beratung["mandat"] == 1: row["activity_type"] = _('Mandatsarbeit') else: row["activity_type"] = _('Beratung') row["hours"] = beratung['dauer'] row["from_time"] = get_datetime(get_datetime_str(datum)) row["spo_dokument"] = beratung['spo_dokument'] row["spo_referenz"] = beratung['spo_referenz'] row["spo_remark"] = beratung['arbeit'] row["nicht_verrechnen"] = beratung['nicht_verrechnen'] ts.append('time_logs', row) #/Beratungen/Mandate #Diverses diverses = json.loads(diverses) datum = _datum + " 00:00:00" for div in diverses: row = {} row["activity_type"] = div["activity_type"] row["hours"] = div['dauer'] row["from_time"] = get_datetime(get_datetime_str(datum)) ts.append('time_logs', row) #/Diverses ts.save(ignore_permissions=True) return ts.name except Exception as error: return error
def save_ts(ma, datum, start, ende, pausen, beratungen_mandate, diverses, working_hours, ts_to_delete): if ts_to_delete: ts_to_delete = frappe.get_doc("Timesheet", ts_to_delete) ts_to_delete.cancel() ts_to_delete.delete() if check_if_ts_exist(ma, datum): frappe.throw( _("Am gewählten Datum existiert für diese(n) Mitarbeiter(in) bereits ein Timesheet" )) #********************************************************** #overwrite the time_log overlap validation of timesheet overwrite_ts_validation() #********************************************************** try: _datum = datum #Arbeitszeit start_datum = _datum + " " + start end_datum = _datum + " " + ende ts = frappe.get_doc({ "doctype": "Timesheet", "employee": ma, "start_date": datum, "end_date": datum, "twh": working_hours, "time_logs": [{ "activity_type": _("Arbeitszeit"), "hours": time_diff_in_hours(get_datetime(get_datetime_str(end_datum)), get_datetime( get_datetime_str(start_datum))), "from_time": get_datetime(get_datetime_str(start_datum)), "to_time": get_datetime(get_datetime_str(end_datum)) }] }) ts.insert() #/Arbeitszeit #Pausen pausen = json.loads(pausen) for pause in pausen: datum = _datum + " " + pause["from"] row = {} row["activity_type"] = 'Pause' row["hours"] = pause['dauer'] row["from_time"] = get_datetime(get_datetime_str(datum)) ts.append('time_logs', row) #/Pausen #Beratungen/Mandate beratungen_mandate = json.loads(beratungen_mandate) datum = _datum + " 00:00:00" for beratung in beratungen_mandate: row = {} if beratung["mandat"] == 1: row["activity_type"] = _('Mandatsarbeit') else: row["activity_type"] = _('Beratung') row["hours"] = beratung['dauer'] row["from_time"] = get_datetime(get_datetime_str(datum)) row["spo_dokument"] = beratung['spo_dokument'] row["spo_referenz"] = beratung['spo_referenz'] row["spo_remark"] = beratung['arbeit'] row["nicht_verrechnen"] = beratung['nicht_verrechnen'] ts.append('time_logs', row) #/Beratungen/Mandate #Diverses diverses = json.loads(diverses) datum = _datum + " 00:00:00" for div in diverses: row = {} row["activity_type"] = div["activity_type"] row["hours"] = div['dauer'] row["from_time"] = get_datetime(get_datetime_str(datum)) ts.append('time_logs', row) #/Diverses ts.save(ignore_permissions=True) return ts.name except Exception as error: return error
def update_ts(ma, ts, datum, start, ende, pausen, beratungen_mandate, diverses, working_hours): #********************************************************** #overwrite the time_log overlap validation of timesheet overwrite_ts_validation() #********************************************************** try: ts = frappe.get_doc("Timesheet", ts) if ts.employee != ma: frappe.throw( "Das ist nicht Ihr Timesheet. Dieses Timesheet kann nur von {employee_name} bearbeitet werden." .format(employee_name=ts.employee_name)) ts.twh = working_hours ts.time_logs = [] _datum = datum #Arbeitszeit start_datum = _datum + " " + start end_datum = _datum + " " + ende row = {} row["activity_type"] = _('Arbeitszeit') row["hours"] = time_diff_in_hours( get_datetime(get_datetime_str(end_datum)), get_datetime(get_datetime_str(start_datum))) row["from_time"] = get_datetime(get_datetime_str(start_datum)) row["to_time"] = get_datetime(get_datetime_str(end_datum)) ts.append('time_logs', row) #/Arbeitszeit #Pausen pausen = json.loads(pausen) for pause in pausen: datum = _datum + " " + pause["from"] row = {} row["activity_type"] = _('Pause') row["hours"] = pause['dauer'] row["from_time"] = get_datetime(get_datetime_str(datum)) ts.append('time_logs', row) #/Pausen #Beratungen/Mandate beratungen_mandate = json.loads(beratungen_mandate) datum = _datum + " 00:00:00" for beratung in beratungen_mandate: row = {} if beratung["mandat"] == 1: row["activity_type"] = _('Mandatsarbeit') else: row["activity_type"] = _('Beratung') row["hours"] = beratung['dauer'] row["from_time"] = get_datetime(get_datetime_str(datum)) row["spo_dokument"] = beratung['spo_dokument'] row["spo_referenz"] = beratung['spo_referenz'] row["spo_remark"] = beratung['arbeit'] row["nicht_verrechnen"] = beratung['nicht_verrechnen'] ts.append('time_logs', row) #/Beratungen/Mandate #Diverses diverses = json.loads(diverses) datum = _datum + " 00:00:00" for div in diverses: row = {} row["activity_type"] = div["activity_type"] row["hours"] = div['dauer'] row["from_time"] = get_datetime(get_datetime_str(datum)) ts.append('time_logs', row) #/Diverses ts.save(ignore_permissions=True) return "ok" except Exception as error: return error