From 6a6cf89445e99fc7c6be0fefa4a231a8ee936636 Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Thu, 24 Feb 2022 16:07:58 +0100
Subject: [PATCH] close #198

---
 .../server/universities_controller.rb         |  3 +-
 app/models/university.rb                      |  4 +++
 app/models/university/with_invoice.rb         | 32 +++++++++++++++++++
 app/views/server/universities/_form.html.erb  |  9 ++++++
 app/views/server/universities/index.html.erb  |  9 ++++++
 app/views/server/universities/show.html.erb   | 10 ++++++
 config/locales/university/en.yml              |  2 ++
 config/locales/university/fr.yml              |  2 ++
 ...4107_add_invoice_elements_to_university.rb |  7 ++++
 db/schema.rb                                  |  5 ++-
 test/fixtures/universities.yml                |  3 ++
 11 files changed, 84 insertions(+), 2 deletions(-)
 create mode 100644 app/models/university/with_invoice.rb
 create mode 100644 db/migrate/20220224144107_add_invoice_elements_to_university.rb

diff --git a/app/controllers/server/universities_controller.rb b/app/controllers/server/universities_controller.rb
index 5dd42f655..dd43e821a 100644
--- a/app/controllers/server/universities_controller.rb
+++ b/app/controllers/server/universities_controller.rb
@@ -2,6 +2,7 @@ class Server::UniversitiesController < Server::ApplicationController
   load_and_authorize_resource
 
   def index
+    @universities = @universities.ordered
     breadcrumb
   end
 
@@ -58,6 +59,6 @@ class Server::UniversitiesController < Server::ApplicationController
   end
 
   def university_params
-    params.require(:university).permit(:name, :address, :zipcode, :city, :country, :private, :identifier, :logo, :logo_delete, :sms_sender_name)
+    params.require(:university).permit(:name, :address, :zipcode, :city, :country, :private, :identifier, :logo, :logo_delete, :sms_sender_name, :invoice_date, :invoice_amount)
   end
 end
diff --git a/app/models/university.rb b/app/models/university.rb
index ee253140a..7e65310db 100644
--- a/app/models/university.rb
+++ b/app/models/university.rb
@@ -7,6 +7,9 @@
 #  city              :string
 #  country           :string
 #  identifier        :string
+#  invoice_amount    :string
+#  invoice_date      :date
+#  invoice_date_yday :integer
 #  mail_from_address :string
 #  mail_from_name    :string
 #  name              :string
@@ -21,6 +24,7 @@ class University < ApplicationRecord
   include WithCommunication
   include WithEducation
   include WithIdentifier
+  include WithInvoice
   include WithResearch
   include WithUsers
 
diff --git a/app/models/university/with_invoice.rb b/app/models/university/with_invoice.rb
new file mode 100644
index 000000000..7c9c43ecb
--- /dev/null
+++ b/app/models/university/with_invoice.rb
@@ -0,0 +1,32 @@
+module University::WithInvoice
+  extend ActiveSupport::Concern
+
+  included do
+
+    before_save :denormalize_invoice_date
+
+    scope :invoiced, -> { where.not(invoice_amount: nil) }
+
+    def invoice_proximity
+      if next_invoice_in_days < 30
+        'danger'
+      elsif next_invoice_in_days < 60
+        'warning'
+      end
+    end
+
+    private
+
+    def denormalize_invoice_date
+      self.invoice_date_yday = self.invoice_date.nil? ? nil : self.invoice_date.yday
+    end
+
+    def next_invoice_in_days
+      return 999999 if invoice_date_yday.nil?
+      # ignores bisextil year but... who cares?
+      today = Time.now.yday
+      today < invoice_date_yday ? invoice_date_yday - today : invoice_date_yday + 365 - today
+    end
+
+  end
+end
diff --git a/app/views/server/universities/_form.html.erb b/app/views/server/universities/_form.html.erb
index 7b868882d..f24ed12e0 100644
--- a/app/views/server/universities/_form.html.erb
+++ b/app/views/server/universities/_form.html.erb
@@ -26,6 +26,15 @@
                   direct_upload: true %>
     </div>
   </div>
+  <h3 class="mt-5">Invoice informations</h3>
+  <div class="row">
+    <div class="col-md-4">
+      <%= f.input :invoice_date %>
+    </div>
+    <div class="col-md-4">
+      <%= f.input :invoice_amount %>
+    </div>
+  </div>
   <% content_for :action_bar_right do %>
     <%= submit f %>
   <% end %>
diff --git a/app/views/server/universities/index.html.erb b/app/views/server/universities/index.html.erb
index 75b999cfd..0c2667786 100644
--- a/app/views/server/universities/index.html.erb
+++ b/app/views/server/universities/index.html.erb
@@ -6,6 +6,8 @@
       <th><%= University.human_attribute_name('name') %></th>
       <th><%= University.human_attribute_name('url') %></th>
       <th><%= University.human_attribute_name('public_or_private') %></th>
