<commit_before><?
include '../scat.php';
include '../lib/txn.php';

bcscale(2);

$id= (int)$_REQUEST['id'];

if (!$id)
  die_jsonp("No transaction specified.");

$method= $_REQUEST['method'];
$amount= $_REQUEST['amount'];

// validate method
if (!in_array($method,
              array('cash','credit','gift','check','discount'))) {
  die_jsonp("Invalid method specified.");
}

$txn= txn_load($db, $id);

// if set, allow overpayment and create a 'change' record
$change= (bool)($_REQUEST['change'] != 'false');

// if no change and amount + paid > total, barf
if (!$change && bccomp(bcadd($amount, $txn['total_paid']), $txn['total']) > 0) {
  die_jsonp("Amount is too much.");
}

$cc_fields= "";

if ($method == 'credit') {
  $cc= array();
  foreach(array('cc_txn', 'cc_approval', 'cc_lastfour',
                'cc_expire', 'cc_type') as $field) {
    $cc[]= "$field = '" . $db->real_escape_string($_REQUEST[$field]) . "', ";
  }

  $cc_fields= join('', $cc);
}

// add payment record
$q= "INSERT INTO payment
        SET txn = $id, method = '$method', amount = $amount,
        $cc_fields
        processed = NOW()";
$r= $db->query($q)
  or die_query($db, $q);

$payment= $db->insert_id;

// if amount + paid > total, add change record
$change_paid= 0.0;
if (bccomp(bcadd($amount, $txn['total_paid']), $txn['total']) > 0) {
  $change_paid= $txn['total'] - ($amount + $txn['total_paid']);

  $q= "INSERT INTO payment
          SET txn = $id, method = 'change', amount = $change_paid,
          processed = NOW()";
  $r= $db->query($q)
    or die_query($db, $q);
}<commit_msg>Use bcmath for calculating change paid<commit_after><?
include '../scat.php';
include '../lib/txn.php';

bcscale(2);

$id= (int)$_REQUEST['id'];

if (!$id)
  die_jsonp("No transaction specified.");

$method= $_REQUEST['method'];
$amount= $_REQUEST['amount'];

// validate method
if (!in_array($method,
              array('cash','credit','gift','check','discount'))) {
  die_jsonp("Invalid method specified.");
}

$txn= txn_load($db, $id);

// if set, allow overpayment and create a 'change' record
$change= (bool)($_REQUEST['change'] != 'false');

// if no change and amount + paid > total, barf
if (!$change && bccomp(bcadd($amount, $txn['total_paid']), $txn['total']) > 0) {
  die_jsonp("Amount is too much.");
}

$cc_fields= "";

if ($method == 'credit') {
  $cc= array();
  foreach(array('cc_txn', 'cc_approval', 'cc_lastfour',
                'cc_expire', 'cc_type') as $field) {
    $cc[]= "$field = '" . $db->real_escape_string($_REQUEST[$field]) . "', ";
  }

  $cc_fields= join('', $cc);
}

// add payment record
$q= "INSERT INTO payment
        SET txn = $id, method = '$method', amount = $amount,
        $cc_fields
        processed = NOW()";
$r= $db->query($q)
  or die_query($db, $q);

$payment= $db->insert_id;

// if amount + paid > total, add change record
$change_paid= 0.0;
if (bccomp(bcadd($amount, $txn['total_paid']), $txn['total']) > 0) {
  $change_paid= bcsub($txn['total'],bcadd($amount,$txn['total_paid']));

  $q= "INSERT INTO payment
          SET txn = $id, method = 'change', amount = $change_paid,
          processed = NOW()";
  $r= $db->query($q)
    or die_query($db, $q);
}
{'fraction_non_alphanumeric': 0.22354788069073783, 'fraction_numerical': 0.0031397174254317113, 'mean_word_length': 3.879019908116386, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4296851', 'n_tokens_mistral': 1181, 'n_tokens_neox': 1156, 'n_words': 277}