I have the following situation:
a script that calculates charges based on days (subtracting 3
free days)
there are occurrences where the 3 days may span more than 1 month
I have two files the days spanning to the next month, the
current month file
I need to be able to take the data from the file that has spanned from
the previous month to the current month and overwrite the free days in
the current month calculations
file1 (days from previous month)
BIGCO-C CMO 15153 L 6/30/13 2
BIGCO-C MKT 575 L 6/30/13 2
BIGCO-C MP 706776 L 6/30/13 2
BIGCO-C MP 718037 L 6/30/13 2
BIGCO-C MP 718071 L 6/30/13 2
MEDCO-C UP 89269 L 6/30/13 2
MEDCO-C UP 89800 L 6/30/13 2
MEDCO-C UP 93068 L 6/30/13 2
PURRCO-C UP 89778 L 6/30/13 2
WILYCO-C CMO 15034 L 6/30/13 2
WILYCO-C MKT 546 L 6/30/13 2
WILYCO-C MP 705339 L 6/30/13 1
WILYCO-C MP 705693 L 6/30/13 2
(the end column number is the days that have already been used)
so the number needs to be subtracted by 3 and and excluded from
the charges in my script just don't know how to make it happen
file2 (current month - small sample)
BIGCO CNW 180304 L 2013,07,16 2013,07,22
BIGCO UP 218226 L 2013,07,20 2013,07,22
BIGCO UP 219144 L 2013,07,23 2013,07,25
BIGCO-C CMO 15032 L 2013,07,01 2013,07,06
BIGCO-C CMO 15076 L 2013,07,01 2013,07,12
BIGCO-C CMO 15128 L 2013,07,01 2013,07,09
BIGCO-C CMO 15153 L 2013,07,01 2013,07,02
BIGCO-C MKT 575 L 2013,07,01 2013,07,02
BIGCO-C MP 705622 L 2013,07,01 2013,07,04
BIGCO-C MP 718037 L 2013,07,01 2013,07,22
BIGCO-C MP 718071 L 2013,07,01 2013,07,17
BIGCO-C UP 95233 L 2013,07,01 2013,07,20
MEDCO CHTT 721530 L 2013,07,16 2013,07,17
MEDCO CHTT 721679 L 2013,07,17 2013,07,18
MEDCO-C UP 88876 E 2013,07,01 2013,07,01
MEDCO-C UP 89269 L 2013,07,01 2013,07,02
MEDCO-C UP 89800 L 2013,07,01 2013,07,02
MEDCO-C UP 93068 L 2013,07,01 2013,07,02
PURRCO UP 95544 L 2013,07,12 2013,07,20
PURRCO-C CTRN 100318 L 2013,07,01 2013,07,15
PURRCO-C UP 89778 L 2013,07,01 2013,07,03
BIGGYCO UP 95664 L 2013,07,05 2013,07,09
BIGGYCO UP 221706 L 2013,07,05 2013,07,09
BIGGYCO-C CMO 20814 L 2013,07,01 2013,07,02
WILYCO CNW 437100 L 2013,07,26 2013,07,30
WILYCO CNW 437106 L 2013,07,08
WILYCO MKT 4399 L 2013,07,03
WILYCO MKT 4559 L 2013,07,23 2013,07,26
WILYCO MP 705320 L 2013,07,23 2013,07,25
WILYCO MP 705860 L 2013,07,26
WILYCO MP 706800 L 2013,07,26 2013,07,31
WILYCO MP 706827 L 2013,07,26
WILYCO MP 706974 L 2013,07,16 2013,07,18
WILYCO UP 94038 L 2013,07,26
WILYCO UP 95048 L 2013,07,23 2013,07,26
WILYCO UP 95050 L 2013,07,08
WILYCO-C CMO 15034 L 2013,07,01 2013,07,11
WILYCO-C MKT 514 L 2013,07,01 2013,07,02
WILYCO-C MKT 546 L 2013,07,01 2013,07,11
WILYCO-C MP 705339 L 2013,07,01 2013,07,12
WILYCO-C MP 705693 L 2013,07,01 2013,07,12
script to calculate the current month charges using file2 generating file3
#!/usr/bin/perl
#
use strict;
use warnings;
use Locale::Currency::Format;
#use Date::Calc qw( Delta_Days Today Days_in_Month );
use Date::Calc qw( Delta_Days Today Days_in_Month Add_Delta_Days);
my $cnt = 0;
my $amt;
my $mainDir = '/Users/xxx/Scripts/BigRR/RRMarks';
my $filename = '$mainDir\test.txt';
open (my $file, '<', (@ARGV)) or die $!;
open OUT, ">", $filename or die "$filename : $!\n";
# work out end of current month
my ($y,$m,$d) = Today();
my $mth_end = sprintf "%04d,%02d,%02d",$y,$m,Days_in_Month($y,$m);
# ($year,$month,$day) = Add_Delta_Days($year,$month,$day,$Dd);
my $prev_mth_end = sprintf "%04d,%02d,%02d",
Add_Delta_Days($y,$m,1,-1);
printf OUT " Placement Release
+ Tot Chg\n";
printf OUT " Date Date
+ Days Days Cost\n";
while (<$file>) {
my @dates = split '\s+', $_;
$cnt++;
# fill empty dates with month end
my @ymd1 = split ',',$dates[4] //= $prev_mth_end;
my @ymd2 = split ',',$dates[5] //= $prev_mth_end;
my $diff = Delta_Days(@ymd1, @ymd2);
my $free = ($diff - 3);
if ($free >= 0) {
my $amt = $free;
my $cost = ($amt * 100);
my $pfmt = "%-14s %-5s %-8s %-3s %-12s %-12s %-6s %-6s %-6s\n";
printf OUT $pfmt, $dates[0], $dates[1], $dates[2], $dates[3], fmt
+_mdy($dates[4]), fmt_mdy($dates[5]), $diff, $amt, fmt_curr($cost);
}
elsif ($free < 0) {
my $amt = 0;
my $cost = ($amt * 100);
my $pfmt = "%-14s %-5s %-8s %-3s %-12s %-12s %-6s %-6s %-6s\n";
printf OUT $pfmt, $dates[0], $dates[1], $dates[2], $dates[3], fmt_
+mdy($dates[4]), fmt_mdy($dates[5]), $diff, $amt, fmt_curr($cost);
}
# format currency
sub fmt_curr {
my $num = shift;
my $rounded = $num;
if ($num =~ m|\.|) {
$rounded = sprintf("%.2f", $num); #round to 2 decimal places
}
# add comma's
while ($rounded =~ s/^(-?\d+)(\d\d\d)/$1,$2/) {}
return ('$' .$rounded);
}
# change yyyy,mm,dd to mm-dd-yy
sub fmt_mdy {
my $ymd = shift;
$ymd =~ s/ //g;
# return ' 'x10 if $ymd eq $NULL_DATE;
my ($y,$m,$d) = split /\D/,$ymd;
if ($y < 99){ $y += 2000 };
return sprintf "%04d-%02d-%02d",$y,$m,$d;
}
}
print $cnt, "\n";
file3 - sample output that is created:
Placement Release Tot Chg
Date Date Days Days Cost
WILYCO-C BNSF 408033 L 2013-07-01 2013-07-03 2 0 $0
MEDCO CHTT 720576 L 2013-07-05 2013-07-09 4 1 $100
MEDCO CHTT 721283 L 2013-07-05 2013-07-09 4 1 $100
MEDCO CHTT 721307 L 2013-07-16 2013-07-17 1 0 $0
BIGGCO CHTT 721366 L 2013-07-22 2013-07-30 8 5 $500
BIGGCO CHTT 721369 L 2013-07-12 2013-07-15 3 0 $0
BIGGCO CHTT 721398 L 2013-07-17 2013-07-18 1 0 $0
BIGGCO CHTT 721469 L 2013-07-12 2013-07-15 3 0 $0
MEDCO CHTT 721530 L 2013-07-16 2013-07-17 1 0 $0
MEDCO CHTT 721679 L 2013-07-17 2013-07-18 1 0 $0
BIGGCO CHTT 721743 L 2013-07-05 2013-07-08 3 0 $0
PURRCO CMO 13611 L 2013-07-16 2013-07-27 1 8 $800