Exemple #1
0
 private function printReport()
 {
     $intBaseId = getRequest('base', false);
     $intCompanyId = getRequest('company', false);
     $grouping = getRequest('grouping', '');
     $format = getRequest('format', 'html');
     $printFields = getRequest('fields', array());
     $rowTypes = getRequest('row_types', 'all');
     $dateRange = explode(' - ', getRequest('date', ''));
     $startDate = $dateRange[0];
     $endDate = isset($dateRange[1]) ? $dateRange[1] : $startDate;
     if ($startDate) {
         $startDate = dateConvDate2DBDate($startDate);
     }
     if ($endDate) {
         $endDate = dateConvDate2DBDate($endDate);
     }
     $rowDateRange = explode(' - ', getRequest('row_date', ''));
     $rowStartDate = $rowDateRange[0];
     $rowEndDate = isset($rowDateRange[1]) ? $rowDateRange[1] : $rowStartDate;
     if ($rowStartDate) {
         $rowStartDate = dateConvDate2DBDate($rowStartDate);
     }
     if ($rowEndDate) {
         $rowEndDate = dateConvDate2DBDate($rowEndDate);
     }
     $paymentDateRange = explode(' - ', getRequest('payment_date', ''));
     $paymentStartDate = $paymentDateRange[0];
     $paymentEndDate = isset($paymentDateRange[1]) ? $paymentDateRange[1] : '';
     if ($paymentStartDate) {
         $paymentStartDate = dateConvDate2DBDate($paymentStartDate);
     }
     if ($paymentEndDate) {
         $paymentEndDate = dateConvDate2DBDate($paymentEndDate);
     }
     $arrParams = array();
     $strQuery = "SELECT i.id, i.invoice_no, i.invoice_date, i.due_date, i.payment_date, i.ref_number, i.ref_number, c.company_name AS name, c.billing_address, ist.name as state " . "FROM {prefix}invoice i " . "LEFT OUTER JOIN {prefix}company c ON c.id = i.company_id " . "LEFT OUTER JOIN {prefix}invoice_state ist ON i.state_id = ist.id " . "WHERE i.deleted=0";
     if ($startDate) {
         $strQuery .= ' AND i.invoice_date >= ?';
         $arrParams[] = $startDate;
     }
     if ($endDate) {
         $strQuery .= ' AND i.invoice_date <= ?';
         $arrParams[] = $endDate;
     }
     if ($paymentStartDate) {
         $strQuery .= ' AND i.payment_date >= ?';
         $arrParams[] = $paymentStartDate;
     }
     if ($paymentEndDate) {
         $strQuery .= ' AND i.payment_date <= ?';
         $arrParams[] = $paymentEndDate;
     }
     if ($intBaseId) {
         $strQuery .= ' AND i.base_id = ?';
         $arrParams[] = $intBaseId;
     }
     if ($intCompanyId) {
         $strQuery .= ' AND i.company_id = ?';
         $arrParams[] = $intCompanyId;
     }
     $strQuery2 = '';
     $strQuery3 = "SELECT id, name " . "FROM {prefix}invoice_state WHERE deleted=0 " . "ORDER BY order_no";
     $intRes = mysqli_query_check($strQuery3);
     while ($row = mysqli_fetch_assoc($intRes)) {
         $intStateId = $row['id'];
         $strStateName = $row['name'];
         $strTemp = "stateid_{$intStateId}";
         $tmpSelected = getRequest($strTemp, false);
         if ($tmpSelected) {
             $strQuery2 .= 'i.state_id = ? OR ';
             $arrParams[] = $intStateId;
         }
     }
     if ($strQuery2) {
         $strQuery2 = ' AND (' . substr($strQuery2, 0, -4) . ')';
     }
     $strQuery .= "{$strQuery2} ORDER BY ";
     switch ($grouping) {
         case 'state':
             $strQuery .= "state_id, invoice_date, invoice_no";
             break;
         case 'client':
             $strQuery .= "name, invoice_date, invoice_no";
             break;
         default:
             $strQuery .= "invoice_date, invoice_no";
     }
     $this->printHeader($format, $printFields, $startDate, $endDate);
     $intTotSum = 0;
     $intTotVAT = 0;
     $intTotSumVAT = 0;
     $currentGroup = false;
     $groupTotSum = 0;
     $groupTotVAT = 0;
     $groupTotSumVAT = 0;
     $intRes = mysqli_param_query($strQuery, $arrParams);
     while ($row = mysqli_fetch_assoc($intRes)) {
         switch ($grouping) {
             case 'state':
                 $invoiceGroup = $row['state'];
                 break;
             case 'month':
                 $invoiceGroup = substr($row['invoice_date'], 4, 2);
                 break;
             case 'client':
                 $invoiceGroup = $row['name'];
                 break;
             default:
                 $invoiceGroup = false;
         }
         $rowParams = array($row['id']);
         $strQuery = "SELECT ir.description, ir.pcs, ir.price, ir.discount, ir.row_date, ir.vat, ir.vat_included " . "FROM {prefix}invoice_row ir " . "WHERE ir.invoice_id=? AND ir.deleted=0";
         if ($rowStartDate) {
             $strQuery .= ' AND ir.row_date >= ?';
             $rowParams[] = $rowStartDate;
         }
         if ($rowEndDate) {
             $strQuery .= ' AND ir.row_date <= ?';
             $rowParams[] = $rowEndDate;
         }
         if ($rowTypes != 'all') {
             if ($rowTypes == 'normal') {
                 $strQuery .= ' AND ir.reminder_row = 0';
             } else {
                 if ($rowTypes == 'reminder') {
                     $strQuery .= ' AND ir.reminder_row in (1, 2)';
                 }
             }
         }
         $intRes2 = mysqli_param_query($strQuery, $rowParams);
         $intRowSum = 0;
         $intRowVAT = 0;
         $intRowSumVAT = 0;
         $rows = false;
         while ($row2 = mysqli_fetch_assoc($intRes2)) {
             $rows = true;
             list($intSum, $intVAT, $intSumVAT) = calculateRowSum($row2['price'], $row2['pcs'], $row2['vat'], $row2['vat_included'], $row2['discount']);
             $intRowSum += $intSum;
             $intRowVAT += $intVAT;
             $intRowSumVAT += $intSumVAT;
             $intTotSum += $intSum;
             $intTotVAT += $intVAT;
             $intTotSumVAT += $intSumVAT;
         }
         if (!$rows) {
             continue;
         }
         if ($grouping && $currentGroup !== false && $currentGroup != $invoiceGroup) {
             $this->printGroupSums($format, $printFields, $row, $groupTotSum, $groupTotVAT, $groupTotSumVAT);
             $groupTotSum = 0;
             $groupTotVAT = 0;
             $groupTotSumVAT = 0;
         }
         $currentGroup = $invoiceGroup;
         $groupTotSum += $intRowSum;
         $groupTotVAT += $intRowVAT;
         $groupTotSumVAT += $intRowSumVAT;
         $this->printRow($format, $printFields, $row, $intRowSum, $intRowVAT, $intRowSumVAT);
     }
     if ($grouping) {
         $this->printGroupSums($format, $printFields, $row, $groupTotSum, $groupTotVAT, $groupTotSumVAT);
     }
     $this->printTotals($format, $printFields, $intTotSum, $intTotVAT, $intTotSumVAT);
     $this->printFooter($format, $printFields);
 }
