function createIForm($astrFormElements, $elem, $intKeyValue, $newRecord, $strForm) { ?> <div class="iform <?php echo $elem['style']; ?> ui-corner-tl ui-corner-bl ui-corner-br ui-corner-tr ui-helper-clearfix" id="<?php echo $elem['name']; ?> "<?php echo $elem['elem_attributes'] ? ' ' . $elem['elem_attributes'] : ''; ?> > <div class="ui-corner-tl ui-corner-tr fg-toolbar ui-toolbar ui-widget-header"><?php echo $elem['label']; ?> </div> <?php if ($newRecord) { ?> <div id="inewmessage" class="new_message"><?php echo $GLOBALS['locSaveRecordToAddRows']; ?> </div> </div> <?php return; } ?> <script type="text/javascript"> /* <![CDATA[ */ function format_currency(value, decimals) { var s = parseFloat(value).toFixed(decimals).replace('.', '<?php echo $GLOBALS['locDecimalSeparator']; ?> '); <?php if ($GLOBALS['locThousandSeparator']) { ?> var parts = s.split('<?php echo $GLOBALS['locDecimalSeparator']; ?> '); var regexp = /(\d+)(\d{3})<?php echo $GLOBALS['locDecimalSeparator']; ?> ?/; while (regexp.test(parts[0])) { parts[0] = parts[0].replace(regexp, '$1' + '<?php echo $GLOBALS['locThousandSeparator']; ?> ' + '$2'); } s = parts[0]; if (parts.length > 1) { s += '<?php echo $GLOBALS['locDecimalSeparator']; ?> ' + parts[1]; } <?php } ?> return s; } function round_number(num, dec) { return Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec); } function init_rows() { <?php $subFormElements = getFormElements($elem['name']); $rowSumColumns = getFormRowSumColumns($elem['name']); $strParentKey = getFormParentKey($elem['name']); $clearRowValuesAfterAdd = getFormClearRowValuesAfterAdd($elem['name']); $onAfterRowAdded = getFormOnAfterRowAdded($elem['name']); $formJSONType = getFormJSONType($elem['name']); foreach ($subFormElements as $subElem) { if ($subElem['type'] != 'LIST') { continue; } echo ' var arr_' . $subElem['name'] . ' = {"0":"-"'; $res = mysqli_query_check($subElem['listquery']); $translate = strstr($subElem['style'], ' translated'); while ($row = mysqli_fetch_row($res)) { if ($translate && isset($GLOBALS["loc{$row[1]}"])) { $row[1] = $GLOBALS["loc{$row[1]}"]; } echo ',' . $row[0] . ':"' . addcslashes($row[1], '\\"\\/') . '"'; } echo "};\n"; } ?> $.getJSON('json.php?func=get_<?php echo $elem['name']; ?> &parent_id=<?php echo $intKeyValue; ?> ', function(json) { $('#itable > tbody > tr[id!=form_row]').remove(); var table = document.getElementById('itable'); for (var i = 0; i < json.records.length; i++) { var record = json.records[i]; var tr = $('<tr/>'); <?php foreach ($subFormElements as $subElem) { if (in_array($subElem['type'], array('HID_INT', 'SECHID_INT', 'BUTTON', 'NEWLINE'))) { continue; } $name = $subElem['name']; $class = $subElem['style']; if ($subElem['type'] == 'LIST' || $subElem['type'] == 'SEARCHLIST') { echo " if (record.{$name} == null) record.{$name} = 0; \$('<td/>').addClass('{$class}' + (record.deleted == 1 ? ' deleted' : '')).text(record.{$name}_text).appendTo(tr);\n"; } elseif ($subElem['type'] == 'INT') { if (isset($subElem['decimals'])) { echo " \$('<td/>').addClass('{$class}' + (record.deleted == 1 ? ' deleted' : '')).text(record.{$name} ? format_currency(record.{$name}, {$subElem['decimals']}) : '').appendTo(tr);\n"; } else { echo " \$('<td/>').addClass('{$class}' + (record.deleted == 1 ? ' deleted' : '')).text(record.{$name} ? record.{$name}.replace('.', '{$GLOBALS['locDecimalSeparator']}') : '').appendTo(tr);\n"; } } elseif ($subElem['type'] == 'INTDATE') { echo " \$('<td/>').addClass('{$class}' + (record.deleted == 1 ? ' deleted' : '')).text(record.{$name}.substr(6, 2) + '.' + record.{$name}.substr(4, 2) + '.' + record.{$name}.substr(0, 4)).appendTo(tr);\n"; } elseif ($subElem['type'] == 'CHECK') { echo " \$('<td/>').addClass('{$class}' + (record.deleted == 1 ? ' deleted' : '')).text(record.{$name} == 1 ? \"" . $GLOBALS['locYesButton'] . '" : "' . $GLOBALS['locNoButton'] . "\").appendTo(tr);\n"; } elseif ($subElem['type'] == 'ROWSUM') { ?> var items = record.<?php echo $rowSumColumns['multiplier']; ?> ; var price = record.<?php echo $rowSumColumns['price']; ?> ; var discount = record.<?php echo $rowSumColumns['discount']; ?> || 0; var VATPercent = record.<?php echo $rowSumColumns['vat']; ?> ; var VATIncluded = record.<?php echo $rowSumColumns['vat_included']; ?> ; price *= (1 - discount / 100); var sum = 0; var sumVAT = 0; var VAT = 0; if (VATIncluded == 1) { sumVAT = round_number(items * price, 2); sum = round_number(sumVAT / (1 + VATPercent / 100), 2); VAT = sumVAT - sum; } else { sum = round_number(items * price, 2); VAT = round_number(sum * (VATPercent / 100), 2); sumVAT = sum + VAT; } sum = format_currency(sum, <?php echo isset($subElem['decimals']) ? $subElem['decimals'] : 2; ?> ); VAT = format_currency(VAT, <?php echo isset($subElem['decimals']) ? $subElem['decimals'] : 2; ?> ); sumVAT = format_currency(sumVAT, <?php echo isset($subElem['decimals']) ? $subElem['decimals'] : 2; ?> ); var title = '<?php echo $GLOBALS['locVATLess'] . ': '; ?> ' + sum + ' – ' + '<?php echo $GLOBALS['locVATPart'] . ': '; ?> ' + VAT; $('<td/>').addClass('<?php echo $class; ?> ' + (record.deleted == 1 ? ' deleted' : '')).append('<span title="' + title + '">' + sumVAT + '<\/span>').appendTo(tr); <?php } else { echo " \$('<td/>').addClass('{$class}' + (record.deleted == 1 ? ' deleted' : '')).text(record.{$name} ? record.{$name} : '').appendTo(tr);\n"; } } if (sesWriteAccess()) { ?> $('<td/>').addClass('button').append('<a class="tinyactionlink row_edit_button rec' + record.id + '" href="#"><?php echo $GLOBALS['locEdit']; ?> <\/a>').appendTo(tr); $('<td/>').addClass('button').append('<a class="tinyactionlink row_copy_button rec' + record.id + '" href="#"><?php echo $GLOBALS['locCopy']; ?> <\/a>').appendTo(tr); <?php } ?> $(table).append(tr); } <?php if (isset($rowSumColumns['show_summary']) && $rowSumColumns['show_summary']) { ?> var totSum = 0; var totVAT = 0; var totSumVAT = 0; for (var i = 0; i < json.records.length; i++) { var record = json.records[i]; var items = record.<?php echo $rowSumColumns['multiplier']; ?> ; var price = record.<?php echo $rowSumColumns['price']; ?> ; var discount = record.<?php echo $rowSumColumns['discount']; ?> || 0; var VATPercent = record.<?php echo $rowSumColumns['vat']; ?> ; var VATIncluded = record.<?php echo $rowSumColumns['vat_included']; ?> ; price *= (1 - discount / 100); var sum = 0; var sumVAT = 0; var VAT = 0; if (VATIncluded == 1) { sumVAT = round_number(items * price, 2); sum = round_number(sumVAT / (1 + VATPercent / 100), 2); VAT = sumVAT - sum; } else { sum = round_number(items * price, 2); VAT = round_number(sum * (VATPercent / 100), 2); sumVAT = sum + VAT; } totSum += sum; totVAT += VAT; totSumVAT += sumVAT; } var tr = $('<tr/>').addClass('summary'); $('<td/>').addClass('input').attr('colspan', '10').attr('align', 'right').text('<?php echo $GLOBALS['locTotalExcludingVAT']; ?> ').appendTo(tr); $('<td/>').addClass('input').attr('align', 'right').text(format_currency(totSum, 2)).appendTo(tr); $(table).append(tr); tr = $('<tr/>').addClass('summary'); $('<td/>').addClass('input').attr('colspan', '10').attr('align', 'right').text('<?php echo $GLOBALS['locTotalVAT']; ?> ').appendTo(tr); $('<td/>').addClass('input').attr('align', 'right').text(format_currency(totVAT, 2)).appendTo(tr); $(table).append(tr); var tr = $('<tr/>').addClass('summary'); $('<td/>').addClass('input').attr('colspan', '10').attr('align', 'right').text('<?php echo $GLOBALS['locTotalIncludingVAT']; ?> ').appendTo(tr); $('<td/>').addClass('input').attr('align', 'right').text(format_currency(totSumVAT, 2)).appendTo(tr); $(table).append(tr); <?php } ?> $('a[class~="row_edit_button"]').click(function(event) { var row_id = $(this).attr('class').match(/rec(\d+)/)[1]; popup_editor(event, '<?php echo $GLOBALS['locRowModification']; ?> ', row_id, false); return false; }); $('a[class~="row_copy_button"]').click(function(event) { var row_id = $(this).attr('class').match(/rec(\d+)/)[1]; popup_editor(event, '<?php echo $GLOBALS['locRowCopy']; ?> ', row_id, true); return false; }); $('a[class~="tinyactionlink"]').button(); init_rows_done(); }); } <?php if (sesWriteAccess()) { ?> function save_row(form_id) { var form = document.getElementById(form_id); var obj = new Object(); <?php foreach ($subFormElements as $subElem) { if (!in_array($subElem['type'], array('HID_INT', 'SECHID_INT', 'BUTTON', 'NEWLINE', 'ROWSUM', 'CHECK', 'INT'))) { ?> obj.<?php echo $subElem['name']; ?> = document.getElementById(form_id + '_<?php echo $subElem['name']; ?> ').value; <?php } elseif ($subElem['type'] == 'CHECK') { ?> obj.<?php echo $subElem['name']; ?> = document.getElementById(form_id + '_<?php echo $subElem['name']; ?> ').checked ? 1 : 0; <?php } elseif ($subElem['type'] == 'INT') { ?> obj.<?php echo $subElem['name']; ?> = document.getElementById(form_id + '_<?php echo $subElem['name']; ?> ').value.replace('<?php echo $GLOBALS['locDecimalSeparator']; ?> ', '.'); <?php } } ?> obj.<?php echo $elem['parent_key'] . " = {$intKeyValue}"; ?> ; if (form.row_id) obj.id = form.row_id.value; $.ajax({ 'url': "json.php?func=put_<?php echo $formJSONType; ?> ", 'type': 'POST', 'dataType': 'json', 'data': $.toJSON(obj), 'contentType': 'application/json; charset=utf-8', 'success': function(data) { if (data.missing_fields) { errormsg('<?php echo $GLOBALS['locErrValueMissing']; ?> : ' + data.missing_fields); } else { if (form_id == 'iform') $('.add_row_button').removeClass('ui-state-highlight'); init_rows(); if (form_id == 'iform_popup') $("#popup_edit").dialog('close'); if (!obj.id) { <?php echo $onAfterRowAdded; foreach ($subFormElements as $subElem) { if (!in_array($subElem['type'], array('HID_INT', 'SECHID_INT', 'BUTTON', 'NEWLINE', 'ROWSUM'))) { if (isset($subElem['default']) && strstr($subElem['default'], 'ADD')) { // The value is taken from whatever form was used but put into iform ?> var fld = document.getElementById(form_id + '_<?php echo $subElem['name']; ?> '); document.getElementById('iform_<?php echo $subElem['name']; ?> ').value = parseInt(fld.value) + 5; <?php } elseif ($clearRowValuesAfterAdd && $subElem['type'] != 'INTDATE') { if ($subElem['type'] == 'LIST' || $subElem['type'] == 'SEARCHLIST') { ?> document.getElementById('iform_<?php echo $subElem['name']; ?> ').selectedIndex = 0; <?php } elseif ($subElem['type'] == 'CHECK') { ?> document.getElementById('iform_<?php echo $subElem['name']; ?> ').checked = 0; <?php } else { ?> document.getElementById('iform_<?php echo $subElem['name']; ?> ').value = ''; <?php } } } } ?> } } }, 'error': function(XMLHTTPReq, textStatus, errorThrown) { if (textStatus == 'timeout') alert('Timeout trying to save row'); else alert('Error trying to save row: ' + XMLHTTPReq.status + ' - ' + XMLHTTPReq.statusText); return false; } }); } function update_row_dates(id) { var buttons = new Object(); buttons["<?php echo $GLOBALS['locUpdateRowDates']; ?> "] = function() { var date = $("#popup_date_edit_field").val(); if (date == '') { alert('<?php echo $GLOBALS['locErrValueMissing']; ?> '); return; } var params = { func: 'update_invoice_row_dates', id: <?php echo $intKeyValue; ?> , date: date }; $.ajax({ 'url': 'json.php', 'data': params, 'type': 'GET', 'dataType': 'json', 'contentType': 'application/json; charset=utf-8', 'success': function(data) { if (data.status != 'ok') { alert(data.errors); } else { $("#popup_date_edit").dialog('close'); init_rows(); } }, 'error': function(XMLHTTPReq, textStatus, errorThrown) { if (textStatus == 'timeout') errormsg('Timeout trying to update row dates'); else errormsg('Error trying to update row dates: ' + XMLHTTPReq.status + ' - ' + XMLHTTPReq.statusText); return false; } }); }; buttons["<?php echo $GLOBALS['locClose']; ?> "] = function() { $("#popup_date_edit").dialog('close'); }; $("#popup_date_edit").dialog({ modal: true, width: 420, height: 120, resizable: false, buttons: buttons, title: '<?php echo $GLOBALS['locUpdateAllRowDates']; ?> ' }); } function delete_row(form_id) { var form = document.getElementById(form_id); var id = form.row_id.value; $.ajax({ 'url': "json.php?func=delete_<?php echo $formJSONType; ?> &id=" + id, 'type': 'GET', 'dataType': 'json', 'contentType': 'application/json; charset=utf-8', 'success': function(data) { init_rows(); if (form_id == 'iform_popup') $("#popup_edit").dialog('close'); }, 'error': function(XMLHTTPReq, textStatus, errorThrown) { if (textStatus == 'timeout') errormsg('Timeout trying to save row'); else errormsg('Error trying to save row: ' + XMLHTTPReq.status + ' - ' + XMLHTTPReq.statusText); return false; } }); } function popup_editor(event, title, id, copy_row) { $.getJSON('json.php?func=get_<?php echo $formJSONType; ?> &id=' + id, function(json) { if (!json.id) return; var form = document.getElementById('iform_popup'); if (copy_row) form.row_id.value = ''; else form.row_id.value = id; <?php foreach ($subFormElements as $subElem) { if (in_array($subElem['type'], array('HID_INT', 'SECHID_INT', 'BUTTON', 'NEWLINE', 'ROWSUM'))) { continue; } $name = $subElem['name']; if ($subElem['type'] == 'SEARCHLIST') { ?> var item = { id: json.<?php echo $name; ?> , text: json.<?php echo $name; ?> _text }; $('#<?php echo "iform_popup_{$name}"; ?> ').select2('data', item); <?php } elseif ($subElem['type'] == 'LIST') { ?> for (var i = 0; i < form.<?php echo "iform_popup_{$name}"; ?> .options.length; i++) { var item = form.<?php echo "iform_popup_{$name}"; ?> .options[i]; if (item.value == json.<?php echo $name; ?> ) { item.selected = true; break; } } <?php } elseif ($subElem['type'] == 'INT') { if (isset($subElem['default']) && strstr($subElem['default'], 'ADD')) { ?> var value; if (copy_row) value = document.getElementById('<?php echo "iform_{$name}"; ?> ').value; else value = json.<?php echo $name; ?> ? json.<?php echo $name; ?> .replace('.', '<?php $GLOBALS['locDecimalSeparator']; ?> ') : ''; form.<?php echo "iform_popup_{$name}"; ?> .value = value; <?php } else { if (isset($subElem['decimals'])) { ?> form.<?php echo "iform_popup_{$name}"; ?> .value = json.<?php echo $name; ?> ? format_currency(json.<?php echo $name; ?> , <?php echo $subElem['decimals']; ?> ) : ''; <?php } else { ?> form.<?php echo "iform_popup_{$name}"; ?> .value = json.<?php echo $name; ?> ? json.<?php echo $name; ?> .replace('.', '<?php echo $GLOBALS['locDecimalSeparator']; ?> ') : ''; <?php } } } elseif ($subElem['type'] == 'INTDATE') { ?> form.<?php echo "iform_popup_{$name}"; ?> .value = json.<?php echo $name; ?> ? json.<?php echo $name; ?> .substr(6, 2) + '.' + json.<?php echo $name; ?> .substr(4, 2) + '.' + json.<?php echo $name; ?> .substr(0, 4) : ''; <?php } elseif ($subElem['type'] == 'CHECK') { ?> form.<?php echo "iform_popup_{$name}"; ?> .checked = json.<?php echo $name; ?> != 0 ? true : false; <?php } else { ?> form.<?php echo "iform_popup_{$name}"; ?> .value = json.<?php echo $name; ?> ; <?php } } ?> var buttons = new Object(); buttons["<?php echo $GLOBALS['locSave']; ?> "] = function() { save_row('iform_popup'); }; if (!copy_row) buttons["<?php echo $GLOBALS['locDelete']; ?> "] = function() { if(confirm('<?php echo $GLOBALS['locConfirmDelete']; ?> ')==true) { delete_row('iform_popup'); } return false; }; buttons["<?php echo $GLOBALS['locClose']; ?> "] = function() { $("#popup_edit").dialog('close'); }; $("#popup_edit").dialog({ modal: true, width: 840, height: 150, resizable: false, buttons: buttons, title: title, }); }); } <?php } ?> /* ]]> */ </script> <form method="post" name="iform" id="iform"> <table class="iform" id="itable"> <thead> <tr> <?php foreach ($subFormElements as $subElem) { if (!in_array($subElem['type'], array('HID_INT', 'SECHID_INT', 'BUTTON', 'NEWLINE'))) { ?> <th class="label ui-state-default <?php echo strtolower($subElem['style']); ?> _label"><?php echo $subElem['label']; ?> </th> <?php } } ?> </tr> </thead> <tbody> <?php if (sesWriteAccess()) { ?> <tr id="form_row"> <?php foreach ($subFormElements as $subElem) { if (!in_array($subElem['type'], array('HID_INT', 'SECHID_INT', 'BUTTON', 'NEWLINE', 'ROWSUM'))) { $value = getFormDefaultValue($subElem, $intKeyValue); ?> <td class="label <?php echo strtolower($subElem['style']); ?> _label"> <?php echo htmlFormElement('iform_' . $subElem['name'], $subElem['type'], $value, $subElem['style'], $subElem['listquery'], 'MODIFY', 0, '', array(), $subElem['elem_attributes']); ?> </td> <?php } elseif ($subElem['type'] == 'ROWSUM') { ?> <td class="label <?php echo strtolower($subElem['style']); ?> _label"> </td> <?php } } if ($strForm == 'invoice') { ?> <td class="button"> <a class="tinyactionlink add_row_button" href="#" onclick="save_row('iform'); return false;"><?php echo $GLOBALS['locAddRow']; ?> </a> </td> <td class="button"> <a class="tinyactionlink update_row_dates" href="#" onclick="update_row_dates(); return false;"><?php echo $GLOBALS['locUpdateRowDates']; ?> </a> </td> <?php } else { ?> <td class="button" colspan="2"> <a class="tinyactionlink add_row_button" href="#" onclick="save_row('iform'); return false;"><?php echo $GLOBALS['locAddRow']; ?> </a> </td> <?php } ?> </tr> </tbody> </table> </form> </div> <div id="popup_edit" style="display: none; width: 900px; overflow: hidden"> <form method="post" name="iform_popup" id="iform_popup"> <input type="hidden" name="row_id" value=""> <input type="hidden" name="<?php echo $strParentKey; ?> " value="<?php echo $intKeyValue; ?> "> <table class="iform"> <tr> <?php foreach ($subFormElements as $elem) { if (!in_array($elem['type'], array('HID_INT', 'SECHID_INT', 'BUTTON', 'NEWLINE', 'ROWSUM'))) { ?> <td class="label <?php echo strtolower($elem['style']); ?> _label"> <?php echo $elem['label']; ?> <br> <?php echo htmlFormElement('iform_popup_' . $elem['name'], $elem['type'], '', $elem['style'], $elem['listquery'], 'MODIFY', 0, '', array(), $elem['elem_attributes']); ?> </td> <?php } elseif ($elem['type'] == 'SECHID_INT') { ?> <input type="hidden" name="<?php echo 'iform_popup_' . $elem['name']; ?> " value="<?php echo gpcStripSlashes($astrValues[$elem['name']]); ?> "> <?php } elseif ($elem['type'] == 'BUTTON') { ?> <td class="label"> </td> <?php } } } ?> </tr> </table> </form> </div> <div id="popup_date_edit" style="display: none; width: 300px; overflow: hidden"> <form method="post" name="form_date_popup" id="form_date_popup"> <input id="popup_date_edit_field" type="text" class="medium hasCalendar"> </form> </div> <?php }
function getGet($strKey, $varDefault) { return isset($_GET[$strKey]) ? gpcStripSlashes($_GET[$strKey]) : $varDefault; }