App\Http\Controllers\ClientPortalController::getInvoiceZipDocuments PHP Method

getInvoiceZipDocuments() protected method

protected getInvoiceZipDocuments ( $invoice, &$size )
    protected function getInvoiceZipDocuments($invoice, &$size = 0)
    {
        $documents = $invoice->documents;
        foreach ($invoice->expenses as $expense) {
            $documents = $documents->merge($expense->documents);
        }
        $documents = $documents->sortBy('size');
        $size = 0;
        $maxSize = MAX_ZIP_DOCUMENTS_SIZE * 1000;
        $toZip = [];
        foreach ($documents as $document) {
            if ($size + $document->size > $maxSize) {
                break;
            }
            if (!empty($toZip[$document->name])) {
                // This name is taken
                if ($toZip[$document->name]->hash != $document->hash) {
                    // 2 different files with the same name
                    $nameInfo = pathinfo($document->name);
                    for ($i = 1;; $i++) {
                        $name = $nameInfo['filename'] . ' (' . $i . ').' . $nameInfo['extension'];
                        if (empty($toZip[$name])) {
                            $toZip[$name] = $document;
                            $size += $document->size;
                            break;
                        } else {
                            if ($toZip[$name]->hash == $document->hash) {
                                // We're not adding this after all
                                break;
                            }
                        }
                    }
                }
            } else {
                $toZip[$document->name] = $document;
                $size += $document->size;
            }
        }
        return $toZip;
    }