diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index 45a56c8f..87b03322 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -1056,6 +1056,10 @@ :args {:invoice_id {:type :id}} :resolve :mutation/unvoid-invoice} + :unautopay_invoice {:type :invoice + :args {:invoice_id {:type :id}} + :resolve :mutation/unautopay-invoice} + :void_payment {:type :payment :args {:payment_id {:type :id}} :resolve :mutation/void-payment} @@ -1343,6 +1347,7 @@ :mutation/merge-vendors gq-vendors/merge-vendors :mutation/void-invoice gq-invoices/void-invoice :mutation/unvoid-invoice gq-invoices/unvoid-invoice + :mutation/unautopay-invoice gq-invoices/unautopay-invoice :mutation/void-payment gq-checks/void-check :mutation/edit-expense-accounts gq-invoices/edit-expense-accounts :mutation/import-ledger gq-ledger/import-ledger diff --git a/src/clj/auto_ap/graphql/invoices.clj b/src/clj/auto_ap/graphql/invoices.clj index e9706f2c..93cad210 100644 --- a/src/clj/auto_ap/graphql/invoices.clj +++ b/src/clj/auto_ap/graphql/invoices.clj @@ -239,6 +239,18 @@ (-> (d-invoices/get-by-id id) (->graphql)))) +(defn unautopay-invoice [context {id :invoice_id} value] + (let [invoice (d/entity (d/db conn) id) + _ (assert (:invoice/client invoice)) + _ (assert-can-see-client (:id context) (:db/id (:invoice/client invoice)))] + (audit-transact [[:db/add id :invoice/status :invoice-status/unpaid] + [:db/add id :invoice/outstanding-balance (:invoice/total invoice)] + [:db/retract id :invoice/scheduled-payment (:invoice/scheduled-payment invoice)]] + (:id context)) + + (-> (d-invoices/get-by-id id) + (->graphql)))) + (defn edit-expense-accounts [context args value] (assert-can-see-client (:id context) (:db/id (:invoice/client (d-invoices/get-by-id (:invoice_id args))))) (let [invoice-id (:invoice_id args) diff --git a/src/clj/auto_ap/square/core.clj b/src/clj/auto_ap/square/core.clj index 63debfa4..a1bac333 100644 --- a/src/clj/auto_ap/square/core.clj +++ b/src/clj/auto_ap/square/core.clj @@ -128,6 +128,19 @@ result))) +(defn order + + ([o] + (log/info "Searching for" o) + (let [result (->> (client/get (str "https://connect.squareup.com/v2/orders/" o) + {:headers {"Square-Version" "2020-08-12" + "Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7" + "Content-Type" "application/json"} + :as :json}) + :body)] + + (log/info "found " (count result)) + result))) (defn amount->money [amt] (* 0.01 (or (:amount amt) 0.0))) @@ -150,7 +163,8 @@ "LQTHXQY69MYB6" ["NGDG" "DB"] "L7S9MXZBJ00HY" ["NGGG" "LM"] "LRC7WVD77ZM81" ["NGLK" "SM"] - "FZ3ZYC77T3W1T" ["NGA1" "KA"]} location)) + "FZ3ZYC77T3W1T" ["NGA1" "KA"] + "LG5X0MHA4NZDM" ["NGSM" "SM"]} location)) ;; to get totals: (comment @@ -190,8 +204,8 @@ :returns (+ (- (-> order :return_amounts :total_money amount->money) (-> order :return_amounts :tax_money amount->money) (-> order :return_amounts :tip_money amount->money) - (-> order :return_amounts :service_charge_money amount->money) - (-> order :return_amounts :discount_money amount->money))) + (-> order :return_amounts :service_charge_money amount->money)) + (-> order :return_amounts :discount_money amount->money)) :charges (->> (:tenders order) (map (fn [t] (remove-nils @@ -217,7 +231,9 @@ (remove-nils #:order-line-item {:item-name (:name li) - :category (item-id->category-name (:catalog_object_id li)) + :category (if (= "GIFT_CARD" (:item_type li)) + "Gift Card" + (item-id->category-name (:catalog_object_id li))) :total (amount->money (:total_money li)) :tax (amount->money (:total_tax_money li)) :discount (amount->money (:total_discount_money li))}))))})))))) @@ -263,10 +279,12 @@ (if (> try-count 4) false true))})))] n)))))) -(defn daily-settlements [] - (->> (locations) - (map :id) - (filter location_id->client-location) +(defn daily-settlements + ([] (daily-settlements (->> (locations) + (map :id) + (filter location_id->client-location)))) + ([location-ids] + (->> location-ids (mapcat (fn [l] (for [settlement (settlements l) :let [[client loc] (location_id->client-location l)]] @@ -274,13 +292,15 @@ :total (amount->money (:total_money settlement)) :client [:client/code client] :location loc - :fee (- (reduce + 0 (map (fn [entry] - (if (= (:type entry) "REFUND") - (- (amount->money (:fee_money entry))) - (amount->money (:fee_money entry)))) - (:entries settlement)))) + :fee (- (reduce + 0.0 (map (fn [entry] + (if (= (:type entry) "REFUND") + (- (amount->money (:fee_money entry))) + (amount->money (:fee_money entry)))) + (:entries settlement)))) :date (-> (:initiated_at settlement) - (coerce/to-date))}))))) + (coerce/to-date))}))) + (filter :expected-deposit/date))) + ) (defn refunds [l] (let [refunds (:refunds (:body (client/get (str "https://connect.squareup.com/v2/refunds?location_id=" l) @@ -329,24 +349,28 @@ (catch Exception e (log/error e)))))) -(defn upsert-settlements [] - (lc/with-context {:source "Square settlements loading "} - (try - (let [existing (->> (d/query {:query {:find ['?external-id] - :in ['$] - :where ['[_ :expected-deposit/external-id ?external-id]]} - :args [(d/db conn)]}) - (map first) - set) - _ (log/info (count existing) "settlements already exist") - to-create (filter #(not (existing (:expected-deposit/external-id %))) - (daily-settlements))] - (doseq [x (partition-all 20 to-create)] - (log/info "Loading expected deposit" (count x)) - @(d/transact conn x))) - (catch Exception e - (log/error e))) - (log/info "Done loading settlements"))) +(defn upsert-settlements + ([] (upsert-settlements nil)) + ([location-ids] + (lc/with-context {:source "Square settlements loading "} + (try + (let [existing (->> (d/query {:query {:find ['?external-id] + :in ['$] + :where ['[_ :expected-deposit/external-id ?external-id]]} + :args [(d/db conn)]}) + (map first) + set) + _ (log/info (count existing) "settlements already exist") + to-create (filter #(not (existing (:expected-deposit/external-id %))) + (if location-ids + (daily-settlements location-ids) + (daily-settlements)))] + (doseq [x (partition-all 20 to-create)] + (log/info "Loading expected deposit" (count x)) + @(d/transact conn x))) + (catch Exception e + (log/error e))) + (log/info "Done loading settlements")))) (defn upsert-refunds [] (doseq [{location :id} (locations)] diff --git a/src/cljs/auto_ap/views/components/invoice_table.cljs b/src/cljs/auto_ap/views/components/invoice_table.cljs index 15bb71f2..c979fec4 100644 --- a/src/cljs/auto_ap/views/components/invoice_table.cljs +++ b/src/cljs/auto_ap/views/components/invoice_table.cljs @@ -2,6 +2,7 @@ (:require [auto-ap.events :as events] [auto-ap.routes :as routes] [auto-ap.subs :as subs] + [auto-ap.status :as status] [auto-ap.views.components.buttons :as buttons] [auto-ap.views.components.dropdown :refer @@ -90,6 +91,21 @@ invoice-read]}]} :on-success (fn [result] [::invoice-updated (:unvoid-invoice result)])}})) +(re-frame/reg-event-fx + ::unautopay + (fn [{:keys [db]} [_ {id :id}]] + {:graphql + {:token (-> db :user) + :owns-state {:multi ::unautopay + :which id} + :query-obj {:venia/operation {:operation/type :mutation + :operation/name "UnautopayInvoice"} + + :venia/queries [{:query/data [:unautopay-invoice + {:invoice-id id} + invoice-read]}]} + :on-success (fn [result] + [::invoice-updated (:unautopay-invoice result)])}})) (re-frame/reg-event-fx ::invoice-updated @@ -99,6 +115,7 @@ (defn row [{:keys [invoice check-boxes selected-client overrides checkable? expense-event actions]}] (let [{:keys [client status payments expense-accounts invoice-number date due total outstanding-balance id vendor] :as i} invoice accounts-by-id @(re-frame/subscribe [::subs/accounts-by-id client]) + unautopay-states @(re-frame/subscribe [::status/multi ::unautopay]) account->name #(:name (accounts-by-id (:id %)))] [grid/row {:class (:class i) :id id :checkable? checkable? :entity invoice} (when-not selected-client @@ -121,7 +138,11 @@ [grid/cell {} (str/join ", " (set (map :location expense-accounts)))] [grid/cell {:class "has-text-right"} (nf total )] - [grid/cell {:class "has-text-right"} (nf outstanding-balance )] + [grid/cell {:class "has-text-right"} + (if (:scheduled-payment i) + [:<> [:div.tag.is-info.is-light "Autopay"] " "] + ) + (nf outstanding-balance )] [grid/button-cell {} [:div.buttons (when (seq expense-accounts) @@ -185,7 +206,13 @@ (when (and (get actions :void) (= ":voided" (:status i))) [buttons/fa-icon {:icon "fa-undo" - :event [::unvoid-invoice i]}])]]])) + :event [::unvoid-invoice i]}]) + (when (and (get actions :void) + (= ":paid" (:status i)) + (:scheduled-payment i)) + [buttons/fa-icon {:icon "fa-undo" + :class (status/class-for (get unautopay-states (:id i))) + :event [::unautopay i]}])]]])) (defn invoice-table [{:keys [id check-boxes overrides actions data-page checkable-fn]}] (let [selected-client @(re-frame/subscribe [::subs/client]) diff --git a/src/cljs/auto_ap/views/pages/transactions/form.cljs b/src/cljs/auto_ap/views/pages/transactions/form.cljs index 0f3963d7..6488f1e9 100644 --- a/src/cljs/auto_ap/views/pages/transactions/form.cljs +++ b/src/cljs/auto_ap/views/pages/transactions/form.cljs @@ -80,7 +80,7 @@ value (expense-accounts-field/can-replace-with-default? (:accounts data))) [[:accounts] (expense-accounts-field/default-account (:accounts data) - @(re-frame/subscribe [::subs/vendor-default-account value (:client data)]) + @(re-frame/subscribe [::subs/vendor-default-account (:id value) (:client data)]) (:amount data) locations)] []))))) @@ -414,15 +414,20 @@ :field [:vendor] :disabled (or (boolean (:payment data)) should-disable-for-client?)}]) - (field nil - [expense-accounts-field - {:type "expense-accounts" - :field [:accounts] - :max (Math/abs (js/parseFloat (:amount data))) - :descriptor "credit account" - :disabled (or (boolean (:payment data)) - should-disable-for-client?) - :locations locations}]) + (with-meta + (field nil + [expense-accounts-field + {:type "expense-accounts" + :field [:accounts] + :max (Math/abs (js/parseFloat (:amount data))) + :descriptor "credit account" + :disabled (or (boolean (:payment data)) + should-disable-for-client?) + :locations locations}]) + {:key (str (:id (:vendor data)))}) + + + diff --git a/taskdef.json b/taskdef.json index 1bccbf75..456cd8eb 100644 --- a/taskdef.json +++ b/taskdef.json @@ -92,5 +92,5 @@ "FARGATE" ], "cpu": "2048", - "memory": "4096" + "memory": "8192" } diff --git a/terraform/deploy.tf b/terraform/deploy.tf index 29f576ad..44d6d123 100644 --- a/terraform/deploy.tf +++ b/terraform/deploy.tf @@ -14,7 +14,7 @@ resource "aws_ecs_task_definition" "integreat_app" { family = "integreat_app_${var.stage}" container_definitions = file("${var.stage}-taskdef.json") - memory = 4096 + memory = 8192 cpu = 2048 network_mode = "awsvpc" requires_compatibilities = ["FARGATE"]