(ns fix-incorrect-locations)


(count 
 (d/query {:query {:find ['?e]
                   :in ['$]
                   :where ['[?e :journal-entry/line-items ?jel]
                           '[?jel :journal-entry-line/account ?a]
                           '[?a :account/location "A"]
                           '(not [?jel :journal-entry-line/location "A"])
                           ]}
           :args [(d/db auto-ap.datomic/conn)]}))



#_(->> (d/query {:query {:find ['?ea]
                       :in ['$]
                       :where ['[?e :journal-entry/line-items ?jel]
                               '[?jel :journal-entry-line/account ?a]
                               '[?a :account/location "A"]
                               '(not [?jel :journal-entry-line/location "A"])
                               '[?e :journal-entry/original-entity ?i]
                               '[?i :invoice/invoice-number]
                               '[?i :invoice/expense-accounts ?ea]
                               '[?ea :invoice-expense-account/account ?a]
                               '(not [?ea :invoice-expense-account/location "A"])
                               ]}
               :args [(d/db auto-ap.datomic/conn)]})
     (map (fn [[ea]]
            {:db/id ea
             :invoice-expense-account/location "A"}))
     (d/transact auto-ap.datomic/conn)
     deref)


#_(count 
 (->> 
  (d/query {:query {:find ['?ta]
                    :in ['$]
                    :where ['[?e :journal-entry/line-items ?jel]
                            '[?jel :journal-entry-line/account ?a]
                            '[?a :account/location "A"]
                            '(not [?jel :journal-entry-line/location "A"])
                            '[?e :journal-entry/original-entity ?t]
                            '[?t :transaction/accounts ?ta]
                            '[?ta :transaction-account/account ?a]
                            '(not [?ta :transaction-account/location "A"]) 
                            ]}
            :args [(d/db auto-ap.datomic/conn)]})
  (map (fn [[ea]]
         {:db/id ea
          :transaction-account/location "A"}))
  (d/transact auto-ap.datomic/conn)
  deref))

#_(count 
 (->> 
  (d/query {:query {:find ['?jel]
                    :in ['$]
                    :where ['[?e :journal-entry/line-items ?jel]
                            '[?jel :journal-entry-line/account ?a]
                            '[?a :account/location "A"]
                            '(not [?jel :journal-entry-line/location "A"])
                            '[?e :journal-entry/external-id]
                            ]}
            :args [(d/db auto-ap.datomic/conn)]})
  (map (fn [[ea]]
         {:db/id ea
          :journal-entry-line/location "A"}))
  (d/transact auto-ap.datomic/conn)
  deref))
