Skip to content

Commit 247e615

Browse files
authored
Merge pull request #343 from Purple-Stock/staging
Staging
2 parents 8025ce2 + 6021349 commit 247e615

File tree

7 files changed

+99
-36
lines changed

7 files changed

+99
-36
lines changed

app/controllers/productions_controller.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,9 @@ def set_tailors
135135

136136
def production_params
137137
params.require(:production).permit(
138-
:cut_date, :tailor_id, :service_order_number, :expected_delivery_date,
139-
:confirmed, :paid, :consider, :observation, :notions_cost, :fabric_cost, :payment_date,
140-
production_products_attributes: [:id, :product_id, :quantity, :pieces_delivered, :delivery_date,
141-
:dirty, :error, :discard, :unit_price, :_destroy]
138+
:service_order_number, :tailor_id, :cut_date, :expected_delivery_date, :payment_date,
139+
:notions_cost, :fabric_cost, :observation, :confirmed, :paid,
140+
production_products_attributes: [:id, :product_id, :quantity, :unit_price, :total_price, :pieces_delivered, :dirty, :error, :discard, :returned, :delivery_date, :_destroy]
142141
)
143142
end
144143

app/models/production_product.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# updated_at :datetime not null
1616
# product_id :bigint not null
1717
# production_id :bigint not null
18+
# returned :boolean default(FALSE)
1819
#
1920
# Indexes
2021
#
@@ -33,9 +34,11 @@ class ProductionProduct < ApplicationRecord
3334
validates :quantity, presence: true, numericality: { greater_than_or_equal_to: 0 }
3435
validates :pieces_delivered, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
3536
validates :unit_price, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
37+
validates :returned, inclusion: { in: [true, false] }
3638

3739
before_save :set_default_pieces_delivered
3840
before_save :calculate_total_price
41+
before_save :set_default_unit_price
3942

4043
private
4144

@@ -46,4 +49,8 @@ def set_default_pieces_delivered
4649
def calculate_total_price
4750
self.total_price = quantity * unit_price if quantity.present? && unit_price.present?
4851
end
52+
53+
def set_default_unit_price
54+
self.unit_price ||= 0
55+
end
4956
end

