From e06861158e93b985711f46e2e566dfbbdc8796c0 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Fri, 1 Apr 2022 07:59:55 -0700 Subject: [PATCH] complete parity between reports. --- src/clj/auto_ap/graphql/ledger.clj | 20 ++++- src/clj/auto_ap/pdf/ledger.clj | 69 ++++++++++++++- src/cljc/auto_ap/ledger/reports.cljc | 7 +- .../views/pages/ledger/balance_sheet.cljs | 88 ++++++++++++++----- .../views/pages/ledger/profit_and_loss.cljs | 18 ++-- src/cljs/auto_ap/views/utils.cljs | 16 ++-- 6 files changed, 170 insertions(+), 48 deletions(-) diff --git a/src/clj/auto_ap/graphql/ledger.clj b/src/clj/auto_ap/graphql/ledger.clj index 8f1d55ca..ea2a9a7d 100644 --- a/src/clj/auto_ap/graphql/ledger.clj +++ b/src/clj/auto_ap/graphql/ledger.clj @@ -9,7 +9,7 @@ :refer [->graphql <-graphql assert-admin assert-can-see-client result->page]] [auto-ap.parse.util :as parse] [auto-ap.utils :refer [by dollars=]] - [auto-ap.pdf.ledger :refer [print-pnl]] + [auto-ap.pdf.ledger :refer [print-pnl print-balance-sheet]] [clj-time.coerce :as coerce] [clojure.tools.logging :as log] [com.walmartlabs.lacinia.util :refer [attach-resolvers]] @@ -214,6 +214,12 @@ (->graphql result))) +(defn balance-sheet-pdf [context args value] + (let [data (get-balance-sheet context args value) + result (print-balance-sheet (:id context) args data)] + + (->graphql result))) + (defn assoc-error [f] (fn [entry] @@ -512,7 +518,7 @@ :account_type {:type :account_type} :name {:type 'String}}} - :profit_and_loss_pdf + :report_pdf {:fields {:url {:type 'String} :name {:type 'String}}} @@ -581,13 +587,20 @@ :include_deltas {:type 'Boolean}} :resolve :get-profit-and-loss} - :profit_and_loss_pdf {:type :profit_and_loss_pdf + :profit_and_loss_pdf {:type :report_pdf :args {:client_id {:type :id} :client_ids {:type '(list :id)} :periods {:type '(list :date_range)} :include_deltas {:type 'Boolean}} :resolve :profit-and-loss-pdf} + :balance_sheet_pdf {:type :report_pdf + :args {:client_id {:type :id} + :include_comparison {:type 'Boolean} + :date {:type :iso_date} + :comparison_date {:type :iso_date}} + :resolve :balance-sheet-pdf} + :ledger_page {:type :ledger_page :args {:filters {:type :ledger_filters}} :resolve :get-ledger-page}}) @@ -656,6 +669,7 @@ :get-balance-sheet get-balance-sheet :get-profit-and-loss get-profit-and-loss :profit-and-loss-pdf profit-and-loss-pdf + :balance-sheet-pdf balance-sheet-pdf :mutation/delete-external-ledger delete-external-ledger :mutation/import-ledger import-ledger}) diff --git a/src/clj/auto_ap/pdf/ledger.clj b/src/clj/auto_ap/pdf/ledger.clj index 470c9b84..059238b1 100644 --- a/src/clj/auto_ap/pdf/ledger.clj +++ b/src/clj/auto_ap/pdf/ledger.clj @@ -10,7 +10,8 @@ [clojure.java.io :as io] [clojure.string :as str] [config.core :refer [env]] - [datomic.api :as d]) + [datomic.api :as d] + [clojure.tools.logging :as log]) (:import (java.io ByteArrayOutputStream) (java.text DecimalFormat) @@ -141,6 +142,40 @@ 9))] table)) +(defn make-balance-sheet [args data] + + (let [data (<-graphql data) + args (<-graphql args) + args (assoc args + :periods (filter identity (cond-> [(:date args)] + (:include-comparison args) (conj (:comparison-date args))))) + clients (d/pull-many (d/db conn) '[:client/name :db/id] [(:client-id args)]) + data (concat (->> (:balance-sheet-accounts data) + (map (fn [b] + (assoc b + :period (:date args))))) + (->> (:comparable-balance-sheet-accounts data) + (map (fn [b] + (assoc b + :period (:comparison-date args)))))) + pnl-data (l-reports/->PNLData args data (by :db/id clients)) + report (l-reports/summarize-balance-sheet pnl-data) + 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 "Balance Sheet - " (str/join ", " (map :client/name clients)))]] + (conj [:paragraph {:color [128 0 0] :size 9} (:warning report)]) + (conj + (table->pdf report + (cond-> [30 13] + (:include-comparison args) (into [13 13]))))) + output-stream) + (.toByteArray output-stream))) + (defn make-pnl [args data] (let [data (<-graphql data) @@ -188,7 +223,7 @@ output-stream) (.toByteArray output-stream))) -(defn args->name [args] +(defn pnl-args->name [args] (let [min-date (atime/unparse-local (->> args :periods (map :start) first) atime/iso-date) @@ -198,10 +233,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 balance-sheet-args->name [args] + (let [date (atime/unparse-local + (:date args) + atime/iso-date) + name (str/replace (->> args :client_id (d/pull (d/db conn) [:client/name]) :client/name ) #" " "_" )] + (format "Balance-sheet-%s-for-%s" date name))) + (defn print-pnl [user args data] (let [uuid (str (UUID/randomUUID)) pdf-data (make-pnl args data) - name (args->name args) + name (pnl-args->name args) key (str "reports/pnl/" 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) @@ -218,3 +260,24 @@ :report/created (java.util.Date.)}]) {:report/name name :report/url url })) + +(defn print-balance-sheet [user args data] + (let [uuid (str (UUID/randomUUID)) + pdf-data (make-balance-sheet args data) + name (balance-sheet-args->name args) + key (str "reports/balance-sheet/" 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_id 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 4cadc119..169578f4 100644 --- a/src/cljc/auto_ap/ledger/reports.cljc +++ b/src/cljc/auto_ap/ledger/reports.cljc @@ -14,7 +14,7 @@ (defn date->str [d] #?(:clj (atime/unparse-local d atime/normal-date) - :cljs (au/date->str d au/standard))) + :cljs (au/date->str d au/pretty))) (def ranges @@ -407,8 +407,8 @@ (defn balance-sheet-headers [pnl-data] [(cond-> [{:value "Period Ending"} - {:value (:date (:args pnl-data))}] - (:include-comparison (:args pnl-data)) (into [{:value (:comparison-date (:args pnl-data))} {:value "+/-"}]))]) + {:value (date->str (:date (:args pnl-data)))}] + (:include-comparison (:args pnl-data)) (into [{:value (date->str (:comparison-date (:args pnl-data)))} {:value "+/-"}]))]) (defn append-deltas [table] (->> table @@ -420,7 +420,6 @@ (:value b))})])))) (defn summarize-balance-sheet [pnl-data] - (println (:periods (:args pnl-data))) (let [table (-> [] (into (detail-rows pnl-data :assets diff --git a/src/cljs/auto_ap/views/pages/ledger/balance_sheet.cljs b/src/cljs/auto_ap/views/pages/ledger/balance_sheet.cljs index 3f68237c..78d8fd10 100644 --- a/src/cljs/auto_ap/views/pages/ledger/balance_sheet.cljs +++ b/src/cljs/auto_ap/views/pages/ledger/balance_sheet.cljs @@ -3,7 +3,9 @@ [auto-ap.forms :as forms] [auto-ap.ledger.reports :as l-reports] [auto-ap.status :as status] + [clojure.string :as str] [auto-ap.subs :as subs] + [auto-ap.views.components.modal :as modal] [auto-ap.views.components.layouts :refer [appearing-side-bar side-bar-layout]] [auto-ap.views.components.switch-field :refer [switch-field]] @@ -22,6 +24,7 @@ [re-frame.core :as re-frame] [reagent.core :as reagent] [vimsical.re-frame.fx.track :as track] + [vimsical.re-frame.cofx.inject :as inject] [auto-ap.views.pages.ledger.report-table :as rtable])) (defn data-params->query-params [params] @@ -69,25 +72,68 @@ (re-frame/reg-event-fx ::report-requested - [with-user (forms/in-form ::form)] - (fn [{:keys [db user]} [_]] + [with-user (forms/in-form ::form) (re-frame/inject-cofx ::inject/sub [::subs/client])] + (fn [{:keys [db user ::subs/client]} [_]] {:db (dissoc db :report) :graphql {:token user :query-obj {:venia/queries [[:balance-sheet - (assoc (:data db) - :client-id (:id @(re-frame/subscribe [::subs/client]))) + (-> (:data db) + (assoc :client-id (:id client)) + (update :date (fnil #(date->str % standard) nil)) + (update :comparison-date (fnil #(date->str % standard) nil))) [[:balance-sheet-accounts [:name :amount :account-type :id :numeric-code]] [:comparable-balance-sheet-accounts [:name :amount :account-type :id :numeric-code]]]]]} :owns-state {:single ::page} :on-success [::received]}})) +(defn email-body [report-url] + (js/encodeURIComponent + (str + "Hello, +Click here (" report-url ") to download your financial reports. We have not finished reviewing and reconciling these numbers with you. Please review and let us know if anything seems missing or in need of correction. +Click here (http://app.integreatconsult.com/) to login to the Financials app to review the details here. +Click here (https://share.vidyard.com/watch/MHTo5PyXPxXUpVH93RWFM9?) for a video on how to run a P&L on your own. +To see a history of past financial reports, click here: https://app.integreatconsult.com/reports/ + +NOTE: Please review the transactions we may have question for you here: https://app.integreatconsult.com/transactions/requires-feedback. You can either edit the transaction to what expense account it should be or email back what it should be."))) + +(re-frame/reg-event-fx + ::received-pdf + [(re-frame/inject-cofx ::inject/sub [::subs/client])] + (fn [{:keys [::subs/client]} [_ result]] + {:dispatch [::modal/modal-requested {:title "Your report is ready" + :body [:div + [:div "Click " + [:a {:href (-> result :balance-sheet-pdf :url) :target "_new"} "here"] " to view it."] + (when (seq (:emails client)) + [:div "Once you've confirmed you're happy with it, click " + [:a {:href (str "mailto:" (str/join ";" (map :email (:emails client))) "?body=" (email-body (-> result :balance-sheet-pdf :url)) + "&subject=" (-> result :balance-sheet-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) ")")) + (:emails client))) "."])]}]})) + +(re-frame/reg-event-fx + ::export-pdf + [with-user (forms/in-form ::form) (re-frame/inject-cofx ::inject/sub [::subs/client])] + (fn [{:keys [db user ::subs/client]} [_]] + {:db (dissoc db :report) + :graphql {:token user + :query-obj {:venia/queries [[:balance-sheet-pdf + (-> (:data db) + (assoc :client-id (:id client)) + (update :date (fnil #(date->str % standard) nil)) + (update :comparison-date (fnil #(date->str % standard) nil))) + [:url :name]]]} + + :owns-state {:single ::page} + :on-success [::received-pdf]}})) + (re-frame/reg-event-fx ::investigate-clicked - (fn [{:keys [db]} [_ {:keys [from-numeric-code to-numeric-code date-range] :as g}]] - (println g) - + (fn [{:keys [db]} [_ {:keys [from-numeric-code to-numeric-code date-range]}]] {:db (-> db (assoc ::ledger-list-active? true)) :dispatch [::data-page/additional-params-changed ::ledger {:client-id (:id @(re-frame/subscribe [::subs/client])) :from-numeric-code from-numeric-code @@ -126,15 +172,17 @@ (re-frame/reg-event-fx ::unmounted-balance-sheet - (fn [_ _] - {:dispatch [::data-page/dispose ::ledger] + (fn [{:keys [db]} _] + { + :db (dissoc db ::ledger-list-active?) + :dispatch [::data-page/dispose ::ledger] ::track/dispose {:id ::ledger-params}})) (re-frame/reg-event-fx ::mounted-balance-sheet (fn [{:keys [db]} _] - {:db (forms/start-form db ::form {:date (date->str (local-now) standard) - :comparison-date (date->str (t/minus (local-now) (t/years 1)) standard) + {:db (forms/start-form db ::form {:date (local-now) + :comparison-date (t/minus (local-now) (t/years 1)) :include-comparison true}) ::track/register {:id ::ledger-params :subscription [::data-page/params ::ledger] @@ -151,7 +199,6 @@ {:keys [data]} @(re-frame/subscribe [::forms/form ::form])] (form-inline {} [:div - [status/status-notification {:statuses [[::status/single ::page]]}] [:div.report-controls [:div.level [:div.level-left @@ -159,7 +206,8 @@ [:div.control [:p.help "Date"] (raw-field - [date-picker-friendly {:type "date" + [date-picker-friendly {:cljs-date? true + :type "date" :field [:date]}])]] [:div.level-item [:div.control @@ -176,8 +224,9 @@ [:div.control [:p.help "Comparison Date"] (raw-field - [date-picker-friendly {:type "date" - :field [:comparison-date]}])])]] + [date-picker-friendly {:cljs-date? true + :type "date" + :field [:comparison-date]}])])]] [:div.level-right [:div.buttons @@ -196,18 +245,14 @@ (assoc b :period (:comparison-date args) :amount (js/parseFloat (:amount b))))))) - client-names (->> @(re-frame/subscribe [::subs/clients-by-id]) (map (fn [[k v]] [k (:name v)])) (into {})) pnl-data (l-reports/->PNLData args pnl-data client-names) report (l-reports/summarize-balance-sheet pnl-data)] - [rtable/table {:widths (into [30 13 - (when (:include-comparison args) - 13) - (when (:include-comparison args) - 13)]) + [rtable/table {:widths (cond-> [30 13] + (:include-comparison args) (into [13 13])) :click-event ::investigate-clicked :table report}])) @@ -220,7 +265,6 @@ [status/status-notification {:statuses [[::status/single ::page]]}] [report-form] - [status/big-loader status] (when (and (not= :loading (:state status)) report) diff --git a/src/cljs/auto_ap/views/pages/ledger/profit_and_loss.cljs b/src/cljs/auto_ap/views/pages/ledger/profit_and_loss.cljs index a27e5fa8..d10f686c 100644 --- a/src/cljs/auto_ap/views/pages/ledger/profit_and_loss.cljs +++ b/src/cljs/auto_ap/views/pages/ledger/profit_and_loss.cljs @@ -5,7 +5,6 @@ [auto-ap.status :as status] [auto-ap.subs :as subs] [auto-ap.views.pages.ledger.report-table :as rtable] - [auto-ap.utils :refer [dollars-0?]] [auto-ap.views.components.buttons :as buttons] [auto-ap.views.components.layouts :refer [appearing-side-bar side-bar-layout]] @@ -18,8 +17,6 @@ [auto-ap.views.utils :refer [date->str date-picker-friendly - ->$ - ->% dispatch-event local-today multi-field @@ -71,7 +68,7 @@ (re-frame/reg-event-fx ::report-requested [with-user (forms/in-form ::form)] - (fn [{:keys [db user] :as cofx}] + (fn [{:keys [db user]}] (cond-> {:graphql {:token user :owns-state {:single ::page} :query-obj {:venia/queries [[:profit-and-loss @@ -138,7 +135,7 @@ NOTE: Please review the transactions we may have question for you here: https:// (re-frame/reg-event-db ::change-internal (forms/change-handler ::form - (fn [data field value] + (fn [_ field value] (cond (= [:periods] field) [field value] @@ -206,8 +203,7 @@ NOTE: Please review the transactions we may have question for you here: https:// (re-frame/reg-event-fx ::investigate-clicked (fn [{:keys [db]} [_ {:keys [location from-numeric-code to-numeric-code client-id] - {:keys [start end]} :date-range - :as g}]] + {:keys [start end]} :date-range}]] {:db (-> db (assoc ::ledger-list-active? true)) :dispatch [::data-page/additional-params-changed ::ledger {:client-id client-id :from-numeric-code from-numeric-code @@ -219,7 +215,7 @@ NOTE: Please review the transactions we may have question for you here: https:// (re-frame/reg-sub ::can-submit - (fn [db] + (fn [_] true)) (def pnl-form (forms/vertical-form {:can-submit [::can-submit] @@ -237,12 +233,12 @@ NOTE: Please review the transactions we may have question for you here: https:// ]) @box))) -(defn report-controls [pnl-form] +(defn report-controls [_] (let [!box (reagent/atom nil) active (reagent/atom nil)] (fn [pnl-form] - (let [{:keys [form-inline field raw-field error-notification submit-button ]} pnl-form - {:keys [data report active? error id]} @(re-frame/subscribe [::forms/form ::form]) + (let [{:keys [raw-field]} pnl-form + {:keys [data]} @(re-frame/subscribe [::forms/form ::form]) {:keys [periods selected-period include-deltas]} data] [:div.report-controls [:div.level.mb-2 diff --git a/src/cljs/auto_ap/views/utils.cljs b/src/cljs/auto_ap/views/utils.cljs index 12b29232..a741bdb4 100644 --- a/src/cljs/auto_ap/views/utils.cljs +++ b/src/cljs/auto_ap/views/utils.cljs @@ -100,6 +100,13 @@ e (date->str (time/from-default-time-zone (c/from-date e)) standard)))))) +(defn dispatch-cljs-date-change [event] + (fn [e g] + (re-frame/dispatch (conj event + (if (str/blank? e) + e + (time/from-default-time-zone (c/from-date e))))))) + ;; TODO inline on-changes causes each field to be rerendered each time. When we fix this ;; let's make sure that we find away not to trigger a re-render for every component any time any form field ;; changes @@ -305,10 +312,10 @@ (let [field (if (keyword? field) [field] field) event (if (keyword? event) [event] event) selected (get-in subscription field) + selected (cond (string? selected) (c/to-date (time/to-default-time-zone (time/from-default-time-zone (str->date selected standard)))) - (instance? goog.date.DateTime selected) (c/to-date (time/to-default-time-zone (time/from-default-time-zone selected))) @@ -317,9 +324,10 @@ :else selected ) - keys (assoc keys - :on-change (dispatch-date-change (conj event field)) + :on-change (when (:cljs-date? keys) + (dispatch-date-change (conj event field)) + (dispatch-cljs-date-change (conj event field))) :selected selected :class (str class (when (and spec (not (s/valid? spec (get-in subscription field)))) @@ -451,8 +459,6 @@ :next-month-label "" :type "date")]) - - (defn local-now [] (t/to-default-time-zone (t/now)))