Send a confirmation email

This commit is contained in:
Daniel O'Connell 2019-10-02 18:48:37 +02:00
parent f650d5f40e
commit 654429d22b
6 changed files with 52 additions and 11 deletions

View File

@ -41,6 +41,7 @@ See `resources/config.edn` for an example configuration.
* :nip - (required) the NIP of the seller, e.g. 1234567890 * :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" * :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" * :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 * :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" * :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" * :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" * :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 * :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 ### Items
@ -102,3 +104,17 @@ should look like the following:
:credentials {:tempo-token "5zq7zF9LADefEGAs12eDDas3FDttiM" :credentials {:tempo-token "5zq7zF9LADefEGAs12eDDas3FDttiM"
:jira-token "qypaAsdFwASasEddDDddASdC" :jira-token "qypaAsdFwASasEddDDddASdC"
:jira-user "mr.blobby@boots.rs"} :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.

View File

@ -5,6 +5,7 @@
:url "https://www.eclipse.org/legal/epl-2.0/"} :url "https://www.eclipse.org/legal/epl-2.0/"}
:dependencies [[org.clojure/clojure "1.10.0"] :dependencies [[org.clojure/clojure "1.10.0"]
[org.clojure/tools.cli "0.4.2"] [org.clojure/tools.cli "0.4.2"]
[com.draines/postal "2.0.3"]
[clj-http "3.10.0"] [clj-http "3.10.0"]
[cheshire "5.9.0"] [cheshire "5.9.0"]
[clj-pdf "2.4.0"]] [clj-pdf "2.4.0"]]

View File

@ -1,10 +1,12 @@
[{:seller {:name "Mr. Blobby" [{:seller {:name "Mr. Blobby"
:email "mr.blobby@buty.sa"
:address "ul. podwodna, 12-345, Mierzów" :address "ul. podwodna, 12-345, Mierzów"
:nip 1234567890 :nip 1234567890
:phone 876543216 :phone 876543216
:account "65 2345 1233 1233 4322 3211 4567" :account "65 2345 1233 1233 4322 3211 4567"
:bank "Skok hop"} :bank "Skok hop"}
:buyer {:name "Buty S.A." :buyer {:name "Buty S.A."
:email "faktury@buty.sa"
:address "ul. Szewska 32, 76-543, Bąków" :address "ul. Szewska 32, 76-543, Bąków"
:nip 9875645342} :nip 9875645342}
:items [{:vat 8 :netto 123.21 :title "Buty kowbojskie"} :items [{:vat 8 :netto 123.21 :title "Buty kowbojskie"}
@ -15,4 +17,8 @@
:font-path "/usr/share/fonts/truetype/freefont/FreeSans.ttf" :font-path "/usr/share/fonts/truetype/freefont/FreeSans.ttf"
:credentials {:tempo-token "5zq7zF9LADefEGAs12eDDas3FDttiM" :credentials {:tempo-token "5zq7zF9LADefEGAs12eDDas3FDttiM"
:jira-token "qypaAsdFwASasEddDDddASdC" :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}}}]

View File

@ -3,7 +3,8 @@
[invoices.settings :refer [invoices]] [invoices.settings :refer [invoices]]
[invoices.jira :refer [prev-timesheet prev-month]] [invoices.jira :refer [prev-timesheet prev-month]]
[clojure.tools.cli :refer [parse-opts]] [clojure.tools.cli :refer [parse-opts]]
[clojure.string :as str]) [clojure.string :as str]
[postal.core :refer [send-message]])
(:gen-class)) (:gen-class))
(defn invoice-number [when number] (defn invoice-number [when number]
@ -36,18 +37,32 @@
(not (contains? item :netto)) (assoc item :netto 0) (not (contains? item :netto)) (assoc item :netto 0)
:else item)) :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}] (defn date-applies? [when {to :to from :from}]
(and (or (nil? to) (-> when .toString (compare to) (< 0))) (and (or (nil? to) (-> when .toString (compare to) (< 0)))
(or (nil? from) (-> when .toString (compare from) (>= 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]] (defn for-month [when {seller :seller buyer :buyer items :items creds :credentials font-path :font-path} & [number]]
(->>
(pdf/render seller buyer (pdf/render seller buyer
(->> items (->> items
(filter (partial date-applies? when)) (filter (partial date-applies? when))
(map (partial set-price (prev-timesheet when creds)))) (map (partial set-price (prev-timesheet when creds))))
(pdf/last-working-day when) (pdf/last-working-day when)
(invoice-number when number) (invoice-number when number)
font-path)) font-path)
(send-email (:email buyer) (:email seller) creds)))
(defn get-invoices [nips config] (defn get-invoices [nips config]
(if (seq nips) (if (seq nips)

View File

@ -26,4 +26,6 @@
(defn prev-timesheet (defn prev-timesheet
"Get the timesheet for the previous month" "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)))

View File

@ -82,7 +82,8 @@
"" ""
(format-total items vat) (format-total items vat)
(format-total items brutto)]])] (format-total items brutto)]])]
(str title ".pdf")))) (str title ".pdf"))
title))
(defn skip-days-off [when] (defn skip-days-off [when]