(println "hi")

(d/pull (d/db conn) '[*] 17592279036264)

{:sales-order/client #:db{:id 17592244691542},
 :sales-order/location "CD",
 :sales-order/external-id
 "ezcater/order/NGCD-CD-98690c6c-ca84-49f7-b1d3-96b3fcf68b24",
 :sales-order/discount -52.37,
 :sales-order/vendor #:db{:id 17592273052793},
 :sales-order/returns 0.0,
 :sales-order/date #inst "2022-11-21T19:30:00.000-00:00",
 :sales-order/service-charge 46.83,
 :db/id 17592279036264,
 :sales-order/charges
 [{:charge/total 274.88,
   :charge/processor #:db{:id 17592274224064},
   :charge/type-name "CARD",
   :charge/external-id
   "ezcater/charge/98690c6c-ca84-49f7-b1d3-96b3fcf68b24",
   :charge/location "CD",
   :charge/date #inst "2022-11-21T19:30:00.000-00:00",
   :db/id 17592279036265,
   :charge/client #:db{:id 17592244691542},
   :charge/tip 23.05}],
 :sales-order/reference-link
 "https://ezmanage.ezcater.com/orders/98690c6c-ca84-49f7-b1d3-96b3fcf68b24",
 :sales-order/line-items
 [{:db/id 17592279036266,
   :order-line-item/item-name "EZCater Catering",
   :order-line-item/total 274.88,
   :order-line-item/tax 21.330000000000002,
   :order-line-item/category "EZCater Catering",
   :order-line-item/discount -52.37,
   :order-line-item/external-id
   "ezcater/order/NGCD-CD-98690c6c-ca84-49f7-b1d3-96b3fcf68b24-0"}],
 :sales-order/tip 23.05,
 :sales-order/tax 21.330000000000002,
 :sales-order/total 274.88}


(def target-order (lookup-order {"id" "bf3dcf5c-a68f-42d9-9084-049133e03d3d", "parent_type" "Caterer", "parent_id" "91541331-d7ae-4634-9e8b-ccbbcfb2ce70", "entity_type" "Order", "entity_id" "98690c6c-ca84-49f7-b1d3-96b3fcf68b24", "key" "accepted", "occurred_at" "2022-07-21T19:21:07.549Z"}))

(def ezcater-orders )

ezcater-orders

(first ezcater-orders)

(d/pull (d/db conn) '[{:client/ezcater-locations [* {:ezcater-location/caterer [*]}]}] [:client/code "NGCD"])

#:client{:ezcater-locations
         [{:db/id 17592275985819,
           :ezcater-location/location "CD",
           :ezcater-location/caterer
           {:db/id 17592267152223,
            :ezcater-caterer/uuid
            "eca924c1-1008-4e9c-9e74-8f344d079e7a",
            :ezcater-caterer/name "Nick The Greek (2075 Diamond Blvd)",
            :ezcater-caterer/search-terms
            "Nick The Greek 2075 Diamond Blvd"}}]}


#:client{:ezcater-locations
         [{:db/id 17592275985819,
           :ezcater-location/location "CD",
           :ezcater-location/caterer #:db{:id 17592267152223}}]}


(def all-ezcater-orders 
  (doall (for [[order uuid] (d/q '[:find ?eid ?uuid
                                   :in $
                                   :where [?e :sales-order/vendor :vendor/ccp-ezcater]
                                   [?e :sales-order/date ?d]
                                   [(ground (clj-time.coerce/to-date #inst "2022-11-28T00:00:00-08:00")) ?d2]
                                   [(>= ?d ?d2)]
                                   [?e :sales-order/external-id ?eid]
                                   [?e :sales-order/client ?c]
                                   [?c :client/ezcater-locations ?l]
                                   [?l :ezcater-location/caterer ?c2]
                                   [?c2 :ezcater-caterer/uuid ?uuid]]
                                 (d/db conn)) 
               :let         [id (last (clojure.string/split order #"/"))
                             id (clojure.string/join "-" (drop 2 (clojure.string/split order #"-")))
                             lookup-map {"id" "bf3dcf5c-a68f-42d9-9084-049133e03d3d", "parent_type" "Caterer", "parent_id" uuid, "entity_type" "Order", "entity_id" id, "key" "accepted", "occurred_at" "2022-07-21T19:21:07.549Z"}
                             ezcater-order (lookup-order lookup-map)
                             extant-order (d/pull (d/db conn) '[:sales-order/total
                                                                :sales-order/tax
                                                                :sales-order/tip
                                                                :sales-order/discount
                                                                :sales-order/external-id
                                                                {:sales-order/charges [:charge/tax
                                                                                       :charge/tip
                                                                                       :charge/total
                                                                                       :charge/external-id]}]
                                                  [:sales-order/external-id order])

                             updated-order (-> (order->sales-order ezcater-order)
                                               (select-keys
                                                 #{:sales-order/total
                                                   :sales-order/tax
                                                   :sales-order/tip
                                                   :sales-order/discount
                                                   :sales-order/charges
                                                   :sales-order/external-id})
                                               (update :sales-order/charges (fn [c]
                                                                              (map #(select-keys % #{:charge/tax :charge/tip :charge/total
                                                                                                     :charge/external-id}) c))))]

               :when (not= updated-order extant-order)

               ]

           updated-order
           )))

#'auto-ap.ezcater.core/all-ezcater-orders


#'auto-ap.ezcater.core/target-order


(order->sales-order (first all-ezcater-orders))

#:sales-order{:vendor :vendor/ccp-ezcater,
              :service-charge 46.83,
              :date #clj-time/date-time "2022-11-21T19:30:00.000Z",
              :reference-link
              "https://ezmanage.ezcater.com/orders/98690c6c-ca84-49f7-b1d3-96b3fcf68b24",
              :charges
              [#:charge{:type-name "CARD",
                        :date
                        #clj-time/date-time "2022-11-21T19:30:00.000Z",
                        :client [:client/code nil],
                        :location nil,
                        :external-id
                        "ezcater/charge/98690c6c-ca84-49f7-b1d3-96b3fcf68b24",
                        :processor :ccp-processor/ezcater,
                        :total 251.83,
                        :tip 0.0}],
              :client [:client/code nil],
              :tip 0.0,
              :tax 21.330000000000002,
              :external-id
              "ezcater/order/--98690c6c-ca84-49f7-b1d3-96b3fcf68b24",
              :total 251.83,
              :line-items
              [#:order-line-item{:external-id
                                 "ezcater/order/--98690c6c-ca84-49f7-b1d3-96b3fcf68b24-0",
                                 :item-name "EZCater Catering",
                                 :category "EZCater Catering",
                                 :discount 0.68,
                                 :tax 21.330000000000002,
                                 :total 251.83}],
              :discount 0.68,
              :location nil,
              :returns 0.0}

(ccp-fee target-order)

9.81


(as->  a
  [a (order->sales-order a)])

[{:uuid "8f7b7aa6-c48d-44f9-9be6-037cf68b65e9",
  :orderSourceType "MARKETPLACE",
  :caterer
  {:name "Nick The Greek",
   :uuid "eca924c1-1008-4e9c-9e74-8f344d079e7a",
   :address {:street "2075 Diamond Blvd"}},
  :event
  {:timestamp "2022-11-10T19:30:00Z",
   :catererHandoffFoodTime "2022-11-10T19:00:00Z",
   :orderType "THIRD_PARTY_DELIVERY"},
  :catererCart
  {:orderItems
   [{:name "Olympus Package",
     :quantity 13,
     :posItemId nil,
     :totalInSubunits {:currency "USD", :subunits 29835}}],
   :totals {:catererTotalDue 266.77},
   :feesAndDiscounts [{:cost {:currency "USD", :subunits 3000}}]},
  :totals
  {:customerTotalDue {:currency "USD", :subunits 39669},
   :pointOfSaleIntegrationFee {:currency "USD", :subunits 0},
   :tip {:currency "USD", :subunits 0},
   :salesTax {:currency "USD", :subunits 2834},
   :salesTaxRemittance {:currency "USD", :subunits 0},
   :subTotal {:currency "USD", :subunits 29835}},
  :client-code nil,
  :client-location nil}
 #:sales-order{:vendor :vendor/ccp-ezcater,
               :service-charge 59.06,
               :date #clj-time/date-time "2022-11-10T19:30:00.000Z",
               :reference-link
               "https://ezmanage.ezcater.com/orders/8f7b7aa6-c48d-44f9-9be6-037cf68b65e9",
               :charges
               [#:charge{:type-name "CARD",
                         :date
                         #clj-time/date-time "2022-11-10T19:30:00.000Z",
                         :client [:client/code nil],
                         :location nil,
                         :external-id
                         "ezcater/charge/8f7b7aa6-c48d-44f9-9be6-037cf68b65e9",
                         :processor :ccp-processor/ezcater,
                         :total 326.69,
                         :tip 0.0}],
               :client [:client/code nil],
               :tip 0.0,
               :tax 28.34,
               :external-id
               "ezcater/order/--8f7b7aa6-c48d-44f9-9be6-037cf68b65e9",
               :total 326.69,
               :line-items
               [#:order-line-item{:external-id
                                  "ezcater/order/--8f7b7aa6-c48d-44f9-9be6-037cf68b65e9-0",
                                  :item-name "EZCater Catering",
                                  :category "EZCater Catering",
                                  :discount 0.86,
                                  :tax 28.34,
                                  :total 326.69}],
               :discount 0.86,
               :location nil,
               :returns 0.0}]


