summaryrefslogtreecommitdiff
path: root/bom/calc.pl
blob: dd044485f8fe1aff68b4e4c602e1b5cb6eb4d7df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/perl
#
# bom/calc.pl - Calculate device cost based on stock database
#
# Written 2014-2015 by Werner Almesberger
# Copyright 2014-2015 by Werner Almesberger
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#


$cache = ".cache";


sub usage
{
	print STDERR <<"EOF";
usage: $0 [-b] [-c cache_dir] [-d] [-e file.equiv] [-g|-s]
       [-v] [-x exclude ... [-C] [-h]] units [quantity supplier part ...]
EOF
	exit(1);
}


sub one_part
{
	local ($qty, $src, $name) = @_;
	my $file;

	($file = $name) =~ tr|/|_|;
	$file = $cache."/".$src."_".$file;

	return unless -r $file;

	open(FILE, $file) || die "$file: $!";
	my @a = split(/\s+/, <FILE>);
	my $desc = <FILE>;
	chop $desc;
	close FILE;

	if ($#exclude != -1) {
		$best_desc = $desc if length $desc > length $best_desc;
		return;
	}

	splice(@a, 0, 2);	# drop supplier and part name
	my $stock = shift @a;
	my $want = $qty * $units;

	$src_stock{$src} = $stock if $stock > $src_stock{$src};
	$avail += $stock;

	return if $stock < $want && $found;
	undef $best if $stock >= $want && !$found;

	while ($#a != -1) {
		die "processing $src $name" unless $#a & 1;
		my $break = shift @a;
		my $price = shift @a;
		my $n = $want < $break ? $break : $want;

		next if $best < $n * $price && defined $best;
		next if $best == $n * $price && $best_n >= $n && defined $best;
		$best = $n * $price;
		$best_n = $n;
		$best_src = $src;
		$best_name = $name;
		$best_desc = $desc;
		$best_break = $break;
		$found = 1 if $want <= $stock;
	}
}


sub try_parts
{
	local ($qty, $src, $name) = @_;
	my @a = ();

	return if defined $tried{"$src $name"};
	$tried{"$src $name"} = 1;

	&one_part(@_);
	if ($#exclude == -1 || !grep($_ eq $src, @exclude)) {
		@a = ("$src $name");
		$found = 1;
	}

	for (@{ $equiv{"$src $name"}}) {
		@a = (@a, &try_parts($qty, @{ $_ }));
	}
	return @a;
}


sub print_csv_desc
{
	local ($comma_follows) = @_;

	return unless $descriptions;

	my $tmp = $best_desc;

	$tmp =~ s/"/""/g;
	print "," unless $comma_follows;
	print "\"$tmp\"";
	print "," if $comma_follows;
}


sub part
{
	local ($qty, $src, $name, $group) = @_;
	local $best = undef;
	local $best_desc = undef;
	local $found = 0;
	local $avail = 0;

	undef %tried;
	undef %src_stock;
	my @a = &try_parts(@_);

	if ($#exclude != -1) {
		if (!$found) {
			warn "no matching part found for $src $name\n";
			$missing = 1;
		} else {
			if ($csv) {
				print "$qty,";
				&print_csv_desc(1);
				print join(",", map("\"$_\"", sort @a)), "\n";
			} else {
				printf("%3d ", $qty);
				print join(", ", sort @a), "\n";
				print "\t$best_desc\n"
				    if $descriptions && $best_desc ne "";
			}
		}
		return;
	}

	my $all = 0;
	for (values %src_stock) {
		$all += $_;
	}
	print STDERR "$src $name: sum $avail != peaks $all\n"
	    if $avail != $all && $stock;

	if ($stock) {
		die "clash on name \"$name\"" if defined $stock{$name};
		$stock{$avail == $all ? $name : "*$name"} =
		    [ $qty, $all, sort { @{ $b }[1] <=> @{ $a }[1] }
		      map([ $_, $src_stock{$_} ], keys %src_stock) ]
		    if $qty;
		return;
	}

	my $want = $qty * $units;
	if ($found && $avail < $want) {
		warn "want $want $src $name, known stock may be $avail\n";
		$missing = 1;
	}
	return unless defined $best;

	if ($csv) {
		print "$best_break," if $show_break;
		print "$best_n,\"$group\",\"$best_src $best_name\",$best";
		&print_csv_desc(0);
		print "\n";
	} elsif ($verbose) {
		$max_src = 9;
		$max_name = 40;
		$max_num = 9;

		$max_name -= 7 if $show_break;

		$max_name += $max_src - length $best_src
		    if length $best_src > $max_src;
		$max_name = 0 if $max_name < 0;

		$max_num += $max_name - length $best_name
		    if length $best_name > $max_name;
		$max_num = 0 if $max_num < 0;

		printf("%6d/", $best_break) if $show_break;
		printf("%6d ", $best_n);
		if (length $group > 9) {
			printf("%s\n%16s", $group, "");
		} else {
			printf("%-9s", $group);
		}
		printf(" %-${max_src}s %-${max_name}s %${max_num}.4f\n",
		    $best_src, $best_name, $best);
		printf("%-16s %s\n", "", $best_desc)
		    if $descriptions && $best_desc ne "";
	}
	$sum += $best;
	$sum{$group ne "" ? $group : "Other"} += $best;
}


