def invoice(invoice_id): """Save comments and uploaded modified invoices.""" if not logged_in(): return redirect(url_for("admin.index")) invoice_obj = db.invoice(invoice_id) api = InvoiceAPI(db, lims, invoice_obj) kth_inv = api.prepare("KTH") ki_inv = api.prepare("KI") if not (kth_inv and ki_inv): flash(" ,".join(list(set(api.log)))) undo_invoice(invoice_id) return redirect(request.referrer) if not invoice_obj.price: final_price = api.total_price() else: final_price = invoice_obj.price return render_template( "invoices/invoice.html", invoice=invoice_obj, invoice_dict={ "KTH": kth_inv, "KI": ki_inv }, default_price=api.total_price(), final_price=final_price, record_type=invoice_obj.record_type, )
def undo_invoice(invoice_id): invoice_obj = db.invoice(invoice_id) record_type = invoice_obj.record_type records = db.invoice_samples(invoice_id=invoice_id) invoice_obj.delete() for record in records: record.invoice_id = None db.session.commit() return url_for(".new", record_type=record_type)
def modified_invoice(invoice_id, cost_center): """Enables download of modified invoices saved in the database.""" if not logged_in(): return redirect(url_for("admin.index")) invoice_obj = db.invoice(invoice_id) file_name = "invoice_" + cost_center + str(invoice_id) + ".xlsx" temp_dir = tempfile.mkdtemp() file_path = os.path.join(temp_dir, file_name) with open(file_path, "wb") as file_object: if cost_center == "KTH": file_object.write(invoice_obj.excel_kth) elif cost_center == "KI": file_object.write(invoice_obj.excel_ki) pass return send_from_directory(directory=temp_dir, filename=file_name, as_attachment=True)
def invoice_template(invoice_id): """Generate invoice template""" if not logged_in(): return redirect(url_for("admin.index")) cost_center = request.args.get("cost_center", "KTH") invoice_obj = db.invoice(invoice_id) api = InvoiceAPI(db, lims, invoice_obj) invoice_dict = api.prepare(cost_center) workbook = render_xlsx(invoice_dict) temp_dir = tempfile.gettempdir() filename = "Invoice_{}_{}.xlsx".format(invoice_obj.id, cost_center) excel_path = os.path.join(temp_dir, filename) workbook.save(excel_path) return send_from_directory(directory=temp_dir, filename=filename, as_attachment=True)
def upload_invoice_news_to_db(): invoice_id = request.form.get("invoice_id") invoice_obj = db.invoice(invoice_id) invoice_obj.comment = request.form.get("comment") if request.form.get("final_price") != invoice_obj.price: invoice_obj.price = request.form.get("final_price") if request.form.get("invoice_sent") and not invoice_obj.invoiced_at: invoice_obj.invoiced_at = date.today() elif not request.form.get("invoice_sent"): invoice_obj.invoiced_at = None kth_excel_file = request.files.get("KTH_excel") if kth_excel_file: invoice_obj.excel_kth = kth_excel_file.stream.read() ki_excel_file = request.files.get("KI_excel") if ki_excel_file: invoice_obj.excel_ki = ki_excel_file.stream.read() db.commit() return url_for("invoices.invoice", invoice_id=invoice_id)