# This integrates 2 instances for easy management
require 'net/http'
namespace :aibes_instance do

	desc "create transactions"
  	task :system_configurations => :environment do 
  		stands = Stand.order(id: :asc)
  		stands.each do |stand|
  			puts("*** dealing with #{stand.id}")
  			unless AibesStand.exists?(stand_number: stand.stand_number, project_id: stand.project_id)
  				AibesStand.create(
  					id: stand.id,
  					stand_number: stand.stand_number,
  					size: stand.size,
  					price_psm: stand.price_psm,
  					sale_value: stand.sale_value,
  					available: stand.available,
  					class_category_id: stand.class_category_id,
  					property_class_id: stand.property_class_id,
  					project_id: stand.project_id,
  					sale_type_id: stand.sale_type_id,
  					general_plan_id: stand.general_plan_id,
  					lender_id: stand.lender_id,
  					currency_id: stand.currency_id,
  					currency_class_id: stand.currency_class_id,
  					vat_inclusive: stand.vat_inclusive,
  					selling_currency_id: stand.selling_currency_id,
  					is_taxable: stand.is_taxable,
  					tenure: stand.tenure
  				)
  			end
  		end
  		# # get all system configurations
  		# integrated_accounts = IntegratedCustomerAccount.group(:project_id)
  		# integrated_accounts.each_with_index do |i|
  		# 	project = i.project
  		# 	unless AibesProject.exists?(id: project.id)
  		# 		AibesProject.create(
  		# 			id: project.id,
  		# 			name: project.name,
  		# 			scope: project.scope,
  		# 			location: project.location,
  		# 			description: project.description,
  		# 			salias: project.salias,
  		# 			compounding_interest: project.compounding_interest,
  		# 			prepost_interest: project.prepost_interest,
  		# 			deposit_term: project.deposit_term
  		# 		)
  		# 	end
  		# end
  		# SystemSetting.all.each do |setting|
  		# 	unless AibesSystemSetting.exists?(id: setting.id)
  		# 		AibesSystemSetting.create(
  		# 			id: setting.id,
  		# 			config_key: setting.config_key,
  		# 			config_value: setting.config_value,
  		# 			setting_category_id: setting.setting_category_id,
  		# 			project_id: setting.project_id,
  		# 			default_value: setting.default_value
  		# 		)
  		# 	end
  		# end
  		# PropertyClass.all.each do |property_class|
  		# 	unless AibesPropertyClass.exists?(id: property_class.id)
  		# 		if AibesPropertyClass.create(
  		# 			id: property_class.id,
  		# 			name: property_class.name,
  		# 			description: property_class.description
	  	# 			)
	  	# 			property_class.class_categories.each do |cat|
		#   				AibesClassCategory.create(
		#   				id: cat.id, 
		#   				property_class_id: cat.property_class_id, 
		#   				description: cat.description, 
		#   				prefix: cat.prefix
		#   				)
		#   			end
		#   		end
  		# 	end
  		# end
  		# GeneralPlan.all.each do |gp|
  		# 	unless AibesGeneralPlan.exists?(id: gp.id)
  		# 		AibesGeneralPlan.create(id: gp.id, name: gp.name)
  		# 	end
  		# end
  		# Lender.all.each do |lender|
  		# 	unless AibesLender.exists?(id: lender.id)
  		# 		AibesLender.create(id: lender.id, name: lender.name, lender_category_id: lender.lender_category_id, description: lender.description)
  		# 	end
  		# end
  		# PaymentType.all.each do |payment_type|
  		# 	unless AibesPaymentType.exists?(id: payment_type.id)
  		# 		AibesPaymentType.create(name: payment_type.name, short_name: payment_type.short_name, active: payment_type.active, currency_id: payment_type.currency_id, currency_class_id: payment_type.currency_class_id, additional_required: payment_type.additional_required)
  		# 	end
  		# end
  	end

	desc "create customers"
  	task :customers => :environment do 
  		# get the integrated accounts
  		@integrated_accounts = IntegratedCustomerAccount.not_integrated
  		@integrated_accounts.each_with_index do |integrated_account,i|
  			account = integrated_account.customer_account
  			customer = integrated_account.customer_account.customer
  			# check if the customer account exists?
  			unless AibesCustomerAccount.exists?(id: account.id)
  				puts("Create new account #{account.property_number}")
  				if AibesCustomerAccount.create(
  					id: account.id,
  					customer_id: customer.id,
  					stand_id: account.stand_id,
  					sale_type_id: account.sale_type_id,
  					stand_value: account.stand_value,
  					stand_value_psm: account.stand_value_psm,
  					deposit_amount: account.deposit_amount,
  					deposit_rate: account.deposit_rate,
  					original_loan_amount: account.original_loan_amount,
  					upfront_fees: account.upfront_fees,
  					number_of_years: account.number_of_years,
  					upfront_fee_rate: account.upfront_fee_rate,
  					loan_balance: account.loan_balance,
  					interest_rate: account.interest_rate,
  					registration_date: account.registration_date,
  					contract_start_date: account.contract_start_date,
  					contract_end_date: account.contract_end_date,
  					deleted: account.deleted,
  					account_name: account.account_name,
  					agent_id: account.agent_id,
  					stand_size: account.stand_size,
  					property_number:account.property_number,
  					status: account.status,
  					lender_id: account.lender_id,
  					class_category_id: account.class_category_id,
  					property_class_id: account.property_class_id,
  					project_id: account.project_id,
  					currency_class_id: account.currency_class_id,
  					currency_id: account.currency_id,
  					property_value_discount: account.property_value_discount,
  					is_takeon: account.is_takeon,
  					takeon_date: account.takeon_date,
  					general_plan_id: account.general_plan_id,
  					deposit_tenor: account.deposit_tenor,
  					registration_year: account.registration_year,
  					purchase_year: account.purchase_year)

  					CustomerAccountFee.where(customer_account_id: account.id).each do |fee|
  						unless AibesCustomerAccountFee.exists?(id: fee.id)
  							AibesCustomerAccountFee.create(
  								customer_account_id: fee.customer_account_id,
  								payment_title_id: fee.payment_title_id,
  								date_posted: fee.date_posted,
  								amount: fee.amount,
  								deleted: fee.deleted
  							)
  						end
  					end
  				end
  			end

  			puts("*********** #{i+=1} #{customer.customer_name}")
  			# check if the customer already exists?
  			new_customer = AibesCustomer.find_or_initialize_by(id: customer.id)
  			if new_customer.new_record?
  				puts("*** new record #{customer.customer_name} - #{customer.id}")
				new_customer.id = customer.id
				new_customer.firstname = customer.firstname
				new_customer.lastname = customer.lastname
				new_customer.idnumber = customer.idnumber
				new_customer.address_1 = customer.address_1
				new_customer.address_2 = customer.address_2
				new_customer.city = customer.city
				new_customer.mobile_1 = customer.mobile_1
				new_customer.mobile_2 = customer.mobile_2
				new_customer.phone = customer.phone
				new_customer.email = customer.email
				new_customer.employer_name = customer.employer_name
				new_customer.employment_type = customer.employment_type
				new_customer.salary = customer.salary
				new_customer.dob = customer.dob
				new_customer.sex = customer.sex
				new_customer.registration_date = customer.registration_date
				new_customer.active = customer.active
				new_customer.customer_name = customer.customer_name
				new_customer.registration_number = customer.registration_number
				new_customer.customer_type_id = customer.customer_type_id
				new_customer.is_individual = customer.is_individual
				new_customer.middlename = customer.middlename
				new_customer.country = customer.country
				new_customer.id_type = customer.id_type
				new_customer.referral = customer.referral
				new_customer.country_based = customer.country_based
				new_customer.customer_group_id = customer.customer_group_id
				new_customer.user_id = customer.user_id
				new_customer.save!

				customer.customer_additionals.each do |additional|
					new_additional = AibesCustomerAdditional.find_or_initialize_by(id: additional.id)
					new_additional.id = additional.id
					new_additional.customer_id = customer.id
					new_additional.firstname = additional.firstname
					new_additional.lastname = additional.lastname
					new_additional.idnumber = additional.idnumber
					new_additional.address_1 = additional.address_1
					new_additional.address_2 = additional.address_2
					new_additional.city = additional.city
					new_additional.mobile_1 = additional.mobile_1
					new_additional.mobile_2 = additional.mobile_2
					new_additional.phone = additional.phone
					new_additional.email = additional.email
					new_additional.dob = additional.dob
					new_additional.sex = additional.sex
					new_additional.registration_date = additional.registration_date
					new_additional.active = additional.active
					new_additional.middlename = additional.middlename
					new_additional.country = additional.country
					new_additional.id_type = additional.id_type
					new_additional.save!
				end
  			else
  				puts("#{customer.customer_name} already exists!")
  			end
  		end
  	end

	desc "create transactions"
  	task :transactions => :environment do 
  		# get integrated accounts transactions
  		AibesCustomerAccount.all.each do |aibes_account|
  			@customer_account = aibes_account
  			initial_transactions = Transaction.where(customer_account_id: aibes_account.id, posted: true)
  			initial_transactions.each do |trans|
  				unless AibesTransaction.exists?(transaction_code: trans.transaction_code)
	  				puts("dealing with initial #{trans.id} - #{trans.customer_account_id} - #{trans.amount}")
	  				@transaction = AibesTransaction.new
	  				@transaction.transaction_code = trans.transaction_code
	  				@transaction.customer_account_id = @customer_account.id
					@transaction.reference_number = trans.reference_number
					@transaction.receipt_number = trans.receipt_number
				    @transaction.payment_type_id = trans.payment_type_id
				    @transaction.currency_id = trans.currency_id
				    @transaction.payment = trans.payment
				    @transaction.legal = trans.legal
				    @transaction.sms = trans.sms
				    @transaction.currency_id = trans.currency_id
				    @transaction.currency_class_id = trans.currency_class_id
				    @transaction.transaction_date = trans.transaction_date
				    @transaction.rate = trans.rate
				    @transaction.amount = trans.amount
				    @transaction.payment_title_id = trans.payment_title_id
				    @transaction.payment_category_id = trans.payment_category_id
				    @transaction.bank_id = trans.bank_id
				    @transaction.bank_statement_desc = trans.bank_statement_desc
				    @transaction.statement_reference_number = trans.statement_reference_number
				    @transaction.posted = false
				    @transaction.journal = trans.journal
				    @transaction.journal_description = trans.journal_description
				    @transaction.journal_reference = trans.journal_reference
				    @transaction.suspense = true
				    @transaction.user_id = trans.user_id
				    @transaction.transaction_type_id = trans.transaction_type_id
				    @transaction.invoice_number = trans.invoice_number
					@transaction.save
				end
  			end
			initial_receipt = SystemSetting.find_by(config_key: "InitialReceiptNo").config_value
  			Transaction.where(customer_account_id: aibes_account.id, transaction_type_id: 3).each do |transaction|
  				unless AibesTransaction.exists?(transaction_code: transaction.transaction_code)
	  				puts("dealing with receipt #{transaction.id} - #{transaction.customer_account_id} - #{transaction.amount}")
	  				@transaction = AibesTransaction.new
	  				@transaction.transaction_code = transaction.transaction_code
	  				@transaction.customer_account_id = @customer_account.id
	  				@last_payment = AibesTransaction.where(posted: false, transaction_type_id: 3).last
					if @last_payment.nil?
						@transaction.reference_number = initial_receipt
						@transaction.receipt_number = initial_receipt
					else
						@transaction.reference_number = @last_payment.reference_number.next
						@transaction.receipt_number = @last_payment.receipt_number.next
					end
				    @transaction.payment_type_id = transaction.payment_type_id
				    @transaction.currency_id = transaction.currency_id
				    @transaction.payment = transaction.payment
				    @transaction.legal = transaction.legal
				    @transaction.sms = transaction.sms
				    @transaction.currency_id = transaction.currency_id
				    @transaction.currency_class_id = transaction.currency_class_id
				    @transaction.transaction_date = transaction.transaction_date
				    @transaction.rate = transaction.rate
				    @transaction.amount = transaction.amount
				    @transaction.payment_title_id = transaction.payment_title_id
				    @transaction.payment_category_id = transaction.payment_category_id
				    @transaction.bank_id = transaction.bank_id
				    @transaction.bank_statement_desc = transaction.bank_statement_desc
				    @transaction.statement_reference_number = transaction.statement_reference_number
				    @transaction.posted = false
				    @transaction.journal = transaction.journal
				    @transaction.journal_description = transaction.journal_description
				    @transaction.journal_reference = transaction.journal_reference
				    @transaction.suspense = true
				    @transaction.user_id = transaction.user_id
				    @transaction.transaction_type_id = transaction.transaction_type_id
				    @transaction.invoice_number = transaction.invoice_number
					if @transaction.save
						transaction = @transaction.id
						user = @transaction.user_id
						AibesSuspenseAccount.post_suspense_transaction(user,transaction)
					end
				end
  			end
  		end
  	end

	desc "create transactions"
  	task :create_missing_fees => :environment do 
  		# get integrated accounts transactions
  		AibesCustomerAccount.all.each do |aibes_account|
  			@customer_account = aibes_account
  			fees = CustomerAccountFee.where(customer_account_id: @customer_account)
  			fees.each do |fee|
  				customer_account_fee = AibesCustomerAccountFee.find_or_initialize_by(payment_title_id: fee.payment_title_id, customer_account_id: fee.customer_account_id)
  				customer_account_fee.customer_account_id = fee.customer_account_id
  				customer_account_fee.payment_title_id = fee.payment_title_id
  				customer_account_fee.date_posted = @customer_account.registration_date
  				customer_account_fee.amount = fee.amount  
  				customer_account_fee.save!
  			end
  		end
  	end
end