From b81a6dbc9693f22e31b85312666a485c3a159350 Mon Sep 17 00:00:00 2001 From: nandar Date: Mon, 5 Mar 2018 11:07:59 +0630 Subject: [PATCH] qr code generate --- .../batch_line_items_controller.rb | 73 +++++++++++++++++- app/controllers/batches_controller.rb | 72 ++++++++++++++++- app/models/batch.rb | 3 + app/models/batch_line_item.rb | 30 ++++++- app/views/.DS_Store | Bin 6148 -> 6148 bytes app/views/batches/index.html.erb | 12 ++- config/database.yml | 4 +- config/routes.rb | 6 +- db/schema.rb | 3 +- ...fest-867c31ce41e7c0cf7cad32de5e89abc3.json | 2 +- 10 files changed, 190 insertions(+), 15 deletions(-) diff --git a/app/controllers/batch_line_items_controller.rb b/app/controllers/batch_line_items_controller.rb index 7333b8b..a8253d2 100644 --- a/app/controllers/batch_line_items_controller.rb +++ b/app/controllers/batch_line_items_controller.rb @@ -2,7 +2,7 @@ class BatchLineItemsController < ApplicationController require "csv" skip_before_filter :verify_authenticity_token before_action :authenticate_member! - + require 'digest/md5' def index # user_id=current_member.user_id @@ -99,13 +99,13 @@ class BatchLineItemsController < ApplicationController @batchLineItems = BatchLineItem.joins('inner join '+sub_query +" as batches on batches.id=batch_line_items.batch_id") .joins('inner join product_categories product on product.id=batches.product_category_id') .where('batch_id in (?)',batch_list) - .select("*,product.product_type_id ,product.name,'' as secret_token,'' as location") + .select("*,product.product_type_id ,product.name,'' as secret_token,'' as location_name") else Batch.where("client_id=? ",client_id).update_all("export_count = export_count + 1,exported_by='"+exported_by+"'") sub_query="(select * from batches where client_id ="+client_id+" and product_category_id="+ product_category_id+")" @batchLineItems = BatchLineItem.joins('inner join '+sub_query+' as batches on batches.id=batch_line_items.batch_id') .joins('inner join product_categories as product on product.id=batches.product_category_id') - .select("*,product.product_type_id ,product.name,'' as secret_token,'' as location") + .select("*,product.product_type_id ,product.name,'' as secret_token,'' as location_name") end datestr= Time.now.strftime("%Y/%m/%d %H:%M:%S") @@ -115,4 +115,71 @@ class BatchLineItemsController < ApplicationController format.csv { send_data @batchLineItems.to_csv(client_id,location_code), filename: "#{filename}.csv" } end end + + def generate_cards + end + + def generate_card_account_items + # Generate Code + batch_id=params[:id] + client_id="" + qty = params[:qty] + total_count=0 + + # check_member= Member.authenticate_session_token(session_token) + # if !check_member.nil? + encrypt_key="" + asset_identity="" + + find_batch = Batch.find_by_id(batch_id) + if !find_batch.nil? + puts 'hkjfklasj' + card_type=find_batch.adult_or_child.upcase + product_category_id=find_batch.product_category_id + find_product_category=ProductCategory.find_by_id(product_category_id) + + if !find_product_category.nil? + product_type=find_product_category.product_type_id + + find_user=Client.find_by_id(find_batch.client_id) + if !find_user.nil? + encrypt_key=find_user.secrect_key + location_code=find_user.location_code + + client_id=find_user.id + if !encrypt_key.nil? + # Generate with qty + qty.to_i.times do |i| + serial_no=BatchLineItem.generate_serial_no(client_id.to_s) + + asset_identity=BatchLineItem.generate_account_no(location_code) + security_code=BatchLineItem.generate_security_code + out=BatchLineItem.create_product(asset_identity,serial_no,batch_id,"","",security_code) + total_count+=1 + end + respond_to do |format| + format.html { redirect_to batch_url(batch_id), notice: 'Successfully generated.' } + format.json { head :no_content } + end + # Generate with qty + + else + @out=false,'Invalid User!' + end + else + @out=false,"Client doesn't exist!" + end + else + @out=false,"Product Category doesn't exist!" + end + else + @out=false,"Batch doesn't exist!" + end + # else + # @out=false,'Sorry!Unauthorized user!' + # end + # Generate Code + + + end end \ No newline at end of file diff --git a/app/controllers/batches_controller.rb b/app/controllers/batches_controller.rb index 198bd66..4ea6a0f 100644 --- a/app/controllers/batches_controller.rb +++ b/app/controllers/batches_controller.rb @@ -1,17 +1,75 @@ class BatchesController < ApplicationController skip_before_filter :verify_authenticity_token - before_action :authenticate_member! + # before_action :authenticate_member! + before_action :set_batch,:authenticate_member!, only: [:show, :edit, :update, :destroy] def index @product_category_id="" @clients=Client.all.order('id desc') @product_categories=ProductCategory.all.order('id desc') @batches=Batch.joins('inner join users on users.id=batches.user_id') .select('batches.*,users.name as user_name').order('batches.id desc').page(params[:page]) - @count = Batch.joins('inner join users on users.id=batches.user_id') + @count = Batch.joins('inner join users on users.id=batches.user_id') .select('batches.*').count end + def show + @batchLineItems=BatchLineItem.where("batch_id=?",params[:id]).page(params[:page]).per(10) + @result_count= @batchLineItems.total_count + end + + + def new + @batch = Batch.new + end + + def edit + end + + + def create + date = DateTime.now.beginning_of_day.utc.to_time.strftime("%Y-%m-%d") + order_ref = params[:batch][:order_ref] + puts order_ref + adult_or_child=params[:adult_or_child] + client_id=params[:client_id] + product_category_id=params[:product_category_id] + @batch = Batch.create_batch(date,order_ref,2,"adult_or_child",6,3) + puts @batch.order_ref + + respond_to do |format| + if @batch.save + format.html { redirect_to @batch, notice: 'Batch was successfully created.' } + format.json { render :show, status: :created, location: @batch } + else + format.html { render :new } + format.json { render json: @batch.errors, status: :unprocessable_entity } + end + end + end + + + def update + + respond_to do |format| + if @batch.update(batch_params) + format.html { redirect_to @batch, notice: 'Batch was successfully updated.' } + format.json { render :show, status: :ok, location: @batch } + else + format.html { render :edit } + format.json { render json: @batch.errors, status: :unprocessable_entity } + end + end + end + + def destroy + @batch.destroy + respond_to do |format| + format.html { redirect_to batches_url, notice: 'Batch was successfully destroyed.' } + format.json { head :no_content } + end + end + def search client_id=params[:client_id] product_category_id=params[:product_category_id] @@ -51,4 +109,14 @@ class BatchesController < ApplicationController end end + private + # Use callbacks to share common setup or constraints between actions. + def set_batch + @batch = Batch.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def batch_params + params.require(:batch).permit(:date,:user_id, :client_id, :order_ref,:batch_start,:batch_end,:exported_by,:qty_processing,:qty_success,:qty_fail,:batch_start_time,:batch_end_time,:export_count,:remark,:adult_or_child,:product_category_id) + end end \ No newline at end of file diff --git a/app/models/batch.rb b/app/models/batch.rb index 61da858..3123db7 100644 --- a/app/models/batch.rb +++ b/app/models/batch.rb @@ -1,5 +1,8 @@ class Batch < ApplicationRecord has_many :batch_line_item + belongs_to :client + belongs_to :product_category + belongs_to :user def self.create_batch(date,order_ref,user_id,adult_or_child,client_id,product_category_id) batch = Batch.new diff --git a/app/models/batch_line_item.rb b/app/models/batch_line_item.rb index d3bd64a..dac29e5 100644 --- a/app/models/batch_line_item.rb +++ b/app/models/batch_line_item.rb @@ -3,6 +3,7 @@ class BatchLineItem < ApplicationRecord attr_accessor :secret_token attr_accessor :location attr_accessor :product_type + require 'digest/md5' def self.generate_account_no(location_code) super_merchant = "101" @@ -21,6 +22,18 @@ class BatchLineItem < ApplicationRecord end return account_no end + def self.generate_security_code + + o = [('0'..'8')].map { |i| i.to_a }.flatten + security_code = (0...8).map { o[rand(o.length)] }.join + + find_account_no = BatchLineItem.find_by_security_code(security_code) + if !find_account_no.nil? + o = [('0'..'8')].map { |i| i.to_a }.flatten + security_code = (0...8).map { o[rand(o.length)] }.join + end + return security_code + end def self.generate_serial_no(client_id) find_lookup=Lookup.find_by_name('generate_serial_no') if !find_lookup.nil? @@ -54,16 +67,22 @@ class BatchLineItem < ApplicationRecord if !find_client.nil? encrypt_key=find_client.secrect_key end - attributes = %w{serial_no asset_identity batch_id manufacture_uid asset_type secret_token location} + attributes = %w{serial_no asset_identity batch_id manufacture_uid asset_type secret_token location security_code} CSV.generate(headers: true) do |csv| csv << attributes all.each do |encoder| - if encoder.product_type_id ==1 + if encoder.product_type_id ==1 + str="account_no="+encoder.asset_identity.to_s+"&manufacture_uid="+encoder.manufacture_uid.to_s+"&serial_no="+encoder.serial_no.to_s else str="manufacture_uid="+encoder.manufacture_uid.to_s+"&serial_no="+encoder.serial_no.to_s end + security_code = encoder.security_code + digest = Digest::MD5.hexdigest(security_code) + puts 'digest............' + puts digest + encryptd_data=BatchLineItem.encrypted(str,encrypt_key) encoder.secret_token=encryptd_data encoder.location=location_code @@ -89,7 +108,7 @@ class BatchLineItem < ApplicationRecord return encrypted end - def self.create_product(asset_identity,serial_no,batch_id,manufacture_uid,card_type) + def self.create_product(asset_identity,serial_no,batch_id,manufacture_uid,card_type,security_code=nil) batchLineItem=BatchLineItem.new batchLineItem.asset_identity=asset_identity batchLineItem.serial_no=serial_no @@ -98,7 +117,10 @@ class BatchLineItem < ApplicationRecord 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 !security_code.nil? + batchLineItem.security_code = security_code + puts 'security_code;fksdfklsdjfklsdfklsdjfklsdjfklsdjflskd' + end batch=Batch.find_by_id(batch_id) if batchLineItem.save diff --git a/app/views/.DS_Store b/app/views/.DS_Store index b617d82e48629d1b778b1dfded82dc39b25e7cd8..cc360ab00901a80e5a250a21b25c95a84d5ad85f 100644 GIT binary patch delta 526 zcmZvZ%Sr<=6oyY*b!M!^EJg)UsB1;W3+=+4myHMxRO-UYkaU6#jF~zUTcnfnQ4VC421J1=j2QBpHu79Iv-6Yjf;cPi`gkt1AqhofwISZ z-@_G<_WO0B6(v$Yp$ftcdy>F#Q$ZOj;D84LBydy$_c-iBSStk5^W|QL3tCS-|0i3H z85}7JLRJr}%<~yOsWr~3o(Pp?y(N;VzWxDCHwM!~nRJE>r8DW>LdCBa6jg`eG7TIu zQ4lU?G}sods%&w&=J6Vro52QEJlKhfxA1pZ(9x6?1+P2AT=Au%Lh>Nybr_{>A>Ec= zU-l2B84jHinUc<9CmAoiI!GtH&QB3U?#@eQ;g1719#Lx|gj5Qi_^SPPqV!k<( zZRGP7%7t9BsT;aJIlW$FM`xEe_YaRx&o8eZpI_+RQsf^tCLU%t{)KN88nz&^ziVBu i?d_}8zFSQn*0Ig)_n*||usKH})-g_uG4|Fr?^3@p3yk*w delta 98 zcmZoMXfc=|#>CJ*u~2NHo}vgd0|Nsi1A_oVa!yiyeh!ewz&5c^11!$WkjPNLkeN~p smD_xn*@b1Z0E-IqW_AvK4xk~M8CkwFPv#dfWMr6Z!y~;pMq~vu05rcAcmMzZ diff --git a/app/views/batches/index.html.erb b/app/views/batches/index.html.erb index 5faa33f..bb839c4 100644 --- a/app/views/batches/index.html.erb +++ b/app/views/batches/index.html.erb @@ -4,6 +4,11 @@ Batches +
+ + <%= link_to t('.new', :default => t("helpers.links.new")),new_batch_path,:class => 'btn btn-primary' %> + +
@@ -46,7 +51,8 @@ Qty Fail Export Count Exported By - Created At + Created At + Action @@ -60,6 +66,10 @@ <%= batch.export_count %> <%= batch.exported_by %> <%= batch.created_at.strftime("%e,%b %Y %I:%M %p") rescue '' %> + + <%= link_to 'Detail', + batch_path(batch), :class => 'btn btn-primary btn-sm' %> + <% end %> diff --git a/config/database.yml b/config/database.yml index 53e4b5e..308e2c5 100644 --- a/config/database.yml +++ b/config/database.yml @@ -14,8 +14,8 @@ default: &default development: <<: *default - database: nemo_all - username: postgres + database: nemo_oct_31 + username: nda default: &default diff --git a/config/routes.rb b/config/routes.rb index 0880b3c..a5b4663 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,7 +6,7 @@ Rails.application.routes.draw do root :to => 'home#index' get "dashboard", to: "home#dashboard", as: "dashboard" - resources :batches,:only =>[:index] + resources :batches devise_for :members resources :batch_line_items,:only =>[:index] resources :users @@ -22,6 +22,10 @@ Rails.application.routes.draw do match 'batch_line_items/export' => 'batch_line_items#export', :as => 'export', :via => [:post ,:get] get 'get_batch' => 'batch_line_items#get_batch',:as => 'get_batch' + + get 'generate_cards/:id' => 'batch_line_items#generate_cards',:as => 'generate_cards' + post "generate_card_account_items" =>"batch_line_items#generate_card_account_items" + namespace :api, defaults: {format: :json} do #login post "auth/login" =>"auth#login" diff --git a/db/schema.rb b/db/schema.rb index 0f560aa..f80e7ec 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170210103359) do +ActiveRecord::Schema.define(version: 20180226100816) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -26,6 +26,7 @@ ActiveRecord::Schema.define(version: 20170210103359) do t.datetime "verified_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "security_code" t.index ["batch_id"], name: "index_batch_line_items_on_batch_id", using: :btree end diff --git a/public/assets/.sprockets-manifest-867c31ce41e7c0cf7cad32de5e89abc3.json b/public/assets/.sprockets-manifest-867c31ce41e7c0cf7cad32de5e89abc3.json index 77707e2..d36dffe 100644 --- a/public/assets/.sprockets-manifest-867c31ce41e7c0cf7cad32de5e89abc3.json +++ b/public/assets/.sprockets-manifest-867c31ce41e7c0cf7cad32de5e89abc3.json @@ -1 +1 @@ -{"files":{"application-55570bc046304f178e10e45bec721268c8bf8e0a3bc8f2c1f69ff39df39dee3e.js":{"logical_path":"application.js","mtime":"2017-01-19T15:25:44+06:30","size":416216,"digest":"55570bc046304f178e10e45bec721268c8bf8e0a3bc8f2c1f69ff39df39dee3e","integrity":"sha256-VVcLwEYwTxeOEORb7HISaMi/jgo7yPLB9p/znfOd7j4="},"application-13eed745f8cdce49cda636dbb6e1d13489cce811adff36b761521f71e31214e9.css":{"logical_path":"application.css","mtime":"2017-01-19T15:25:44+06:30","size":735,"digest":"13eed745f8cdce49cda636dbb6e1d13489cce811adff36b761521f71e31214e9","integrity":"sha256-E+7XRfjNzknNpjbbtuHRNInM6BGt/za3YVIfceMSFOk="},"application-dd37038c1f0879ef01c745fd8ef7928f3e45a56984deeabd50392be0a3162d6f.js":{"logical_path":"application.js","mtime":"2017-01-21T16:54:30+06:30","size":459409,"digest":"dd37038c1f0879ef01c745fd8ef7928f3e45a56984deeabd50392be0a3162d6f","integrity":"sha256-3TcDjB8Iee8Bx0X9jveSjz5FpWmE3uq9UDkr4KMWLW8="},"application-9db673cb6d4bf3821c5d6ed2f654d4ecb84adecf44609892b94d84b104a4ba68.css":{"logical_path":"application.css","mtime":"2017-01-21T16:54:27+06:30","size":261683,"digest":"9db673cb6d4bf3821c5d6ed2f654d4ecb84adecf44609892b94d84b104a4ba68","integrity":"sha256-nbZzy21L84IcXW7S9lTU7LhK3s9EYJiSuU2EsQSkumg="}},"assets":{"application.js":"application-dd37038c1f0879ef01c745fd8ef7928f3e45a56984deeabd50392be0a3162d6f.js","application.css":"application-9db673cb6d4bf3821c5d6ed2f654d4ecb84adecf44609892b94d84b104a4ba68.css"}} \ No newline at end of file +{"files":{"application-55570bc046304f178e10e45bec721268c8bf8e0a3bc8f2c1f69ff39df39dee3e.js":{"logical_path":"application.js","mtime":"2017-01-19T15:25:44+06:30","size":416216,"digest":"55570bc046304f178e10e45bec721268c8bf8e0a3bc8f2c1f69ff39df39dee3e","integrity":"sha256-VVcLwEYwTxeOEORb7HISaMi/jgo7yPLB9p/znfOd7j4="},"application-13eed745f8cdce49cda636dbb6e1d13489cce811adff36b761521f71e31214e9.css":{"logical_path":"application.css","mtime":"2017-01-19T15:25:44+06:30","size":735,"digest":"13eed745f8cdce49cda636dbb6e1d13489cce811adff36b761521f71e31214e9","integrity":"sha256-E+7XRfjNzknNpjbbtuHRNInM6BGt/za3YVIfceMSFOk="},"application-dd37038c1f0879ef01c745fd8ef7928f3e45a56984deeabd50392be0a3162d6f.js":{"logical_path":"application.js","mtime":"2017-01-21T16:54:30+06:30","size":459409,"digest":"dd37038c1f0879ef01c745fd8ef7928f3e45a56984deeabd50392be0a3162d6f","integrity":"sha256-3TcDjB8Iee8Bx0X9jveSjz5FpWmE3uq9UDkr4KMWLW8="},"application-9db673cb6d4bf3821c5d6ed2f654d4ecb84adecf44609892b94d84b104a4ba68.css":{"logical_path":"application.css","mtime":"2017-01-21T16:54:27+06:30","size":261683,"digest":"9db673cb6d4bf3821c5d6ed2f654d4ecb84adecf44609892b94d84b104a4ba68","integrity":"sha256-nbZzy21L84IcXW7S9lTU7LhK3s9EYJiSuU2EsQSkumg="},"spinner-c1cf81bef2ea82eaa43265a5ff786b7cd74e7d5f4f2de104b586f092ca0fb886.gif":{"logical_path":"spinner.gif","mtime":"2017-02-10T11:04:52+06:30","size":723,"digest":"c1cf81bef2ea82eaa43265a5ff786b7cd74e7d5f4f2de104b586f092ca0fb886","integrity":"sha256-wc+BvvLqguqkMmWl/3hrfNdOfV9PLeEEtYbwksoPuIY="},"application-4b07da359561fb578da05680a32fde19ae94afccd5ce3efeef8c5d0f2caa96df.js":{"logical_path":"application.js","mtime":"2017-03-08T14:08:26+06:30","size":482933,"digest":"4b07da359561fb578da05680a32fde19ae94afccd5ce3efeef8c5d0f2caa96df","integrity":"sha256-SwfaNZVh+1eNoFaAoy/eGa6Ur8zVzj7+74xdDyyqlt8="},"application-e6b1e1192547851708d9ee55b2e3889c54242c9f13777b26a8d33acbd5dedbac.css":{"logical_path":"application.css","mtime":"2017-10-26T17:47:18+06:30","size":260095,"digest":"e6b1e1192547851708d9ee55b2e3889c54242c9f13777b26a8d33acbd5dedbac","integrity":"sha256-5rHhGSVHhRcI2e5VsuOInFQkLJ8Td3smqNM6y9Xe26w="}},"assets":{"application.js":"application-4b07da359561fb578da05680a32fde19ae94afccd5ce3efeef8c5d0f2caa96df.js","application.css":"application-e6b1e1192547851708d9ee55b2e3889c54242c9f13777b26a8d33acbd5dedbac.css","spinner.gif":"spinner-c1cf81bef2ea82eaa43265a5ff786b7cd74e7d5f4f2de104b586f092ca0fb886.gif"}} \ No newline at end of file