def upload(): if not frappe.has_permission("Attendance", "create"): raise frappe.PermissionError from frappe.utils.csvutils import read_csv_content_from_uploaded_file from frappe.modules import scrub rows = read_csv_content_from_uploaded_file() rows = filter(lambda x: x and any(x), rows) if not rows: msg = [_("Please select a csv file")] return {"messages": msg, "error": msg} columns = [scrub(f) for f in rows[4]] columns[0] = "name" columns[3] = "att_date" ret = [] error = False from frappe.utils.csvutils import check_record, import_doc for i, row in enumerate(rows[5:]): if not row: continue row_idx = i + 5 d = frappe._dict(zip(columns, row)) d["doctype"] = "Attendance" if d.name: d["docstatus"] = frappe.db.get_value("Attendance", d.name, "docstatus") try: check_record(d) ret.append(import_doc(d, "Attendance", 1, row_idx, submit=True)) except Exception, e: error = True ret.append('Error for row (#%d) %s : %s' % (row_idx, len(row)>1 and row[1] or "", cstr(e)))
def upload(): if not frappe.has_permission("BRS Entries", "create"): raise frappe.PermissionError from frappe.utils.csvutils import read_csv_content_from_uploaded_file from frappe.modules import scrub rows = read_csv_content_from_uploaded_file() rows = filter(lambda x: x and any(x), rows) if not rows: msg = [_("Please select a csv file")] return {"messages": msg, "error": msg} #Columns located at 4th row columns = [scrub(f) for f in rows[2]] ret = [] error = False from frappe.utils.csvutils import check_record, import_doc for i, row in enumerate(rows[3:]): if not row: continue row_idx = i + 3 d = frappe._dict(zip(columns, row)) d["doctype"] = "BRS Entries" try: check_record(d) ret.append(import_doc(d, "BRS Entries", 1, row_idx, submit=True)) except Exception, e: error = True ret.append('Error for row (#%d) %s : %s' % (row_idx, len(row)>1 and row[1] or "", cstr(e))) frappe.errprint(frappe.get_traceback())
def import_attendances(rows): def remove_holidays(rows): rows = [row for row in rows if row[4] != "Holiday"] return rows from frappe.modules import scrub rows = list(filter(lambda x: x and any(x), rows)) columns = [scrub(f) for f in rows[4]] columns[0] = "name" columns[3] = "attendance_date" rows = rows[5:] ret = [] error = False rows = remove_holidays(rows) from frappe.utils.csvutils import check_record, import_doc for i, row in enumerate(rows): if not row: continue row_idx = i + 5 d = frappe._dict(zip(columns, row)) d["doctype"] = "Attendance" if d.name: d["docstatus"] = frappe.db.get_value( "Attendance", d.name, "docstatus") try: check_record(d) ret.append(import_doc(d, "Attendance", 1, row_idx, submit=True)) frappe.publish_realtime('import_attendance', dict( progress=i, total=len(rows) )) except AttributeError: pass except Exception as e: error = True ret.append('Error for row (#%d) %s : %s' % (row_idx, len(row) > 1 and row[1] or "", cstr(e))) frappe.errprint(frappe.get_traceback()) if error: frappe.db.rollback() else: frappe.db.commit() frappe.publish_realtime('import_attendance', dict( messages=ret, error=error ))
def csv_opertions(): if not frappe.has_permission("Attendance", "create"): raise frappe.PermissionError from frappe.utils.csvutils import read_csv_content_from_uploaded_file from frappe.modules import scrub rows = read_csv_content_from_uploaded_file() rows = list(filter(lambda x: x and any(x), rows)) if not rows: msg = [_("Please select a csv file")] return {"messages": msg, "error": msg} columns = [f for f in rows[0]] columns[0] = "attendance_date" columns[2] = "employee" columns[10] = "check_in" columns[11] = "check_out" ret = [] error = False from frappe.utils.csvutils import check_record, import_doc for i, row in enumerate(rows[1:]): if not row: continue row_idx = i + 1 d = frappe._dict(zip(columns, row)) d["doctype"] = "Attendance" if d.name: d["docstatus"] = frappe.db.get_value("Attendance", d.name, "docstatus") try: check_record(d) ret.append(import_doc(d, "Attendance", 1, row_idx, submit=True)) except AttributeError: pass except Exception as e: error = True ret.append('Error for row (#%d) %s : %s' % (row_idx, len(row) > 1 and row[1] or "", cstr(e))) frappe.errprint(frappe.get_traceback()) if error: frappe.db.rollback() else: frappe.db.commit() return {"messages": ret, "error": error}
def upload(): if not frappe.has_permission("Attendance", "create"): raise frappe.PermissionError from frappe.utils.csvutils import read_csv_content_from_uploaded_file from frappe.modules import scrub rows = read_csv_content_from_uploaded_file() rows = filter(lambda x: x and any(x), rows) if not rows: msg = [_("Please select a csv file")] return {"messages": msg, "error": msg} #fixme error when importing certain header #columns = [scrub(f) for f in rows[0]] columns = ["employee","att_date","arrival_time","departure_time"] ret = [] error = False started = False from frappe.utils.csvutils import check_record, import_doc for i, row in enumerate(rows[1:]): if not row: continue started = True row_idx = i + 1 d = frappe._dict(zip(columns, row)) d["doctype"] = "Attendance" try: check_record(d) ret.append(import_doc(d, "Attendance", 1, row_idx, submit=False)) except Exception, e: error = True ret.append('Error for row (#%d) %s : %s' % (row_idx+1, len(row)>1 and row[1] or "", "Check data")) frappe.errprint(row_idx) frappe.errprint(frappe.get_traceback())
def upload(): if not frappe.has_permission("Shift Schedule", "create"): raise frappe.PermissionError print("in upload\n\n") from frappe.utils.csvutils import read_csv_content_from_uploaded_file from frappe.modules import scrub rows = read_csv_content_from_uploaded_file() rows = filter(lambda x: x and any(x), rows) if not rows: msg = [_("Please select a csv file")] return {"messages": msg, "error": msg} columns = [] # columns = [scrub(f) for f in rows[1]] columns.append("name") # columns[1] = "date" ret = [] error = False from frappe.utils.csvutils import check_record, import_doc for i, row in enumerate(rows[4:]): print("in for loop\n\n") print("row[1]", row[1]) if not row: continue row_idx = i + 3 d = frappe._dict(zip(columns, row)) d["doctype"] = "Shift Schedule" d["employee"] = row[1] d["store"] = row[3] # d["attendance_date"] = rows[3][4] print("row\n") print(row) new_date_list = [] new_shift_time_list = [] print("new logic\n") for i in rows[3]: new_date_list.append(i) new_date_list = new_date_list[4:] new_shift_time_list = row[4:] print("\n new new_date_list", new_date_list) print("\n new new_shift_time_list", new_shift_time_list) print(len(new_shift_time_list), "new_shift_time_list\n") # print(row[1]) # length_of_dates=len(new_shift_time_list) length_of_dates = 8 for i in range(length_of_dates - 1): print("\n", i) print(new_shift_time_list[i]) print(new_date_list[i]) d["attendance_date"] = new_date_list[i] d["shift_time"] = new_shift_time_list[i] import datetime # new_date = datetime.datetime.strptime(row[11],'%d-%b-%y').strftime('%d-%m-%Y') # d["date"] = new_date if d.name: d["docstatus"] = frappe.db.get_value("Shift Schedule", d.name, "docstatus") try: check_record(d) ret.append( import_doc(d, "Shift Schedule", 1, row_idx, submit=True)) except Exception, e: error = True ret.append('Error for row (#%d) %s : %s' % (row_idx, len(row) > 1 and row[1] or "", cstr(e))) frappe.errprint(frappe.get_traceback())
def upload(): if not frappe.has_permission("Attendance", "create"): raise frappe.PermissionError from frappe.utils.csvutils import read_csv_content_from_uploaded_file from frappe.modules import scrub rows = read_csv_content_from_uploaded_file() rows = filter(lambda x: x and any(x), rows) #frappe.errprint(rows) if not rows: msg = [_("Please select a csv file")] return {"messages": msg, "error": msg} columns = [scrub(f) for f in rows[5]] #frappe.errprint(columns) columns[0] = "name" columns[3] = "att_date" #frappe.errprint(columns) ret = [] error = False from frappe.utils.csvutils import check_record dict1 = {} att_id = '' worked_hours = '' for i, row in enumerate(rows[6:]): #frappe.errprint(i) #frappe.errprint(row) if row[1] and row[3]: dict1 = { 'employee': row[1], 'att_date': row[3], 'status': row[4], 'fiscal_year': row[5], 'company': row[6], 'naming_series': row[7], 'employee_name': row[2] } # dict1['attendance_time_sheet'] = {'in_time':row[10],'out_time':row[11]} dict1['in_time'] = row[10] dict1['out_time'] = row[11] #frappe.errprint(dict1) if not row: continue row_idx = i + 6 #d = frappe._dict(zip(columns, row)) dict1["doctype"] = "Attendance" if dict1.get('name'): dict1["docstatus"] = frappe.db.get_value("Attendance", dict1.get('name'), "docstatus") try: check_record(dict1) if row[1] and row[3]: #frappe.errprint(dict1) att_id = import_doc(dict1, "Attendance", 1, row_idx, submit=True) make_child_entry(att_id, dict1, worked_hours) ret.append('Inserted row (#%d) %s' % (row_idx + 1, getlink('Attendance', att_id))) else: #frappe.errprint([worked_hours]) make_child_entry(att_id, dict1, worked_hours) #frappe.errprint(d.name) except Exception, e: error = True ret.append('Error for row (#%d) %s : %s' % (row_idx, len(row) > 1 and row[1] or "", cstr(e))) frappe.errprint(frappe.get_traceback())
def csv_opertions(): #------------------To import csv file on the doctype and validating it is in csv standard----------------- if not frappe.has_permission("Attendance", "create"): raise frappe.PermissionError from frappe.utils.csvutils import read_csv_content_from_uploaded_file from frappe.modules import scrub rows = read_csv_content_from_uploaded_file() rows = list(filter(lambda x: x and any(x), rows)) if not rows: msg = [_("Please select a csv file")] return {"messages": msg, "error": msg} # --------------Creating records for attendance from the csv provided to us---------------------------- columns = [f for f in rows[0]] columns[0] = "attendance_date" columns[1] = "employee_code" columns[2] = "employee_name" columns[10] = "check_in" columns[11] = "check_out" ret = [] error = False # -----------------checking for previous records creation and validation-------------------------- from frappe.utils.csvutils import check_record, import_doc for i, row in enumerate(rows[1:]): if not row: continue row_idx = i + 1 d = frappe._dict(zip(columns, row)) d["doctype"] = "Attendance" if not d["check_in"]: frappe.msgprint( "Some records didn't submit because of incomplete information. Please check error log for details." ) frappe.log_error( frappe.get_traceback(), """Attendance for date '{0}' for employee '{1}' did not mark due to missing check_in or check_out""" .format(d["attendance_date"], d["employee_name"])) continue if not d["check_out"]: frappe.msgprint( "Some records didn't submit because of incomplete information. Please check error log for details." ) frappe.log_error( frappe.get_traceback(), """Attendance for date '{0}' for employee '{1}' did not mark due to missing check_in or check_out""" .format(d["attendance_date"], d["employee_name"])) continue if d.name: d["docstatus"] = frappe.db.get_value("Attendance", d.name, "docstatus") try: check_record(d) ret.append(import_doc(d, "Attendance", 1, row_idx, submit=True)) except AttributeError: pass except Exception as e: error = True ret.append('Error for row (#%d) %s : %s' % (row_idx, len(row) > 1 and row[1] or "", cstr(e))) frappe.errprint(frappe.get_traceback()) if error: frappe.db.rollback() else: frappe.db.commit() return {"messages": ret, "error": error} # --------------------Function Ends----------------------------------
def upload(import_settings=None): if not frappe.has_permission("Attendance", "create"): raise frappe.PermissionError from frappe.utils.csvutils import read_csv_content_from_uploaded_file from frappe.modules import scrub rows = read_csv_content_from_uploaded_file() rows = list(filter(lambda x: x and any(x), rows)) if not rows: msg = [_("Please select a csv file")] return {"messages": msg, "error": msg} #fixme error when importing certain header #columns = [scrub(f) for f in rows[0]] columns = ["employee", "attendance_date", "arrival_time", "departure_time"] ret = [] error = False started = False import json params = json.loads(frappe.form_dict.get("params") or '{}') if not params.get("import_settings"): import_settings = "default" else: import_settings = params.get("import_settings") from frappe.utils.csvutils import check_record, import_doc for i, row in enumerate(rows[1:]): if not row: continue started = True row_idx = i + 1 d = frappe._dict(zip(columns, row)) d["doctype"] = "Attendance" date_error = False try: parse_date(d.attendance_date) except Exception as e: date_error = True ret.append('Error for row (#%d) %s : %s' % (row_idx + 1, len(row) > 1 and row[1] or "", cstr(e))) except ValueError as e: date_error = True ret.append('Error for row (#%d) %s : %s' % (row_idx + 1, len(row) > 1 and row[1] or "", cstr(e))) except: date_error = True ret.append('Error for row (#%d) %s' % (row_idx + 1, len(row) > 1 and row[1] or "")) if date_error == True: if import_settings != "ignore": error = True continue formatted_attendance_date = getdate(parse_date(d.attendance_date)) if import_settings == "ignore": attendance = frappe.db.sql( """select name,docstatus,attendance_date from `tabAttendance` where employee = %s and attendance_date = %s""", (d.employee, formatted_attendance_date), as_dict=True) if attendance: link = [ '<a href="#Form/Attendance/{0}">{0}</a>'.format( str(attendance[0].name)) ] # ret.append('Ignored row (#%d) %s : %s - %s' % (row_idx+1, # len(row)>1 and row[1] or "", cstr(d.employee),link)) else: try: check_record(d) ret.append( import_doc(d, "Attendance", 1, row_idx, submit=False)) except Exception, e: # error = True ret.append( 'Error for row (#%d) %s : %s' % (row_idx + 1, len(row) > 1 and row[1] or "", cstr(e))) # frappe.errprint(frappe.get_traceback()) elif import_settings == "update": attendance = frappe.db.sql( """select name,docstatus,attendance_date from `tabAttendance` where employee = %s and attendance_date = %s""", (d.employee, formatted_attendance_date), as_dict=True) if attendance: d["docstatus"] = attendance[0].docstatus d["name"] = attendance[0].name try: check_record(d) ret.append( import_doc(d, "Attendance", 1, row_idx, submit=False)) except Exception, e: error = True ret.append( 'Error for row (#%d) %s : %s' % (row_idx + 1, len(row) > 1 and row[1] or "", cstr(e)))
(d.employee, formatted_attendance_date), as_dict=True) if attendance: error = True link = [ '<a href="#Form/Attendance/{0}">{0}</a>'.format( str(attendance[0].name)) ] ret.append( 'Error for row (#%d) %s : %s - %s. Attendance Date %s Already Marked or Check Spreadsheet For Duplicates' % (row_idx + 1, len(row) > 1 and row[1] or "", cstr(d.employee), str(d.attendance_date), link)) else: try: check_record(d) ret.append( import_doc(d, "Attendance", 1, row_idx, submit=False)) except Exception, e: error = True ret.append( 'Error for row (#%d) %s : %s' % (row_idx + 1, len(row) > 1 and row[1] or "", cstr(e))) # frappe.errprint(frappe.get_traceback()) if not started: error = True ret.append('Error reading csv file') if error: frappe.db.rollback()