diff --git a/README.md b/README.md index 98f9ffb..a48ce6c 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ See `resources/config.edn` for an example configuration. * :nip - (required) the NIP of the seller, e.g. 1234567890 * :account - (required) the number of the account to which the payment should go, e.g. "12 4321 8765 1000 0000 1222 3212" * :bank - (required) the name of the bank in which the account is held, e.g. "Piggy bank" + * :email - (optional) the email of the seller, e.g. "mr.blobby@bla.com". This is required if a confirmation email is to be sent * :phone - (optional) the phone number of the seller 555333111 * :team - (optional) a team name, to be prepended to the name of the resulting pdf, e.g. "the A team" @@ -52,6 +53,7 @@ The buyer can have the following keys * :name - (required) the name of the seller, e.g. "Mr. Blobby" * :address - (required) the address of the seller, e.g. "ul. Szeroka 12, 12-345, Buty" * :nip - (required) the NIP of the seller, e.g. 1234567890 + * :email - (optional) the email of the buyer, e.g. "faktury@bla.com". This is required if a confirmation email is to be sent ### Items @@ -102,3 +104,17 @@ should look like the following: :credentials {:tempo-token "5zq7zF9LADefEGAs12eDDas3FDttiM" :jira-token "qypaAsdFwASasEddDDddASdC" :jira-user "mr.blobby@boots.rs"} + +If a confirmation email is to be sent, a :smtp key must also be provided, e.g.: + + + :credentials {:smtp {:host "smtp.gmail.com" + :user "mr.blobby@buty.sa" + :pass "asd;l;kjsdfkljld" + :ssl true}} + +## Confirmation emails + +Each invoice can also be sent via email to the appropriate seller. For this to work, both the seller +and the buyer must have an :email key set and the credentials must contain a :smtp key with the +:smtp settings for the email server. diff --git a/project.clj b/project.clj index 40b0158..1e3323d 100644 --- a/project.clj +++ b/project.clj @@ -5,6 +5,7 @@ :url "https://www.eclipse.org/legal/epl-2.0/"} :dependencies [[org.clojure/clojure "1.10.0"] [org.clojure/tools.cli "0.4.2"] + [com.draines/postal "2.0.3"] [clj-http "3.10.0"] [cheshire "5.9.0"] [clj-pdf "2.4.0"]] diff --git a/resources/config.edn b/resources/config.edn index 6cd1748..c1e0f6d 100644 --- a/resources/config.edn +++ b/resources/config.edn @@ -1,10 +1,12 @@ [{:seller {:name "Mr. Blobby" + :email "mr.blobby@buty.sa" :address "ul. podwodna, 12-345, Mierzów" :nip 1234567890 :phone 876543216 :account "65 2345 1233 1233 4322 3211 4567" :bank "Skok hop"} :buyer {:name "Buty S.A." + :email "faktury@buty.sa" :address "ul. Szewska 32, 76-543, Bąków" :nip 9875645342} :items [{:vat 8 :netto 123.21 :title "Buty kowbojskie"} @@ -15,4 +17,8 @@ :font-path "/usr/share/fonts/truetype/freefont/FreeSans.ttf" :credentials {:tempo-token "5zq7zF9LADefEGAs12eDDas3FDttiM" :jira-token "qypaAsdFwASasEddDDddASdC" - :jira-user "mr.blobby@boots.rs"}}] + :jira-user "mr.blobby@boots.rs" + :smtp {:host "smtp.gmail.com" + :user "mr.blobby@buty.sa" + :pass "asd;l;kjsdfkljld" + :ssl true}}}] diff --git a/src/invoices/core.clj b/src/invoices/core.clj index 0e3237e..fc48a5b 100644 --- a/src/invoices/core.clj +++ b/src/invoices/core.clj @@ -3,7 +3,8 @@ [invoices.settings :refer [invoices]] [invoices.jira :refer [prev-timesheet prev-month]] [clojure.tools.cli :refer [parse-opts]] - [clojure.string :as str]) + [clojure.string :as str] + [postal.core :refer [send-message]]) (:gen-class)) (defn invoice-number [when number] @@ -36,18 +37,32 @@ (not (contains? item :netto)) (assoc item :netto 0) :else item)) +(defn send-email [to from {smtp :smtp} invoice] + (when (not-any? nil? [to from smtp invoice]) + (->> + (send-message smtp {:from from + :to [to] + :subject invoice + :body [{:type :attachment + :content (java.io.File. (str invoice ".pdf")) + :content-type "application/pdf"}]}) + :error (= :SUCCESS) + (println " - email sent: ")))) + (defn date-applies? [when {to :to from :from}] (and (or (nil? to) (-> when .toString (compare to) (< 0))) (or (nil? from) (-> when .toString (compare from) (>= 0))))) (defn for-month [when {seller :seller buyer :buyer items :items creds :credentials font-path :font-path} & [number]] - (pdf/render seller buyer - (->> items - (filter (partial date-applies? when)) - (map (partial set-price (prev-timesheet when creds)))) - (pdf/last-working-day when) - (invoice-number when number) - font-path)) + (->> + (pdf/render seller buyer + (->> items + (filter (partial date-applies? when)) + (map (partial set-price (prev-timesheet when creds)))) + (pdf/last-working-day when) + (invoice-number when number) + font-path) + (send-email (:email buyer) (:email seller) creds))) (defn get-invoices [nips config] (if (seq nips) diff --git a/src/invoices/jira.clj b/src/invoices/jira.clj index 7d65881..d2f1ea9 100644 --- a/src/invoices/jira.clj +++ b/src/invoices/jira.clj @@ -26,4 +26,6 @@ (defn prev-timesheet "Get the timesheet for the previous month" - [when credentials] (get-timesheet (me credentials) (prev-month when) credentials)) + [when credentials] + (clojure.core/when (:jira-user credentials) + (get-timesheet (me credentials) (prev-month when) credentials))) diff --git a/src/invoices/pdf.clj b/src/invoices/pdf.clj index 6758c86..105b365 100644 --- a/src/invoices/pdf.clj +++ b/src/invoices/pdf.clj @@ -82,7 +82,8 @@ "" (format-total items vat) (format-total items brutto)]])] - (str title ".pdf")))) + (str title ".pdf")) + title)) (defn skip-days-off [when]