From f7737ef653da245cb8a6cd43d4c1964176cc95ae Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Wed, 29 Mar 2017 15:09:14 +0800 Subject: [PATCH] added create purchase and receipts --- .../admin/client_managements_controller.rb | 41 ++++++++++ app/models/contract_purchase.rb | 21 +++++ app/models/purchase_receipt.rb | 24 ++++++ app/models/request_contract.rb | 5 +- .../_purchase_form.html.erb | 55 +++++++++++++ .../_site_spec_form.html.erb | 17 +++- .../client_managements/add_receipt.html.erb | 15 ++++ .../client_managements/edit_receipt.html.erb | 15 ++++ .../admin/client_managements/index.html.erb | 2 - .../client_managements/receipts.html.erb | 81 +++++++++++++++++++ .../client_managements/see_contract.html.erb | 38 ++++++++- .../client_managements/see_contract.html.erb | 11 ++- config/locales/en.yml | 7 +- config/locales/zh_tw.yml | 7 +- config/routes.rb | 5 ++ 15 files changed, 331 insertions(+), 13 deletions(-) create mode 100644 app/models/contract_purchase.rb create mode 100644 app/models/purchase_receipt.rb create mode 100644 app/views/admin/client_managements/_purchase_form.html.erb create mode 100644 app/views/admin/client_managements/add_receipt.html.erb create mode 100644 app/views/admin/client_managements/edit_receipt.html.erb create mode 100644 app/views/admin/client_managements/receipts.html.erb diff --git a/app/controllers/admin/client_managements_controller.rb b/app/controllers/admin/client_managements_controller.rb index 3d622d5..489aec2 100644 --- a/app/controllers/admin/client_managements_controller.rb +++ b/app/controllers/admin/client_managements_controller.rb @@ -66,6 +66,43 @@ class Admin::ClientManagementsController < OrbitAdminController end def make_purchase + contract = RequestContract.find(params[:id]) rescue nil + if !contract.nil? + purchase = ContractPurchase.new + purchase.request_contract = contract + purchase.total_amount = contract.total_amount + purchase.save + end + redirect_to see_contract_admin_client_management_path(contract.id) + end + + def receipts + @purchase = ContractPurchase.find(params[:id]) rescue nil + @contract = @purchase.request_contract + @total_received_amount = @purchase.total_amount_recieved + @total_adjustable_amount = @purchase.total_adjusted_amount + end + + def add_receipt + @receipt = PurchaseReceipt.new + @purchase = ContractPurchase.find(params[:id]) rescue nil + end + + def create_receipt + receipt = PurchaseReceipt.create(receipt_params) + redirect_to receipts_admin_client_management_path(receipt.contract_purchase.id) + end + + def edit_receipt + @receipt = PurchaseReceipt.find(params[:id]) + @purchase = @receipt.contract_purchase rescue nil + end + + def update_receipt + receipt = PurchaseReceipt.find(params[:id]) + receipt.update_attributes(receipt_params) + receipt.save + redirect_to receipts_admin_client_management_path(receipt.contract_purchase.id) end def add_sites @@ -89,6 +126,10 @@ class Admin::ClientManagementsController < OrbitAdminController private + def receipt_params + params.require(:purchase_receipt).permit! + end + def contract_params params.require(:request_contract).permit! end diff --git a/app/models/contract_purchase.rb b/app/models/contract_purchase.rb new file mode 100644 index 0000000..a45dc96 --- /dev/null +++ b/app/models/contract_purchase.rb @@ -0,0 +1,21 @@ +class ContractPurchase + include Mongoid::Document + include Mongoid::Timestamps + + field :total_amount, type: Integer, :default => 0 + + belongs_to :request_contract + has_many :purchase_receipts + + def total_amount_recieved + self.purchase_receipts.sum(:amount_received) + end + + def total_adjusted_amount + self.purchase_receipts.sum(:adjustable_amount) + end + + def cleared? + self.total_amount == (self.total_amount_recieved + self.total_adjusted_amount) + end +end \ No newline at end of file diff --git a/app/models/purchase_receipt.rb b/app/models/purchase_receipt.rb new file mode 100644 index 0000000..ef1bf4f --- /dev/null +++ b/app/models/purchase_receipt.rb @@ -0,0 +1,24 @@ +class PurchaseReceipt + include Mongoid::Document + include Mongoid::Timestamps + + mount_uploader :receipt_file, AssetUploader + mount_uploader :received_receipt_file, AssetUploader + + field :amount, type: Integer, :default => 0 + field :amount_received, type: Integer, :default => 0 + field :adjustable_amount, type: Integer, :default => 0 + field :adjustable_amount_detail + field :amount_received_on, type: DateTime + field :receipt_number + + belongs_to :contract_purchase + + def amount_pending + self.amount - (self.amount_received + self.adjustable_amount) + end + + def is_pending? + self.amount_pending > 0 + end +end \ No newline at end of file diff --git a/app/models/request_contract.rb b/app/models/request_contract.rb index 9ec1696..0fd3417 100644 --- a/app/models/request_contract.rb +++ b/app/models/request_contract.rb @@ -11,15 +11,18 @@ class RequestContract field :template_cost, type: Integer, :default => 0 field :customized_template_cost, type: Integer, :default => 0 field :rwd_cost, type: Integer, :default => 0 + field :other + field :other_cost, type: Integer, :default => 0 field :confirmed, type: Boolean, :default => false mount_uploader :contract_file, AssetUploader mount_uploader :signed_contract_file, AssetUploader has_one :site_request + has_one :contract_purchase def total_amount - self.new_site_cost + self.hosting_with_rulingdigital_cost + self.service_cost + self.option_module_cost + self.customized_module_cost + self.template_cost + self.customized_template_cost + self.rwd_cost + self.new_site_cost + self.hosting_with_rulingdigital_cost + self.service_cost + self.option_module_cost + self.customized_module_cost + self.template_cost + self.customized_template_cost + self.rwd_cost + self.other_cost end def is_confirmed? diff --git a/app/views/admin/client_managements/_purchase_form.html.erb b/app/views/admin/client_managements/_purchase_form.html.erb new file mode 100644 index 0000000..2ced525 --- /dev/null +++ b/app/views/admin/client_managements/_purchase_form.html.erb @@ -0,0 +1,55 @@ +
+ <%= f.label :receipt_number, "Receipt Number:" %> + <%= f.text_field :receipt_number %> +
+ +
+ <%= f.label :amount, "Amount:" %> + <%= f.number_field :amount, :min => "0", :max => @purchase.total_amount %> +
+ +<% if !@receipt.receipt_file.url.nil? %> +
+ Receipt File: + Download File +
+<% end %> + +
+ <%= f.label :receipt_file, "Receipt File:" %> + <%= f.file_field :receipt_file %> +
+ +<% if !@receipt.new_record? %> +
+ <%= f.label :amount_received, "Amount Received:" %> + <%= f.number_field :amount_received, :min => "0", :max => @receipt.amount %> +
+
+ <%= f.label :adjustable_amount, "Adjustable Amount:" %> + <%= f.number_field :adjustable_amount, :min => "0" %> +
+
+ <%= f.label :adjustable_amount_detail, "Adjustable Amount Detail:" %> + <%= f.text_field :adjustable_amount_detail %> +
+
+ <%= f.label :amount_received_on, "Amount Received On:" %> + <%= f.datetime_picker :amount_received_on, :no_label => true, :new_record => @receipt.new_record? %> +
+ <% if !@receipt.received_receipt_file.url.nil? %> +
+ Signed Receipt File: + Download File +
+ <% end %> + +
+ <%= f.label :received_receipt_file, "Signed Receipt File:" %> + <%= f.file_field :received_receipt_file %> +
+<% end %> +<% if @receipt.new_record? %> + <%= f.hidden_field :contract_purchase_id, :value => @purchase.id.to_s %> +<% end %> +<%= f.submit "Submit", :class => "btn btn-primary" %> \ No newline at end of file diff --git a/app/views/admin/client_managements/_site_spec_form.html.erb b/app/views/admin/client_managements/_site_spec_form.html.erb index 5c816fa..bc9aa01 100644 --- a/app/views/admin/client_managements/_site_spec_form.html.erb +++ b/app/views/admin/client_managements/_site_spec_form.html.erb @@ -12,13 +12,13 @@ <% else %>
<%= f.label :new_site_cost, "New Site Cost :"%> - <%= f.number_field :new_site_cost, :min => "0" %> + <%= f.number_field :new_site_cost, :min => "0" %>
<% if @site_request.host_with_rulingdigital %> -
+
<%= f.label :hosting_with_rulingdigital_cost, "Host with RulingDigital:" %> - <%= f.number_field :hosting_with_rulingdigital_cost, :min => "0" %> -
+ <%= f.number_field :hosting_with_rulingdigital_cost, :min => "0" %> +
<% end %> <% end %> <% if @site_request.type == "existing" && @site_request.maintenance %> @@ -89,6 +89,15 @@ <% end %> +
+ + <%= f.label :other, "Other Requirements:" %> + <%= f.text_area :other %> + + + <%= f.number_field :other_cost, :min => "0" %> + +
<% if !@contract.contract_file.url.nil? %>
Contract File : diff --git a/app/views/admin/client_managements/add_receipt.html.erb b/app/views/admin/client_managements/add_receipt.html.erb new file mode 100644 index 0000000..9f719cd --- /dev/null +++ b/app/views/admin/client_managements/add_receipt.html.erb @@ -0,0 +1,15 @@ +<% content_for :page_specific_css do %> + <%= stylesheet_link_tag "client_management/backend" %> +<% end %> +<% content_for :page_specific_javascript do %> + <%= javascript_include_tag "lib/bootstrap-datetimepicker" %> + <%= javascript_include_tag "lib/datetimepicker/datetimepicker.js" %> +<% end %> +
+
+ <%= form_for @receipt, :url => {:action => :create_receipt}, :html => {:class => "form-horizontal contract-form"} do |f| %> +