app/models/services/pdf/payment_order_pdf_generator.rb

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,104 +42,127 @@ def generate_tailor_details(pdf)
4242
pdf.text "Data de conclusão: #{@production.production_products.maximum(:delivery_date)&.strftime("%d/%m/%Y")}"
4343
pdf.move_down 20
4444
end
45-
45+
4646
def generate_products_table(pdf)
4747
pdf.text "Peças Entregues", size: 14, style: :bold
4848
pdf.move_down 10
49-
50-
data = [["Produto", "Quantidade", "Preço Un.", "Sujo", "Erro", "Descarte", "Desconto", "Total"]]
51-
49+
50+
data = [["Produto", "Quantidade", "Preço Un.", "Sujo", "Erro", "Descarte", "Devolvido", "Desconto", "Total"]]
51+
52+
total_all_rows = 0
53+
total_discount = 0
54+
5255
@production.production_products.each do |pp|
5356
adjusted_quantity = pp.pieces_delivered - (pp.dirty + pp.error + pp.discard)
5457
discount = pp.unit_price * (pp.dirty + pp.error + pp.discard)
55-
adjusted_price = pp.unit_price * adjusted_quantity - discount
56-
58+
returned_discount = pp.returned ? pp.total_price : 0
59+
total_discount_row = discount + returned_discount
60+
adjusted_price = pp.unit_price * adjusted_quantity - total_discount_row
61+
62+
total_all_rows += pp.total_price
63+
total_discount += total_discount_row
64+
5765
data << [
5866
pp.product.name,
5967
pp.pieces_delivered,
6068
number_to_currency(pp.unit_price),
6169
pp.dirty,
6270
pp.error,
6371
pp.discard,
64-
number_to_currency(discount),
72+
pp.returned ? 'Sim' : 'Não',
73+
number_to_currency(total_discount_row),
6574
number_to_currency(adjusted_price)
6675
]
6776
end
68-
69-
column_widths = [120, 60, 60, 40, 40, 40, 60, 80]
70-
77+
78+
# Add a row for totals
79+
data << [
80+
"Total",
81+
@production.production_products.sum(:pieces_delivered),
82+
"",
83+
@production.production_products.sum(:dirty),
84+
@production.production_products.sum(:error),
85+
@production.production_products.sum(:discard),
86+
@production.production_products.where(returned: true).count,
87+
number_to_currency(total_discount),
88+
number_to_currency(total_all_rows - total_discount)
89+
]
90+
91+
column_widths = [120, 60, 60, 40, 40, 40, 60, 60, 80]
92+
7193
# Calculate row heights
7294
row_heights = data.map do |row|
7395
row.map.with_index do |cell, i|
7496
pdf.height_of(cell.to_s, width: column_widths[i], size: 10) + 10 # Add some padding
7597
end.max
7698
end
77-
99+
78100
pdf.bounding_box([0, pdf.cursor], width: pdf.bounds.width, height: row_heights.sum + 1) do
79101
y_position = pdf.bounds.top
80-
102+
81103
data.each_with_index do |row, row_index|
82104
row_height = row_heights[row_index]
83-
105+
84106
# Fill header row
85107
if row_index == 0
86108
pdf.fill_color "DDDDDD"
87109
pdf.fill_rectangle [0, y_position], pdf.bounds.width, row_height
88110
pdf.fill_color "000000"
89111
end
90-
112+
91113
# Draw horizontal line
92114
pdf.stroke_horizontal_line 0, pdf.bounds.width, at: y_position
93-
115+
94116
# Draw cell contents
95117
x_position = 0
96118
row.each_with_index do |cell, col_index|
97119
width = column_widths[col_index]
98120
pdf.bounding_box([x_position, y_position], width: width, height: row_height) do
99-
pdf.text_box cell.to_s,
100-
size: 10,
121+
pdf.text_box cell.to_s,
122+
size: 10,
101123
align: :center,
102124
valign: :center,
103125
overflow: :shrink_to_fit,
104-
style: (row_index == 0 ? :bold : :normal),
126+
style: (row_index == 0 || row_index == data.length - 1 ? :bold : :normal),
105127
at: [0, pdf.cursor],
106128
width: width,
107129
height: row_height
108130
end
109131
x_position += width
110132
end
111-
133+
112134
y_position -= row_height
113135
end
114-
136+
115137
# Draw vertical lines
116138
column_widths.reduce(0) do |x_position, width|
117139
pdf.stroke_vertical_line pdf.bounds.top, pdf.bounds.bottom, at: x_position
118140
x_position + width
119141
end
120142
pdf.stroke_vertical_line pdf.bounds.top, pdf.bounds.bottom, at: pdf.bounds.width
121-
143+
122144
# Draw bottom line
123145
pdf.stroke_horizontal_line 0, pdf.bounds.width, at: pdf.bounds.bottom
124146
end
125-
147+
126148
pdf.move_down 20
127149
end
128150

129151
def generate_totals(pdf)
130152
total_discount = @production.production_products.sum do |pp|
131-
pp.unit_price * (pp.dirty + pp.error + pp.discard)
132-
end
133-
134-
total_price = @production.production_products.sum do |pp|
135-
adjusted_quantity = pp.pieces_delivered - (pp.dirty + pp.error + pp.discard)
136153
discount = pp.unit_price * (pp.dirty + pp.error + pp.discard)
137-
pp.unit_price * adjusted_quantity - discount
154+
discount += pp.total_price if pp.returned
155+
discount
138156
end
139157

158+
total_price = @production.production_products.sum(&:total_price)
159+
total_to_pay = total_price - total_discount
160+
161+
pdf.text "Total do corte: #{number_to_currency(total_price)}", style: :bold, align: :right
162+
pdf.move_down 10
140163
pdf.text "Total desconto: #{number_to_currency(total_discount)}", style: :bold, align: :right
141164
pdf.move_down 10
142-
pdf.text "Total a pagar: #{number_to_currency(total_price)}", style: :bold, align: :right
165+
pdf.text "Total a pagar: #{number_to_currency(total_to_pay)}", style: :bold, align: :right
143166
pdf.move_down 30
144167
end
145168

app/views/productions/_production_product_fields.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
<div class="col-md-1 d-flex align-items-end">
4141
<%= link_to_remove_association 'Remove', f, class: 'btn btn-danger btn-sm' %>
4242
</div>
43+
<div class="col-md-1">
44+
<%= f.label :returned, 'Returned', class: 'form-label' %>
45+
<%= f.check_box :returned, class: 'form-check-input' %>
46+
</div>
4347
</div>
4448
</div>
4549
</div>

app/views/productions/new.html.erb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@
5454
</div>
5555
</div>
5656

