Rev 313 | Rev 319 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 313 | Rev 318 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | #!/usr/bin/perl -w
|
1 | #!/usr/bin/perl -w
|
2 | use strict; |
2 | use strict; |
3 | # $Id: inventory.pl 313 2008-12-28 23:03:20Z agaran $
|
3 | # $Id: inventory.pl 318 2008-12-29 10:58:58Z agaran $
|
4 | # Thu, 13 Nov 2008 21:06:23 +0100
|
4 | # Thu, 13 Nov 2008 21:06:23 +0100
|
5 | # Maciej 'agaran' Pijanka <agaran@pld-linux.org>
|
5 | # Maciej 'agaran' Pijanka <agaran@pld-linux.org>
|
6 | # for OpenARM SBC Project
|
6 | # for OpenARM SBC Project
|
7 | # license: gpl v3
|
7 | # license: gpl v3
|
8 | 8 | ||
Line 114... | Line 114... | ||
114 | # this subroutine is used as callback function
|
114 | # this subroutine is used as callback function
|
115 | # executed by file_lookup
|
115 | # executed by file_lookup
|
116 | sub parse_ifile($) { |
116 | sub parse_ifile($) { |
117 | my ($filepath) = @_; |
117 | my ($filepath) = @_; |
118 | 118 | ||
- | 119 | ||
119 | open(IN, $filepath) or return 1; |
120 | open(IN, $filepath) or return 1; |
120 | 121 | ||
121 | my %data; |
122 | my %data; |
122 | 123 | ||
123 | while (not eof IN) { |
124 | while (not eof IN) { |
Line 310... | Line 311... | ||
310 | inf_printf("Skipping file %s because contain no data for me", |
311 | inf_printf("Skipping file %s because contain no data for me", |
311 | shortdir($filepath)); |
312 | shortdir($filepath)); |
312 | return; |
313 | return; |
313 | }
|
314 | }
|
314 | 315 | ||
315 | my $cost = 0; |
- | |
316 | foreach my $id (keys %data) { |
316 | foreach my $id (keys %data) { |
317 | my %tmp; |
317 | my %tmp; |
318 | map { $tmp{$_} = 1 } @{$data{$id}{RefDes}}; |
318 | map { $tmp{$_} = 1 } @{$data{$id}{RefDes}}; |
319 | @{$data{$id}{RefDes}} = keys %tmp; |
319 | @{$data{$id}{RefDes}} = keys %tmp; |
320 | my $cnt = scalar @{$data{$id}{RefDes}}; |
320 | my $cnt = scalar @{$data{$id}{RefDes}}; |
321 | if (!defined $Inv[$id]{Price}) { |
321 | if (!defined $Inv[$id]{Price}) { |
322 | wrn_printf("%s has no price, setting to 0.0", $Inv[$id]{Manufacturer_Partno}); |
322 | wrn_printf("%s has no price, setting to 0.0", $Inv[$id]{Manufacturer_Partno}); |
323 | $Inv[$id]{Price} = 0; |
323 | $Inv[$id]{Price} = 0; |
324 | }
|
324 | }
|
325 | my $icost = $Inv[$id]{Price} * $cnt; |
325 | push @{$BomData{$id}{RefDes}}, @{$data{$id}{RefDes}}; |
- | 326 | ||
326 | # printf "%-20s %.4f %s\n", $Inv[$id]{Manufacturer_Partno}, $cnt, $icost, join (', ', @{$data{$id}{RefDes}});
|
327 | # printf "%-20s %.4f %s\n", $Inv[$id]{Manufacturer_Partno}, $cnt, $icost, join (', ', @{$data{$id}{RefDes}});
|
327 | $cost += $icost; |
- | |
328 | # %BomData{ById}{$id}{RefDes}
|
328 | # %BomData{ById}{$id}{RefDes}
|
329 | }
|
329 | }
|
330 | printf "Total (%s): %.4f\n", shortdir($filepath), $cost; |
- | |
331 | }
|
330 | }
|
332 | 331 | ||
333 | sub file_lookup ($$$$) ; |
332 | sub file_lookup ($$$$) ; |
334 | sub file_lookup ($$$$) { |
333 | sub file_lookup ($$$$) { |
335 | my ($dir, $depth, $regexp, $callback) = @_; |
334 | my ($dir, $depth, $regexp, $callback) = @_; |
Line 402... | Line 401... | ||
402 | file_lookup($Config{docdir}, 0, qr/\/information.txt$/, \&parse_ifile); |
401 | file_lookup($Config{docdir}, 0, qr/\/information.txt$/, \&parse_ifile); |
403 | 402 | ||
404 | # process BOM files
|
403 | # process BOM files
|
405 | file_lookup($Config{bomdir}, 0, qr/\.bom$/, \&parse_bom); |
404 | file_lookup($Config{bomdir}, 0, qr/\.bom$/, \&parse_bom); |
406 | 405 | ||
- | 406 | my $n = 0; |
|
- | 407 | my $cost = 0.0; |
|
- | 408 | ||
- | 409 | sub shortstring($$) { |
|
- | 410 | my ($str,$lim) = @_; |
|
- | 411 | return substr($str,0,$lim-4).'(..)' if (length ($str) > $lim); |
|
- | 412 | return $str; |
|
- | 413 | }
|
|
- | 414 | ||
- | 415 | ||
- | 416 | ||
- | 417 | open (BOMOUT, '>output.bom') || die 'cant open output.bom: $!'; |
|
- | 418 | open (PARTMAP, '>output.map') || die 'cant open output.map: $!'; |
|
- | 419 | ||
- | 420 | printf BOMOUT "| | %-33s| %-20s| %-18s| %-11s|%-5s|%-6s|%-6s|\n|----+-----------------------------------+". |
|
- | 421 | "----------------------+--------------------+-------------+------+------+------|\n", |
|
- | 422 | 'description','manufact. partno','manufacturer','order code','quant.','price','cost'; |
|
- | 423 | printf PARTMAP "%-35s| refdes\n\n", 'part'; |
|
- | 424 | ||
- | 425 | ||
- | 426 | foreach my $id (keys %BomData) { |
|
- | 427 | my %tmp; |
|
- | 428 | map { $tmp{$_} = 1 } @{$BomData{$id}{RefDes}}; |
|
- | 429 | @{$BomData{$id}{RefDes}} = keys %tmp; |
|
- | 430 | my $cnt = scalar @{$BomData{$id}{RefDes}}; |
|
- | 431 | if (!defined $Inv[$id]{Price}) { |
|
- | 432 | wrn_printf("%s has no price, setting to 0.0", $Inv[$id]{Manufacturer_Partno}); |
|
- | 433 | $Inv[$id]{Price} = 0; |
|
- | 434 | }
|
|
- | 435 | my $icost = $cnt * $Inv[$id]{Price}; |
|
- | 436 | ||
- | 437 | printf BOMOUT "|%4d|%-35s|%-22s|%-20s|%-13s|%6d|%6.3f|%6.3f|\n", $n, shortstring($Inv[$id]{Description},35), |
|
- | 438 | shortstring($Inv[$id]{Manufacturer_Partno},22), shortstring($Inv[$id]{Manufacturer},20), |
|
- | 439 | shortstring($Inv[$id]{Ordercode}, 13), $cnt, $Inv[$id]{Price}, $icost; |
|
- | 440 | ||
- | 441 | printf PARTMAP "%-35s|%s\n", shortstring($Inv[$id]{Description},35), join (', ', @{$BomData{$id}{RefDes}}); |
|
- | 442 | $cost += $icost; |
|
- | 443 | # %BomData{ById}{$id}{RefDes}
|
|
- | 444 | $n ++; |
|
- | 445 | }
|
|
- | 446 | printf BOMOUT "|%s|\n| %-118s|\n|%s|\n", ("-" x 119), sprintf ("Total cost: %.3f", $cost), ("-" x 119); |
|
- | 447 | ||
- | 448 | close BOMOUT; |
|
- | 449 | close PARTMAP; |