diff --git a/src/cljs/auto_ap/views/pages/transactions/form.cljs b/src/cljs/auto_ap/views/pages/transactions/form.cljs index 89584ec9..d7b57c4c 100644 --- a/src/cljs/auto_ap/views/pages/transactions/form.cljs +++ b/src/cljs/auto_ap/views/pages/transactions/form.cljs @@ -104,13 +104,22 @@ :on-error [::forms/save-error ::edit-transaction]}}))) (re-frame/reg-event-fx - ::change-account - (fn [{:keys [db]} [_ f a]] - (if-let [forced-location (and (= f [:account-id]) - (:location @(re-frame/subscribe [::subs/account a])))] - {:dispatch-n [[::forms/change ::edit-transaction f a] - [::forms/change ::edit-transaction [:location] forced-location]]} - {:dispatch [::forms/change ::edit-transaction f a]}))) + ::change-vendor + [(forms/in-form ::edit-transaction)] + (fn [{{:keys [data]} :db} [_ field value]] + (let [has-only-one-expense-account? (and value + (or (not (seq (:accounts data))) + (<= 1 (count (:accounts data)))) + (not (get-in data [:accounts 0 :account :id])))] + (if has-only-one-expense-account? + {:dispatch [::forms/change ::edit-transaction + field value + [:accounts] [{:id (str "new-" (random-uuid)) + :amount (Math/abs (:amount data)) + :amount-percentage 100 + :amount-mode "%" + :account @(re-frame/subscribe [::subs/vendor-default-account value])}]]} + {:dispatch [::forms/change ::edit-transaction field value]})))) (re-frame/reg-event-db ::manual-match @@ -195,9 +204,8 @@ :type "typeahead" :auto-focus true :field [:vendor-id] - :text-field [:vendor-name] :disabled (boolean (:payment data)) - :event change-event + :event [::change-vendor] :subscription data}]]]]