Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.37 KB | None | 0 0
  1. def import_bookings
  2. file_path = "/home/amura/carcanet_booking_test.xls"
  3. file = File.open(file_path)
  4. #client = Client.find("54a7eb6ca7a03936ab000d89")
  5. client = Client.find("56a6097eaa8fea9c2200019e")
  6. book = Spreadsheet.open(file)
  7. field_order = {"0" => "lead_crm_id", "1" => "Booking name 1", "2" => "Booking Name 2", "3" => "Booking Name 3","4"=>"booking_date","5"=>"email id","6"=>"contact no","7"=>"project_unit_id","8"=>"project_unit_name","9"=>"primary_post_sales_email","10"=>"water_connection_due_after_days","11"=>"mseb_connection_due_after_days","12"=>"corpus_charges_due_after_days","13"=>"registration_due_after_days","14"=>"stamp_duty_due_after_days","15"=>"received amount","16"=>"agreement_amount_due_after_0_ WEEKS","17"=>"15/Aug","18"=>"30/Aug","19"=>"15/Sep","20"=>"30/Sep","21"=>"15/Oct","22"=>"30/Oct","23"=>"15/Nov","24"=>"30/Nov","25"=>"15/Dec","26"=>"30/Dec","27"=>"15/Jan","28"=>"30/Jan","29"=>"sum","30"=>"agreement cost"}
  8. count = 0
  9. additional_cost_names = {}
  10. sheet = book.worksheet 0
  11. sheet.each_with_index do |row,i|
  12. if i > 0
  13. lead_crm_id = row[field_order.key("lead_crm_id").to_i].to_s
  14.  
  15. lead_phone = row[field_order.key("contact no").to_i].to_s.strip
  16. lead_email = row[field_order.key("email id").to_i].to_s.strip.downcase
  17. booking_name1 = row[field_order.key("Booking name 1").to_i].to_s.strip rescue ""
  18. booking_name2 = row[field_order.key("Booking Name 2").to_i].to_s.strip rescue ""
  19. booking_name3 = row[field_order.key("Booking Name 3").to_i].to_s.strip rescue ""
  20. project_unit_id = row[field_order.key("project_unit_id").to_i].to_s.strip
  21. project_unit = ProjectUnit.find project_unit_id
  22. puts "unit found-------------", project_unit.name
  23. #project_tower_id = "576396ba3bb2f8a972000287" #name: 106 acre
  24. project_tower_id = "56a60be7aa8fea2ac7000281"
  25. project_tower = ProjectTower.find project_tower_id
  26. puts "tower name---------", project_tower.name
  27.  
  28. #project_id = "54c10aafa7a0395bc8000274" #name: Royal Purandar
  29. project_id ="56a60b97aa8feaba9d000272"
  30. project = Project.find project_id
  31. puts "proj name*************", project.name
  32.  
  33. #post_sales_user = client.users.find "568d19f3a7a0396a66000031" #user sonam
  34. post_sales_user = client.users.find "56fa7f01aa8fea29c0000021"
  35. puts "ps name*************", post_sales_user.name
  36.  
  37. start_date = Date.parse("01/08/2016")
  38. booking_date = Date.strptime(row[field_order.key("booking_date").to_i].to_s.strip, "%d.%m.%y")
  39. puts "booking date----------------", booking_date
  40.  
  41. #dl_template_id = "568f7adfa7a039e1700006f5" #Dl template 1
  42. dl_template_id = "56a60b98aa8feaba9d000274"
  43.  
  44. if lead_crm_id.present?
  45. lead_crm_id = lead_crm_id.split("#")[1].strip
  46. lead = client.leads.where(lead_id: lead_crm_id).first
  47. else
  48. lead = client.leads.new
  49. lead.primary_email = EmailAddress.new(email: lead_email) if lead_email.present?
  50. lead.lead_profile = LeadProfile.new()
  51.  
  52. # phone = PhoneNumberFormatter.format_number(lead_phone.strip) rescue nil
  53. # lead.secondary_phones = phone.ph_number
  54. # puts "lead phone****************8", lead.primary_phone
  55.  
  56. if booking_name1.present?
  57. name_array = booking_name1.split(" ")
  58. lead.first_name = name_array.first
  59. lead.last_name = name_array.last
  60. end
  61. lead.sales = client.sales.first
  62. lead.currently_in = lead.sales.department if lead.sales.present?
  63. puts "lead******************valid-----", lead.valid?
  64. lead.save!
  65. end
  66. puts "lead id****************", lead.id, lead.name
  67. if lead.present?
  68. puts "lead present*******************", lead.id
  69.  
  70. #create schedule
  71. payment_schedule = client.payment_schedules.new
  72. payment_schedule.payment_scheduler = project_tower
  73. payment_schedule.name = project_unit.name + "_schedule"
  74. payment_schedule.status = "approved"
  75. payment_schedule.floor_wise_type = "custom"
  76. payment_schedule.date_wise_schedule = true
  77. payment_schedule.schedule = []
  78.  
  79. water_date = start_date + row[field_order.key("water_connection_due_after_days").to_i].to_s.strip.to_i.weeks
  80. water_date = water_date.strftime("%d/%m/%Y")
  81.  
  82. mseb_date = start_date + row[field_order.key("mseb_connection_due_after_days").to_i].to_s.strip.to_i.weeks
  83. mseb_date = mseb_date.strftime("%d/%m/%Y")
  84.  
  85. corpus_date = start_date + row[field_order.key("corpus_charges_due_after_days").to_i].to_s.strip.to_i.weeks
  86. corpus_date = corpus_date.strftime("%d/%m/%Y")
  87.  
  88. reg_buffer = row[field_order.key("registration_due_after_days").to_i].to_s.strip.to_i
  89. if reg_buffer == -1
  90. reg_buffer = 0
  91. #create new recipt obj
  92. end
  93. registration_date = start_date + reg_buffer.weeks
  94. registration_date = registration_date.strftime("%d/%m/%Y")
  95.  
  96. stamp_duty_buffer = row[field_order.key("stamp_duty_due_after_days").to_i].to_s.strip.to_i
  97. if stamp_duty_buffer == -1
  98. stamp_duty_buffer = 0
  99. #create new receipt_object
  100. end
  101. stamp_duty_date = start_date + stamp_duty_buffer.weeks
  102. stamp_duty_date = stamp_duty_date.strftime("%d/%m/%Y")
  103.  
  104. payment_schedule.schedule << {"order"=>"1", "key"=>"#{water_date}", "op1"=>"100", "calculation_type"=>"calculate", "cost_type"=>"water_connection", "template_id"=> dl_template_id}
  105. payment_schedule.schedule << {"order"=>"2", "key"=>"#{mseb_date}", "op1"=>"100", "calculation_type"=>"calculate", "cost_type"=>"mseb_connection", "template_id"=> dl_template_id}
  106. payment_schedule.schedule << {"order"=>"3", "key"=>"#{corpus_date}", "op1"=>"100", "calculation_type"=>"calculate", "cost_type"=>"corpus_charges", "template_id"=> dl_template_id}
  107. payment_schedule.schedule << {"order"=>"4", "key"=>"#{registration_date}", "op1"=>"100", "calculation_type"=>"calculate", "cost_type"=>"registration_charges", "template_id"=> dl_template_id}
  108. payment_schedule.schedule << {"order"=>"5", "key"=>"#{stamp_duty_date}", "op1"=>"100", "calculation_type"=>"calculate", "cost_type"=>"stamp_duty", "template_id"=> dl_template_id}
  109. count = 6
  110. agreement_first_amount = row[field_order.key("agreement_amount_due_after_0_ WEEKS").to_i].to_s.strip
  111. aug_15 = row[field_order.key("15/Aug").to_i].to_s.strip
  112. aug_30 = row[field_order.key("30/Aug").to_i].to_s.strip
  113. sep_15 = row[field_order.key("15/Sep").to_i].to_s.strip
  114. sep_30 = row[field_order.key("30/Sep").to_i].to_s.strip
  115. oct_15 = row[field_order.key("15/Oct").to_i].to_s.strip
  116. oct_30 = row[field_order.key("30/Oct").to_i].to_s.strip
  117. nov_15 = row[field_order.key("15/Nov").to_i].to_s.strip
  118. nov_30 = row[field_order.key("30/Nov").to_i].to_s.strip
  119. dec_15 = row[field_order.key("15/Dec").to_i].to_s.strip
  120. dec_30 = row[field_order.key("30/Dec").to_i].to_s.strip
  121. jan_15 = row[field_order.key("15/Jan").to_i].to_s.strip
  122. jan_30 = row[field_order.key("30/Jan").to_i].to_s.strip
  123.  
  124. if agreement_first_amount.present?
  125. payment_schedule.schedule << {"order"=>"#{count}", "key"=>start_date.strftime("%d/%m/%Y"), "op1"=>agreement_first_amount, "calculation_type"=>"absolute", "cost_type"=>"agreement_value", "template_id"=> dl_template_id}
  126. count += 1
  127.  
  128. end
  129.  
  130. if aug_15.present?
  131. payment_schedule.schedule << {"order"=>"#{count}", "key"=>"15/08/2016", "op1"=>aug_15, "calculation_type"=>"absolute", "cost_type"=>"agreement_value", "template_id"=> dl_template_id}
  132. count += 1
  133. end
  134. if aug_30.present?
  135. payment_schedule.schedule << {"order"=>"#{count}", "key"=>"30/08/2016", "op1"=>aug_15, "calculation_type"=>"absolute", "cost_type"=>"agreement_value", "template_id"=> dl_template_id}
  136. count += 1
  137. end
  138.  
  139. if sep_15.present?
  140. payment_schedule.schedule << {"order"=>"#{count}", "key"=>"15/09/2016", "op1"=>sep_15, "calculation_type"=>"absolute", "cost_type"=>"agreement_value", "template_id"=> dl_template_id}
  141. count += 1
  142. end
  143. if sep_30.present?
  144. payment_schedule.schedule << {"order"=>"#{count}", "key"=>"30/09/2016", "op1"=>sep_30, "calculation_type"=>"absolute", "cost_type"=>"agreement_value", "template_id"=> dl_template_id}
  145. count += 1
  146. end
  147.  
  148. if oct_15.present?
  149. payment_schedule.schedule << {"order"=>"#{count}", "key"=>"15/10/2016", "op1"=>oct_15, "calculation_type"=>"absolute", "cost_type"=>"agreement_value", "template_id"=> dl_template_id}
  150. count += 1
  151. end
  152. if oct_30.present?
  153. payment_schedule.schedule << {"order"=>"#{count}", "key"=>"30/10/2016", "op1"=>oct_30, "calculation_type"=>"absolute", "cost_type"=>"agreement_value", "template_id"=> dl_template_id}
  154. count += 1
  155. end
  156.  
  157. if nov_15.present?
  158. payment_schedule.schedule << {"order"=>"#{count}", "key"=>"15/11/2016", "op1"=>nov_15, "calculation_type"=>"absolute", "cost_type"=>"agreement_value", "template_id"=> dl_template_id}
  159. count += 1
  160. end
  161. if nov_30.present?
  162. payment_schedule.schedule << {"order"=>"#{count}", "key"=>"30/11/2016", "op1"=>nov_30, "calculation_type"=>"absolute", "cost_type"=>"agreement_value", "template_id"=> dl_template_id}
  163. count += 1
  164. end
  165.  
  166. if dec_15.present?
  167. payment_schedule.schedule << {"order"=>"#{count}", "key"=>"15/12/2016", "op1"=>dec_15, "calculation_type"=>"absolute", "cost_type"=>"agreement_value", "template_id"=> dl_template_id}
  168. count += 1
  169. end
  170. if dec_30.present?
  171. payment_schedule.schedule << {"order"=>"#{count}", "key"=>"30/11/2016", "op1"=>dec_30, "calculation_type"=>"absolute", "cost_type"=>"agreement_value", "template_id"=> dl_template_id}
  172. count += 1
  173. end
  174.  
  175. if jan_15.present?
  176. payment_schedule.schedule << {"order"=>"#{count}", "key"=>"15/01/2017", "op1"=>jan_15, "calculation_type"=>"absolute", "cost_type"=>"agreement_value", "template_id"=> dl_template_id}
  177. count += 1
  178. end
  179. if jan_30.present?
  180. payment_schedule.schedule << {"order"=>"#{count}", "key"=>"30/01/2017", "op1"=>jan_30, "calculation_type"=>"absolute", "cost_type"=>"agreement_value", "template_id"=> dl_template_id}
  181. else
  182. payment_schedule.schedule << {"order"=>"#{count}", "key"=>"30/01/2017", "op1"=>"100", "calculation_type"=>"calculate", "cost_type"=>"agreement_value", "template_id"=> dl_template_id}
  183. end
  184. count += 1
  185. if payment_schedule.save
  186. names = []
  187. names << lead.name if lead.name.present?
  188. names << booking_name1 if booking_name1.present?
  189. names << booking_name2 if booking_name2.present?
  190. names << booking_name3 if booking_name3.present?
  191. names = names.flatten.compact
  192.  
  193. booking_detail = BookingDetail.new
  194. booking_detail.lead = lead
  195. booking_detail.client_id = lead.client_id
  196. booking_detail.sales_id = lead.sales.id
  197. booking_detail.primary_post_sales_id = post_sales_user.id
  198. booking_detail.email = lead.p_em.present? ? lead.p_em : lead_email
  199. booking_detail.phone = lead.p_ph.present? ? lead.p_ph : lead_phone
  200. booking_detail.booking_names = names.clone
  201. booking_detail.stage = "tentative"
  202. booking_detail.payment_schedule = payment_schedule
  203. booking_detail.project_unit_id = project_unit_id
  204. booking_detail.build_cost_sheet_with_costs({})
  205. if booking_detail.valid?
  206. booking_detail.save
  207. booking_detail.stage = "confirmed"
  208. booking_detail.save
  209. else
  210. puts "Booking invalid----------------", booking_detail.errors.full_messages
  211. end
  212.  
  213. else
  214. puts "error in saving schedule-------------------", payment_schedule.errors.full_messages
  215. end
  216. end
  217. end#End if i>0
  218. end#end of loop
  219. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement