From bb85a5f2fa040f9b86c276a1fb35823a30828051 Mon Sep 17 00:00:00 2001 From: Daniel O'Connell Date: Wed, 22 Jan 2020 18:34:06 +0100 Subject: [PATCH] sundry fixes --- README.md | 3 ++- src/invoices/core.clj | 10 +++++----- src/invoices/email.clj | 22 ++++++++++++---------- src/invoices/pdf.clj | 2 +- src/invoices/timesheets.clj | 9 +++++---- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 5db9b4e..1f1df42 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ the JIRA credentials with correct values). The following options are available: -n, --number Invoice number. In the case of multiple invoices, they will have subsequent numbers - -w, --when The month for which to generate the invoice + -w, --when The date for which to generate the invoice -c, --company The NIPs of companies for which to generate invoices. If not provided, all the companies will be used -h, --help Display a help message @@ -125,6 +125,7 @@ worklog entry like the following: :worklogs [{:type :jira :ids [:from-jira] + :month-offset -2 ; Can be used to get a different month than the currently processed one. In this case, 2 months previous :tempo-token "5zq7zF9LADefEGAs12eDDas3FDttiM" :jira-token "qypaAsdFwASasEddDDddASdC" :jira-user "mr.blobby@boots.rs"}] diff --git a/src/invoices/core.clj b/src/invoices/core.clj index b039b01..9adac0d 100644 --- a/src/invoices/core.clj +++ b/src/invoices/core.clj @@ -26,8 +26,8 @@ (doall (map (partial run-callback invoice) callbacks))) (defn for-month [{seller :seller buyer :buyer smtp :smtp callbacks :callbacks :as invoice} when & [number font]] - (let [file (pdf/render invoice (last-working-day when) (invoice-number when number))] - ;; (email/send-invoice file (:email buyer) smtp) + (let [file (pdf/render invoice when (invoice-number when number))] + (email/send-invoice file (:email buyer) smtp) (run-callbacks file callbacks))) (defn item-price [worklogs item] @@ -48,7 +48,7 @@ (defn process-invoices [{invoices :invoices :as config} month worklogs] (let [invoices (map (partial prepare-invoice config month worklogs) invoices)] - (doseq [[i invoice] (map-indexed vector invoices)] + (doseq [[i invoice] (->> invoices (filter #(-> % :items seq)) (map-indexed vector))] (for-month invoice month (inc i)) (println)))) @@ -56,8 +56,8 @@ [["-n" "--number NUMBER" "Invoice number. In the case of multiple invoices, they will have subsequent numbers" :default 1 :parse-fn #(Integer/parseInt %)] - ["-w" "--when DATE" "The month for which to generate the invoice" - :default (-> (java.time.LocalDate/now) (.minusMonths 1)) + ["-w" "--when DATE" "The day for which to generate the invoice" + :default (-> (java.time.LocalDate/now) (.minusMonths 1) last-working-day) :parse-fn #(java.time.LocalDate/parse %)] ;; A non-idempotent option (:default is applied first) ["-c" "--company NIP" "companies for which to generate invoices. All, if not provided" diff --git a/src/invoices/email.clj b/src/invoices/email.clj index 0b05b9a..ba40085 100644 --- a/src/invoices/email.clj +++ b/src/invoices/email.clj @@ -8,16 +8,18 @@ [clojure-mail.message :refer (read-message) :as mess])) (defn send-invoice [invoice to {from :user :as smtp}] - (when (and (not-any? nil? [to from smtp invoice]) - (->> - (send-message smtp {:from from - :to [to] - :subject (.getName invoice) - :body [{:type :attachment - :content (.getAbsolutePath invoice) - :content-type "application/pdf"}]}) - :error (= :SUCCESS))) - (println " - email sent to " to))) + (if (sequential? to) + (doseq [address to] (send-invoice invoice address smtp)) + (when (and (not-any? nil? [to from smtp invoice]) + (->> + (send-message smtp {:from from + :to [to] + :subject (.getName invoice) + :body [{:type :attachment + :content (.getAbsolutePath invoice) + :content-type "application/pdf"}]}) + :error (= :SUCCESS))) + (println " - email sent to " to)))) (defn server-find-messages "Find all messages in the given folder, filtering them by subject and sender (use nil to ignore)." diff --git a/src/invoices/pdf.clj b/src/invoices/pdf.clj index e4a3767..e40d7c4 100644 --- a/src/invoices/pdf.clj +++ b/src/invoices/pdf.clj @@ -1,6 +1,6 @@ (ns invoices.pdf (:require [clj-pdf.core :refer [pdf]] - [invoices.time :refer [skip-days-off last-working-day]] + [invoices.time :refer [skip-days-off]] [invoices.calc :refer [brutto vat round]] [clojure.string :as str]) (:import [java.awt Font])) diff --git a/src/invoices/timesheets.clj b/src/invoices/timesheets.clj index a42ec22..bdc22b2 100644 --- a/src/invoices/timesheets.clj +++ b/src/invoices/timesheets.clj @@ -31,10 +31,11 @@ (jira-timesheet (me credentials) (prev-month when) credentials))) -(defn get-timesheet [month {type :type :as creds}] - (condp = type - :jira (jira-timesheet (me creds) month creds) - :imap (email/get-worklogs month creds))) +(defn get-timesheet [month {type :type offset :month-offset :as creds}] + (let [month (-> month (.plusMonths (or offset 0)))] + (condp = type + :jira (jira-timesheet (me creds) month creds) + :imap (email/get-worklogs month creds)))) (defn timesheets "Return timesheets for the given month from the given worklogs."