started on expense accounts.

This commit is contained in:
Bryce Covert
2018-05-31 18:43:12 -07:00
parent d38059cb1d
commit b98d00a196
14 changed files with 128 additions and 12 deletions

View File

@@ -0,0 +1,31 @@
(ns auto-ap.db.invoices-expense-accounts
(:require [honeysql.core :as sql]
[clojure.java.jdbc :as j]
[auto-ap.db.utils :refer [clj->db kebab->snake db->clj get-conn query] :as utils]
[honeysql.helpers :as helpers]))
(defn get-missing []
(query {:select [:i.id :v.default-expense-account :i.total]
:from [[:invoices :i]]
:join [[:vendors :v]
[:= :v.id :i.vendor-id]]
:left-join [[:invoices-expense-accounts :nonexist]
[:= :i.id :nonexist.invoice-id]]
:where [:and [:= :nonexist.id nil]
[:not= :v.default-expense-account nil]] }))
(defn get-for-invoice [id]
(query {:select [:*]
:from [:invoices-expense-accounts]
:where [:= :invoice-id id]}))
(defn assign-defaults! []
(j/db-do-prepared (get-conn)
(sql/format {:insert-into [[:invoices-expense-accounts [:invoice-id :expense-account-id :amount]]
{:select [:i.id :v.default-expense-account :i.total]
:from [[:invoices :i]]
:join [[:vendors :v]
[:= :v.id :i.vendor-id]]
:left-join [[:invoices-expense-accounts :nonexist]
[:= :i.id :nonexist.invoice-id]]
:where [:and [:= :nonexist.id nil]
[:not= :v.default-expense-account nil]] }] })))

View File

@@ -22,6 +22,7 @@
:secondary-contact
:secondary-email
:secondary-phone
:default-expense-account
:data])
(defn unparse [x]

View File

@@ -13,6 +13,7 @@
[auto-ap.db.checks :as checks]
[auto-ap.routes.checks :as rchecks]
[auto-ap.graphql.users :as gq-users]
[auto-ap.graphql.expense-accounts :as expense-accounts]
[auto-ap.graphql.invoices :as gq-invoices]
[auto-ap.db.reminders :as reminders]
[auto-ap.db.invoices-checks :as invoices-checks]
@@ -43,6 +44,7 @@
:vendor
{:fields {:id {:type 'Int}
:name {:type 'String}
:default_expense_account {:type 'Int}
:invoice_reminder_schedule {:type 'String}}}
:reminder
{:fields {:id {:type 'Int}
@@ -74,11 +76,26 @@
:companies {:type '(list :company)
:resolve :get-user-companies}}}
:expense_account {:fields {:id {:type 'Int}
:name {:type 'String}
:parent {:type :expense_account
:resolve :get-expense-account-parent}}}
:invoices_expense_accounts
{:fields {:id {:type 'Int}
:invoice_id {:type 'Int}
:expense_account_id {:type 'Int}
:expense_account {:type :expense_account
:resolve :get-expense-account}
:amount {:type 'String}}}
:invoice
{:fields {:id {:type 'Int}
:total {:type 'String}
:outstanding_balance {:type 'String}
:invoice_number {:type 'String}
:expense_accounts {:type '(list :invoices_expense_accounts)
:resolve :get-invoices-expense-accounts}
:date {:type 'String}
:company_id {:type 'Int}
:checks {:type '(list :invoice_check)
@@ -303,13 +320,16 @@
:get-company-for-invoice get-company-for-invoice
:get-invoices-checks get-invoices-checks
:get-check-by-id get-check-by-id
:get-invoices-expense-accounts gq-invoices/get-invoices-expense-accounts
:get-company get-company
:get-user get-user
:get-user-companies get-user-companies
:mutation/print-checks print-checks
:mutation/edit-user gq-users/edit-user
:mutation/add-invoice gq-invoices/add-invoice
:get-vendor get-vendor})
:get-vendor get-vendor
:get-expense-account expense-accounts/get-expense-account
:get-expense-account-parent expense-accounts/get-parent})
schema/compile))

View File

@@ -0,0 +1,9 @@
(ns auto-ap.graphql.expense-accounts
(:require [auto-ap.graphql.utils :refer [->graphql]]
[auto-ap.expense-accounts :as expense-accounts]))
(defn get-expense-account [context args value]
(->graphql (expense-accounts/expense-accounts (:expense_account_id value))))
(defn get-parent [context args value]
(->graphql (expense-accounts/expense-accounts (:parent value))))

View File

@@ -1,6 +1,7 @@
(ns auto-ap.graphql.invoices
(:require [auto-ap.graphql.utils :refer [->graphql]]
[auto-ap.db.invoices :as invoices]
[auto-ap.db.invoices-expense-accounts :as invoices-expense-accounts]
[auto-ap.time :refer [parse normal-date]]))
(defn add-invoice [context {{:keys [total invoice_number company_id vendor_id date] :as in} :invoice} value]
@@ -15,3 +16,8 @@
:date (parse date normal-date)}])
(first)
(->graphql)))
(defn get-invoices-expense-accounts [context args value]
(->graphql
(invoices-expense-accounts/get-for-invoice (:id value))))

View File

@@ -6,6 +6,7 @@
[auto-ap.utils :refer [by]]
[auto-ap.parse :as parse]
[auto-ap.routes.utils :refer [wrap-secure]]
[auto-ap.db.invoices-expense-accounts :as expense-accounts]
[compojure.core :refer [GET POST context defroutes
wrap-routes]]
[clojure.string :as str]))
@@ -138,7 +139,9 @@
inserted-row-count (invoices/upsert-multi! insert-rows)
already-imported-count (- (count insert-rows) inserted-row-count)
]
(expense-accounts/assign-defaults!)