Add Receipt

+ <%= render :partial => "purchase_form", :locals => {:f => f} %> + <% end %> +
+
\ No newline at end of file diff --git a/app/views/admin/client_managements/edit_receipt.html.erb b/app/views/admin/client_managements/edit_receipt.html.erb new file mode 100644 index 0000000..b94200b --- /dev/null +++ b/app/views/admin/client_managements/edit_receipt.html.erb @@ -0,0 +1,15 @@ +<% content_for :page_specific_css do %> + <%= stylesheet_link_tag "client_management/backend" %> +<% end %> +<% content_for :page_specific_javascript do %> + <%= javascript_include_tag "lib/bootstrap-datetimepicker" %> + <%= javascript_include_tag "lib/datetimepicker/datetimepicker.js" %> +<% end %> +
+
+ <%= form_for @receipt, :url => {:action => :update_receipt}, :html => {:class => "form-horizontal contract-form"} do |f| %> +

Add Receipt

+ <%= render :partial => "purchase_form", :locals => {:f => f} %> + <% end %> +
+
\ No newline at end of file diff --git a/app/views/admin/client_managements/index.html.erb b/app/views/admin/client_managements/index.html.erb index 4a3442a..3cd7bd4 100644 --- a/app/views/admin/client_managements/index.html.erb +++ b/app/views/admin/client_managements/index.html.erb @@ -1,5 +1,3 @@ - - diff --git a/app/views/admin/client_managements/receipts.html.erb b/app/views/admin/client_managements/receipts.html.erb new file mode 100644 index 0000000..001c5cf --- /dev/null +++ b/app/views/admin/client_managements/receipts.html.erb @@ -0,0 +1,81 @@ +<% content_for :page_specific_css do %> + <%= stylesheet_link_tag "client_management/backend" %> +<% end %> + +
+
+

