-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from TreinaDev/lanca-multas
Implementa Lançamento de Multas pelo Síndico
- Loading branch information
Showing
25 changed files
with
784 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
class FinesController < ApplicationController | ||
before_action :set_condo, only: %i[new create] | ||
before_action :authorize_superintendent, only: %i[new create] | ||
before_action :set_breadcrumbs_for_register, only: %i[new create] | ||
|
||
def new | ||
@fine = SingleCharge.new(condo: @condo) | ||
end | ||
|
||
def create | ||
@fine = SingleCharge.new(fine_params) | ||
|
||
if @fine.valid? | ||
return redirect_to @condo if post_response | ||
|
||
flash.now.alert = t('alerts.single_charge.server_error') | ||
else | ||
flash.now.alert = t('alerts.single_charge.fine_not_created') | ||
end | ||
render 'new', status: :unprocessable_entity | ||
end | ||
|
||
private | ||
|
||
def set_breadcrumbs_for_register | ||
add_breadcrumb @condo.name.to_s, @condo | ||
add_breadcrumb I18n.t('breadcrumb.fine.new') | ||
end | ||
|
||
def authorize_superintendent | ||
return if resident_signed_in? && @condo.superintendent && @condo.superintendent.tenant == current_resident | ||
|
||
redirect_to root_path | ||
end | ||
|
||
def set_condo | ||
@condo = Condo.find params[:condo_id] | ||
end | ||
|
||
def find_tower_and_floor | ||
return unless params['single_charge'] | ||
|
||
tower = Tower.find_by(id: params['single_charge']['tower_id']) | ||
return tower.floors[params['single_charge']['floor'].to_i - 1] if tower | ||
|
||
nil | ||
end | ||
|
||
def find_unit_id | ||
floor = find_tower_and_floor | ||
|
||
return floor.units[params['single_charge']['unit'].to_i - 1].id if floor | ||
|
||
nil | ||
end | ||
|
||
def fine_params | ||
params.require(:single_charge) | ||
.permit(:value, :description) | ||
.merge({ charge_type: :fine, condo: @condo, unit_id: find_unit_id }) | ||
end | ||
|
||
def single_charge_params | ||
{ single_charge: { | ||
description: @fine.description, | ||
value_cents: @fine.value_cents, | ||
charge_type: @fine.charge_type, | ||
issue_date: Time.zone.today, | ||
condo_id: @fine.condo.id, | ||
common_area_id: nil, | ||
unit_id: @fine.unit.id | ||
} } | ||
end | ||
|
||
def post_response | ||
request = Faraday.new(url: Rails.configuration.api['base_url'].to_s) | ||
.post('/api/v1/single_charges/', single_charge_params.to_json, | ||
'Content-Type' => 'application/json') | ||
return flash.notice = "Multa lançada com sucesso para a #{@fine.unit.print_identifier}" if request.success? | ||
|
||
nil | ||
rescue Faraday::ConnectionFailed | ||
nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
|
||
export default class extends Controller { | ||
static targets = ['condo', 'tower', 'floor', 'unit'] | ||
|
||
searchTowers(condoId) { | ||
fetch(`${window.origin}/residents/find_towers?id=${condoId}`) | ||
.then((response) => { | ||
return response.json() | ||
}) | ||
.then((towers) => { | ||
this.towerTarget.innerHTML = "" | ||
towers.forEach(tower => { | ||
this.towerTarget.options.add(new Option(tower.name, tower.id)) | ||
}); | ||
this.towers = towers | ||
this.changeTower() | ||
}) | ||
.catch(() => { console.log('Towers not found') }) | ||
} | ||
|
||
connect() { | ||
this.searchTowers(this.element.getAttribute('condo-id')) | ||
} | ||
|
||
changeTower() { | ||
let tower = this.towers[this.towerTarget.selectedIndex] | ||
console.log(tower) | ||
this.unitTarget.innerHTML = "" | ||
this.floorTarget.innerHTML = "" | ||
|
||
for (let floor = 1; floor <= tower.floor_quantity; floor++) { | ||
this.floorTarget.options.add(new Option(`${floor}`, `${floor}`)) | ||
} | ||
|
||
for (let unit = 1; unit <= tower.units_per_floor; unit++) { | ||
this.unitTarget.options.add(new Option(`${unit}`, `${unit}`)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class SingleCharge < ApplicationRecord | ||
belongs_to :condo | ||
belongs_to :unit | ||
belongs_to :common_area, optional: true | ||
|
||
validates :value_cents, :charge_type, presence: true | ||
validates :description, presence: true, if: -> { charge_type == 'fine' } | ||
|
||
validate :unit_valid? | ||
|
||
enum charge_type: { fine: 0, common_area_fee: 1 } | ||
|
||
monetize :value_cents, as: :value, with_model_currency: :currency | ||
|
||
private | ||
|
||
def unit_valid? | ||
return true if unit&.owner | ||
|
||
errors.add(:unit, 'não possui um proprietário.') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<div class="bg-white rounded-5 py-4 px-5 shadow mt-5"> | ||
<h2 class="text-center mt-4">Lançar Multa</h2> | ||
<%= form_with model: [@condo, @fine], url: condo_fines_path(@condo, @fine) do |f| %> | ||
<div id="form_data" data-controller="fines" condo-id='<%= @condo.id %>'> | ||
<div class="d-flex p-2"> | ||
<div class="form-group col-md-4 pe-3"> | ||
<%= f.label :tower_id %> | ||
<%= f.collection_select :tower_id, {}, {}, {}, {}, :'data-fines-target' => "tower", :'data-action' => "change->fines#changeTower", class: "form-control form-select #{'is-invalid' if @fine.errors[:tower_id].any?}" %> | ||
<%= render("shared/errors", model: @fine, attribute: :tower_id) if @fine.errors[:tower_id].any? %> | ||
</div> | ||
|
||
<div class="form-group col-md-4 pe-3"> | ||
<%= f.label :floor %> | ||
<%= f.collection_select :floor, {}, {}, {}, {}, :'data-fines-target' => "floor", class: "form-control form-select #{'is-invalid' if @fine.errors[:floor].any?}" %> | ||
<%= render("shared/errors", model: @fine, attribute: :floor) if @fine.errors[:floor].any? %> | ||
</div> | ||
|
||
<div class="form-group col-md-4 pe-3"> | ||
<%= f.label :unit %> | ||
<%= f.collection_select :unit, {}, {}, {}, {}, :'data-fines-target' => "unit", class: "form-control form-select #{'is-invalid' if @fine.errors[:unit].any?}" %> | ||
<%= render("shared/errors", model: @fine, attribute: :unit) if @fine.errors[:unit].any? %> | ||
</div> | ||
</div> | ||
<div class="d-flex p-2"> | ||
<div class="form-group col-md-10 pe-2"> | ||
<%= f.label :description, class: "form-label" %> | ||
<%= f.text_field :description, placeholder: 'Descreva o motivo da multa...', class: "form-control #{'is-invalid' if @fine.errors[:description].any?}" %> | ||
<%= render("shared/errors", model: @fine, attribute: :description) if @fine.errors[:description].any? %> | ||
</div> | ||
|
||
<div class="form-group col-md-2 pe-3"> | ||
<%= f.label :value, class: "form-label" %> | ||
<%= f.text_field :value, placeholder: '0,00', step: '0.01', pattern: '\d+(\,\d{2})?', class: "form-control #{'is-invalid' if @fine.errors[:value].any?}" %> | ||
<%= render("shared/errors", model: @fine, attribute: :value) if @fine.errors[:value].any? %> | ||
</div> | ||
</div> | ||
<div class="form-group d-flex justify-content-center"> | ||
<%= f.submit 'Lançar Multa', class: "btn btn-dark rounded-pill px-4 mt-3" %> | ||
</div> | ||
</div> | ||
<% end %> | ||
</div> |
Oops, something went wrong.