{:uuid "8f7b7aa6-c48d-44f9-9be6-037cf68b65e9",
 :orderSourceType "MARKETPLACE",
 :caterer
 {:name "Nick The Greek",
  :uuid "eca924c1-1008-4e9c-9e74-8f344d079e7a",
  :address {:street "2075 Diamond Blvd"}},
 :event
 {:timestamp "2022-11-10T19:30:00Z",
  :catererHandoffFoodTime "2022-11-10T19:00:00Z",
  :orderType "THIRD_PARTY_DELIVERY"},
 :catererCart
 {:orderItems
  [{:name "Olympus Package",
    :quantity 13,
    :posItemId nil,
    :totalInSubunits {:currency "USD", :subunits 29835}}],
  :totals {:catererTotalDue 266.77},
  :feesAndDiscounts [{:cost {:currency "USD", :subunits 3000}}]},
 :totals
 {:customerTotalDue {:currency "USD", :subunits 39669},
  :pointOfSaleIntegrationFee {:currency "USD", :subunits 0},
  :tip {:currency "USD", :subunits 0},
  :salesTax {:currency "USD", :subunits 2834},
  :salesTaxRemittance {:currency "USD", :subunits 0},
  :subTotal {:currency "USD", :subunits 29835}},
 :client-code nil,
 :client-location nil}

