From 7d6e6842b8f1706d17cc995367387232a1c42d05 Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Mon, 17 Jun 2024 08:47:36 +0300
Subject: [PATCH] Upload receipts to Paperless.

---
 Documents/Shore/taxes/Makefile      |  4 ++
 Documents/Shore/taxes/send_receipts | 65 +++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+)
 create mode 100755 Documents/Shore/taxes/send_receipts

diff --git a/Documents/Shore/taxes/Makefile b/Documents/Shore/taxes/Makefile
index 4f80a35..a273fcf 100644
--- a/Documents/Shore/taxes/Makefile
+++ b/Documents/Shore/taxes/Makefile
@@ -2,6 +2,10 @@ last_month != date +%Y-%m --date 'last month'
 last_zip != find -maxdepth 1 -name '*.zip' \! -name '${last_month}.zip' -printf '%f\n' | sort -r | head -1
 new_files != find reciepts/ -type f -newer ${last_zip}
 
+.PHONY: send
+send: ${last_month}.zip
+	./send_receipts "$<"
+
 ${last_month}.zip: ${new_files}
 	zip $@ $(patsubst %,'%',$^)
 
diff --git a/Documents/Shore/taxes/send_receipts b/Documents/Shore/taxes/send_receipts
new file mode 100755
index 0000000..275010d
--- /dev/null
+++ b/Documents/Shore/taxes/send_receipts
@@ -0,0 +1,65 @@
+#!/usr/bin/env python3
+"""Send last month's receipts to Paperless.tax."""
+
+import argparse
+import datetime
+import email
+import email.message
+import mimetypes
+import pathlib
+import smtplib
+import zipfile
+
+import dateutil.relativedelta  # pylint: disable=import-error
+import passhole.passhole  # pylint: disable=import-error
+
+KEEPASS_DB = pathlib.Path("~/Documents/Database.kdbx").expanduser()
+SMTP_HOST = "smtp.shore.co.il"
+
+
+if __name__ == "__main__":
+    arg_parser = argparse.ArgumentParser(description=__doc__)
+    arg_parser.add_argument(
+        "zip_file",
+        help="Path to the zip file with the receipts.",
+        type=pathlib.Path,
+    )
+    args = arg_parser.parse_args()
+
+    # Get passwords from the Keepass database.
+    db = passhole.passhole.open_database(database=KEEPASS_DB)
+    entry = db.find_entries_by_title("Paperless")[0]
+    paperless_address = entry.get_custom_property("Direct email address")
+    entry = db.find_entries_by_title(
+        "LDAP", group=db.find_groups_by_name("shore.co.il")[0]
+    )[0]
+    smtp_username = entry.username
+    smtp_password = entry.password
+
+    # What month these receipts belong to?
+    now = datetime.datetime.now()
+    last_month = now + dateutil.relativedelta.relativedelta(months=-1)
+
+    # Build the email message.
+    msg = email.message.EmailMessage()
+    msg["Subject"] = last_month.strftime("%B %Y receipts")
+    msg["From"] = "nimrod@shore.co.il"
+    msg["To"] = paperless_address
+
+    # Attach the receipts.
+    with zipfile.ZipFile(args.zip_file) as zf:
+        for receipt in zf.namelist():
+            with zf.open(receipt) as attachment:
+                maintype, subtype = mimetypes.guess_type(receipt)[0].split("/")
+                msg.add_attachment(
+                    attachment.read(),
+                    maintype=maintype,
+                    subtype=subtype,
+                    filename=receipt,
+                )
+
+    # Send the message.
+    with smtplib.SMTP(SMTP_HOST) as s:
+        s.starttls()
+        s.login(smtp_username, smtp_password)
+        s.send_message(msg)
-- 
GitLab