-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfake_orders.pl
More file actions
executable file
·70 lines (63 loc) · 1.84 KB
/
Copy pathfake_orders.pl
File metadata and controls
executable file
·70 lines (63 loc) · 1.84 KB
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
#!/usr/bin/env perl
use strict;
use warnings;
use autodie;
use Data::Random qw(:all);
my ($customers_file, $sales_file, $parts_file) = @ARGV;
my $customers = load_file($customers_file);
my $sales = load_file($sales_file);
my $parts = load_file($parts_file);
my @orders;
for my $sp (@$sales) {
my $n_orders = int rand(100)+1;
for (my $i=0;$i<$n_orders;$i++) {
my $tot=0;
my $c = int rand(@$customers);
my $n_parts = rand(5)+1;
my @pq;
for(my $j=0;$j<$n_parts;$j++) {
my $p = int rand(@$parts);
my $q = int rand(100)+1;
push @pq, "$parts->[$p]{part_number}:$q";
my $price = $parts->[$p]{retail_price};
$price =~ s/\$//;
$tot += $price*$q;
}
my $orderdate = rand_date(min => '2002-9-21', max => 'now');
push @orders, [$orderdate,$sp->{name},$customers->[$c]{name},join(",",@pq),$tot];
}
}
my $i=0;
print join ("\t", qw(order_number order_date sales_associate customer products/quantities total_sale)),"\n";
for my $order (sort by_date @orders) {
$i++;
my $orderNumber = sprintf("CHA-CHING-%07d", $i);
print join ("\t", $orderNumber, @$order), "\n";
}
exit;
sub by_date {
my @aYMD = $a->[0] =~ m/(\d+)-(\d+)-(\d+)/;
my @bYMD = $b->[0] =~ m/(\d+)-(\d+)-(\d+)/;
$aYMD[0] <=> $bYMD[0] or $aYMD[1] <=> $bYMD[1] or $aYMD[2] <=> $bYMD[2];
}
sub load_file {
my $file = shift;
open (my $fh, "<", $file);
my $header = <$fh>;
chomp $header;
my @cols = split /\t/, $header;
my @res;
while (<$fh>) {
chomp;
my @x = split /\t/, $_;
next if @x != @cols;
my %h;
for(my $i=0;$i<@x;$i++) {
$h{$cols[$i]} = $x[$i];
}
push @res, \%h;
}
close $fh;
print STDERR "load_file($file) ",scalar @res,"\n";
return \@res;
}