# capture output
@stdout_stream = capture_stdout do
Rake::Task['prestations:ajouter'].reenable # in case you're going to invoke the same task second time.
- Rake::Task['prestations:ajouter'].invoke(params[:enregistrer])
+ Rake::Task['prestations:ajouter'].invoke(current_user.id, params[:enregistrer])
end
+ end
+
+ def ajout_factures
+ authorize(:admin)
+ end
+
+ def ajout_factures_do
+ authorize(:admin)
+
+ require 'rake'
+ Rake::Task.clear # necessary to avoid tasks being loaded several times in dev mode
+ Rails.application.load_tasks # providing your application name is 'sample'
+
+ # capture output
+ @stdout_stream = capture_stdout do
+ Rake::Task['factures:facturer'].reenable # in case you're going to invoke the same task second time.
+ Rake::Task['factures:facturer'].invoke(current_user.id, params[:enregistrer])
+ end
end
end
# GET /factures
# GET /factures.json
def index
- @factures = Facture.all
+ authorize Facture
+
+ @factures = current_user.organisation.factures
+ @structures = current_user.organisation.structures
+
+ unless params[:structure_id].blank?
+ @factures = @factures.joins(:compte).where(comptes: { structure_id: params[:structure_id] })
+ end
+
+ unless params[:date].blank?
+ @factures = @factures.where("DATE(date) = ?", params[:date])
+ end
+
+ unless params[:search].blank?
+ @factures = @factures.joins(:compte).where("UPPER(comptes.nom) like ? OR factures.réf like ?", "%#{params[:search].upcase}%", "%#{params[:search].upcase}%")
+ end
+
+ @factures = @factures.paginate(page: params[:page])
end
# GET /factures/1
# GET /factures/1.json
def show
+ authorize Facture
end
# GET /factures/new
# DELETE /factures/1
# DELETE /factures/1.json
def destroy
+ authorize Facture
+
+ # marquer les prestations liées à cette facture comme non facturées
+ @facture.prestations.update(facture_id: nil)
+
@facture.destroy
respond_to do |format|
format.html { redirect_to factures_url, notice: 'Facture was successfully destroyed.' }
belongs_to :compte
- has_many :facture_lignes
+ has_many :facture_lignes, dependent: :destroy
+ has_many :prestations
validates :réf, presence: true
validates_uniqueness_of :réf, scope: [:compte_id]
+ self.per_page = 10
+
def self.fabrique_une_référence_facture(index)
"#{Date.today.month}-#{Date.today.year}/#{index}"
end
has_many :prestation_types, inverse_of: :organisation
has_many :tarif_types
has_many :facture_chronos
- has_many :tarifs, through: :tarif_types
has_many :comptes, through: :structures
+ has_many :classrooms, through: :structures
has_many :enfants, through: :comptes
+ has_many :factures, through: :comptes
has_many :prestations, through: :enfants
has_many :reservations, through: :enfants
has_many :absences, through: :enfants
- has_many :classrooms, through: :structures
+ has_many :tarifs, through: :tarif_types
validates :nom, :email, presence: true
end
def ajout_prestations_do?
+ ajout_prestations?
+ end
+
+ def ajout_factures?
user.admin?
end
-
+
+ def ajout_factures_do?
+ ajout_factures?
+ end
+
end
--- /dev/null
+class FacturePolicy < ApplicationPolicy
+ class Scope < Scope
+ def resolve
+ scope.all
+ end
+ end
+end
--- /dev/null
+<div class="pb-2 mt-4 mb-2 border-bottom">
+ <h1><i class="fas fa-calendar-day"></i> Facturation des prestations</h1>
+</div>
+
+<p>
+ <h4>Permet de lancer la routine de FACTURATION des prestations consommées</h4>
+ <small>Cette fonction prend l'ensemble des prestations d'une période pour en générer les factures</small>
+</p>
+<br />
+
+<%= bootstrap_form_with(url: "ajout_factures_do", method: :post, local: true) do |form| %>
+ <%= form.check_box :enregistrer, {custom: :switch, label: "Enregistrer les changements ? (non: visualisation seulement, ne fait rien de définitif :)"} %>
+ <br /><br />
+ <%= form.submit "Lancer", class: "btn btn-success" %>
+<% end %>
+
+
--- /dev/null
+<div class="pb-2 mt-4 mb-2 border-bottom">
+ <h1><i class="fas fa-calendar-day"></i> Facturation des prestations</h1>
+</div>
+
+<pre>
+ <%= simple_format(@stdout_stream) %>
+</pre>
\ No newline at end of file
<% end %>
</li>
<li class="list-group-item">
- _
+ <%= link_to admin_ajout_factures_path, class: "text-dark" do %>
+ <h4><i class="fas fa-calendar-day"></i> Facturation des prestations</h4>
+ <% end %>
</li>
<li class="list-group-item">
_
--- /dev/null
+<tr>
+ <td><%= facture.réf %></td>
+ <td><%= l facture.date %></td>
+ <td><%= l facture.échéance %></td>
+ <td><%= facture.compte.nom %></td>
+ <td><%= l facture.envoyée_le if facture.envoyée_le %></td>
+ <td><%= number_to_currency(facture.montant, unit: '€', precision: 2, format: "%n %u") %></td>
+ <td><%= facture.vérifiée %></td>
+ <td><%= facture.mémo %></td>
+ <td><%= link_to 'Show', facture %></td>
+ <td><%= link_to 'Edit', edit_facture_path(facture) %></td>
+ <td><%= link_to 'Destroy', facture, method: :delete, data: { confirm: 'Are you sure?' } %></td>
+</tr>
-<p id="notice"><%= notice %></p>
+<div class="pb-2 mt-4 mb-2 border-bottom">
+ <h1><i class="fas fa-child"></i> Factures</h1>
+</div>
-<h1>Factures</h1>
+<%= bootstrap_form_tag url: factures_path, method: :get do |f| %>
+ <div class="row">
+ <div class="col-sm-3"><%= f.select :structure_id, options_from_collection_for_select(@structures, 'id', 'nom', params[:structure_id]), { include_blank: true, label: "Structure" }, { class: "selectpicker", onchange: "this.form.submit()" } %></div>
+ <div class="col-sm-2"><%= f.date_field :date, value: params[:date], onchange: "this.form.submit()" %></div>
+ <div class="col"><%= f.text_field :search, value: params[:search], label:"Nom du compte/Référence facture", onchange: "this.form.submit()" %></div>
+ </div>
+<% end %>
-<table>
+
+<table class="table table-striped table-hover">
<thead>
<tr>
- <th>Compte</th>
<th>Réf</th>
<th>Date</th>
<th>échéance</th>
+ <th>Compte</th>
<th>Envoyée le</th>
<th>Montant</th>
<th>Vérifiée</th>
</thead>
<tbody>
- <% @factures.each do |facture| %>
- <tr>
- <td><%= facture.compte %></td>
- <td><%= facture.réf %></td>
- <td><%= facture.date %></td>
- <td><%= facture.échéance %></td>
- <td><%= facture.envoyée_le %></td>
- <td><%= facture.montant %></td>
- <td><%= facture.vérifiée %></td>
- <td><%= facture.mémo %></td>
- <td><%= link_to 'Show', facture %></td>
- <td><%= link_to 'Edit', edit_facture_path(facture) %></td>
- <td><%= link_to 'Destroy', facture, method: :delete, data: { confirm: 'Are you sure?' } %></td>
- </tr>
- <% end %>
+ <%= render @factures %>
</tbody>
</table>
-<br>
+<%= will_paginate @factures, renderer: WillPaginate::ActionView::BootstrapLinkRenderer %>
-<%= link_to 'New Facture', new_facture_path %>
+<br>
\ No newline at end of file
<p>
<strong>Compte:</strong>
- <%= @facture.compte %>
+ <%= @facture.compte.nom %>
</p>
<p>
<p>
<strong>Montant:</strong>
- <%= @facture.montant %>
+ <%= number_to_currency(@facture.montant, unit: '€', precision: 2, format: "%n %u") %>
</p>
<p>
<%= link_to 'Edit', edit_facture_path(@facture) %> |
<%= link_to 'Back', factures_path %>
+
+
+<table class="table">
+ <% @facture.facture_lignes.each do |ligne| %>
+ <tr>
+ <td><%= ligne.qté %></td>
+ <td><%= ligne.prestation_type.nom %></td>
+ <td><%= number_to_currency(ligne.prix, unit: '€', precision: 2, format: "%n %u") %></td>
+ <td><%= number_to_currency(ligne.total, unit: '€', precision: 2, format: "%n %u") %></td>
+ </tr>
+ <% end %>
+</table>
<li class="nav-item <%= (params[:controller]=='prestations' ? 'active' : '') %>">
<%= link_to prestations_index_url, class: "nav-link" do %><i class="fas fa-utensils"></i></span> Prestations<% end %>
</li>
+ <li class="nav-item <%= (params[:controller]=='factures' ? 'active' : '') %>">
+ <%= link_to factures_url, class: "nav-link" do %><i class="fas fa-utensils"></i></span> Factures<% end %>
+ </li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
get 'admin/index'
get 'admin/ajout_prestations'
post 'admin/ajout_prestations_do'
+ get 'admin/ajout_factures'
+ post 'admin/ajout_factures_do'
resources :tarifs
resources :absences
namespace :factures do
desc "Facturation"
- task :facturer, [:current_user, :enregistrer] => :environment do |task, args|
+ task :facturer, [:current_user_id, :enregistrer] => :environment do |task, args|
- puts "current_user id = #{args[:current_user]}"
puts "Enregistrer les modifications !" if args[:enregistrer] == "1"
+
+ puts "current_user id = #{args[:current_user_id]}"
+ user = User.find(args[:current_user_id])
date_début = Date.today.beginning_of_week
date_fin = Date.today.end_of_week
# quels sont les comptes ayants de prestations consommées durant la période ?
comptes = Prestation
- .à_facturer
- .where("date BETWEEN DATE(?) AND (?)", date_début, date_fin)
- .joins(:enfant)
- .group("enfants.compte_id")
- .count(:id)
+ .à_facturer
+ .where("date BETWEEN DATE(?) AND (?)", date_début, date_fin)
+ .joins(:enfant)
+ .group("enfants.compte_id")
+ .select(:id, :nom)
+ .count(:id)
# pour chaque compte, faire le chiffrage des prestations consommées
comptes.each do |id, count|