class Api::BatchLineItemsController < ApplicationController skip_before_filter :verify_authenticity_token def register batch_id=params[:batch_id] session_token=params[:session_token] manufacture_uid = params[:card_manufacture] client_id="" check_member= Member.authenticate_session_token(session_token) if !check_member.nil? encrypt_key="" wristband_code="" find_batch = Batch.find_by_id(batch_id) if !find_batch.nil? card_type=find_batch.adult_or_child.upcase find_user=Client.find_by_id(find_batch.client_id) if !find_user.nil? encrypt_key=find_user.secrect_key product_type=find_user.product_type client_id=find_user.id if !encrypt_key.nil? check_manufacture = BatchLineItem.find_by_manufacture_uid(manufacture_uid) if check_manufacture.nil? serial_no=BatchLineItem.generate_serial_no(client_id.to_s) if product_type.downcase=="ticket" str="manufacture_uid="+manufacture_uid.to_s+"&serial_no="+serial_no.to_s digest_data= Digest::MD5.hexdigest(str) hex_data=digest_data.hex hex_str=hex_data.to_s wristband_code=hex_str[0..15] check_wirstband=BatchLineItem.find_by_asset_identity(wristband_code) if check_wirstband.nil? @out=create_product(wristband_code,serial_no,batch_id,manufacture_uid,card_type) else @out=false,'Wristband is already registered!' end elsif product_type=="card" @out=create_product(wristband_code,serial_no,batch_id,manufacture_uid,card_type) end else @out=false,'Wristband is already registered!' end else @out=false,'Invalid User!' end else @out=false,'Client does not exist!' end else @out=false,'Batch does not exist!' end else @out=false,'Sorry!Unauthorized user!' end end def create_product(wristband_code,serial_no,batch_id,manufacture_uid,card_type) batchLineItem=BatchLineItem.new batchLineItem.asset_identity=wristband_code batchLineItem.serial_no=serial_no batchLineItem.batch_id=batch_id batchLineItem.manufacture_uid = manufacture_uid batchLineItem.asset_type = card_type batchLineItem.encoded_at = DateTime.now.beginning_of_day.utc.to_time.strftime("%Y-%m-%d") batchLineItem.verified_at =DateTime.now.beginning_of_day.utc.to_time.strftime("%Y-%m-%d") if batchLineItem.save lookup=Lookup.find_by_name('generate_serial_no') max_serail_no=lookup.max_value lookup.max_value=max_serail_no.to_i+1 lookup.save @result = true,batchLineItem.serial_no,batchLineItem.asset_identity else @result=false,'Error occurs in registration encoder!' end end end