57+
<div class="row mb-3">
58+
<div class="col-md-6">
59+
<%= form.label :returned, 'Returned', class: 'form-label' %>
60+
<%= form.number_field :returned, step: 1, class: 'form-control' %>
61+
</div>
62+
</div>
63+
5764
<div class="row mb-3">
5865
<div class="col-12">
5966
<%= form.label :observation, model_class.human_attribute_name(:observation), class: 'form-label' %>

app/views/productions/show.html.erb

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@
7878
<th>Sujo</th>
7979
<th>Erro</th>
8080
<th>Descarte</th>
81+
<th>Quantidade Devolvida</th>
8182
<th>Peças Faltantes</th>
8283
<th>Data de Entrega</th>
84+
<th>Devolvido</th>
8385
</tr>
8486
</thead>
8587
<tbody>
@@ -88,16 +90,28 @@
8890
<td><%= pp.product&.name || 'Nenhum produto atribuído' %></td>
8991
<td><%= pp.quantity %></td>
9092
<td><%= number_to_currency(pp.unit_price) %></td>
91-
<td><%= number_to_currency(pp.unit_price * (pp.dirty + pp.error + pp.discard)) %></td>
93+
<td>
94+
<% if pp.unit_price %>
95+
<%= number_to_currency(pp.unit_price * ((pp.dirty || 0) + (pp.error || 0) + (pp.discard || 0))) %>
96+
<% else %>
97+
N/A
98+
<% end %>
99+
<% if pp.returned %>
100+
<br>
101+
<%= number_to_currency(pp.total_price) %>
102+
<% end %>
103+
</td>
92104
<td><%= number_to_currency(pp.total_price) %></td>
93105
<td><%= pp.pieces_delivered || 0 %></td>
94106
<td><%= pp.dirty || 0 %></td>
95107
<td><%= pp.error || 0 %></td>
96108
<td><%= pp.discard || 0 %></td>
109+
<td><%= pp.returned ? pp.quantity : 0 %></td>
97110
<td>
98111
<%= pp.quantity - ((pp.pieces_delivered || 0) + (pp.dirty || 0) + (pp.error || 0) + (pp.discard || 0)) %>
99112
</td>
100113
<td><%= pt_only_date_format(pp.delivery_date) || 'Não informado' %></td>
114+
<td><%= pp.returned ? 'Yes' : 'No' %></td>
101115
</tr>
102116
<% end %>
103117
</tbody>
@@ -106,12 +120,17 @@
106120
<th>Total da Produção</th>
107121
<td><%= @production.production_products.sum(:quantity) %></td>
108122
<td></td>
109-
<td><%= number_to_currency(@production.production_products.sum { |pp| pp.unit_price * (pp.dirty + pp.error + pp.discard) }) %></td>
123+
<td>
124+
<%= number_to_currency(@production.production_products.sum { |pp| pp.unit_price * (pp.dirty + pp.error + pp.discard) }) %>
125+
<br>
126+
<%= number_to_currency(@production.production_products.where(returned: true).sum(:total_price)) %>
127+
</td>
110128
<td><%= number_to_currency(@production.total_price) %></td>
111129
<td><%= @production.production_products.sum(:pieces_delivered) %></td>
112130
<td><%= @production.production_products.sum(:dirty) %></td>
113131
<td><%= @production.production_products.sum(:error) %></td>
114132
<td><%= @production.production_products.sum(:discard) %></td>
133+
<td><%= @production.production_products.where(returned: true).sum(:quantity) %></td>
115134
<td>
116135
<%= @production.production_products.sum { |pp|
117136
pp.quantity - ((pp.pieces_delivered || 0) + (pp.dirty || 0) + (pp.error || 0) + (pp.discard || 0))
@@ -133,7 +152,6 @@
133152
<%= link_to t('.destroy', :default => t("helpers.links.destroy")), production_path(@production), :method => 'delete',
134153
:data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
135154
:class => 'btn btn-danger' %>
136-
<%= link_to 'Download Service Order PDF', service_order_pdf_production_path(@production), class: 'btn btn-primary', target: '_blank' %>
137155
</div>
138156

139157
<% content_for(:page_title, 'Show Production') %>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddReturnedToProductionProducts < ActiveRecord::Migration[6.1]
2+
def change
3+
add_column :production_products, :returned, :boolean, default: false
4+
end
5+
end

0 commit comments

Comments
 (0)