+      <th><%= University.human_attribute_name('invoice_date') %></th>
+      <th><%= University.human_attribute_name('invoice_amount') %></th>
       <th></th>
     </tr>
   </thead>
@@ -15,6 +17,13 @@
         <td><%= link_to university, [:server, university] %></td>
         <td><%= link_to university.url, university.url, target: :_blank %></td>
         <td><%= university.private ? University.human_attribute_name('private') : University.human_attribute_name('public') %></td>
+        <% if university.invoice_amount.blank? %>
+          <td></td>
+          <td></td>
+        <% else %>
+          <td class="<%= university.invoice_proximity.blank? ? '' : "table-#{university.invoice_proximity}" %>"><%= l university.invoice_date, format: "%d %B" unless university.invoice_amount.blank? %></td>
+          <td class="<%= university.invoice_proximity.blank? ? '' : "table-#{university.invoice_proximity}" %>"><%= university.invoice_amount %></td>
+        <% end %>
         <td class="text-end">
           <div class="btn-group" role="group">
             <%= link_to t('edit'),
diff --git a/app/views/server/universities/show.html.erb b/app/views/server/universities/show.html.erb
index 1d9729c71..169a94c32 100644
--- a/app/views/server/universities/show.html.erb
+++ b/app/views/server/universities/show.html.erb
@@ -17,6 +17,16 @@
             <td class="text-end"><%= @university.public_send variable %></td>
           </tr>
         <% end %>
+        <% unless @university.invoice_amount.blank? %>
+          <tr class="<%= @university.invoice_proximity.blank? ? '' : "table-#{@university.invoice_proximity}" %>">
+            <td><%= t('activerecord.attributes.university.invoice_date') %></td>
+            <td class="text-end"><%= l @university.invoice_date, format: "%d %B" %></td>
+          </tr>
+          <tr class="<%= @university.invoice_proximity.blank? ? '' : "table-#{@university.invoice_proximity}" %>">
+            <td><%= t('activerecord.attributes.university.invoice_amount') %></td>
+            <td class="text-end"><%= @university.invoice_amount %>€</td>
+          </tr>
+        <% end %>
       </table>
     </div>
   </div>
diff --git a/config/locales/university/en.yml b/config/locales/university/en.yml
index a61d7c284..904b123da 100644
--- a/config/locales/university/en.yml
+++ b/config/locales/university/en.yml
@@ -6,6 +6,8 @@ en:
         city: City
         country: Country
         identifier: Identifier
+        invoice_amount: Invoice amount
+        invoice_date: Invoice date
         logo: Logo
         name: Name
         private: Private
diff --git a/config/locales/university/fr.yml b/config/locales/university/fr.yml
index 35473c389..d9642ab43 100644
--- a/config/locales/university/fr.yml
+++ b/config/locales/university/fr.yml
@@ -6,6 +6,8 @@ fr:
         city: Ville
         country: Pays
         identifier: Identifiant
+        invoice_amount: Montant de facturation
+        invoice_date: Date de facturation
         logo: Logo
         name: Nom
         private: Etablissement privé
diff --git a/db/migrate/20220224144107_add_invoice_elements_to_university.rb b/db/migrate/20220224144107_add_invoice_elements_to_university.rb
new file mode 100644
index 000000000..7ad199533
--- /dev/null
+++ b/db/migrate/20220224144107_add_invoice_elements_to_university.rb
@@ -0,0 +1,7 @@
+class AddInvoiceElementsToUniversity < ActiveRecord::Migration[6.1]
+  def change
+    add_column :universities, :invoice_date, :date
+    add_column :universities, :invoice_date_yday, :integer
+    add_column :universities, :invoice_amount, :string
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 544615fe4..51b023ae3 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 2022_02_24_140838) do
+ActiveRecord::Schema.define(version: 2022_02_24_144107) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
@@ -558,6 +558,9 @@ ActiveRecord::Schema.define(version: 2022_02_24_140838) do
     t.string "mail_from_name"
     t.string "mail_from_address"
     t.string "sms_sender_name"
+    t.date "invoice_date"
+    t.integer "invoice_date_yday"
+    t.string "invoice_amount"
   end
 
   create_table "university_people", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
diff --git a/test/fixtures/universities.yml b/test/fixtures/universities.yml
index 34655ffa6..0ce6f9287 100644
--- a/test/fixtures/universities.yml
+++ b/test/fixtures/universities.yml
@@ -7,6 +7,9 @@
 #  city              :string
 #  country           :string
 #  identifier        :string
+#  invoice_amount    :string
+#  invoice_date      :date
+#  invoice_date_yday :integer
 #  mail_from_address :string
 #  mail_from_name    :string
 #  name              :string
-- 
GitLab