From b05114937cf32b247c5d82d9f98ae8dba269f25c Mon Sep 17 00:00:00 2001 From: paladin Date: Wed, 27 Jan 2016 21:36:33 +0700 Subject: [PATCH] tao request --- Gemfile | 2 + Gemfile.lock | 8 +++ .../customer/invoices_controller.rb | 24 ++++++++ .../customer/requests_controller.rb | 3 + .../supplier/requests_controller.rb | 13 +++-- .../supplier/vehicles_controller.rb | 3 +- app/models/setting.rb | 4 ++ app/models/user.rb | 4 +- app/views/customer/invoices/index.html.erb | 43 ++++++++++++++ app/views/layouts/application.html.erb | 11 ++++ app/views/share/request/show.html.erb | 1 + app/views/supplier/home/index.html.erb | 14 +++-- app/views/supplier/requests/_form.html.erb | 0 app/views/supplier/requests/index.html.erb | 32 +++++++++++ bin/delayed_job | 5 ++ config/environment.rb | 1 + config/routes.rb | 4 +- .../20160121130056_create_delayed_jobs.rb | 22 ++++++++ db/schema.rb | 56 ++++++++++++++++++- 19 files changed, 234 insertions(+), 16 deletions(-) create mode 100644 app/controllers/customer/invoices_controller.rb create mode 100644 app/models/setting.rb create mode 100644 app/views/customer/invoices/index.html.erb create mode 100644 app/views/supplier/requests/_form.html.erb create mode 100644 app/views/supplier/requests/index.html.erb create mode 100755 bin/delayed_job create mode 100644 db/migrate/20160121130056_create_delayed_jobs.rb diff --git a/Gemfile b/Gemfile index d5e61b9..74c1734 100644 --- a/Gemfile +++ b/Gemfile @@ -21,6 +21,8 @@ gem "sdoc", "~> 0.4.0", group: :doc gem "carrierwave" gem "mini_magick" +gem 'will_paginate', '>= 3.0.pre' +gem 'delayed_job_active_record' group :development, :test do gem "byebug" diff --git a/Gemfile.lock b/Gemfile.lock index e2a8fb4..d6d20a2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -63,6 +63,11 @@ GEM execjs coffee-script-source (1.9.1.1) debug_inspector (0.0.2) + delayed_job (4.1.1) + activesupport (>= 3.0, < 5.0) + delayed_job_active_record (4.1.0) + activerecord (>= 3.0, < 5) + delayed_job (>= 3.0, < 5) devise (3.5.2) bcrypt (~> 3.0) orm_adapter (~> 0.1) @@ -166,6 +171,7 @@ GEM binding_of_caller (>= 0.7.2) railties (>= 4.0) sprockets-rails (>= 2.0, < 4.0) + will_paginate (3.0.7) PLATFORMS ruby @@ -176,6 +182,7 @@ DEPENDENCIES byebug carrierwave coffee-rails (~> 4.1.0) + delayed_job_active_record devise jbuilder (~> 2.0) jquery-rails @@ -190,6 +197,7 @@ DEPENDENCIES turbolinks uglifier (>= 1.3.0) web-console (~> 2.0) + will_paginate (>= 3.0.pre) BUNDLED WITH 1.10.6 diff --git a/app/controllers/customer/invoices_controller.rb b/app/controllers/customer/invoices_controller.rb new file mode 100644 index 0000000..a09a5a9 --- /dev/null +++ b/app/controllers/customer/invoices_controller.rb @@ -0,0 +1,24 @@ +class Customer::InvoicesController < Customer::BaseController + before_action :authenticate_user! + + + def index + @invoices = Invoice.where(:request_id => (Request.select(:request_id).where(:customer_id => Customer.select(:customer_id).find_by_user_id(current_user.id)))) + end + + def accept + @invoice = Invoice.find_by_invoice_id(params[:invoice_id]) + @request = Request.find_by_request_id(:request_id => @invoice.request_id) + if @invoice.nil? + flash[:danger] = "No request found" + redirect_to invoices_path + else + @invoice.status = 'accepted' + @invoice.save + @request.status = 'pending' + @request.save + flash[:notice] = "Successful accept response" + redirect_to controller: "invoices", action: "index" + end + end +end \ No newline at end of file diff --git a/app/controllers/customer/requests_controller.rb b/app/controllers/customer/requests_controller.rb index f5965b6..14d539a 100644 --- a/app/controllers/customer/requests_controller.rb +++ b/app/controllers/customer/requests_controller.rb @@ -29,6 +29,7 @@ def create @request.start_point_long = submit_params[:start_point_long].to_f @request.end_point_lat = submit_params[:end_point_lat].to_f @request.end_point_long = submit_params[:end_point_long].to_f + @request.status = "none" distance = GoogleAPI.new().distanceEstimate(@request.start_point_lat, @request.start_point_long, @@ -91,4 +92,6 @@ def validate_customer redirect_to "root_path" end end + + end diff --git a/app/controllers/supplier/requests_controller.rb b/app/controllers/supplier/requests_controller.rb index e3a152c..3cb404f 100644 --- a/app/controllers/supplier/requests_controller.rb +++ b/app/controllers/supplier/requests_controller.rb @@ -2,6 +2,10 @@ class Supplier::RequestsController < Supplier::BaseController before_action :authenticate_user! before_action :request_is_approved? + def index + @requests = Request.all + end + def show @request = Request.find_by_request_id params[:id] @breadcrumb = [current_user.role,"detailed request"] @@ -18,7 +22,8 @@ def approve newInvoice = @request.invoices.build #In this context current user always be supplier #FIXME: Check role ? - newInvoice.supplier_id = current_user.id + newInvoice.supplier_id = Supplier.select(:supplier_id).find_by_user_id(current_user.id) + newInvoice.status = "pending" newInvoice.offer_price = params[:offer_price] newInvoice.message = params[:message] newInvoice.save @@ -30,11 +35,11 @@ def approve private def request_is_approved? - invoice = Invoice.find_by request_id: params[:id], supplier_id: current_user.id + invoice = Invoice.find_by request_id: params[:id], supplier_id: Supplier.select(:supplier_id).find_by_user_id(current_user.id) if invoice.nil? - @show_approve_form = false - else @show_approve_form = true + else + @show_approve_form = false end end end diff --git a/app/controllers/supplier/vehicles_controller.rb b/app/controllers/supplier/vehicles_controller.rb index df25d30..57fd5f1 100644 --- a/app/controllers/supplier/vehicles_controller.rb +++ b/app/controllers/supplier/vehicles_controller.rb @@ -1,6 +1,7 @@ class Supplier::VehiclesController < Supplier::BaseController def index - @vehicles = Vehicle.all + @vehicles = Vehicle.all.paginate(:page=>params[:page], :per_page => 10) + #@orders = Order.all.order('created_at DESC').paginate(:page=>params[:page], :per_page => 10) end def show diff --git a/app/models/setting.rb b/app/models/setting.rb new file mode 100644 index 0000000..2974575 --- /dev/null +++ b/app/models/setting.rb @@ -0,0 +1,4 @@ +class Setting < ActiveRecord::Base + self.table_name = "setting" + +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index e275784..0718e5a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,8 +7,8 @@ class User < ActiveRecord::Base def get_detailed_info if self.role == "customer" Customer.find_by user_id: self.id - elsif self.role == "provider" - Provider.find_by user_id: self.id + elsif self.role == "supplier" + Supplier.find_by user_id: self.id end end diff --git a/app/views/customer/invoices/index.html.erb b/app/views/customer/invoices/index.html.erb new file mode 100644 index 0000000..80db83e --- /dev/null +++ b/app/views/customer/invoices/index.html.erb @@ -0,0 +1,43 @@ +
+
+ Link + + + +
+
+






+
+
+ + + + + + + + + + + <% @invoices.each do |invoice|%> + <% if invoice.status == "pending" %> + + <%= form_tag("/customer/accept_invoice", method: "post", class: "") do %> + <%= hidden_field_tag :invoice_id, invoice.id %> + + + + + + <% end %> + + + <% end %> + <% end %> + + +
Invoice id supplier id offer price message
<%= invoice.id %><%= invoice.supplier_id %><%= invoice.offer_price %><%= invoice.message %> + <%= submit_tag("Accept", class: "btn btn-default") %> +
+
+
\ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 8b2c9b6..8eca03f 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -19,6 +19,17 @@ <%= link_to image_tag("img_home.png"), root_path, class: "navbar-brand", style: "margin-bottom: 10px" %> + <% if current_user.role == "supplier" %> +
<%= link_to "You have " + (Request.where(:status => "none").count - Invoice.where(:supplier_id => Supplier.select(:supplier_id).find_by_user_id(current_user.id)).count).to_s + " request(s)", supplier_profile_path %>

+
abc <%= Invoice.where(:supplier_id => (Supplier.select(:supplier_id).find_by_user_id(current_user.id)), :status => "accepted").count%>
+ <% end %> + + <% if current_user.role == "customer" %> +
+ <%= link_to "You have " + Invoice.where(:request_id => (Request.select(:request_id).where(:customer_id => Customer.select(:customer_id).find_by_user_id(current_user.id))), :status => "pending").count.to_s + " response(s)", customer_invoices_path %> + +
+ <% end %> <% elsif %> + <% end %> diff --git a/app/views/supplier/home/index.html.erb b/app/views/supplier/home/index.html.erb index 4617901..52af43c 100644 --- a/app/views/supplier/home/index.html.erb +++ b/app/views/supplier/home/index.html.erb @@ -22,15 +22,19 @@ <% @requests.each do |item| %> - <%= item.id %> - <%= item.customer.user.email %> - This text is static content.... - - + <% invoice = Invoice.find_by request_id: item.id, supplier_id: Supplier.select(:supplier_id).find_by_user_id(current_user.id) %> + <% if invoice.nil? %> + <%= item.id %> + <%= item.customer.user.email %> + This text is static content.... + + + <% if item.status == "none" %> This request curently open, <%= link_to "more...", supplier_request_path(item.id) %> <% end %> + <% end %> <% end %> diff --git a/app/views/supplier/requests/_form.html.erb b/app/views/supplier/requests/_form.html.erb new file mode 100644 index 0000000..e69de29 diff --git a/app/views/supplier/requests/index.html.erb b/app/views/supplier/requests/index.html.erb new file mode 100644 index 0000000..8158d67 --- /dev/null +++ b/app/views/supplier/requests/index.html.erb @@ -0,0 +1,32 @@ +
+

View all requests

+
+ +
+ + + + + + + + + + + + <% @requests.each do |request| %> + + + + + + + + <% end %> + +
#Date createFromToAction
<%= request.id %><%= request.created_at %>Ha NoiHo Chi Minh + <%= link_to "", supplier_request_path(request), class: "glyphicon glyphicon-eye-open" %>   + <%= link_to "", edit_supplier_request_path(request), class: "glyphicon glyphicon-pencil" %>   + +
+
\ No newline at end of file diff --git a/bin/delayed_job b/bin/delayed_job new file mode 100755 index 0000000..edf1959 --- /dev/null +++ b/bin/delayed_job @@ -0,0 +1,5 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment')) +require 'delayed/command' +Delayed::Command.new(ARGV).daemonize diff --git a/config/environment.rb b/config/environment.rb index ee8d90d..ce16aad 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -3,3 +3,4 @@ # Initialize the Rails application. Rails.application.initialize! +#config.active_job.queue_adapter = :delayed_job diff --git a/config/routes.rb b/config/routes.rb index 24ebc87..02495ed 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,7 +13,7 @@ get "profile" => "home#index" post "approve_request" => "requests#approve" resources :vehicles - resources :requests, only: [:show] + resources :requests end namespace :admin do @@ -22,6 +22,8 @@ namespace :customer do resources :requests + resources :invoices + post "accept_invoice" => "invoices#accept" get "profile" => "profile#show" end end diff --git a/db/migrate/20160121130056_create_delayed_jobs.rb b/db/migrate/20160121130056_create_delayed_jobs.rb new file mode 100644 index 0000000..27fdcf6 --- /dev/null +++ b/db/migrate/20160121130056_create_delayed_jobs.rb @@ -0,0 +1,22 @@ +class CreateDelayedJobs < ActiveRecord::Migration + def self.up + create_table :delayed_jobs, force: true do |table| + table.integer :priority, default: 0, null: false # Allows some jobs to jump to the front of the queue + table.integer :attempts, default: 0, null: false # Provides for retries, but still fail eventually. + table.text :handler, null: false # YAML-encoded string of the object that will do work + table.text :last_error # reason for last failure (See Note below) + table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future. + table.datetime :locked_at # Set when a client is working on this object + table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead) + table.string :locked_by # Who is working on this object (if locked) + table.string :queue # The name of the queue this job is in + table.timestamps null: true + end + + add_index :delayed_jobs, [:priority, :run_at], name: "delayed_jobs_priority" + end + + def self.down + drop_table :delayed_jobs + end +end diff --git a/db/schema.rb b/db/schema.rb index c2ab9b8..f665692 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20151105025317) do +ActiveRecord::Schema.define(version: 20160121130056) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -33,9 +33,44 @@ t.string "tel", limit: 20 end + create_table "delayed_jobs", force: :cascade do |t| + t.integer "priority", default: 0, null: false + t.integer "attempts", default: 0, null: false + t.text "handler", null: false + t.text "last_error" + t.datetime "run_at" + t.datetime "locked_at" + t.datetime "failed_at" + t.string "locked_by" + t.string "queue" + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "delayed_jobs", ["priority", "run_at"], name: "delayed_jobs_priority", using: :btree + + create_table "invoices", primary_key: "invoice_id", force: :cascade do |t| + t.integer "supplier_id" + t.integer "vehicle_id" + t.integer "schedule_id" + t.integer "request_id" + t.float "offer_price" + t.string "status", limit: 15 + t.datetime "created_at" + t.datetime "updated_at" + t.string "message", limit: 500 + end + # Could not dump table "location" because of following StandardError # Unknown type 'geometry(Point,4269)' for column 'point' + create_table "message", force: :cascade do |t| + t.string "table_name", limit: 30 + t.integer "table_id" + t.string "article", limit: 50 + t.string "content", limit: 500 + end + create_table "properties", primary_key: "property_id", force: :cascade do |t| t.string "name", limit: 60 t.string "unit", limit: 5 @@ -56,6 +91,11 @@ t.datetime "time" t.datetime "created_at" t.datetime "updated_at" + t.decimal "start_point_lat" + t.decimal "start_point_long" + t.decimal "end_point_lat" + t.decimal "end_point_long" + t.integer "distance_estimate" end create_table "schedule", primary_key: "schedule_id", force: :cascade do |t| @@ -63,6 +103,11 @@ t.integer "request_id" end + create_table "setting", force: :cascade do |t| + t.string "name", limit: 15 + t.integer "value" + end + create_table "spatial_ref_sys", primary_key: "srid", force: :cascade do |t| t.string "auth_name", limit: 256 t.integer "auth_srid" @@ -70,11 +115,12 @@ t.string "proj4text", limit: 2048 end - create_table "supplier", primary_key: "s_id", force: :cascade do |t| + create_table "supplier", primary_key: "supplier_id", force: :cascade do |t| t.string "name", limit: 60 t.string "address", limit: 60 t.integer "tel", limit: 8 t.string "email", limit: 60 + t.integer "user_id" end create_table "trip", id: false, force: :cascade do |t| @@ -125,11 +171,15 @@ add_foreign_key "abstract_trip", "location", column: "end_point", primary_key: "location_id", name: "abstract_trip_end_point_fkey" add_foreign_key "abstract_trip", "location", column: "start_point", primary_key: "location_id", name: "abstract_trip_start_point_fkey" add_foreign_key "abstract_trip", "vehicle_category", column: "category_id", name: "abstract_trip_category_id_fkey" + add_foreign_key "invoices", "request", primary_key: "request_id", name: "invoice_request_id_fkey" + add_foreign_key "invoices", "schedule", primary_key: "schedule_id", name: "invoice_schedule_id_fkey" + add_foreign_key "invoices", "supplier", primary_key: "supplier_id", name: "invoice_supplier_id_fkey" + add_foreign_key "invoices", "vehicle", primary_key: "vehicle_id", name: "invoice_vehicle_id_fkey" add_foreign_key "request", "customer", primary_key: "customer_id", name: "request_cus_id_fkey" add_foreign_key "schedule", "request", primary_key: "request_id", name: "schedule_request_id_fkey" add_foreign_key "trip", "schedule", primary_key: "schedule_id", name: "trip_schedule_id_fkey" add_foreign_key "v_category_properties", "properties", primary_key: "property_id", name: "v_category_properties_property_id_fkey" add_foreign_key "v_category_properties", "vehicle_category", column: "category_id", name: "v_category_properties_v_category_id_fkey" - add_foreign_key "vehicle", "supplier", column: "s_id", primary_key: "s_id", name: "vehicle_s_id_fkey" + add_foreign_key "vehicle", "supplier", column: "s_id", primary_key: "supplier_id", name: "vehicle_s_id_fkey" add_foreign_key "vehicle", "vehicle_category", column: "category_id", name: "vehicle_category_id_fkey" end