$return['html'] = '<h1>Edit ' . $TYPES[$_POST['type']]['pluralName'] . '</h1>'; foreach ($TYPES[$_POST['type']]['formData'] as $key => $section) { $return['html'] .= '<section><h2>' . $key . '</h2><div class="sectionData">'; foreach ($section as $column) { $return['html'] .= '<ul>'; foreach ($column as $field) { $formalName = $TYPES[$_POST['type']]['fields'][$field]['formalName']; $attributes = $TYPES[$_POST['type']]['fields'][$field]['verifyData']; if ($attributes[1] == 'int' || $attributes[1] == 'str' || $attributes[1] == 'dec') { $return['html'] .= '<li><input type="checkbox">'; $return['html'] .= '<label for="' . $field . '">' . $formalName . '</label>'; $return['html'] .= '<input type="text" name="' . $field . '" autocomplete="off" disabled></li>'; } elseif ($attributes[1] == 'id' || $attributes[1] == 'opt') { $return['html'] .= '<li><input type="checkbox">'; $return['html'] .= '<label for="' . $field . '">' . $formalName . '</label><select name="' . $field . '" disabled>'; $return['html'] .= $attributes[1] == 'id' ? generateTypeOptions($attributes[2], true) : generateFieldOptions($_POST['type'], $field, true); $return['html'] .= '</select></li>'; } elseif ($attributes[1] == 'disp') { $return['html'] .= '<li> </li>'; } elseif ($attributes[1] == 'date') { $return['html'] .= '<li><label for="' . $field . '">' . $formalName . '</label>'; $return['html'] .= '<input type="text" class="dateInput" name="' . $field . '" autocomplete="off" value="' . formatDate($item[$field]) . '"></li>'; } } $return['html'] .= '</ul>'; } $return['html'] .= '</div></section>'; } $return['html'] .= '<div class="btnSpacer"><button id="editBtn">Edit</button></div>'; echo json_encode($return); }
public function customAjax($id, $data) { global $dbh; global $SETTINGS; $return = ['status' => 'success']; if ($data['subAction'] == 'list') { //list subAction $return['products'] = generateTypeOptions('product', true); $return['locations'] = generateTypeOptions('location', true); } elseif ($data['subAction'] == 'add') { //add subAction $return['status'] = 'fail'; $subType = $data['subType']; unset($data['subAction']); unset($data['subType']); $data = cleanData('expense', $subType, $data); $return = verifyData('expense', $subType, 'add', $data); if ($return['status'] != 'fail') { if ($subType == 'payment') { $dateTS = DateTime::createFromFormat($SETTINGS['dateFormat'] . '|', $data['date'])->getTimestamp(); $sth = $dbh->prepare('INSERT INTO expensePayments (expenseID, date, paymentType, paymentAmount) VALUES(:expenseID, :date, :paymentType, :paymentAmount)'); $sth->execute([':expenseID' => $id, ':date' => $dateTS, ':paymentType' => $data['paymentType'], ':paymentAmount' => $data['paymentAmount']]); $changeData = ['subType' => 'payment', 'date' => $dateTS, 'paymentType' => $data['paymentType'], 'paymentAmount' => $data['paymentAmount']]; } elseif ($subType == 'product') { $sth = $dbh->prepare('SELECT quantity FROM expenses_products WHERE expenseID = :expenseID AND productID = :productID AND locationID = :locationID AND unitPrice = :unitPrice'); $sth->execute([':expenseID' => $id, ':productID' => $data['productID'], ':locationID' => $data['locationID'], ':unitPrice' => $data['unitPrice']]); $result = $sth->fetchAll(); if (count($result) == 1 && $data['recurring'] == 'no') { //if the product is already present in the expense AND we aren't doing a recurring item, add the quantity to the existing row $totalQuantity = $data['quantity'] + $result[0]['quantity']; $sth = $dbh->prepare('UPDATE expenses_products SET quantity = :quantity WHERE expenseID = :expenseID AND productID = :productID AND locationID = :locationID AND unitPrice = :unitPrice'); $sth->execute([':quantity' => $totalQuantity, ':expenseID' => $id, ':productID' => $data['productID'], ':locationID' => $data['locationID'], ':unitPrice' => $data['unitPrice']]); $changeAction = 'E'; //this is technically an edit, not an add $changeData = ['subType' => 'product', 'productID' => $data['productID'], 'locationID' => $data['locationID'], 'unitPrice' => $data['unitPrice'], 'quantity' => $totalQuantity]; } else { if ($data['recurring'] == 'yes') { $startTS = DateTime::createFromFormat($SETTINGS['dateFormat'] . '|', $data['startDate'])->getTimestamp(); $endTS = DateTime::createFromFormat($SETTINGS['dateFormat'] . '|', $data['endDate'])->getTimestamp(); //add the recurring item $sth = $dbh->prepare('SELECT MAX(recurringID) AS recurringID FROM expenses_products'); $sth->execute(); $result = $sth->fetchAll(); $recurringID = $result[0]['recurringID'] + 1; $sth = $dbh->prepare('INSERT INTO expenses_products (expenseID, productID, locationID, unitPrice, quantity, recurringID, dayOfMonth, startDate, endDate) VALUES(:expenseID, :productID, :locationID, :unitPrice, :quantity, :recurringID, :dayOfMonth, :startDate, :endDate)'); $sth->execute([':expenseID' => $id, ':productID' => $data['productID'], ':locationID' => $data['locationID'], ':unitPrice' => $data['unitPrice'], ':quantity' => $data['quantity'], ':recurringID' => $recurringID, ':dayOfMonth' => $data['dayOfMonth'], ':startDate' => $startTS, ':endDate' => $endTS]); //add occasions from start date to now $temp = new DateTime(); $temp->setTimestamp($startTS); $patternStart = new DateTime($data['dayOfMonth'] . '-' . $temp->format('M') . '-' . $temp->format('Y')); $interval = new DateInterval('P1M'); $now = new DateTime(); $period = new DatePeriod($patternStart, $interval, $now); foreach ($period as $date) { $timestamp = $date->getTimestamp(); if ($timestamp >= $startTS && $timestamp <= $endTS) { $sth = $dbh->prepare('INSERT INTO expenses_products (expenseID, productID, locationID, date, unitPrice, quantity, parentRecurringID) VALUES(:expenseID, :productID, :locationID, :date, :unitPrice, :quantity, :parentRecurringID)'); $sth->execute([':expenseID' => $id, ':productID' => $data['productID'], ':locationID' => $data['locationID'], ':date' => $timestamp, ':unitPrice' => $data['unitPrice'], ':quantity' => $data['quantity'], ':parentRecurringID' => $recurringID]); } } $changeData = ['subType' => 'product', 'productID' => $data['productID'], 'locationID' => $data['locationID'], 'unitPrice' => $data['unitPrice'], 'quantity' => $data['quantity'], 'recurring' => $data['recurring'], 'interval' => $data['interval'], 'dayOfMonth' => $data['dayOfMonth'], 'startDate' => $startTS, 'endDate' => $endTS]; } else { //get date of expense $sth = $dbh->prepare('SELECT date FROM expenses WHERE expenseID = :expenseID'); $sth->execute([':expenseID' => $id]); $row = $sth->fetch(); $sth = $dbh->prepare('INSERT INTO expenses_products (expenseID, productID, locationID, date, unitPrice, quantity) VALUES(:expenseID, :productID, :locationID, :date, :unitPrice, :quantity)'); $sth->execute([':expenseID' => $id, ':productID' => $data['productID'], ':locationID' => $data['locationID'], ':date' => $row['date'], ':unitPrice' => $data['unitPrice'], ':quantity' => $data['quantity']]); $changeData = ['subType' => 'product', 'productID' => $data['productID'], 'locationID' => $data['locationID'], 'unitPrice' => $data['unitPrice'], 'quantity' => $data['quantity']]; } } } elseif ($subType == 'other') { $sth = $dbh->prepare('SELECT quantity FROM expenseOthers WHERE expenseID = :expenseID AND name = :name AND unitPrice = :unitPrice'); $sth->execute([':expenseID' => $id, ':name' => $data['name'], ':unitPrice' => $data['unitPrice']]); $result = $sth->fetchAll(); if (count($result) == 1 && $data['recurring'] == 'no') { //if the item is already present in the expense AND we aren't doing a recurring item, add the quantity to the existing row $totalQuantity = $data['quantity'] + $result[0]['quantity']; $sth = $dbh->prepare('UPDATE expenseOthers SET quantity = :quantity WHERE expenseID = :expenseID AND name = :name AND unitPrice = :unitPrice'); $sth->execute([':quantity' => $totalQuantity, ':expenseID' => $id, ':name' => $data['name'], ':unitPrice' => $data['unitPrice']]); $changeAction = 'E'; //this is technically an edit, not an add $changeData = ['subType' => 'other', 'name' => $data['name'], 'unitPrice' => $data['unitPrice'], 'quantity' => $totalQuantity]; } else { if ($data['recurring'] == 'yes') { $startTS = DateTime::createFromFormat($SETTINGS['dateFormat'] . '|', $data['startDate'])->getTimestamp(); $endTS = DateTime::createFromFormat($SETTINGS['dateFormat'] . '|', $data['endDate'])->getTimestamp(); //add the recurring item $sth = $dbh->prepare('SELECT MAX(recurringID) AS recurringID FROM expenseOthers'); $sth->execute(); $result = $sth->fetchAll(); $recurringID = $result[0]['recurringID'] + 1; $sth = $dbh->prepare('INSERT INTO expenseOthers (expenseID, name, unitPrice, quantity, recurringID, dayOfMonth, startDate, endDate) VALUES(:expenseID, :name, :unitPrice, :quantity, :recurringID, :dayOfMonth, :startDate, :endDate)'); $sth->execute([':expenseID' => $id, ':name' => $data['name'], ':unitPrice' => $data['unitPrice'], ':quantity' => $data['quantity'], ':recurringID' => $recurringID, ':dayOfMonth' => $data['dayOfMonth'], ':startDate' => $startTS, ':endDate' => $endTS]); //add occasions from start date to now $temp = new DateTime(); $temp->setTimestamp($startTS); $patternStart = new DateTime($data['dayOfMonth'] . '-' . $temp->format('M') . '-' . $temp->format('Y')); $interval = new DateInterval('P1M'); $now = new DateTime(); $period = new DatePeriod($patternStart, $interval, $now); foreach ($period as $date) { $timestamp = $date->getTimestamp(); if ($timestamp >= $startTS && $timestamp <= $endTS) { $sth = $dbh->prepare('INSERT INTO expenseOthers (expenseID, name, date, unitPrice, quantity, parentRecurringID) VALUES(:expenseID, :name, :date, :unitPrice, :quantity, :parentRecurringID)'); $sth->execute([':expenseID' => $id, ':name' => $data['name'], ':date' => $timestamp, ':unitPrice' => $data['unitPrice'], ':quantity' => $data['quantity'], ':parentRecurringID' => $recurringID]); } } $changeData = ['subType' => 'other', 'name' => $data['name'], 'unitPrice' => $data['unitPrice'], 'quantity' => $data['quantity'], 'recurring' => $data['recurring'], 'interval' => $data['interval'], 'dayOfMonth' => $data['dayOfMonth'], 'startDate' => $startTS, 'endDate' => $endTS]; } else { //get date of expense $sth = $dbh->prepare('SELECT date FROM expenses WHERE expenseID = :expenseID'); $sth->execute([':expenseID' => $id]); $row = $sth->fetch(); $sth = $dbh->prepare('INSERT INTO expenseOthers (expenseID, name, date, unitPrice, quantity) VALUES(:expenseID, :name, :date, :unitPrice, :quantity)'); $sth->execute([':expenseID' => $id, ':name' => $data['name'], ':date' => $row['date'], ':unitPrice' => $data['unitPrice'], ':quantity' => $data['quantity']]); $changeData = ['subType' => 'other', 'name' => $data['name'], 'unitPrice' => $data['unitPrice'], 'quantity' => $data['quantity']]; } } } self::updateAmountDue($id); $temp = isset($changeAction) ? $changeAction : 'A'; addChange('expense', $id, $_SESSION['employeeID'], $temp, json_encode($changeData)); } } elseif ($data['subAction'] == 'edit') { //edit subAction $subType = $data['subType']; unset($data['subAction']); unset($data['subType']); $subID = $data['subID']; unset($data['subID']); $data = cleanData('expense', $subType, $data); $return = verifyData('expense', $subType, 'edit', $data); if ($return['status'] != 'fail') { if ($subType == 'product') { $sth = $dbh->prepare('UPDATE expenses_products SET unitPrice = :unitPrice, quantity = :quantity WHERE expenseProductID = :expenseProductID'); $sth->execute([':unitPrice' => $data['unitPrice'], ':quantity' => $data['quantity'], ':expenseProductID' => $subID]); $changeData = ['subType' => 'product', 'productID' => $data['productID'], 'locationID' => $data['locationID'], 'unitPrice' => $data['unitPrice'], 'quantity' => $data['quantity']]; } elseif ($subType == 'other') { $sth = $dbh->prepare('UPDATE expenseOthers SET unitPrice = :unitPrice, quantity = :quantity WHERE expenseOtherID = :expenseOtherID'); $sth->execute([':unitPrice' => $data['unitPrice'], ':quantity' => $data['quantity'], ':expenseOtherID' => $subID]); $changeData = ['subType' => 'other', 'name' => $data['name'], 'unitPrice' => $data['unitPrice'], 'quantity' => $data['quantity']]; } self::updateAmountDue($id); addChange('expense', $id, $_SESSION['employeeID'], 'E', json_encode($changeData)); } } elseif ($data['subAction'] == 'delete') { //delete subAction if ($data['subType'] == 'payment') { $sth = $dbh->prepare('SELECT date, paymentAmount FROM expensePayments WHERE paymentID = :paymentID'); $sth->execute([':paymentID' => $data['subID']]); $row = $sth->fetch(); $sth = $dbh->prepare('DELETE FROM expensePayments WHERE paymentID = :paymentID'); $sth->execute([':paymentID' => $data['subID']]); $changeData = ['subType' => 'payment', 'date' => $row['date'], 'paymentAmount' => $row['paymentAmount']]; } elseif ($data['subType'] == 'product') { $sth = $dbh->prepare('SELECT productID, locationID, unitPrice, quantity, recurringID FROM expenses_products WHERE expenseProductID = :expenseProductID'); $sth->execute([':expenseProductID' => $data['subID']]); $row = $sth->fetch(); $recurring = $row['recurringID'] === null ? 'no' : 'yes'; //delete item and children (if any) $sth = $dbh->prepare('DELETE FROM expenses_products WHERE expenseProductID = :expenseProductID OR parentRecurringID = :recurringID'); $sth->execute([':expenseProductID' => $data['subID'], ':recurringID' => $row['recurringID']]); $changeData = ['subType' => 'product', 'productID' => $row['productID'], 'locationID' => $row['locationID'], 'unitPrice' => $row['unitPrice'], 'quantity' => $row['quantity'], 'recurring' => $recurring]; } elseif ($data['subType'] == 'other') { $sth = $dbh->prepare('SELECT name, unitPrice, quantity, recurringID FROM expenseOthers WHERE expenseOtherID = :expenseOtherID'); $sth->execute([':expenseOtherID' => $data['subID']]); $row = $sth->fetch(); $recurring = $row['recurringID'] === null ? 'no' : 'yes'; //delete item and children (if any) $sth = $dbh->prepare('DELETE FROM expenseOthers WHERE expenseOtherID = :expenseOtherID OR parentRecurringID = :recurringID'); $sth->execute([':expenseOtherID' => $data['subID'], ':recurringID' => $row['recurringID']]); $changeData = ['subType' => 'other', 'name' => $row['name'], 'unitPrice' => $row['unitPrice'], 'quantity' => $row['quantity'], 'recurring' => $recurring]; } self::updateAmountDue($id); addChange('expense', $id, $_SESSION['employeeID'], 'D', json_encode($changeData)); } return $return; }