Exemple #2
0
function saveFormData($table, &$primaryKey, &$formElements, &$values, &$warnings, $parentKeyName = '', $parentKey = FALSE)
{
    global $dblink;
    $missingValues = '';
    $strFields = '';
    $strInsert = '';
    $strUpdateFields = '';
    $arrValues = [];
    if (!isset($primaryKey) || !$primaryKey) {
        unset($values['id']);
    }
    foreach ($formElements as $elem) {
        $type = $elem['type'];
        if (in_array($type, ['', 'IFORM', 'RESULT', 'BUTTON', 'JSBUTTON', 'IMAGE', 'ROWSUM', 'NEWLINE', 'LABEL']) || isset($elem['read_only']) && $elem['read_only']) {
            continue;
        }
        $name = $elem['name'];
        if (!$elem['allow_null'] && (!isset($values[$name]) || $values[$name] === '')) {
            if ($missingValues) {
                $missingValues .= ', ';
            }
            $missingValues .= $elem['label'];
            continue;
        }
        $value = isset($values[$name]) ? $values[$name] : getFormDefaultValue($elem, $parentKey);
        if ($type == 'PASSWD' && !$value) {
            continue;
        }
        // Don't save empty password
        if (isset($elem['unique']) && $elem['unique']) {
            $query = "SELECT * FROM {$table} WHERE deleted=0 AND {$name}=?";
            $params = [$value];
            if (isset($primaryKey) && $primaryKey) {
                $query .= ' AND id!=?';
                $params[] = $primaryKey;
            }
            $res = mysqli_param_query($query, $params);
            if (mysqli_fetch_array($res)) {
                $warnings = sprintf($GLOBALS['locDuplicateValue'], $elem['label']);
                return false;
            }
        }
        if ($strFields) {
            $strFields .= ', ';
            $strInsert .= ', ';
            $strUpdateFields .= ', ';
        }
        $strFields .= $name;
        $fieldPlaceholder = '?';
        switch ($type) {
            case 'PASSWD':
                $fieldPlaceholder = 'md5(?)';
                $arrValues[] = $values[$name];
                break;
            case 'INT':
            case 'HID_INT':
            case 'SECHID_INT':
                $arrValues[] = $value !== '' && $value !== false ? str_replace(',', '.', $value) : ($elem['allow_null'] ? NULL : 0);
                break;
            case 'LIST':
            case 'SEARCHLIST':
                $arrValues[] = isset($values[$name]) ? $value !== '' ? str_replace(',', '.', $value) : NULL : NULL;
                break;
            case 'CHECK':
                $arrValues[] = $value ? 1 : 0;
                break;
            case 'INTDATE':
                $arrValues[] = $value ? dateConvDate2DBDate($value) : NULL;
                break;
            default:
                $arrValues[] = $value;
        }
        $strInsert .= $fieldPlaceholder;
        $strUpdateFields .= "{$name}={$fieldPlaceholder}";
    }
    if ($missingValues) {
        return $missingValues;
    }
    mysqli_query_check('SET AUTOCOMMIT = 0');
    mysqli_query_check('BEGIN');
    try {
        // Special case for invoice rows - update product stock balance
        if ($table == '{prefix}invoice_row') {
            updateProductStockBalance(isset($primaryKey) ? $primaryKey : null, isset($values['product_id']) ? $values['product_id'] : null, $values['pcs']);
        }
        if (!isset($primaryKey) || !$primaryKey) {
            if ($parentKeyName) {
                $strFields .= ", {$parentKeyName}";
                $strInsert .= ', ?';
                $arrValues[] = $parentKey;
            }
            $strQuery = "INSERT INTO {$table} ({$strFields}) VALUES ({$strInsert})";
            mysqli_param_query($strQuery, $arrValues, 'exception');
            $primaryKey = mysqli_insert_id($dblink);
        } else {
            // Special case for invoice - update product stock balance for all
            // invoice rows if the invoice was previously deleted
            if ($table == '{prefix}invoice') {
                $res = mysqli_param_query('SELECT deleted FROM {prefix}invoice WHERE id=?', [$primaryKey]);
                if (mysqli_fetch_value($res)) {
                    $res = mysqli_param_query('SELECT product_id, pcs FROM {prefix}invoice_row WHERE invoice_id=? AND deleted=0', [$primaryKey]);
                    while ($row = mysqli_fetch_assoc($res)) {
                        updateProductStockBalance(null, $row['product_id'], $row['pcs']);
                    }
                }
            }
            $strQuery = "UPDATE {$table} SET {$strUpdateFields}, deleted=0 WHERE id=?";
            $arrValues[] = $primaryKey;
            mysqli_param_query($strQuery, $arrValues, 'exception');
        }
    } catch (Exception $e) {
        mysqli_query_check('ROLLBACK');
        mysqli_query_check('SET AUTOCOMMIT = 1');
        die($e->getMessage());
    }
    mysqli_query_check('COMMIT');
    mysqli_query_check('SET AUTOCOMMIT = 1');
    // Special case for invoices - check for duplicate invoice numbers
    if ($table == '{prefix}invoice' && isset($values['invoice_no']) && $values['invoice_no']) {
        $query = 'SELECT ID FROM {prefix}invoice where deleted=0 AND id!=? AND invoice_no=?';
        $params = [$primaryKey, $values['invoice_no']];
        if (getSetting('invoice_numbering_per_base')) {
            $query .= ' AND base_id=?';
            $params[] = $values['base_id'];
        }
        if (getSetting('invoice_numbering_per_year')) {
            $query .= ' AND invoice_date >= ' . date('Y') . '0101';
        }
        $res = mysqli_param_query($query, $params);
        if (mysqli_fetch_assoc($res)) {
            $warnings = $GLOBALS['locInvoiceNumberAlreadyInUse'];
        }
    }
    return TRUE;
}
Exemple #3
0
 private function printReport()
 {
     $intStateID = getRequest('stateid', FALSE);
     $intBaseId = getRequest('base', FALSE);
     $intCompanyId = getRequest('company', FALSE);
     $intProductId = getRequest('product', FALSE);
     $format = getRequest('format', 'html');
     $dateRange = explode(' - ', getRequest('date', ''));
     $startDate = $dateRange[0];
     $endDate = isset($dateRange[1]) ? $dateRange[1] : $startDate;
     if ($startDate) {
         $startDate = dateConvDate2DBDate($startDate);
     }
     if ($endDate) {
         $endDate = dateConvDate2DBDate($endDate);
     }
     $arrParams = [];
     $strQuery = 'SELECT i.id ' . 'FROM {prefix}invoice i ' . 'WHERE i.deleted=0';
     if ($startDate) {
         $strQuery .= ' AND i.invoice_date >= ?';
         $arrParams[] = $startDate;
     }
     if ($endDate) {
         $strQuery .= ' AND i.invoice_date <= ?';
         $arrParams[] = $endDate;
     }
     if ($intBaseId) {
         $strQuery .= ' AND i.base_id = ?';
         $arrParams[] = $intBaseId;
     }
     if ($intCompanyId) {
         $strQuery .= ' AND i.company_id = ?';
         $arrParams[] = $intCompanyId;
     }
     $strQuery2 = '';
     $strQuery3 = 'SELECT id, name ' . 'FROM {prefix}invoice_state WHERE deleted=0 ' . 'ORDER BY order_no';
     $intRes = mysqli_query_check($strQuery3);
     while ($row = mysqli_fetch_assoc($intRes)) {
         $intStateId = $row['id'];
         $strStateName = $row['name'];
         $strTemp = "stateid_{$intStateId}";
         $tmpSelected = getRequest($strTemp, FALSE) ? TRUE : FALSE;
         if ($tmpSelected) {
             $strQuery2 .= ' i.state_id = ? OR ';
             $arrParams[] = $intStateId;
         }
     }
     if ($strQuery2) {
         $strQuery2 = ' AND (' . substr($strQuery2, 0, -3) . ')';
     }
     $strQuery .= "{$strQuery2} ORDER BY invoice_no";
     if ($intProductId) {
         $strProductWhere = 'AND ir.product_id = ? ';
         $arrParams[] = $intProductId;
     } else {
         $strProductWhere = '';
     }
     $strProductQuery = 'SELECT p.id, p.product_code, p.product_name, ir.description, ' . 'ir.vat, ir.pcs, t.name as unit, ir.price, ir.vat_included, ir.discount ' . 'FROM {prefix}invoice_row ir ' . 'LEFT OUTER JOIN {prefix}product p ON p.id = ir.product_id ' . 'LEFT OUTER JOIN {prefix}row_type t ON t.id = ir.type_id ' . "WHERE ir.deleted = 0 AND ir.partial_payment = 0 AND ir.invoice_id IN ({$strQuery}) {$strProductWhere}" . 'ORDER BY p.id, ir.description, t.name, ir.vat';
     $this->printHeader($format, $startDate, $endDate);
     $totalSum = 0;
     $totalVAT = 0;
     $totalSumVAT = 0;
     $prevRow = false;
     $productCount = 0;
     $productSum = 0;
     $productVAT = 0;
     $productSumVAT = 0;
     $intRes = mysqli_param_query($strProductQuery, $arrParams);
     while ($row = mysqli_fetch_assoc($intRes)) {
         if ($prevRow !== false && ($prevRow['id'] != $row['id'] || $prevRow['description'] != $row['description'] || $prevRow['unit'] != $row['unit'] || $prevRow['vat'] != $row['vat'])) {
             $this->printRow($format, $prevRow['product_code'], $prevRow['product_name'], $prevRow['description'], $productCount, $prevRow['unit'], $productSum, $prevRow['vat'], $productVAT, $productSumVAT);
             $productCount = 0;
             $productSum = 0;
             $productVAT = 0;
             $productSumVAT = 0;
         }
         $prevRow = $row;
         $productCount += $row['pcs'];
         list($rowSum, $rowVAT, $rowSumVAT) = calculateRowSum($row['price'], $row['pcs'], $row['vat'], $row['vat_included'], $row['discount']);
         $productSum += $rowSum;
         $productVAT += $rowVAT;
         $productSumVAT += $rowSumVAT;
         $totalSum += $rowSum;
         $totalVAT += $rowVAT;
         $totalSumVAT += $rowSumVAT;
     }
     if ($prevRow !== false) {
         $this->printRow($format, $prevRow['product_code'], $prevRow['product_name'], $prevRow['description'], $productCount, $prevRow['unit'], $productSum, $prevRow['vat'], $productVAT, $productSumVAT);
     }
     $this->printTotals($format, $totalSum, $totalVAT, $totalSumVAT);
     $this->printFooter($format);
 }
Exemple #4
0
function updateInvoiceRowDates($invoiceId, $date)
{
    $date = dateConvDate2DBDate($date);
    if ($date === false) {
        return json_encode(['status' => 'error', 'errors' => $GLOBALS['locErrInvalidValue']]);
    }
    mysqli_param_query('UPDATE {prefix}invoice_row SET row_date=? WHERE invoice_id=? AND deleted=0', [$date, $invoiceId]);
    return json_encode(['status' => 'ok']);
}
Exemple #5
0
         }
         $strSearchMatch = getPost("searchmatch_{$name}", '=');
         // do LIKE || NOT LIKE search to elements with text or varchar datatype
         if ($elem['type'] == 'TEXT' || $elem['type'] == 'AREA') {
             if ($strSearchMatch == '=') {
                 $strSearchMatch = 'LIKE';
             } else {
                 $strSearchMatch = 'NOT LIKE';
             }
             $strSearchValue = "'%" . addcslashes($astrValues[$name], "'\\") . "%'";
         } elseif ($astrFormElements[$j]['type'] == 'INT' || $astrFormElements[$j]['type'] == 'LIST' || $astrFormElements[$j]['type'] == 'SELECT' || $astrFormElements[$j]['type'] == 'SEARCHLIST') {
             $strSearchValue = $astrValues[$name];
         } elseif ($astrFormElements[$j]['type'] == 'CHECK') {
             $strSearchValue = $astrValues[$name] ? 1 : 0;
         } elseif ($astrFormElements[$j]['type'] == 'INTDATE') {
             $strSearchValue = dateConvDate2DBDate($astrValues[$name]);
         }
         if ($strSearchValue) {
             $strWhereClause .= "{$strSearchOperator}{$strListTableAlias}{$name} {$strSearchMatch} {$strSearchValue}";
         }
     }
 }
 $strWhereClause = urlencode($strWhereClause);
 if ($blnSearch) {
     $strLink = "index.php?func={$strFunc}&where={$strWhereClause}";
     $strOnLoad = "opener.location.href='{$strLink}'";
 }
 if ($blnSave && $strSearchName) {
     $strQuery = 'INSERT INTO {prefix}quicksearch(user_id, name, func, whereclause) ' . 'VALUES (?, ?, ?, ?)';
     $intRes = mysqli_param_query($strQuery, [$_SESSION['sesUSERID'], $strSearchName, $strFunc, $strWhereClause]);
 } elseif ($blnSave && !$strSearchName) {
Exemple #6
0
 private function printReport()
 {
     $intBaseId = getRequest('base', false);
     $intCompanyId = getRequest('company', false);
     $grouping = getRequest('grouping', '');
     $format = getRequest('format', 'html');
     $printFields = getRequest('fields', []);
     $rowTypes = getRequest('row_types', 'all');
     $dateRange = explode(' - ', getRequest('date', ''));
     $startDate = $dateRange[0];
     $endDate = isset($dateRange[1]) ? $dateRange[1] : $startDate;
     if ($startDate) {
         $startDate = dateConvDate2DBDate($startDate);
     }
     if ($endDate) {
         $endDate = dateConvDate2DBDate($endDate);
     }
     $rowDateRange = explode(' - ', getRequest('row_date', ''));
     $rowStartDate = $rowDateRange[0];
     $rowEndDate = isset($rowDateRange[1]) ? $rowDateRange[1] : $rowStartDate;
     if ($rowStartDate) {
         $rowStartDate = dateConvDate2DBDate($rowStartDate);
     }
     if ($rowEndDate) {
         $rowEndDate = dateConvDate2DBDate($rowEndDate);
     }
     $paymentDateRange = explode(' - ', getRequest('payment_date', ''));
     $paymentStartDate = $paymentDateRange[0];
     $paymentEndDate = isset($paymentDateRange[1]) ? $paymentDateRange[1] : '';
     if ($paymentStartDate) {
         $paymentStartDate = dateConvDate2DBDate($paymentStartDate);
     }
     if ($paymentEndDate) {
         $paymentEndDate = dateConvDate2DBDate($paymentEndDate);
     }
     $arrParams = [];
     $strQuery = 'SELECT i.id, i.invoice_no, i.invoice_date, i.due_date, i.payment_date, i.ref_number, i.ref_number, c.company_name AS name, c.billing_address, ist.name as state, ist.invoice_unpaid as unpaid' . ($grouping == 'vat' ? ', ir.vat' : '') . ' FROM {prefix}invoice i' . ($grouping == 'vat' ? ' INNER JOIN {prefix}invoice_row ir ON ir.invoice_id = i.id' : '') . ' LEFT OUTER JOIN {prefix}company c ON c.id = i.company_id' . ' LEFT OUTER JOIN {prefix}invoice_state ist ON i.state_id = ist.id' . ' WHERE i.deleted=0';
     if ($startDate) {
         $strQuery .= ' AND i.invoice_date >= ?';
         $arrParams[] = $startDate;
     }
     if ($endDate) {
         $strQuery .= ' AND i.invoice_date <= ?';
         $arrParams[] = $endDate;
     }
     if ($paymentStartDate) {
         $strQuery .= ' AND i.payment_date >= ?';
         $arrParams[] = $paymentStartDate;
     }
     if ($paymentEndDate) {
         $strQuery .= ' AND i.payment_date <= ?';
         $arrParams[] = $paymentEndDate;
     }
     if ($intBaseId) {
         $strQuery .= ' AND i.base_id = ?';
         $arrParams[] = $intBaseId;
     }
     if ($intCompanyId) {
         $strQuery .= ' AND i.company_id = ?';
         $arrParams[] = $intCompanyId;
     }
     $strQuery2 = '';
     $strQuery3 = 'SELECT id, name ' . 'FROM {prefix}invoice_state WHERE deleted=0 ORDER BY order_no';
     $intRes = mysqli_query_check($strQuery3);
     while ($row = mysqli_fetch_assoc($intRes)) {
         $intStateId = $row['id'];
         $strStateName = $row['name'];
         $strTemp = "stateid_{$intStateId}";
         $tmpSelected = getRequest($strTemp, false);
         if ($tmpSelected) {
             $strQuery2 .= 'i.state_id = ? OR ';
             $arrParams[] = $intStateId;
         }
     }
     if ($strQuery2) {
         $strQuery2 = ' AND (' . substr($strQuery2, 0, -4) . ')';
     }
     $strQuery .= $strQuery2;
     switch ($grouping) {
         case 'state':
             $strQuery .= ' ORDER BY state_id, invoice_date, invoice_no';
             break;
         case 'client':
             $strQuery .= ' ORDER BY name, invoice_date, invoice_no';
             break;
         case 'vat':
             $strQuery .= ' GROUP BY i.id, ir.vat ORDER BY vat, invoice_date, invoice_no';
             break;
         default:
             $strQuery .= ' ORDER BY invoice_date, invoice_no';
     }
     $this->printHeader($format, $printFields, $startDate, $endDate);
     $intTotSum = 0;
     $intTotVAT = 0;
     $intTotSumVAT = 0;
     $intTotalToPay = 0;
     $currentGroup = false;
     $groupTotSum = 0;
     $groupTotVAT = 0;
     $groupTotSumVAT = 0;
     $groupTotalToPay = 0;
     $totalsPerVAT = [];
     $intRes = mysqli_param_query($strQuery, $arrParams);
     while ($row = mysqli_fetch_assoc($intRes)) {
         switch ($grouping) {
             case 'state':
                 $invoiceGroup = $row['state'];
                 break;
             case 'month':
                 $invoiceGroup = substr($row['invoice_date'], 4, 2);
                 break;
             case 'client':
                 $invoiceGroup = $row['name'];
                 break;
             case 'vat':
                 $invoiceGroup = $row['vat'];
                 break;
             default:
                 $invoiceGroup = false;
         }
         $rowParams = [$row['id']];
         $strQuery = 'SELECT ir.description, ir.pcs, ir.price, ir.discount, ir.row_date, ir.vat, ir.vat_included, ir.partial_payment ' . 'FROM {prefix}invoice_row ir ' . 'WHERE ir.invoice_id=? AND ir.deleted=0';
         if ($rowStartDate) {
             $strQuery .= ' AND ir.row_date >= ?';
             $rowParams[] = $rowStartDate;
         }
         if ($rowEndDate) {
             $strQuery .= ' AND ir.row_date <= ?';
             $rowParams[] = $rowEndDate;
         }
         if ($rowTypes != 'all') {
             if ($rowTypes == 'normal') {
                 $strQuery .= ' AND ir.reminder_row = 0';
             } else {
                 if ($rowTypes == 'reminder') {
                     $strQuery .= ' AND ir.reminder_row in (1, 2)';
                 }
             }
         }
         if ($grouping == 'vat') {
             if ($row['vat'] === null) {
                 $strQuery .= ' AND ir.vat IS NULL';
             } else {
                 $strQuery .= ' AND ir.vat = ?';
                 $rowParams[] = $row['vat'];
             }
         }
         $intRes2 = mysqli_param_query($strQuery, $rowParams);
         $intRowSum = 0;
         $intRowVAT = 0;
         $intRowSumVAT = 0;
         $rowPayments = 0;
         $rows = false;
         while ($row2 = mysqli_fetch_assoc($intRes2)) {
             $rows = true;
             if ($row2['partial_payment']) {
                 $rowPayments -= $row2['price'];
                 continue;
             }
             list($intSum, $intVAT, $intSumVAT) = calculateRowSum($row2['price'], $row2['pcs'], $row2['vat'], $row2['vat_included'], $row2['discount']);
             $intRowSum += $intSum;
             $intRowVAT += $intVAT;
             $intRowSumVAT += $intSumVAT;
             if (!isset($totalsPerVAT[$row2['vat']])) {
                 $totalsPerVAT[$row2['vat']] = ['sum' => $intSum, 'VAT' => $intVAT, 'sumVAT' => $intSumVAT];
             } else {
                 $totalsPerVAT[$row2['vat']]['sum'] += $intSum;
                 $totalsPerVAT[$row2['vat']]['VAT'] += $intVAT;
                 $totalsPerVAT[$row2['vat']]['sumVAT'] += $intSumVAT;
             }
         }
         if (!$rows) {
             continue;
         }
         $intTotSum += $intRowSum;
         $intTotVAT += $intRowVAT;
         $intTotSumVAT += $intRowSumVAT;
         if ($row['unpaid']) {
             $intTotalToPay += $intRowSumVAT - $rowPayments;
         } else {
             $rowPayments = $intRowSumVAT;
         }
         if ($grouping && $currentGroup !== false && $currentGroup != $invoiceGroup) {
             $this->printGroupSums($format, $printFields, $row, $groupTotSum, $groupTotVAT, $groupTotSumVAT, $groupTotalToPay, $grouping == 'vat' ? $GLOBALS['locVAT'] . ' ' . miscRound2Decim($currentGroup) : '');
             $groupTotSum = 0;
             $groupTotVAT = 0;
             $groupTotSumVAT = 0;
             $groupTotalToPay = 0;
         }
         $currentGroup = $invoiceGroup;
         $groupTotSum += $intRowSum;
         $groupTotVAT += $intRowVAT;
         $groupTotSumVAT += $intRowSumVAT;
         $groupTotalToPay += $intRowSumVAT - $rowPayments;
         $this->printRow($format, $printFields, $row, $intRowSum, $intRowVAT, $intRowSumVAT, $intRowSumVAT - $rowPayments);
     }
     if ($grouping) {
         $this->printGroupSums($format, $printFields, $row, $groupTotSum, $groupTotVAT, $groupTotSumVAT, $groupTotalToPay, $grouping == 'vat' ? $GLOBALS['locVAT'] . ' ' . miscRound2Decim($currentGroup) : '');
     }
     ksort($totalsPerVAT, SORT_NUMERIC);
     $this->printTotals($format, $printFields, $intTotSum, $intTotVAT, $intTotSumVAT, $intTotalToPay, $totalsPerVAT);
     $this->printFooter($format, $printFields);
 }