Skip to content

Commit

Permalink
[DEMAD-354] Hojas de firmas y cambios
Browse files Browse the repository at this point in the history
  • Loading branch information
vanessasotob committed May 4, 2022
1 parent 26a5be6 commit 23a2a3f
Show file tree
Hide file tree
Showing 19 changed files with 337 additions and 188 deletions.
4 changes: 2 additions & 2 deletions app/assets/stylesheets/custom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3199,8 +3199,8 @@ h3 {

.expanded.budget.budget-heading,
.jumbo-budget.budget-heading {
min-height: 25rem;
min-height: 13rem;
}
.progress-bar-nav.is-fixed {
height: 22rem;
height: 13rem;
}
36 changes: 27 additions & 9 deletions app/controllers/admin/signature_sheets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,50 @@ def create
@signature_sheet = SignatureSheet.new(signature_sheet_params)
@signature_sheet.author = current_user
if @signature_sheet.signable_type == 'User'
@signature_sheet.signable_id = User.by_username_email_or_document_number(@signature_sheet.signable_id).first.id
@signature_sheet.signable_id = User.by_username_email_or_document_number(params[:signature_sheet][:signable_id]).first.id
@signature_sheet.document_numbers = params[:signature_sheet][:signable_id]
end
if @signature_sheet.save
if ["Proposal","Budget::Investment"].include?(@signature_sheet.signable_type)
@signature_sheet.delay.verify_signatures
end
@signature_sheet.signable_type = "Budget::Investment" if params[:signature_sheet][:signable_type].include? "Budget::Investment2"
if params[:vote_positive].to_s == "true"
@signature_sheet.type_vote = true
elsif params[:vote_negative].to_s == "true"
@signature_sheet.type_vote = false
end
if @signature_sheet.save
@signature_sheet.delay.verify_signatures
redirect_to [:admin, @signature_sheet], notice: I18n.t("flash.actions.create.signature_sheet")
else
render :new
end
rescue => e
begin
Rails.logger.error("Error en la creación de signature_sheets: #{e}")
rescue
end
end

def show
@signature_sheet = SignatureSheet.find(params[:id])
if ["Proposal","Budget::Investment"].include?(@signature_sheet.signable_type)
@voted_signatures = Vote.where(signature: @signature_sheet.signatures.verified).count
if @signature_sheet.type_vote.blank?
@voted_signatures = Vote.where(signature: @signature_sheet.signatures.verified).count
else
@voted_signatures = Budget::Ballot::Line.where(signature: @signature_sheet.signatures.verified).count
end
else
@user_or_budget = @signature_sheet.signable_type == "User" ? User.find(@signature_sheet.signable_id) : Budget::Investment.find(@signature_sheet.signable_id)
@users_voted_budget_investment = @signature_sheet.signable_type == "Vote" ? User.where("id in (?)", Budget::Ballot.joins(:lines).where("budget_ballot_lines.investment_id = ? and budget_ballots.budget_id = ?", @signature_sheet.signable_id, Budget.last.id).pluck("budget_ballots.user_id").uniq) : Budget.last.investments.where("id in (?)", Budget::Ballot.joins(:lines).where("budget_ballots.user_id = ? and budget_ballots.budget_id = ?", @signature_sheet.signable_id, Budget.last.id).pluck("budget_ballot_lines.investment_id").uniq)
@voted_signatures = Budget::Ballot::Line.where(signature: @signature_sheet.signatures.verified).count
end
rescue => e
begin
Rails.logger.error("Error en el show de signature_sheets: #{e}")
rescue
end
end

private

def signature_sheet_params
params.require(:signature_sheet).permit(:signable_type, :signable_id, :document_numbers)
params.require(:signature_sheet).permit(:signable_type, :signable_id, :document_numbers, :investment_ids, :type_vote)
end

def resource_model
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/signature_sheets_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module SignatureSheetsHelper
def signable_options
[[t("activerecord.models.proposal", count: 1), Proposal],
[t("activerecord.models.budget/investment", count: 1), Budget::Investment],
[t("activerecord.models.budget/investment.vote", count: 1), Vote],
[t("activerecord.models.budget/investment.vote", count: 1), "Budget::Investment2"],
[t("activerecord.models.user.votes", count: 1), User]]
end

Expand Down
58 changes: 54 additions & 4 deletions app/models/signature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Signature < ApplicationRecord

belongs_to :signature_sheet
belongs_to :user
belongs_to :investment, class_name: "Budget::Investment"

validates :document_number, presence: true
validates :signature_sheet, presence: true
Expand All @@ -17,14 +18,24 @@ class Signature < ApplicationRecord
def verify
if find_or_create_user?
assign_vote_to_user
mark_as_verified
else
mark_as_unverified
end
rescue => e
begin
Rails.logger.error("Error en el verify: #{e}")
rescue
end
end

def find_or_create_user?
self.user = find_user || create_user
if signable.is_a? User
self.investment = Budget::Investment.where(id: investment_id).first
return true if self.investment
end
return true if self.user
return false
end

def find_user
Expand Down Expand Up @@ -91,11 +102,50 @@ def set_user

def assign_vote_to_user
if signable.is_a? Budget::Investment
signable.vote_by(voter: user, vote: "yes") if can_sign?
else
if signature_sheet.type_vote.blank?
signable.vote_by(voter: user, vote: "yes") if can_sign?
mark_as_verified
else
find_assign_signature_balloting
end
elsif signable.is_a? Proposal
signable.register_vote(user, "yes")
mark_as_verified
elsif signable.is_a? User
find_assign_signature_balloting
end
if signature_sheet.type_vote.blank?
assign_signature_to_vote
end
rescue => e
begin
Rails.logger.error("Error en el assign_vote_to_user: #{e}")
rescue
end
end

def find_assign_signature_balloting
investment ||= signature_sheet.signable
query = Budget::Ballot.where(user: user, budget: investment.budget)
ballot = investment.budget.balloting? ? query.first_or_create : query.first_or_initialize
if !ballot.blank?
lines = ballot.lines.where("budget_ballot_lines.investment_id= ? and budget_ballot_lines.ballot_id= ?", investment, ballot.id)
if signature_sheet.type_vote == true
if lines.blank? && (ballot.amount_available(Budget::Heading.find(investment.heading_id))>= (investment.price.to_i != 0 ? investment.price : investment.total_amount)) && ballot.add_investment(investment, nil )
mark_as_verified
else
mark_as_unverified
end
else
if lines.blank? && (ballot.amount_available_negative(Budget::Heading.find(investment.heading_id)) >= (investment.price.to_i != 0 ? investment.price : investment.total_amount)) && ballot.add_investment(investment, "against")
mark_as_verified
else
mark_as_unverified
end
end
else
mark_as_unverified
end
assign_signature_to_vote
end

def assign_signature_to_vote
Expand Down
44 changes: 34 additions & 10 deletions app/models/signature_sheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,54 @@ class SignatureSheet < ApplicationRecord
belongs_to :signable, polymorphic: true
belongs_to :author, class_name: "User", foreign_key: "author_id"

VALID_SIGNABLES = %w[Proposal Budget::Investment Vote User]
VALID_SIGNABLES = %w[Proposal Budget::Investment User]

has_many :signatures

validates :author, presence: true
validates :signable_type, inclusion: {in: VALID_SIGNABLES}
validates :document_numbers, presence: true, if: -> { need_documents? }
validates :document_numbers, presence: true, if: -> { !need_documents? }
validates :investment_ids, presence: true, if: -> { need_documents? }
validates :signable, presence: true
validate :signable_found
validates :type_vote, presence: true, if: -> { need_type? }

def name
if ["Proposal","Budget::Investment"].include?(signable_type)
"#{signable_name} #{signable_id}"
elsif signable_type.include?("Vote")
"Votaciones del proyecto de gasto #{signable_id}"
else
"Votaciones del usuario #{signable_id} a proyectos de gasto"
if !type_vote.blank?
name = "Votaciones de usuario al proyecto de gasto #{signable_id}"
else
name = "#{signable_name} #{signable_id}"
end
end
if signable_type.include?("User")
name = "Votaciones del usuario con DNI #{parsed_document_numbers.first} a proyectos de gasto"
end
name
end

def signable_name
I18n.t("activerecord.models.#{signable_type.underscore}", count: 1)
end

def verify_signatures
parsed_document_numbers.each do |document_number|
signature = signatures.where(document_number: document_number).first_or_create
signature.verify
if ["Proposal","Budget::Investment"].include?(signable_type)
parsed_document_numbers.each do |document_number|
signature = signatures.where(document_number: document_number).first_or_create
signature.verify
end
else
investment_ids.split(',').each do |investment_id|
signature = signatures.where(investment_id: investment_id.to_i, document_number: parsed_document_numbers.first ).first_or_create
signature.verify
end
end
update(processed: true)
rescue => e
begin
Rails.logger.error("Error en el verify_signatures: #{e}")
rescue
end
end

def parsed_document_numbers
Expand All @@ -48,4 +66,10 @@ def need_documents?
true if ["Proposal","Budget::Investment"].include?(signable_type)
false
end

def need_type?
true if ["User"].include?(signable_type)
false
end

end
40 changes: 36 additions & 4 deletions app/views/admin/signature_sheets/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,51 @@
<p class="help-text" id="document-numbers-help-text"><%= t("admin.signature_sheets.new.document_numbers_note") %></p>
<%= f.text_area :document_numbers, rows: "6", label: false, aria: {describedby: "document-numbers-help-text"} %>
</div>


<div id= "investment" style= "display:none">
<%= f.label :investment_ids %>
<p class="help-text" id="document-numbers-help-text"><%= t("admin.signature_sheets.new.document_numbers_note") %></p>
<%= f.text_area :investment_ids, rows: "6", label: false, aria: {describedby: "document-numbers-help-text"} %>
</div>

<div class= "check-box" style="display:none;">
<div class="row">
<div class="small-12 medium-2 column end">
<%=label_tag :vote_positive, "Voto positivo"%>
<%= check_box_tag :vote_positive, true, false, id: "one"%>
</div>
<div class="small-12 medium-2 column end">
<%=label_tag :vote_negative, "Voto negativo"%>
<%= check_box_tag :vote_negative, true, false, id: "two"%>
</div>
</div>
</div>

<%= f.submit(class: "button", value: t("admin.signature_sheets.new.submit")) %>
<% end %>

<script>
$('#signature_sheet_signable_type').on('change', function() {
if (this.value == "Vote" || this.value == "User")
if (this.value == "Budget::Investment2" || this.value == "User")
{
$('#documents').hide();
$('#one').prop('checked', true);
$('.check-box').show();
if (this.value == "User")
{
$('#investment').show();
$('#documents').hide();
} else {
$('#investment').hide();
$('#documents').show();
}
} else {
$('#documents').show();
$('.check-box').hide();
}
});

$(document).on('click', 'input[type="checkbox"]', function() {
$('input[type="checkbox"]').not(this).prop('checked', false);
});
</script>

0 comments on commit 23a2a3f

Please sign in to comment.