Skip to content

Commit

Permalink
DatabarCoupon for #1211 value-of-transaction coupon (#1215)
Browse files Browse the repository at this point in the history
Recapitulates _(sprintf()) fixes.
Adds standardClearButton to all message popups.
  • Loading branch information
flathat committed Feb 16, 2024
1 parent b6f480c commit f5cf29b
Showing 1 changed file with 84 additions and 16 deletions.
100 changes: 84 additions & 16 deletions pos/is4c-nf/lib/Scanning/SpecialUPCs/DatabarCoupon.php
Expand Up @@ -32,6 +32,8 @@
class DatabarCoupon extends SpecialUPC
{

protected $offerCode = '';

public function isSpecial($upc)
{
if (substr($upc,0,4) == "8110" && strlen($upc) > 13) {
Expand Down Expand Up @@ -106,6 +108,7 @@ public function handle($upc,$json)
// grab offer code, remove from barcode
$offer = substr($upc,$pos,6);
$coupon->offerCode = substr($upc,$pos,6);
$this->offerCode = $offer;
$pos += 6;

// read value length
Expand Down Expand Up @@ -234,7 +237,6 @@ public function handle($upc,$json)
if (isset($upc[$pos]) && $upc[$pos] == "3") {
$pos += 1;
$expires = substr($upc,$pos,6);
$expires = substr($upc,$pos,6);
$pos += 6;

$year = "20".substr($expires,0,2);
Expand All @@ -243,7 +245,9 @@ public function handle($upc,$json)

$tstamp = mktime(23,59,59,$month,$day,$year);
if ($tstamp < time()) {
$json['output'] = DisplayLib::boxMsg(_("Coupon expired ") . date('m/d/Y', $tstamp));
$json['output'] = DisplayLib::boxMsg(
_("Coupon expired ") . date('m/d/Y', $tstamp),
'', false, DisplayLib::standardClearButton());
return $json;
}
}
Expand All @@ -260,7 +264,9 @@ public function handle($upc,$json)

$tstamp = mktime(0,0,0,$month,$day,$year);
if ($tstamp > time()) {
$json['output'] = DisplayLib::boxMsg(sprintf(_("Coupon not valid until %d/%d/%d"), $m, $d, $y));
$json['output'] = DisplayLib::boxMsg(
sprintf(_("Coupon not valid until %d/%d/%d"), $m, $d, $y),
'', false, DisplayLib::standardClearButton());
return $json;
}
}
Expand Down Expand Up @@ -363,7 +369,9 @@ public function handle($upc,$json)
}
break;
default:
$json['output'] = DisplayLib::boxMsg(_("Malformed coupon"));
$json['output'] = DisplayLib::boxMsg(
_("Malformed coupon"),
'', false, DisplayLib::standardClearButton());
return $json;
}

Expand Down Expand Up @@ -394,7 +402,9 @@ public function handle($upc,$json)
$value = MiscLib::truncate2($valReq->price * ($valReq->value/100.00));
break;
default:
$json['output'] = DisplayLib::boxMsg(_("Error: bad coupon " . $coupon->valueCode));
$json['output'] = DisplayLib::boxMsg(
_("Error: bad coupon " . $coupon->valueCode),
'', false, DisplayLib::standardClearButton());
return $json;
}

Expand All @@ -403,7 +413,7 @@ public function handle($upc,$json)
First character is zero
Next characters are company prefix
Remaining characters are offer code in base-36
Remaining characters are offer code
The first zero is there so that the company
prefix will "line up" with matching items in
Expand Down Expand Up @@ -451,16 +461,48 @@ private function validateRequirement($req, $json)
}
$dbc = Database::tDataConnect();

/* simple case first; just wants total transaction value
no company prefixing
/* simple case first; just wants total transaction value
* no company prefixing of items.
* First check for "already applied".
*/
if ($req->code == 2) {
/* Compose the coupon upc value from the prefix and the offer code.
*/
$upcStart = "0" . $req->prefix;
$offer = $this->offerCode;
$remaining = 13 - strlen($upcStart);
if (strlen($offer) < $remaining) {
$offer = str_pad($offer,$remaining,'0',STR_PAD_LEFT);
} elseif (strlen($offer) > $remaining) {
$offer = substr($offer,0,$remaining);
}
$couponUPC = $upcStart.$offer;
// See if the coupon has already been applied.
$dupQ = "SELECT sum(CASE WHEN trans_status='C' THEN 1 ELSE 0 END) as couponqtty
FROM localtemptrans
WHERE upc = ?";
$dupS = $dbc->prepare($dupQ);
$dupR = $dbc->execute($dupS, array($couponUPC));
if ($dbc->numRows($dupR) > 0) {
$dupRow = $dbc->fetchRow($dupR);
if ($dupRow['couponqtty'] != null && $dupRow['couponqtty'] > 0) {
$json['output'] = DisplayLib::boxMsg(
_("Coupon already applied"),
'', false, DisplayLib::standardClearButton());
$req->valid = false;
return array($req, $json);
}
}

$req->department = 0;
return $this->validateTransactionTotal($req, $json);
}

