Advertisement
Kyi_Htoo_Aung

Get Duplicated EPin

Apr 24th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. def get_duplicated_epin() do
  2. Temp.track!()
  3. {:ok, _pd, file} = Temp.open(%{prefix: "Getrecharge_duplicate_stock", suffix: "sales.csv"})
  4. key = ["buying_price", "csv_id", "inserted_at", "order_id","pin", "product_name","provider", "provider_order_id","sale_type", "serial_number", "sold_date"]
  5. query = from(
  6. s in Sales,
  7. where: s.salestype == "epin" and s.status == "success",
  8. select: s.epinstock_id
  9. )
  10.  
  11. list =
  12. Repo.all(query)
  13. |> Enum.group_by(&(&1))
  14. |> Enum.filter(fn {_, [_,_|_]} -> true; _ -> false end)
  15. |> Enum.map(fn {x, _} -> x end)
  16. |> Enum.map(fn x ->
  17.  
  18. query = from(
  19. s in Sales,
  20. where: s.salestype == "epin" and s.status == "success" and s.epinstock_id == ^x,
  21. join: e in Epinstocks,
  22. where: s.epinstock_id == e.id,
  23. join: p in Products,
  24. where: s.product_id == p.id,
  25. select: %{
  26. :buying_price => s.buying_price,
  27. :csv_id => e.csvfile,
  28. :inserted_at => s.inserted_at,
  29. :order_id => s.merchant_order_id,
  30. :pin => e.pin,
  31. :product_name => p.name,
  32. :provider => s.provider,
  33. :provider_order_id => e.batch,
  34. :sale_type => s.salestype,
  35. :serial_number => s.epin_serial,
  36. :sold_date => s.updated_at,
  37. }
  38. )
  39. Repo.all(query)
  40. end )
  41.  
  42. list
  43. |> List.flatten()
  44. |> Enum.map(fn x -> Map.values(QueryHelpers.get_purchase_date(x)) end)
  45. |> List.insert_at(0, key)
  46. |> CSV.encode()
  47. |> Enum.each(&File.write(file, &1, [:append]))
  48.  
  49. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement