sum added to costs

This commit is contained in:
King Kévin 2014-04-06 11:58:45 +02:00
parent 83bd7ea661
commit 36a0b2f4b7
1 changed files with 26 additions and 7 deletions

View File

@ -376,17 +376,21 @@ targets.each do |target|
file "#{target[:name]}_cost.csv" => target[:sch] do |t|
sellers = ['digikey-id','farnell-id','mouser-id'] # provide the (supported) seller SKU as value to this attribute name in the symbols/schematic
boards = [1,10] # calculate the price for as many boards
sum = Array.new(sellers.size){Array.new(boards.size,0.0)} # total cost
stocks = Array.new(sellers.size){Array.new(boards.size){[]}} # is there enough stock
# get component information
attributes = ["manufacturer","manufacturer-id"]+sellers
parts = bom2(t.prerequisites[0],attributes)
parts.collect!{|part| part['manufacturer'] and part['manufacturer-id'] ? part : nil}
parts.compact!
# put result in CVS
CSV.open(t.name, "wb") do |csv|
csv << ["refdes","quantity","manufacturer","part number"]+(sellers.collect{|seller| [seller,"stock","currency"]+boards.collect{|qty| ["unit price for #{qty} board(s)","total price for #{qty} board(s)"]}}).flatten
parts.each do |part|
part['qty'] = part['qty'].to_i
next unless part['manufacturer'] and part['manufacturer-id']
line = [part['refdes'],part['qty'],part['manufacturer'],part['manufacturer-id']]
sellers.each do |seller|
sellers.each_index do |i|
seller = sellers[i]
if part[seller] then
begin
prices = scrape_prices(seller,part[seller])
@ -399,19 +403,24 @@ targets.each do |target|
line << prices[:stock]
line << prices[:currency]
unit = [] # the unit price
boards.each_index do |i|
boards.each_index do |j|
if prices[:prices].empty? then
line += [nil]*2
else
prices[:prices].each do |price|
unit[i] = price[1] if (!unit[i] or price[1]<unit[i]) and price[0]<=part['qty']*boards[i]
unit[j] = price[1] if (!unit[j] or price[1]<unit[j]) and price[0]<=part['qty']*boards[j]
end
if unit[i] then
line << unit[i]
line << unit[i]*part['qty']*boards[i]
if unit[j] then
line << unit[j]
line << unit[j]*part['qty']*boards[j]
stocks[i][j] << true if part['qty']*boards[j]<=prices[:stock]
sum[i][j] += unit[j]*part['qty']*boards[j]
else # use the minimum quantity
line << prices[:prices][0][1]
line << prices[:prices][0][1]*prices[:prices][0][0]
stocks[i][j] << true
sum[i][j] += prices[:prices][0][1]*prices[:prices][0][0]
end
end
end
@ -421,6 +430,16 @@ targets.each do |target|
end
csv << line
end
# summary
line = [nil]*4
sellers.each_index do |i|
line += [nil,nil,nil]
boards.each_index do |j|
line << (stocks[i][j].size==parts.size ? "" : "not ")+"enough stock"
line << sum[i][j]
end
end
csv << line
end
end
end