sub equiv
{
	local ($name) = @_;
	local $eq, @eq;

	undef $eq;
	open(EQUIV, $name) || die "$name: $!";
	while (<EQUIV>) {
		last if /^END\s*$/;
		s/\s*#[^#]+#\s*$//;
		next if /^#/;
		next if /^\s*$/;
		die unless /^(\s*)(\S+)\s+(\S+)\s*$/;
		if ($1 eq "") {
			$eq = "$2 $3";
			@eq = ($2, $3)
		} else {
			die unless defined $eq;
			push(@{ $equiv{$eq} }, [$2, $3]);
			push(@{ $equiv{"$2 $3"} }, [ @eq ]);
		}
	}
	close EQUIV;
}


while ($ARGV[0] =~ /^-/) {
	if ($ARGV[0] eq "-c") {
		shift @ARGV;
		$cache = $ARGV[0];
		&usage unless defined $cache;
	} elsif ($ARGV[0] eq "-b") {
		$show_break = 1;
	} elsif ($ARGV[0] eq "-d") {
		$descriptions = 1;
	} elsif ($ARGV[0] eq "-e") {
		shift @ARGV;
		&usage unless defined $ARGV[0];
		&equiv($ARGV[0]);
	} elsif ($ARGV[0] eq "-g") {
		$groups = 1;
	} elsif ($ARGV[0] eq "-h") {
		$header = 1;
	} elsif ($ARGV[0] eq "-s") {
		$stock = 1;
	} elsif ($ARGV[0] eq "-C") {
		$csv = 1;
	} elsif ($ARGV[0] eq "-v") {
		$verbose = 1;
	} elsif ($ARGV[0] eq "-x") {
		&usage unless defined $ARGV[0];
		shift @ARGV;
		push(@exclude, $ARGV[0]);
	} else {
		&usage;
	}
	shift @ARGV;
}

$units = shift @ARGV;
&usage unless defined $units || $stock;
&usage if defined $units && $stock;

if ($header) {
	if ($#exclude == -1) {
		print '"break",' if $show_break;
		print "\"quantity\",\"group\",\"distributor part\",\"price\"";
		print ',"description"' if $descriptions;
		print "\n";
	} else {
		print '"number",';
		print '"description",' if $descriptions;
		print "\"manufacturer part(s)\"\n";
	}
}

if ($#ARGV == -1) {
	while (<>) {
		next if /^#/;
		next if /^\s*$/;
		die unless /^(\d+)\s+(\S+)\s+(\S+)\s*(\S*)/;
		&part($1, $2, $3, $4) if $1;
	}
} else {
	while ($#ARGV >= 2) {
		&part(splice(@ARGV, 0, 3));
	}
	&usage unless $#ARGV == -1;
}

if ($#exclude != -1) {
	printf STDERR "Warning: some parts are missing\n" if $missing;
	exit;
}

if ($groups) {
	for (sort { $sum{$a} <=> $sum{$b} } keys %sum) {
		print "$_ $sum{$_}\n";
	}
	exit(0);
}

if ($stock) {
	for (sort
	    { $stock{$b}[1] / $stock{$b}[0] <=> $stock{$a}[1] / $stock{$a}[0] }
	    keys %stock) {
		my $s;
		my $qty = $stock{$_}[0];

		($s = $_) =~ s|^(.+)/.*$|$1|;
		print substr($s.(" " x 30), 0, 30);

		print " ", $stock{$_}[1] / $qty;
		for $i (2..$#{ $stock{$_} }) {
			print " ", $stock{$_}[$i][0], " ",
			    $stock{$_}[$i][1] / $qty
			    if defined $stock{$_}[$i][0];
		}
		print "\n";
	}
	exit;
}

printf("Total: %.2f, %.2f each\n", $sum, $sum / $units) unless $csv;
printf STDERR "Warning: some parts are unavailable\n" if $missing;