Skip to content

Commit

Permalink
Merge pull request #385 from Purple-Stock/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
puppe1990 authored Oct 17, 2024
2 parents 5caa87a + bf9c987 commit c1fc127
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 61 deletions.
11 changes: 11 additions & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,14 @@ input::-webkit-inner-spin-button {
.sorted-desc::after {
content: " ▼";
}

.large-checkbox {
width: 1.5em;
height: 1.5em;
margin-top: 0.25em;
}

.large-checkbox + span {
font-size: 1.2em;
vertical-align: middle;
}
4 changes: 4 additions & 0 deletions app/models/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,8 @@ def calendar_date
expected_delivery_date
end
end

def total_value_delivered
production_products.sum { |pp| pp.pieces_delivered * pp.unit_price }
end
end
90 changes: 80 additions & 10 deletions app/views/productions/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<%- model_class = Production -%>
<div class="container mt-4">
<h1 class="mb-4"><%= t '.title', default: t("Edit #{model_class.model_name.human.titleize}") %></h1>
<h1 class="mb-4"><%= t '.title', default: t("helpers.titles.edit", model: model_class.model_name.human.titleize) %></h1>

<div class="card shadow-sm">
<div class="card-header bg-primary text-white">
<h4 class="mb-0">Edit Production</h4>
<h4 class="mb-0"><%= t('productions.edit.title') %></h4>
</div>
<div class="card-body">
<%= form_with(model: @production, local: true) do |form| %>
Expand All @@ -14,8 +14,8 @@
<%= form.text_field :service_order_number, class: 'form-control' %>
</div>
<div class="col-md-4">
<%= form.label :tailor_id, class: 'form-label' %>
<%= form.collection_select :tailor_id, @tailors, :id, :name, { prompt: 'Select a tailor' }, { class: 'form-select select2' } %>
<%= form.label :tailor_id, t('activerecord.attributes.production.tailor'), class: 'form-label' %>
<%= form.collection_select :tailor_id, @tailors, :id, :name, { prompt: t('productions.form.select_tailor') }, { class: 'form-select select2' } %>
</div>
</div>
<div class="row g-3">
Expand All @@ -41,6 +41,16 @@
<%= form.label :fabric_cost, t('productions.form.fabric_cost'), class: 'form-label' %>
<%= form.number_field :fabric_cost, step: 0.01, class: 'form-control' %>
</div>
</div>
<div class="row g-3 mt-2">
<div class="col-md-4">
<%= form.label :confirmed, t('productions.form.confirmed'), class: 'form-label' do %>
<%= form.check_box :confirmed, class: 'form-check-input large-checkbox' %>
<span class="ms-2"><%= t('productions.form.confirmed') %></span>
<% end %>
</div>
</div>
<div class="row g-3 mt-2">
<div class="col-12">
<%= form.label :observation, class: 'form-label' %>
<%= form.text_area :observation, class: 'form-control', rows: 3 %>
Expand All @@ -53,25 +63,40 @@
<%= render 'production_product_fields', f: pp_form %>
<% end %>
<div class="mt-3">
<%= link_to_add_association 'Add Product', form, :production_products,
<%= link_to_add_association t('productions.edit.add_product'), form, :production_products,
partial: 'production_product_fields',
class: 'btn btn-outline-primary' %>
class: 'btn btn-outline-primary add-product' %>
</div>
</div>

<h3 class="mt-4 mb-3"><%= t('.payments') %></h3>
<div class="mt-3 d-flex align-items-center">
<strong><%= t('productions.edit.total_value_delivered') %>:</strong>
<span id="total-value-delivered" class="ms-2"><%= number_to_currency(@production.total_value_delivered) %></span>
<%= link_to t('productions.edit.add_total_as_payment'), '#', class: 'btn btn-sm btn-outline-success ms-3', id: 'add-total-as-payment' %>
</div>

<h3 class="mt-4 mb-3"><%= t('productions.edit.payments') %></h3>
<div class="row g-3 mt-2">
<div class="col-md-4">
<%= form.label :paid, t('productions.form.paid'), class: 'form-label' do %>
<%= form.check_box :paid, class: 'form-check-input large-checkbox' %>
<span class="ms-2"><%= t('productions.form.paid') %></span>
<% end %>
</div>
</div>
<div id="payments">
<%= form.fields_for :payments do |payment_form| %>
<%= render 'payment_fields', f: payment_form %>
<% end %>
<div class="mt-3">
<%= link_to_add_association t('.add_payment'), form, :payments, class: 'btn btn-outline-primary' %>
<%= link_to_add_association t('productions.form.add_payment'), form, :payments,
class: 'btn btn-outline-primary add-payment' %>
</div>
</div>

<div class="mt-4">
<%= form.submit t('helpers.submit.update'), class: 'btn btn-primary' %>
<%= link_to t('.cancel', default: t("helpers.links.cancel")), production_path(@production), class: 'btn btn-outline-secondary' %>
<%= link_to t('helpers.links.cancel'), production_path(@production), class: 'btn btn-outline-secondary' %>
</div>
<% end %>
</div>
Expand All @@ -80,13 +105,58 @@

<% content_for :javascript do %>
<script>
$(document).ready(function() {
document.addEventListener('DOMContentLoaded', function() {
// Initialize Select2
$('.select2').select2();

// Initialize Select2 for dynamically added fields
$('#production-products').on('cocoon:after-insert', function() {
$('.select2').select2();
});

// Add total value as payment
document.getElementById('add-total-as-payment').addEventListener('click', function(e) {
e.preventDefault();
var totalValueText = document.getElementById('total-value-delivered').textContent.trim();

// Parse the Brazilian currency format
var totalValue = parseFloat(totalValueText.replace(/[^\d,]/g, '').replace(',', '.'));

// Trigger the add payment link
var addPaymentLink = document.querySelector('#payments .add-payment');
if (addPaymentLink) {
addPaymentLink.click();
}

// Check the "Paid" checkbox
var paidCheckbox = document.querySelector('input[name="production[paid]"][type="checkbox"]');
if (paidCheckbox) {
paidCheckbox.checked = true;
// Trigger change event to ensure any listeners are notified
paidCheckbox.dispatchEvent(new Event('change'));
}
});

// Use Cocoon's after-insert callback to set the values
$('#payments').on('cocoon:after-insert', function(e, insertedItem) {
var amountInput = insertedItem.find('input[name$="[amount]"]');
var dateInput = insertedItem.find('input[name$="[payment_date]"]');

if (amountInput.length) {
var totalValueText = document.getElementById('total-value-delivered').textContent.trim();
var totalValue = parseFloat(totalValueText.replace(/[^\d,]/g, '').replace(',', '.'));
amountInput.val(totalValue.toFixed(2));
}

if (dateInput.length) {
var today = new Date();
var yyyy = today.getFullYear();
var mm = String(today.getMonth() + 1).padStart(2, '0'); // January is 0!
var dd = String(today.getDate()).padStart(2, '0');
var formattedDate = yyyy + '-' + mm + '-' + dd; // Format as YYYY-MM-DD for date input
dateInput.val(formattedDate);
}
});
});
</script>
<% end %>
30 changes: 24 additions & 6 deletions app/views/productions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<%= form.text_field :service_order_number, value: @next_service_order_number, class: 'form-control' %>
</div>
<div class="col-md-4">
<%= form.label :tailor_id, class: 'form-label' %>
<%= form.collection_select :tailor_id, @tailors, :id, :name, { prompt: 'Select a tailor' }, { class: 'form-select select2' } %>
<%= form.label :tailor_id, t('activerecord.attributes.production.tailor'), class: 'form-label' %>
<%= form.collection_select :tailor_id, @tailors, :id, :name, { prompt: t('productions.form.select_tailor') }, { class: 'form-select select2' } %>
</div>
</div>
<div class="row g-3">
Expand All @@ -41,29 +41,47 @@
<%= form.label :fabric_cost, t('productions.form.fabric_cost'), class: 'form-label' %>
<%= form.number_field :fabric_cost, step: 0.01, class: 'form-control' %>
</div>
</div>
<div class="row g-3 mt-2">
<div class="col-md-4">
<%= form.label :confirmed, t('productions.form.confirmed'), class: 'form-label' do %>
<%= form.check_box :confirmed, class: 'form-check-input large-checkbox' %>
<span class="ms-2"><%= t('productions.form.confirmed') %></span>
<% end %>
</div>
</div>
<div class="row g-3 mt-2">
<div class="col-12">
<%= form.label :observation, class: 'form-label' %>
<%= form.text_area :observation, class: 'form-control', rows: 3 %>
</div>
</div>

<h3 class="mt-4 mb-3"><%= t('.products') %></h3>
<h3 class="mt-4 mb-3"><%= t('productions.new.products') %></h3>
<div id="production-products">
<%= form.fields_for :production_products do |pp_form| %>
<%= render 'production_product_fields', f: pp_form %>
<% end %>
<div class="mt-3">
<%= link_to_add_association t('.add_product'), form, :production_products, class: 'btn btn-outline-primary' %>
<%= link_to_add_association t('productions.new.add_product'), form, :production_products, class: 'btn btn-outline-primary' %>
</div>
</div>

<h3 class="mt-4 mb-3"><%= t('.payments') %></h3>
<h3 class="mt-4 mb-3"><%= t('productions.new.payments') %></h3>
<div class="row g-3 mt-2">
<div class="col-md-4">
<%= form.label :paid, t('productions.form.paid'), class: 'form-label' do %>
<%= form.check_box :paid, class: 'form-check-input large-checkbox' %>
<span class="ms-2"><%= t('productions.form.paid') %></span>
<% end %>
</div>
</div>
<div id="payments">
<%= form.fields_for :payments do |payment_form| %>
<%= render 'payment_fields', f: payment_form %>
<% end %>
<div class="mt-3">
<%= link_to_add_association t('.add_payment'), form, :payments, class: 'btn btn-outline-primary' %>
<%= link_to_add_association t('productions.form.add_payment'), form, :payments, class: 'btn btn-outline-primary' %>
</div>
</div>

Expand Down
19 changes: 9 additions & 10 deletions config/locales/pt-BR.models.productions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ pt-BR:
discount: "Desconto"
total: "Total"
total_value: "Valor Total"
total_pieces_delivered: "Total de Peças Entregues"
total_discount: "Total de Desconto"
total_returned: "Total Devolvido"
total_to_pay: "Total a Pagar"
Expand Down Expand Up @@ -145,9 +144,14 @@ pt-BR:
new_production: "Criar Nova Produção"
products: "Produtos"
add_product: "Adicionar Produto"
payments: "Pagamentos"
edit:
title: "Editar Produção"
products: "Produtos"
payments: "Pagamentos"
total_value_delivered: "Valor Total Entregue"
add_product: "Adicionar Produto"
add_total_as_payment: "Adicionar Total como Pagamento"
actions:
show: "Mostrar"
edit: "Editar"
Expand All @@ -170,26 +174,21 @@ pt-BR:
form:
notions_cost: "Custo de Aviamentos"
fabric_cost: "Custo de Tecido"
confirmed: "Recebimento Confirmado"
paid: "Pago"
select_tailor: "Selecione um costureiro"
payments: "Pagamentos"
add_payment: "Adicionar Pagamento"
price_per_piece_report:
title: "Relatório de Preço por Peça"
filters: "Filtros"
tailor: "Costureiro"
all_tailors: "Todos os Costureiros"
start_date: "Data Inicial"
end_date: "Data Final"
total_cost: "Custo Total"
total_quantity: "Quantidade Total"
price_per_piece: "Preço por Peça"
actions: "Ações"
show: "Mostrar"
edit: "Editar"
actions:
title: "Ações"
show: "Mostrar"
edit: "Editar"
destroy: "Excluir"
helpers:
submit:
create: "Criar %{model}"
Expand All @@ -208,4 +207,4 @@ pt-BR:
no: "Não"
stocks:
physical_balance: "Saldo Físico"
in_production: "Em Produção"
in_production: "Em Produção"
Loading

0 comments on commit c1fc127

Please sign in to comment.