def read_csv_content_from_uploaded_file(ignore_encoding=False): if getattr(frappe, "uploaded_file", None): with open(frappe.uploaded_file, "r") as upfile: fcontent = upfile.read() else: from frappe.utils.file_manager import get_uploaded_content fname, fcontent = get_uploaded_content() return read_csv_content(fcontent, ignore_encoding)
def upload_bank_statement(): if getattr(frappe, "uploaded_file", None): with open(frappe.uploaded_file, "rb") as upfile: fcontent = upfile.read() else: from frappe.utils.file_manager import get_uploaded_content fname, fcontent = get_uploaded_content() if frappe.safe_encode(fname).lower().endswith("csv"): from frappe.utils.csvutils import read_csv_content rows = read_csv_content(fcontent, False) elif frappe.safe_encode(fname).lower().endswith("xlsx"): from frappe.utils.xlsxutils import read_xlsx_file_from_attached_file rows = read_xlsx_file_from_attached_file(fcontent=fcontent) columns = rows[0] rows.pop(0) data = rows return {"columns": columns, "data": data}
def upload_bank_statement(): if getattr(frappe, "uploaded_file", None): with open(frappe.uploaded_file, "rb") as upfile: fcontent = upfile.read() else: from frappe.utils.file_manager import get_uploaded_content fname, fcontent = get_uploaded_content() if frappe.safe_encode(fname).lower().endswith("csv".encode('utf-8')): from frappe.utils.csvutils import read_csv_content rows = read_csv_content(fcontent, False) elif frappe.safe_encode(fname).lower().endswith("xlsx".encode('utf-8')): from frappe.utils.xlsxutils import read_xlsx_file_from_attached_file rows = read_xlsx_file_from_attached_file(fcontent=fcontent) columns = rows[0] rows.pop(0) data = rows return {"columns": columns, "data": data}
def save_uploaded(dn, parent): fname, content = get_uploaded_content() if content: return save_upload_file(fname, content, dn, parent=parent) else: raise Exception
def upload(): fname, fcontent = get_uploaded_content() err = {} regex_pattern = r"^\d{15}-\d{16}-\d{15}-\d{14}\.pdf$" regex_test = re.compile(regex_pattern) # Validasi file if fname[-4:] != ".pdf": # Bukan PDF err["message"] = "Format dokumen harus PDF" elif regex_test.match(fname) is None: # Pattern tidak sesuai err["message"] = "Format nama file tidak sesuai" else: no_faktur = fname.split("-")[1] # Validasi no faktur invoice_name = frappe.db.sql(""" SELECT name FROM `tabSales Invoice` WHERE docstatus = 1 AND workflow_state = "Submitted" AND kode_dokumen = "Faktur Pajak" AND REPLACE(REPLACE(faktur_pajak , "-", ""), ".", "") = '""" + no_faktur + """' """, as_dict=True) if len(invoice_name) == 0: err["message"] = "Invoice dengan nomor faktur " + no_faktur + " tidak ditemukan" else: invoice_name = invoice_name[0]["name"] if "message" not in err: file_doc = save_file(fname, fcontent, "Sales Invoice", invoice_name, is_private=1) # sales_doc = frappe.get_doc("Sales Invoice", invoice_name) # sales_doc.file_faktur_pajak = file_doc.file_url # sales_doc.save() frappe.db.sql(""" UPDATE `tabSales Invoice` SET file_faktur_pajak = '""" + file_doc.file_url + """' WHERE name = '""" + invoice_name + """' """) frappe.db.commit() return { "message": { "filename": fname, "message": "Berhasil attach ke dokumen Sales Invoice <a href='#Form/Sales%20Invoice/" + invoice_name + "'>" + invoice_name + "</a>" } } else: err["filename"] = fname return {"error": err}
def upload_bank_statement(): def get_data_from_file(dataframe, format): if format == 'J K Bank': return process_jk_format(dataframe) if format == 'SBI Bank': return process_sbi_format(dataframe) bs_doc = json.loads(frappe.form_dict['params']) fname, fcontent = get_uploaded_content() f = StringIO(fcontent) rows = get_data_from_file(f, bs_doc['format']) root.debug(rows) headers = rows[0] data = rows[1:] sanatize_header(headers) data_list = [frappe._dict(zip(headers, data_row)) for data_row in data] normalize_data(data_list) ## Trick to assign diff ids for same params for two transactions hash_map = {} for data_row in data_list: hash = compute_hash(data_row) hash_map.setdefault(hash, []) hash_map[hash].append(data_row) bank_txn_with_ids = [] for hash in hash_map.keys(): for index, bank_txn in enumerate(hash_map[hash]): bank_txn.bank_txn_id = '@' + str(index) + hash bank_txn_with_ids.append(bank_txn) bank_txn_ids_str = '"{}"'.format('","'.join( [x.bank_txn_id for x in bank_txn_with_ids])) existing_set = set([ x[0] for x in frappe.db.sql(""" SELECT name FROM `tabBank Statement Line` WHERE name IN ({}) """.format(bank_txn_ids_str)) ]) final_bank_txns = [] for bank_txn in bank_txn_with_ids: if bank_txn.bank_txn_id not in existing_set: final_bank_txns.append(bank_txn) if len(final_bank_txns) == 0: return {} x = [x.txn_date for x in final_bank_txns] bs_doc.update({ "doctype": "Bank Statement", 'from_date': min(x), 'to_date': max(x), 'txn_count': len(final_bank_txns), 'bank_statement_lines': final_bank_txns }) bs = frappe.get_doc(bs_doc) bs.save() return bs