Skip to content

Commit

Permalink
Merge pull request #384 from Purple-Stock/staging
Browse files Browse the repository at this point in the history
fix the service order pdf and fix the behave of production
  • Loading branch information
puppe1990 authored Oct 15, 2024
2 parents f088e40 + 1b98f72 commit 5caa87a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 21 deletions.
26 changes: 12 additions & 14 deletions app/models/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,34 +50,27 @@ class Production < ApplicationRecord

# Add a method to calculate total pieces delivered
def total_pieces_delivered
production_products.sum(&:pieces_delivered)
production_products.sum { |pp| pp.pieces_delivered || 0 }
end

# Add a method to calculate total pieces missing
def total_missing_pieces
production_products.sum do |pp|
next 0 if pp.returned
pp.quantity - ((pp.pieces_delivered || 0) + (pp.dirty || 0) + (pp.error || 0) + (pp.discard || 0))
(pp.quantity || 0) - ((pp.pieces_delivered || 0) + (pp.dirty || 0) + (pp.error || 0) + (pp.discard || 0))
end
end

def total_dirty_pieces
production_products.sum(&:dirty)
production_products.sum { |pp| pp.dirty || 0 }
end

def total_error_pieces
production_products.sum(&:error)
production_products.sum { |pp| pp.error || 0 }
end

def total_discarded_pieces
production_products.sum(&:discard)
end

def total_missing_pieces
production_products.sum do |pp|
next 0 if pp.returned
pp.quantity - ((pp.pieces_delivered || 0) + (pp.dirty || 0) + (pp.error || 0) + (pp.discard || 0))
end
production_products.sum { |pp| pp.discard || 0 }
end

# Remove the before_save callback and the calculate_total_material_cost method
Expand All @@ -91,13 +84,18 @@ def total_missing_pieces

def price_per_piece
total_cost = (notions_cost || 0) + (fabric_cost || 0)
total_quantity = production_products.sum(&:quantity)
total_quantity = production_products.sum { |pp| pp.quantity || 0 }

total_quantity.zero? ? 0 : (total_cost / total_quantity)
end

def total_price
production_products.sum(:total_price)
production_products.sum do |pp|
next 0 if pp.returned
quantity = pp.quantity || 0
unit_price = pp.unit_price || 0
quantity * unit_price
end
end

def total_paid
Expand Down
20 changes: 14 additions & 6 deletions app/models/services/pdf/service_order_pdf_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,16 @@ def generate_products_table(pdf)
data = [["Produto", "Código", "Quantidade", "Preço un.", "Valor total"]]

@production.production_products.each do |pp|
quantity = pp.quantity || 0
unit_price = pp.unit_price || 0
total_price = quantity * unit_price

data << [
pp.product.name,
pp.product.sku,
pp.quantity,
number_to_currency(pp.unit_price),
number_to_currency(pp.total_price)
quantity,
number_to_currency(unit_price),
number_to_currency(total_price)
]
end

Expand Down Expand Up @@ -128,9 +132,13 @@ def generate_products_table(pdf)
end

def generate_totals(pdf)
total_price = @production.production_products.sum do |pp|
(pp.quantity || 0) * (pp.unit_price || 0)
end

pdf.text "Total serviços: #{number_to_currency(0)}", align: :right
pdf.text "Total peças: #{number_to_currency(@production.total_price)}", align: :right
pdf.text "Total da ordem de serviço: #{number_to_currency(@production.total_price)}", style: :bold, align: :right
pdf.text "Total peças: #{number_to_currency(total_price)}", align: :right
pdf.text "Total da ordem de serviço: #{number_to_currency(total_price)}", style: :bold, align: :right
pdf.move_down 30
end

Expand All @@ -153,4 +161,4 @@ def generate_signature(pdf)
end
end
end
end
end
29 changes: 28 additions & 1 deletion spec/models/production_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,15 @@

before do
production.production_products.first.update(
quantity: 10,
unit_price: 10,
pieces_delivered: 8,
dirty: 1,
error: 1
)
production.production_products.last.update(
quantity: 5,
unit_price: 10,
pieces_delivered: 3,
discard: 2
)
Expand Down Expand Up @@ -121,6 +125,11 @@
it 'calculates the total price correctly' do
expect(production.total_price).to eq(150) # (10 * 10) + (5 * 10)
end

it 'excludes returned products from the total price' do
production.production_products.last.update(returned: true)
expect(production.total_price).to eq(100) # (10 * 10)
end
end

describe '#total_paid' do
Expand All @@ -134,7 +143,25 @@
describe '#remaining_balance' do
it 'calculates the remaining balance correctly' do
create(:payment, production: production, amount: 75)
expect(production.remaining_balance).to be_within(0.01).of(75) # 150 - 75
expect(production.remaining_balance).to eq(75) # 150 - 75
end

it 'calculates the remaining balance correctly with returned products' do
production.production_products.last.update(returned: true)
create(:payment, production: production, amount: 75)
expect(production.remaining_balance).to eq(25) # 100 - 75
end
end

describe '#calendar_date' do
it 'returns payment_date when confirmed' do
production.update(confirmed: true, payment_date: Date.today + 7.days)
expect(production.calendar_date).to eq(production.payment_date)
end

it 'returns expected_delivery_date when not confirmed' do
production.update(confirmed: false, expected_delivery_date: Date.today + 14.days)
expect(production.calendar_date).to eq(production.expected_delivery_date)
end
end
end
Expand Down

0 comments on commit 5caa87a

Please sign in to comment.