- #if no error detected, write to disk
- if (!$errormessage){
- my $date=time(); #seconds in utc
-
- #first get new key from hash
- my $key=&General::findhasharraykey (\%couponhash);
- #initialize all fields with ''
- foreach my $i (0 .. 3) { $couponhash{$key}[$i] = "";}
- #define fields
- $couponhash{$key}[0] = $date;
- $couponhash{$key}[1] = $cgiparams{'CODE'};
- $couponhash{$key}[2] = $settings{'EXPIRE'};
- $couponhash{$key}[3] = $cgiparams{'REMARK'};
- #write values to disk
- &General::writehasharray($coupons, \%couponhash);
+ if (!$errormessage) {
+ # Remember selected values
+ foreach my $val (("UNLIMITED", "EXP_HOUR", "EXP_DAY", "EXP_WEEK", "EXP_MONTH")) {
+ $settings{$val} = $cgiparams{$val};
+ }
+ &General::writehash($settingsfile, \%settings);
+
+ &General::readhasharray($coupons, \%couponhash) if (-e $coupons);
+ my $now = time();
+
+ # Calculate expiry time in seconds
+ my $expires = 0;
+
+ if ($settings{'UNLIMITED'} ne 'on') {
+ $expires += $settings{'EXP_HOUR'};
+ $expires += $settings{'EXP_DAY'};
+ $expires += $settings{'EXP_WEEK'};
+ $expires += $settings{'EXP_MONTH'};
+ }
+
+ my $count = $cgiparams{'COUNT'} || 1;
+ while($count-- > 0) {
+ # Generate a new code
+ my $code = &gencode();
+
+ # Check if the coupon code already exists
+ foreach my $key (keys %couponhash) {
+ if($couponhash{$key}[1] eq $code) {
+ # Code already exists, so try again
+ $code = "";
+ $count++;
+ last;
+ }
+ }