Advertisement
Guest User

Untitled

a guest
Jul 8th, 2017
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.00 KB | None | 0 0
  1. puts "Creating warehouses"
  2.  
  3. warehouses = [
  4. {code: 'KOL', eg_code: 'LOK', drug_license_number: '123', store_name: 'warehouse1'},
  5. {code: 'PNQ', eg_code: 'QNP', drug_license_number: '456', store_name: 'warehouse2'}
  6. ]
  7.  
  8. warehouses.each do |data|
  9. puts "creating warehouse with code: #{data.fetch(:code)}"
  10. warehouse = Warehouse.find_or_initialize_by(code: data.fetch(:code))
  11. warehouse.eg_code = data.fetch(:eg_code)
  12. warehouse.drug_license_number = data.fetch(:drug_license_number)
  13. warehouse.store_name = data.fetch(:store_name)
  14. warehouse.fulfillment_center_type = 1
  15. warehouse.active = true
  16. warehouse.update!(data)
  17. end
  18.  
  19. puts "Creating cities"
  20.  
  21. cities = [
  22. {warehouse_code: 'KOL', name: 'Kolkata', state: 'WG', country: 'India', max_allowed_cod_paise: 5000000, customer_care_number: '0336666666', customer_care_starts_at: '08:00', customer_care_closes_at: '20:00'},
  23. {warehouse_code: 'PNQ', name: 'Pune', state: 'MH', country: 'India', max_allowed_cod_paise: 5000000, customer_care_number: '0212666666', customer_care_starts_at: '08:00', customer_care_closes_at: '20:00'}
  24. ]
  25.  
  26. cities.each do |data|
  27. puts "creating - #{data.inspect}"
  28.  
  29. warehouse = Warehouse.find_by_code(data.fetch(:warehouse_code))
  30. city = City.find_or_initialize_by(name: data[:name])
  31. data.merge!({warehouse_id: warehouse.id}).except!(:warehouse_code)
  32. city.update_attributes!(data)
  33. end
  34.  
  35. puts "\nCreating areas"
  36.  
  37. areas = [
  38. {pincode: 411028, name: 'Hadapsar', city_name: 'Pune'},
  39. {pincode: 411029, name: 'kothrud', city_name: 'Pune'},
  40. {pincode: 411030, name: 'Chinchwad', city_name: 'Pune'},
  41. {pincode: 700001, name: 'Rajrhat', city_name: 'Kolkata'}
  42. ]
  43.  
  44. areas.each do |data|
  45. puts "creating - #{data.inspect}"
  46.  
  47. city = City.find_by_name(data[:city_name])
  48. area = Area.find_or_initialize_by(city_id: city.id, name: data[:name])
  49. area.update_attributes!(data.except(:city_name))
  50. end
  51.  
  52.  
  53. puts "\nCreating categories"
  54.  
  55. categories = [
  56. {name: 'Pharmacy', category_type: Category.category_types["MC1"], pharmacy: true},
  57. {name: 'Health Care', category_type: Category.category_types["MC2"], parent_name: 'Pharmacy', pharmacy: true},
  58. {name: 'Hair Care', category_type: Category.category_types["MC3"], parent_name: 'Health Care', pharmacy: true},
  59. {name: 'Cosmetics', category_type: Category.category_types["MC1"], pharmacy: false},
  60. {name: 'Beauty Products', category_type: Category.category_types["MC2"], parent_name: 'Cosmetics', pharmacy: false},
  61. {name: 'Lipsticks', category_type: Category.category_types["MC3"], parent_name: 'Beauty Products', pharmacy: false},
  62. {name: 'Health And Digestion', category_type: Category.category_types["MC2"], parent_name: 'Pharmacy', pharmacy: true},
  63. {name: 'Digestive Aids', category_type: Category.category_types["MC3"], parent_name: 'Health And Digestion', pharmacy: true}
  64. ]
  65.  
  66. categories.each do |data|
  67. puts "creating - #{data.inspect}"
  68.  
  69. parent = Category.find_by_name(data[:parent_name])
  70. data = data.merge(parent_id: parent.try(:id))
  71. category = Category.find_or_initialize_by(name: data[:name], category_type: data[:category_type])
  72. category.update_attributes!(data.except(:parent_name))
  73. end
  74.  
  75.  
  76. puts "\nCreating product types"
  77.  
  78. product_types = [
  79. {name: 'pharmacy_products', category_name: 'Pharmacy'},
  80. {name: 'beauty_products', category_name: 'Cosmetics'}
  81. ]
  82.  
  83. product_types.each do |data|
  84. puts "creating - #{data.inspect}"
  85.  
  86. category = Category.mc1.find_by_name(data[:category_name])
  87. data = data.merge(category_id: category.id)
  88. product_type = ProductType.find_or_initialize_by(name: data[:name])
  89. product_type.update_attributes!(data.except(:category_name))
  90. end
  91.  
  92.  
  93. puts "\nCreating users"
  94.  
  95. users = [
  96. {name: "Catalogue Admin",
  97. email: "catalogueadmin@example.com", password: "welcome",
  98. roles: ["catalogue_manager"],
  99. primary_phone: {
  100. number: '8888888888',
  101. verified: true,
  102. phone_type: 'Mobile',
  103. primary: true
  104. }
  105. },
  106. {name: "Category Admin",
  107. email: "categoryadmin@example.com", password: "welcome",
  108. roles: ["category_manager"]},
  109. {name: "EMR",
  110. email: "emr@example.com", password: "welcome",
  111. roles: ["emr"],
  112. authentication_token: "QqYXW_Su1qdbFUoYwjb1"},
  113. {name: "User Standard",
  114. email: "user@example.com", password: "welcome",
  115. primary_phone: {
  116. number: '9238100975',
  117. phone_type: 'Mobile',
  118. verified: true,
  119. primary: true
  120. },
  121. roles: ["standard"]
  122. },
  123. {name: "Super Admin",
  124. email: "admin@example.com", password: "welcome",
  125. roles: ["super_admin"]},
  126. {name: "Vinculum API",
  127. email: "vinculum@example.com", password: "welcome",
  128. roles: ["warehouse_manager"]},
  129. {name: "Asset Tracker API",
  130. email: "asset-tracker@example.com", password: "welcome",
  131. roles: ["asset_tracker"]},
  132. {name: "CCE User",
  133. email: "cce@example.com", password: "welcome",
  134. roles: ["call_center_executive"]},
  135. {name: "Compliance Manager",
  136. email: "compliance-manager@example.com", password: "welcome",
  137. roles: ["compliance_manager"]}
  138. ]
  139.  
  140. users.each do |attributes|
  141. data = attributes.except(:primary_phone)
  142. puts "creating - #{data.inspect}"
  143. user = User.find_or_initialize_by(email: data[:email])
  144. if user.id
  145. user.update_attributes!(data.except(:password))
  146. else
  147. user.update_attributes!(data.merge(password_confirmation: data[:password]))
  148. end
  149. if phone_attributes = attributes[:primary_phone]
  150. phone = user.primary_phone || user.phones.new
  151. puts "Updating #{user.name}'s phone to #{phone_attributes}"
  152. phone.update_attributes!(phone_attributes)
  153. end
  154. end
  155.  
  156.  
  157. puts "\nCreating Shipping Rules"
  158.  
  159. shipping_rules = [
  160. {min_value_paise: 0, max_value_paise: 10000, shipping_cost_paise: 5000, city_name: 'Pune'},
  161. {min_value_paise: 10001, max_value_paise: 20000, shipping_cost_paise: 4000, city_name: 'Pune'},
  162. {min_value_paise: 20001, max_value_paise: 50000, shipping_cost_paise: 2500, city_name: 'Pune'}
  163. ]
  164.  
  165. shipping_rules.each do |data|
  166. puts "creating - #{data.inspect}"
  167. city = City.find_by_name(data[:city_name])
  168. rule = ShippingRule.find_or_initialize_by(city_id: city.id, min_value_paise: data[:min_value_paise])
  169. rule.update_attributes!(data.except(:city_name))
  170. end
  171.  
  172.  
  173. puts "\nCreating properties"
  174.  
  175. properties = [
  176. {name: Variant::PRESCRIPTION_REQUIRED_PROPERTY_NAME, input_type: Property.input_types["boolean"]},
  177. {name: Variant::MAX_ORDER_SIZE_PROPERTY_NAME, input_type: Property.input_types["number"]},
  178. {name: 'dummy_property', input_type: Property.input_types["string"]},
  179. {name: 'unit_of_sale', input_type: Property.input_types["string"]},
  180. {name: 'inner_package_quantity', input_type: Property.input_types["number"]},
  181. {name: 'schedule', input_type: Property.input_types["dropdown"]},
  182. ]
  183.  
  184. properties.each do |data|
  185. puts "creating - #{data.inspect}"
  186. property = Property.find_or_initialize_by(name: data[:name], display_name: data[:name].gsub('_', ' ').camelize)
  187. property.update_attributes!(data)
  188. end
  189.  
  190. puts "\nCreating ProductTypeProperties"
  191.  
  192. product_type_properties = [
  193. {product_type_name: 'pharmacy_products', property_name: Variant::PRESCRIPTION_REQUIRED_PROPERTY_NAME, on_variant: true, mandatory: true, user_accessible: true},
  194. {product_type_name: 'pharmacy_products', property_name: Variant::MAX_ORDER_SIZE_PROPERTY_NAME, on_variant: false, mandatory: false, user_accessible: true},
  195. {product_type_name: 'pharmacy_products', property_name: 'dummy_property', on_variant: false, mandatory: false, user_accessible: true},
  196. {product_type_name: 'pharmacy_products', property_name: 'inner_package_quantity', on_variant: false, mandatory: false, user_accessible: true},
  197. {product_type_name: 'pharmacy_products', property_name: 'unit_of_sale', on_variant: false, mandatory: false, user_accessible: true},
  198. {product_type_name: 'pharmacy_products', property_name: 'schedule', on_variant: true, mandatory: true, user_accessible: true, valid_values: ['Schedule H', 'Schedule H1']},
  199.  
  200. {product_type_name: 'beauty_products', property_name: Variant::MAX_ORDER_SIZE_PROPERTY_NAME, on_variant: false, mandatory: false, user_accessible: true},
  201. {product_type_name: 'beauty_products', property_name: 'dummy_property', on_variant: false, mandatory: false, user_accessible: true},
  202. {product_type_name: 'beauty_products', property_name: 'unit_of_sale', on_variant: false, mandatory: false, user_accessible: true},
  203. {product_type_name: 'beauty_products', property_name: 'inner_package_quantity', on_variant: false, mandatory: false, user_accessible: true},
  204. ]
  205.  
  206. product_type_properties.each do |data|
  207. puts "creating - #{data.inspect}"
  208. product_type = ProductType.find_by_name(data[:product_type_name])
  209. property = Property.find_by_name(data[:property_name])
  210. ptp = ProductTypeProperty.find_or_initialize_by(product_type: product_type, property: property)
  211. ptp.update_attributes! data.except(:product_type_name, :property_name)
  212. end
  213.  
  214.  
  215. puts "\nCreating Brands"
  216. brands = [
  217. {
  218. name: 'himalaya'
  219. },
  220. {
  221. name: 'alkem'
  222. }
  223. ]
  224.  
  225. brands.each do |data|
  226. unless Brand.where(data).exists?
  227. puts "Creating Brand: #{data.inspect}"
  228. Brand.create!(data)
  229. end
  230. end
  231.  
  232. brand_himalaya = Brand.find_by_name('himalaya')
  233. brand_alkem = Brand.find_by_name('alkem')
  234. puts "\nCreating Products"
  235.  
  236. products = [
  237. {
  238. name: 'Himalaya Herbals Purifying Neem Face Wash', sku: Item::SKU_PENDING,
  239. product_type_name: 'beauty_products', active: true, primary_category_name: 'Beauty Products', manufacturer: 'Himalaya',
  240. brand: brand_himalaya,
  241. item_properties: [
  242. {property_name: Variant::MAX_ORDER_SIZE_PROPERTY_NAME, property_value: 10},
  243. {property_name: 'dummy_property', property_value: nil},
  244. {property_name: 'unit_of_sale', property_value: 'Pack'},
  245. {property_name: 'inner_package_quantity', property_value: 10},
  246. ]
  247. },
  248. {
  249. name: 'Himalaya Herbals Baby Soap (Honey and Milk)', sku: Item::SKU_PENDING,
  250. product_type_name: 'beauty_products', active: true, primary_category_name: 'Beauty Products', manufacturer: 'Himalaya',
  251. brand: brand_himalaya,
  252. item_properties: [
  253. {property_name: Variant::MAX_ORDER_SIZE_PROPERTY_NAME, property_value: 10},
  254. {property_name: 'dummy_property', property_value: nil},
  255. {property_name: 'unit_of_sale', property_value: 'Pack'},
  256. {property_name: 'inner_package_quantity', property_value: 10},
  257. ]
  258. },
  259. {
  260. name: 'PAN - D', sku: Item::SKU_PENDING,
  261. product_type_name: 'pharmacy_products', active: true, primary_category_name: 'Pharmacy', manufacturer: 'Alkem',
  262. brand: brand_alkem,
  263. item_properties: [
  264. {property_name: Variant::PRESCRIPTION_REQUIRED_PROPERTY_NAME, property_value: 'TRUE'},
  265. {property_name: Variant::MAX_ORDER_SIZE_PROPERTY_NAME, property_value: 10},
  266. {property_name: 'dummy_property', property_value: nil},
  267. {property_name: 'unit_of_sale', property_value: 'Pack'},
  268. {property_name: 'inner_package_quantity', property_value: 10},
  269. ]
  270. },
  271. {
  272. name: 'PAN - A', sku: Item::SKU_PENDING,
  273. product_type_name: 'pharmacy_products', active: true, primary_category_name: 'Pharmacy', manufacturer: 'Alkem',
  274. brand: brand_alkem,
  275. item_properties: [
  276. {property_name: Variant::PRESCRIPTION_REQUIRED_PROPERTY_NAME, property_value: 'TRUE'},
  277. {property_name: Variant::MAX_ORDER_SIZE_PROPERTY_NAME, property_value: 10},
  278. {property_name: 'dummy_property', property_value: nil},
  279. {property_name: 'unit_of_sale', property_value: 'Pack'},
  280. {property_name: 'inner_package_quantity', property_value: 10},
  281. ]
  282. }
  283. ]
  284.  
  285. products.each do |data|
  286. puts "creating - #{data.inspect}"
  287. product_type = ProductType.find_by_name(data[:product_type_name])
  288. primary_category = Category.find_by_name(data[:primary_category_name])
  289. product = Product.find_or_initialize_by(name: data[:name])
  290. product.update_attributes! data.except(:product_type_name, :primary_category_name, :item_properties).merge(product_type: product_type, primary_category: primary_category)
  291. product.send(:assign_sku_after_import)
  292. product.save!
  293.  
  294. data[:item_properties].each do |property_data|
  295. property = Property.find_by_name(property_data[:property_name])
  296. ptp = ProductTypeProperty.find_by(product_type: product_type, property: property)
  297. item_property = product.item_properties.find_or_initialize_by(property_id: property.id)
  298. item_property.update_attributes!(property_data.except(:property_name).merge(product_type_property: ptp))
  299. end
  300. end
  301.  
  302.  
  303. puts "\nCreating Variants"
  304.  
  305. variants = [
  306. {name: 'Himalaya Herbals Purifying Neem Face Wash 200ml', sku: Item::SKU_PENDING, active: true, product_name: 'Himalaya Herbals Purifying Neem Face Wash'},
  307. {name: 'Himalaya Herbals Purifying Neem Face Wash 500ml', sku: Item::SKU_PENDING, active: true, product_name: 'Himalaya Herbals Purifying Neem Face Wash'},
  308. {name: 'Himalaya Herbals Baby Soap (Honey and Milk) 75g', sku: Item::SKU_PENDING, active: true, product_name: 'Himalaya Herbals Baby Soap (Honey and Milk)'},
  309. {
  310. name: 'PAN - D', sku: Item::SKU_PENDING, active: true, product_name: 'PAN - D',
  311. item_properties: [{property_name: Variant::PRESCRIPTION_REQUIRED_PROPERTY_NAME, property_value: 'TRUE'}]
  312. },
  313. {
  314. name: 'PAN - A', sku: Item::SKU_PENDING, active: true, product_name: 'PAN - D',
  315. item_properties: [{property_name: Variant::PRESCRIPTION_REQUIRED_PROPERTY_NAME, property_value: 'TRUE'}]
  316. }
  317. ]
  318.  
  319. variants.each do |data|
  320. puts "creating - #{data.inspect}"
  321.  
  322. product = Product.find_by_name(data[:product_name])
  323. variant = Variant.find_or_initialize_by(name: data[:name])
  324. variant.update_attributes! data.except(:product_name, :item_properties).merge(product: product)
  325. variant.send(:assign_sku_after_import)
  326. variant.save!
  327.  
  328. data[:item_properties].each do |property_data|
  329. property = Property.find_by_name(property_data[:property_name])
  330. ptp = ProductTypeProperty.find_by(product_type: variant.product_type, property: property)
  331. item_property = variant.item_properties.find_or_initialize_by(property_id: property.id)
  332. item_property.update_attributes!(property_data.except(:property_name).merge(product_type_property: ptp))
  333. end if data[:item_properties]
  334. end
  335.  
  336.  
  337. puts "\nCreating VariantCity"
  338.  
  339. CityWarehouse.create(city_id: 1, warehouse_id: 1, default_city: true)
  340. CityWarehouse.create(city_id: 2, warehouse_id: 2, default_city: true)
  341.  
  342.  
  343. City.all.each do |city|
  344. first_for_city = nil
  345. second_for_city = nil
  346.  
  347. Variant.all.each do |variant|
  348. puts "Creating VariantCity for #{city.name} & #{variant.name}"
  349. mrp = [100_00, 120_00, 140_00].sample
  350. variant_city_service = VariantStockUpdateService.new(city.warehouse, variant.sku, mrp, 1000)
  351. variant_city_service.process
  352.  
  353. VariantCity.update_all(sales_price_paise: mrp, promotion_percent: 10, max_promotion_percent: 99.99,max_discount_percent: 10.00)
  354. end
  355. end
  356.  
  357. puts "\nCreating Delivery Centers"
  358.  
  359. City.all.each do |city|
  360. puts "Creating DC for #{city.name}"
  361. dc = DistributionCenter.find_or_initialize_by(dc_code: "dc_#{city.name}")
  362. dc.update_attributes! dc_code_id: dc.dc_code
  363. city.areas.each { |area| area.update_attributes!(distribution_center_id: dc.id) }
  364. end
  365.  
  366. puts "\nCreating Delivery Slots"
  367.  
  368. slot_timings = ['09:00-11:00', '11:00-13:00', '13:00-15:00', '15:00-17:00', '17:00-19:00']
  369.  
  370. DistributionCenter.all.each do |dc|
  371. (0..2).to_a.each do |day_diff|
  372. date = Date.today + day_diff.days
  373. slot_timings.each do |slot_timing|
  374. slot_id = "#{dc.id}-#{date}-#{slot_timing}"
  375. slot = DeliverySlot.find_or_initialize_by(slot_id: slot_id)
  376. data = {distribution_center_id: dc.id, slot_description: slot_timing, slot_date: date, on_hand_count: 20, allocated_count: 0}
  377. puts "Creating slot - #{data}"
  378. slot.update_attributes! data
  379. end
  380. end
  381. end
  382.  
  383. #create orders in delivered state
  384. puts "\nCreating Orders in delivered state"
  385. cce_user = User.find_by_email('cce@example.com')
  386. standard_user = User.find_by_email('emr@example.com')
  387.  
  388. City.all.each do |city|
  389. address = standard_user.addresses.first_or_create do |add|
  390. add.full_name = standard_user.name
  391. add.address_line_1 = 'foo address line 1'
  392. add.address_line_2 = 'bar address line 1'
  393. add.city = city
  394. add.area = city.areas.first
  395. add.phone_number = '0123456789'
  396. end
  397. order = Order.find_or_initialize_by(user: standard_user,
  398. city: city,
  399. created_by: cce_user,
  400. state: Order.states[:delivered],
  401. payment_method: Order::PAYMENT_METHODS.first,
  402. shipping_address: address,
  403. billing_address: address)
  404. order.save!
  405. variants = VariantCity.where(city: city).each do |vc|
  406. order.line_items.create(order: order,
  407. variant: vc.variant,
  408. quantity: 1,
  409. mrp_paise: vc.mrp_paise,
  410. total_paise: vc.mrp_paise,
  411. discount_amount_paise: 0,
  412. sales_price_paise: vc.sales_price_paise)
  413. end
  414. end
  415.  
  416.  
  417. puts "\nCreating Business Day seed data"
  418.  
  419. if BusinessDay.all.count == 0
  420. # .all returns active relation
  421. Rake::Task['one_off:initialize_business_days'].invoke
  422. end
  423.  
  424. #omni channel seed data for development,
  425. #many to many Associations between Area and warehouse
  426.  
  427. warehouses = [
  428. {code: 'DURG', eg_code: 'GRUD', drug_license_number: '789', store_name: 'warehouse3', fulfillment_center_type: 2, active: true},
  429. {code: 'BAR', eg_code: 'RAB', drug_license_number: '901', store_name: 'warehouse4', fulfillment_center_type: 2, active: true},
  430. {code: 'DUM', eg_code: 'MUD', drug_license_number: '111', store_name: 'warehouse5', fulfillment_center_type: 2, active: true},
  431. {code: 'HAL', eg_code: 'LAH', drug_license_number: '222', store_name: 'warehouse6', fulfillment_center_type: 2, active: true},
  432. {code: 'KAR', eg_code: 'RAK', drug_license_number: '333', store_name: 'warehouse7', fulfillment_center_type: 2, active: true},
  433. {code: 'NAI', eg_code: 'IAN', drug_license_number: '443', store_name: 'warehouse8', fulfillment_center_type: 2, active: true}
  434. ]
  435. # warehouses = [
  436. # {code: 'KOL', eg_code: 'LOK', drug_license_number: '123', store_name: 'warehouse1'},
  437. # {code: 'PNQ', eg_code: 'QNP', drug_license_number: '456', store_name: 'warehouse2'}
  438. # ]
  439. warehouses.each do |data|
  440. warehouse = Warehouse.find_or_initialize_by(code: data.fetch(:code))
  441. warehouse.update!(data)
  442. end
  443.  
  444.  
  445. Area.all.each do |area|
  446. level = 0
  447. Warehouse.where(fulfillment_center_type: 2).order("RANDOM()").limit(3).each do |warehouse|
  448. area_warehouse = AreaWarehouse.find_or_initialize_by(area_id: area.id , warehouse_id: warehouse.id)
  449. level += 1
  450. area_warehouse.fulfillment_center_level = level
  451. area_warehouse.save
  452. end
  453. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement