Skip to content

Instantly share code, notes, and snippets.

@ewingd
Created March 31, 2014 15:32
Show Gist options
  • Save ewingd/9895012 to your computer and use it in GitHub Desktop.
Save ewingd/9895012 to your computer and use it in GitHub Desktop.
// Starting code, fairly difficult to understand what it is doing.
<?
if ((($bo_row['qtyordu'] - $branch_avail) - $hq_avail) == 0) {
$bo_hq_qty = $hq_avail;
$bo_brn_qty = 0;
} elseif ((($bo_row['qtyordu'] - $branch_avail) - $hq_avail) > 0) {
$bo_hq_qty = $hq_avail;
$bo_brn_qty = (($bo_row['qtyordu'] - $branch_avail) - $hq_avail);
} elseif ((($bo_row['qtyordu'] - $branch_avail) - $hq_avail) < 0) {
/* JMD - 01/07/20 - Added this condition where there is more than enough available
* between the branch and HQ to fill the order */
$bo_hq_qty = ($bo_row['qtyordu'] - $branch_avail);
$bo_brn_qty = 0;
}
?>
// Variable Substitution, reduces the size of the expressions
<?
$hq_need = $bo_row['qtyordu'] - $branch_avail;
if ((($hq_need) - $hq_avail) == 0) {
$bo_hq_qty = $hq_avail;
$bo_brn_qty = 0;
} elseif ((($hq_need) - $hq_avail) > 0) {
$bo_hq_qty = $hq_avail;
$bo_brn_qty = (($hq_need) - $hq_avail);
} elseif ((($hq_need) - $hq_avail) < 0) {
/* JMD - 01/07/20 - Added this condition where there is more than enough available
* between the branch and HQ to fill the order */
$bo_hq_qty = ($hq_need);
$bo_brn_qty = 0;
}
?>
// Re-arrange the conditional equations and remove extra parens
<?
$hq_need = $bo_row['qtyordu'] - $branch_avail;
if ($hq_need == $hq_avail) {
$bo_hq_qty = $hq_avail;
$bo_brn_qty = $hq_need - $hq_avail; // since $hq_need - $hq_avail == 0, we can substitute them in here.
} elseif ($hq_need > $hq_avail) {
$bo_hq_qty = $hq_avail;
$bo_brn_qty = $hq_need - $hq_avail;
} elseif ($hq_need < $hq_avail) {
/* JMD - 01/07/20 - Added this condition where there is more than enough available
* between the branch and HQ to fill the order */
$bo_hq_qty = $hq_need;
$bo_brn_qty = 0;
}
?>
// Combine the identical first 2 conditional branches.
<?
$hq_need = $bo_row['qtyordu'] - $branch_avail;
if (($hq_need >= $hq_avail) {
$bo_hq_qty = $hq_avail;
$bo_brn_qty = $hq_need - $hq_avail;
} elseif ($hq_need < $hq_avail) {
$bo_hq_qty = $hq_need;
$bo_brn_qty = 0;
}
?>
// Swap the order of the sections.
<?
$hq_need = $bo_row['qtyordu'] - $branch_avail;
if ($hq_need < $hq_avail) {
$bo_hq_qty = $hq_need;
$bo_brn_qty = 0;
} elseif (($hq_need >= $hq_avail) {
$bo_hq_qty = $hq_avail;
$bo_brn_qty = $hq_need - $hq_avail;
}
?>
// Change the unnecessary 'elseif' to 'else'
// Reverse the condition in the if statement for improved readability
<?
$hq_need = $bo_row['qtyordu'] - $branch_avail;
if ($hq_avail > $hq_need) { // More than enough available
$bo_hq_qty = $hq_need;
$bo_brn_qty = 0;
} else {
$bo_hq_qty = $hq_avail;
$bo_brn_qty = $hq_need - $hq_avail;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment