Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. def compare_charge_items_and_receipt_items(filepath):
  2. import getaround
  3. from getaround.core import models
  4. import pg8000
  5.  
  6. """For each Rental the Receipt price, the Receipt Item prices, and the Charge Item amounts should all be equal"""
  7.  
  8. conn = pg8000.connect(database="getaround", host="104.155.175.194",
  9. password="g3t@r0und!", port=5432,
  10. user="getaround", ssl=True)
  11.  
  12. curs = conn.cursor()
  13.  
  14. sql = """
  15. SELECT rental.id AS rental_id,
  16. rental.receipt_price_rental AS rental_price,
  17. sum(receipt_item.price - coalesce(receipt_item.discount_amount, 0)
  18. - coalesce(passthrough_fee_item.price, 0)) as sum_receipt_items,
  19.  
  20. sum(coalesce(charge_item.amount,0)) as sum_charge_items,
  21.  
  22. sum(receipt_item.price - coalesce(receipt_item.discount_amount, 0)
  23. - coalesce(passthrough_fee_item.price, 0))
  24. - sum(coalesce(charge_item.amount,0)) as diff
  25. -- these columns should be updated if we fix the issue in #5487
  26. FROM public.rental rental
  27. JOIN public.receipt_item receipt_item ON receipt_item.rental_id = rental.id
  28. LEFT JOIN public.receipt_item passthrough_fee_item ON receipt_item.passthrough_fee_id = passthrough_fee_item.id
  29. LEFT JOIN stripe.charge_item charge_item ON receipt_item.charge_item_id = charge_item.id
  30. LEFT JOIN stripe.charge_item fee_charge_item ON passthrough_fee_item.charge_item_id = fee_charge_item.id
  31.  
  32. WHERE rental.created > '2015/06/01'
  33. AND rental.created < current_timestamp - INTERVAL '1 DAYS'
  34. AND rental.status = 'ACTIVE'
  35. AND receipt_item.canceled = FALSE
  36. GROUP BY rental.id
  37. ORDER BY diff DESC;
  38. """
  39. curs.execute(sql)
  40. results = curs.fetchall()
  41.  
  42. out = open(filepath, "a+")
  43.  
  44. for rental_id, rental_price, sum_receipt_items, sum_charge_items, diff in results:
  45. if diff > 0:
  46. rental = models.Rental.get_by(id=long(rental_id))
  47. request = rental.request
  48. auths = models.Authorization.get(request.authorization_keys)
  49. for auth in auths:
  50. if 'in_' in auth.external_id:
  51. invoice = stripe_facade.retrieve_invoice(auth.external_id)
  52. for stmt in _convert_invoice(invoice):
  53. out.write(stmt.encode("utf-8")+"\n")
  54. print stmt
  55. elif 'ch_' in auth.external_id:
  56. charge = stripe_facade.retrieve_charge(auth.external_id)
  57. for stmt in _convert_charge(charge):
  58. out.write(stmt.encode("utf-8")+"\n")
  59. print stmt
  60. elif 'ii_' in auth.external_id:
  61. invoice_item = stripe_facade.retrieve_invoice_item(auth.external_id)
  62. for stmt in _convert_invoice_item(invoice_item):
  63. out.write(stmt.encode("utf-8")+"\n")
  64. print stmt
  65.  
  66. else:
  67. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement