diff --git a/src/clj/auto_ap/graphql/ledger.clj b/src/clj/auto_ap/graphql/ledger.clj index 98d6e798..2d245a3a 100644 --- a/src/clj/auto_ap/graphql/ledger.clj +++ b/src/clj/auto_ap/graphql/ledger.clj @@ -9,7 +9,7 @@ [auto-ap.graphql.utils :refer [->graphql <-graphql assert-admin assert-can-see-client result->page]] [auto-ap.parse.util :as parse] - [auto-ap.pdf.ledger :refer [print-balance-sheet print-pnl]] + [auto-ap.pdf.ledger :refer [print-balance-sheet print-pnl print-journal-detail-report]] [auto-ap.utils :refer [by dollars= heartbeat]] [clj-time.coerce :as coerce] [clj-time.core :as t] @@ -541,6 +541,14 @@ :journal_entries journal-entries})}) + +(defn journal-detail-report-pdf [context args value] + (let [data (get-journal-detail-report context args value) + result (print-journal-detail-report (:id context) args data)] + + (->graphql result))) + + (def objects {:balance_sheet_account {:fields {:id {:type 'String} @@ -638,6 +646,13 @@ :categories {:type '(list :ledger_category)}} :resolve :get-journal-detail-report} + :journal_detail_report_pdf {:type :report_pdf + :args {:client_id {:type :id} + :client_ids {:type '(list :id)} + :date_range {:type :date_range} + :categories {:type '(list :ledger_category)}} + :resolve :journal-detail-report-pdf} + :profit_and_loss_pdf {:type :report_pdf :args {:client_id {:type :id} :client_ids {:type '(list :id)} @@ -716,6 +731,7 @@ :get-balance-sheet get-balance-sheet :get-profit-and-loss get-profit-and-loss :profit-and-loss-pdf profit-and-loss-pdf + :journal-detail-report-pdf journal-detail-report-pdf :balance-sheet-pdf balance-sheet-pdf :get-journal-detail-report get-journal-detail-report :mutation/delete-external-ledger delete-external-ledger diff --git a/src/clj/auto_ap/pdf/ledger.clj b/src/clj/auto_ap/pdf/ledger.clj index d921283c..e92a40d1 100644 --- a/src/clj/auto_ap/pdf/ledger.clj +++ b/src/clj/auto_ap/pdf/ledger.clj @@ -20,7 +20,8 @@ (defn cell->pdf [cell] (let [cell-contents (cond (and (= :dollar (:format cell)) - (dollars-0? (:value cell))) + (or (nil? (:value cell)) + (dollars-0? (:value cell)))) "-" (= :dollar (:format cell)) @@ -225,6 +226,28 @@ output-stream) (.toByteArray output-stream))) +(defn make-journal-detail-report [args data] + + (println args) + (let [data (<-graphql data) + args (<-graphql args) + clients (d/pull-many (d/db conn) '[:client/code :client/name :db/id] (:client-ids args)) + report (l-reports/journal-detail-report args data (by :db/id :client/code clients)) + output-stream (ByteArrayOutputStream.)] + (log/info report) + (pdf/pdf + (-> [{:left-margin 10 :right-margin 10 :top-margin 15 :bottom-margin 15 + :size :letter + :font {:size 6 + :ttf-name "fonts/calibri-light.ttf"}} + [:heading (str "Journal Detail Report - " (str/join ", " (map :client/name clients)))]] + (conj [:paragraph {:color [128 0 0] :size 9} (:warning report)]) + (conj + (table->pdf report + [20 80 20 20 20]))) + output-stream) + (.toByteArray output-stream))) + (defn pnl-args->name [args] (let [min-date (atime/unparse-local (->> args :periods (map :start) first) @@ -235,6 +258,17 @@ names (str/replace (->> args :client_ids (d/pull-many (d/db conn) [:client/name]) (map :client/name) (str/join "-")) #" " "_" )] (format "Profit-and-loss-%s-to-%s-for-%s" min-date max-date names))) +(defn journal-detail-args->name [args] + (println args) + (let [min-date (atime/unparse-local + (->> args :date_range :start) + atime/iso-date) + max-date (atime/unparse-local + (->> args :date_range :end) + atime/iso-date) + names (str/replace (->> args :client_ids (d/pull-many (d/db conn) [:client/name]) (map :client/name) (str/join "-")) #" " "_" )] + (format "Profit-and-loss-%s-to-%s-for-%s" min-date max-date names))) + (defn balance-sheet-args->name [args] (let [date (atime/unparse-local (:date args) @@ -283,3 +317,24 @@ :report/created (java.util.Date.)}]) {:report/name name :report/url url })) + +(defn print-journal-detail-report [user args data] + (let [uuid (str (UUID/randomUUID)) + pdf-data (make-journal-detail-report args data) + name (journal-detail-args->name args) + key (str "reports/journal-detail/" uuid "/" name ".pdf") + url (str "http://" (:data-bucket env) ".s3-website-us-east-1.amazonaws.com/" key)] + (s3/put-object :bucket-name (:data-bucket env) + :key key + :input-stream (io/make-input-stream pdf-data {}) + :metadata {:content-length (count pdf-data) + :content-type "application/pdf"}) + @(d/transact conn + [{:report/name name + :report/client (:client_ids args) + :report/key key + :report/url url + :report/creator (:user user) + :report/created (java.util.Date.)}]) + {:report/name name + :report/url url })) diff --git a/src/cljc/auto_ap/ledger/reports.cljc b/src/cljc/auto_ap/ledger/reports.cljc index 3b289bcb..979e7126 100644 --- a/src/cljc/auto_ap/ledger/reports.cljc +++ b/src/cljc/auto_ap/ledger/reports.cljc @@ -564,14 +564,20 @@ :rows (reduce (fn [rows category] (into rows + ;; TODO colspan ? (cons [{:value (str (client-codes (:client-id category)) " - " (:location category) " - " (name (:category category)) " - " (:name (:account category)) ) - :colspan 5}] + } + {:value ""} + {:value ""} + {:value ""} + {:value ""}] (map (fn [je] [{:value (user-friendly-date (:date je))} {:value (or (:description je) - (:name (:vendor je)))} + (:name (:vendor je)) + "")} {:value (get-in je [:line-items 0 :debit]) :format :dollar} {:value (get-in je [:line-items 0 :credit]) diff --git a/src/cljs/auto_ap/views/pages/ledger/profit_and_loss_detail.cljs b/src/cljs/auto_ap/views/pages/ledger/profit_and_loss_detail.cljs index bbc20edd..aa52675b 100644 --- a/src/cljs/auto_ap/views/pages/ledger/profit_and_loss_detail.cljs +++ b/src/cljs/auto_ap/views/pages/ledger/profit_and_loss_detail.cljs @@ -99,11 +99,11 @@ NOTE: Please review the transactions we may have question for you here: https:// {:dispatch [::modal/modal-requested {:title "Your report is ready" :body [:div [:div "Click " - [:a {:href (-> result :profit-and-loss-pdf :url) :target "_new"} "here"] " to view it."] + [:a {:href (-> result :journal-detail-report-pdf :url) :target "_new"} "here"] " to view it."] (when (and single-client? (seq client-emails)) [:div "Once you've confirmed you're happy with it, click " - [:a {:href (str "mailto:" (str/join ";" (map :email client-emails)) "?body=" (email-body (-> result :profit-and-loss-pdf :url)) - "&subject=" (-> result :profit-and-loss-pdf :name) " is ready")} + [:a {:href (str "mailto:" (str/join ";" (map :email client-emails)) "?body=" (email-body (-> result :journal-detail-report-pdf :url)) + "&subject=" (-> result :journal-detail-report-pdf :name) " is ready")} "here"] " to open your email client and to send it to " (str/join "," (map (fn [e] (str (:email e) " (" (:description e) ")")) client-emails)) "."])]}]}))) @@ -113,11 +113,13 @@ NOTE: Please review the transactions we may have question for you here: https:// (fn [{:keys [db user]}] (cond-> {:graphql {:token user :owns-state {:single ::page} - :query-obj {:venia/queries [[:profit-and-loss-pdf + :query-obj {:venia/queries [[:journal-detail-report-pdf {:client-ids (map (comp :id :client) (:clients (:data db))) - :include-deltas (:include-deltas (:data db)) - :column-per-location (:column-per-location (:data db)) - :periods (mapv #(select-keys % #{:start :end}) (:periods (:data db)))} + :date-range {:start (date->str (:start (:date-range (:data db))) standard) + :end (date->str (:end (:date-range (:data db))) standard)} + :categories [:sales + :cogs + :payroll]} [:url :name]]]} :on-success [::received-pdf]} :set-uri-params {:date-range (:date-range (:data db))