$req->valid = false;

/* Totals and values from coupon and non-coupon items
* with upc's that match the company prefix (brand).
*/
$query = sprintf("SELECT
max(CASE WHEN trans_status<>'C' THEN unitPrice ELSE 0 END) as price,
sum(CASE WHEN trans_status<>'C' THEN total ELSE 0 END) as total,
Expand All @@ -472,14 +514,27 @@ private function validateRequirement($req, $json)
strlen($req->prefix),$req->prefix);
$result = $dbc->query($query);

/* If there are no prefix matches it returns a row of NULLs
* not an empty set.
*/
if ($dbc->numRows($result) <= 0) {
$json['output'] = DisplayLib::boxMsg(_("Coupon requirements not met"));
$json['output'] = DisplayLib::boxMsg(
_("Coupon requirements not met"),
'', false, DisplayLib::standardClearButton());
return array($req, $json);
}
$row = $dbc->fetchRow($result);
if ($row['price'] == null && $row['total'] == null &&
$row['department'] == null && $row['qty'] == null &&
$row['couponqtty'] == null) {
$json['output'] = DisplayLib::boxMsg(
_("No items from the issuer of this coupon"),
'', false, DisplayLib::standardClearButton());
return array($req, $json);
}
$req->price = $row['price'];
$req->department = $row['department'];

switch($req->code) {
case '0': // various qtty requirements
case '3':
Expand All @@ -488,10 +543,14 @@ private function validateRequirement($req, $json)
case '1':
return $this->validateQty($row['total'], $row['couponqtty'], $req, $json);
case '9':
$json['output'] = DisplayLib::boxMsg(_("Tender coupon manually"));
$json['output'] = DisplayLib::boxMsg(
_("Tender coupon manually"),
'', false, DisplayLib::standardClearButton());
return array($req, $json);
default:
$json['output'] = DisplayLib::boxMsg(_("Error: bad coupon"));
$json['output'] = DisplayLib::boxMsg(
_("Error: bad coupon"),
'', false, DisplayLib::standardClearButton());
return array($req, $json);
}

Expand All @@ -509,13 +568,17 @@ private function validateTransactionTotal($req, $json)
$chkR = $dbc->query($chkQ);
$ttlRequired = MiscLib::truncate2($req->value / 100.00);
if ($dbc->num_rows($chkR) == 0) {
$json['output'] = DisplayLib::boxMsg(sprintf(_("Coupon requires transaction of at least \$%.2f", $ttlRequired)));
$json['output'] = DisplayLib::boxMsg(
sprintf(_("Coupon requires transaction of at least \$%.2f"), $ttlRequired),
'', false, DisplayLib::standardClearButton());
return array($req, $json);
}

$chkW = $dbc->fetch_row($chkR);
if ($chkW[0] < $ttlRequired) {
$json['output'] = DisplayLib::boxMsg(sprintf(_("Coupon requires transaction of at least \$%.2f", $ttlRequired)));
$json['output'] = DisplayLib::boxMsg(
sprintf(_("Coupon requires transaction of at least \$%.2f"), $ttlRequired),
'', false, DisplayLib::standardClearButton());
return array($req, $json);
}

Expand All @@ -529,9 +592,13 @@ private function validateQty($qty, $couponqtty, $req, $json)
if ($available_qty < $req->value) {
// Coupon requirement not met
if ($couponqtty > 0) {
$json['output'] = DisplayLib::boxMsg(_("Coupon already applied"));
$json['output'] = DisplayLib::boxMsg(
_("Coupon already applied"),
'', false, DisplayLib::standardClearButton());
} else {
$json['output'] = DisplayLib::boxMsg(sprintf(_("Coupon requires %d items"), $req->value));
$json['output'] = DisplayLib::boxMsg(
sprintf(_("Coupon requires %d items"), $req->value),
'', false, DisplayLib::standardClearButton());
}
$req->valid = false;
return array($req, $json);
Expand All @@ -548,5 +615,6 @@ private function validateQty($qty, $couponqtty, $req, $json)
$obj->handle("8110100707340143853100110110",array());
$obj->handle("811010041570000752310011020096000",array());
$obj->handle("8110007487303085831001200003101130",array());
$obj->handle("811050860006354409292341000410002000324022996000",array()); // NCG, code 2, $10
*/

0 comments on commit f5cf29b

Please sign in to comment.