+ <% if @purchase.total_amount == (@total_received_amount + @total_adjustable_amount) %> + Received + <% else %> + Pending + <% end %> + Purchase Info +

+
+ Contract Id : + <%= @contract.uid %> +
+
+ Contract Date : + <%= @contract.created_at.strftime("%Y, %B %d") %> +
+
+ Total Amount : + <%= @purchase.total_amount %> NTD. +
+
+ Amount Received : + <%= @total_received_amount %> NTD. +
+
+ Amount Adjusted : + <%= @total_adjustable_amount %> NTD. +
+
+ Amount Pending : + <%= @purchase.total_amount - (@total_received_amount + @total_adjustable_amount) %> NTD. +
+
+
+
+ + + <% ["client_management.receipt_number", "client_management.amount", "client_management.amount_received", "client_management.amount_pending", "client_management.receipt_status", :actions].each_with_index do |f,i| %> + <%= thead(f) %> + <% end %> + + + + <% if !@purchase.purchase_receipts.blank? %> + <% @purchase.purchase_receipts.each do |pr| %> + + + + + + + + + <% end %> + <% else %> + + + + <% end %> + +
<%= pr.receipt_number %><%= pr.amount %><%= pr.amount_received %><%= pr.amount_pending %> + <% if pr.is_pending? %> + Pending + <% else %> + Cleared + <% end %> + Edit
No receipts issued.
+
+
+ <% if @purchase.total_amount > (@total_received_amount + @total_adjustable_amount) %> + Add Receipt + <% end %> + Back to Contract +
+
\ No newline at end of file diff --git a/app/views/admin/client_managements/see_contract.html.erb b/app/views/admin/client_managements/see_contract.html.erb index fc2aa19..b0ac18a 100644 --- a/app/views/admin/client_managements/see_contract.html.erb +++ b/app/views/admin/client_managements/see_contract.html.erb @@ -19,6 +19,18 @@ <%= (@contract.is_confirmed? ? "Yes" : "No").html_safe %>
+ <% if !@contract.contract_purchase.nil? %> +
+ Payment Status : + + <% if @contract.contract_purchase.cleared? %> + Cleared + <% else %> + Pending + <% end %> + +
+ <% end %>

Request

@@ -93,6 +105,16 @@ <% total_amount = total_amount + amount %> <% end %> <% end %> + <% if !@contract.other.nil? || @contract.other_cost > 0 %> +
+ + Other Requirements :
+ <%= nl2br(@contract.other) %> +
+ <%= @contract.other_cost %> +
+ <% total_amount = total_amount + @contract.other_cost %> + <% end %>
Total : <%= total_amount.to_s %> @@ -117,12 +139,22 @@ Edit Contract " class="btn btn-success">Confirm Contract <% else %> - Make Purchase - " class="btn btn-danger">Un-Confirm Contract + <% if @contract.contract_purchase.nil? %> + Make Purchase + " class="btn btn-danger">Un-Confirm Contract + <% else %> + Receipts + <% end %> <% end %> Export Contract Back
- + diff --git a/app/views/client_managements/see_contract.html.erb b/app/views/client_managements/see_contract.html.erb index 9b38b61..ec780dc 100644 --- a/app/views/client_managements/see_contract.html.erb +++ b/app/views/client_managements/see_contract.html.erb @@ -100,7 +100,16 @@ <% end %> <% total_amount = total_amount + amount %> <% end %> - + <% if !@contract.other.nil? || @contract.other_cost > 0 %> +
+ + Other Requirements :
+ <%= nl2br(@contract.other) %> +
+ <%= @contract.other_cost %> +
+ <% total_amount = total_amount + @contract.other_cost %> + <% end %>
Total : <%= total_amount %> diff --git a/config/locales/en.yml b/config/locales/en.yml index fbe71f7..90fce93 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -46,4 +46,9 @@ en: site: Site created_on: Created On loading_history: Loading History - loading_contracts: Loading Contracts \ No newline at end of file + loading_contracts: Loading Contracts + receipt_number: Receipt Number + amount: Total Amount + amount_received: Amount Received + amount_pending: Amount Pending + receipt_status: Receipt Status \ No newline at end of file diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index e0e6e28..cdebed4 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -46,4 +46,9 @@ zh_tw: site: Site created_on: Created On loading_history: Loading History - loading_contracts: Loading Contracts \ No newline at end of file + loading_contracts: Loading Contracts + receipt_number: Receipt Number + amount: Total Amount + amount_received: Amount Received + amount_pending: Amount Pending + receipt_status: Receipt Status \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index ddb6d14..72f6335 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -17,6 +17,11 @@ Rails.application.routes.draw do get "make_purchase" patch "add_sites" get "complete_request" + get "receipts" + get "add_receipt" + get "edit_receipt" + post "create_receipt" + patch "update_receipt" end end end