render('purchase_orders.index', ['company' => auth()->user()->company, 'settings' => auth()->user()->company->settings, 'sidebar' => $this->sidebarMenu()]); } /** * Show specific invoice. * * @param ShowInvoiceRequest $request * @param Invoice $invoice * * @return Factory|View */ public function show(ShowPurchaseOrderRequest $request, PurchaseOrder $purchase_order) { set_time_limit(0); $invitation = $purchase_order->invitations()->where('vendor_contact_id', auth()->guard('vendor')->user()->id)->first(); if ($invitation && auth()->guard('vendor') && ! session()->get('is_silent') && ! $invitation->viewed_date) { $invitation->markViewed(); event(new InvitationWasViewed($purchase_order, $invitation, $purchase_order->company, Ninja::eventVars())); event(new PurchaseOrderWasViewed($invitation, $invitation->company, Ninja::eventVars())); } $data = [ 'purchase_order' => $purchase_order, 'key' => $invitation ? $invitation->key : false, 'settings' => $purchase_order->company->settings, 'sidebar' => $this->sidebarMenu(), 'company' => $purchase_order->company, 'invitation' => $invitation ]; if ($request->query('mode') === 'fullscreen') { return render('purchase_orders.show-fullscreen', $data); } return $this->render('purchase_orders.show', $data); } private function sidebarMenu() :array { $enabled_modules = auth()->guard('vendor')->user()->company->enabled_modules; $data = []; // TODO: Enable dashboard once it's completed. // $this->settings->enable_client_portal_dashboard // $data[] = [ 'title' => ctrans('texts.dashboard'), 'url' => 'client.dashboard', 'icon' => 'activity']; if (self::MODULE_PURCHASE_ORDERS & $enabled_modules) { $data[] = ['title' => ctrans('texts.purchase_orders'), 'url' => 'vendor.purchase_orders.index', 'icon' => 'file-text']; } // $data[] = ['title' => ctrans('texts.documents'), 'url' => 'client.documents.index', 'icon' => 'download']; return $data; } public function bulk(ProcessPurchaseOrdersInBulkRequest $request) { $transformed_ids = $this->transformKeys($request->purchase_orders); if ($request->input('action') == 'download') { return $this->downloadInvoices((array) $transformed_ids); } elseif ($request->input('action') == 'accept') { return $this->acceptPurchaseOrder($request->all()); } return redirect() ->back() ->with('message', ctrans('texts.no_action_provided')); } public function acceptPurchaseOrder($data) { $purchase_orders = PurchaseOrder::query() ->whereIn('id', $this->transformKeys($data['purchase_orders'])) ->where('company_id', auth()->guard('vendor')->user()->vendor->company_id) ->where('is_deleted', 0) ->withTrashed(); $purchase_count_query = clone $purchase_orders; $purchase_orders->whereIn('status_id', [PurchaseOrder::STATUS_DRAFT, PurchaseOrder::STATUS_SENT]) ->cursor()->each(function ($purchase_order) { $purchase_order->service() ->markSent() ->applyNumber() ->setStatus(PurchaseOrder::STATUS_ACCEPTED) ->save(); if (request()->has('signature') && ! is_null(request()->signature) && ! empty(request()->signature)) { (new InjectSignature($purchase_order, request()->signature))->handle(); // InjectSignature::dispatch($purchase_order, request()->signature); } event(new PurchaseOrderWasAccepted($purchase_order, auth()->guard('vendor')->user(), $purchase_order->company, Ninja::eventVars())); }); if ($purchase_count_query->count() == 1) { $purchase_order = $purchase_count_query->first(); return redirect()->route('vendor.purchase_order.show', ['purchase_order' => $purchase_order->hashed_id]); } else { return redirect()->route('vendor.purchase_orders.index'); } } public function downloadInvoices($ids) { $purchase_orders = PurchaseOrder::whereIn('id', $ids) ->where('vendor_id', auth()->guard('vendor')->user()->vendor_id) ->withTrashed() ->get(); if (count($purchase_orders) == 0) { return back()->with(['message' => ctrans('texts.no_items_selected')]); } if (count($purchase_orders) == 1) { $purchase_order = $purchase_orders->first(); $file = $purchase_order->service()->getPurchaseOrderPdf(auth()->guard('vendor')->user()); return response()->streamDownload(function () use ($file) { echo Storage::get($file); }, basename($file), ['Content-Type' => 'application/pdf']); } return $this->buildZip($purchase_orders); } private function buildZip($purchase_orders) { // create new archive $zipFile = new \PhpZip\ZipFile(); try { foreach ($purchase_orders as $purchase_order) { //add it to the zip $zipFile->addFromString(basename($purchase_order->pdf_file_path()), file_get_contents($purchase_order->pdf_file_path(null, 'url', true))); } $filename = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.purchase_orders')).'.zip'; $filepath = sys_get_temp_dir().'/'.$filename; $zipFile->saveAsFile($filepath) // save the archive to a file ->close(); // close archive return response()->download($filepath, $filename)->deleteFileAfterSend(true); } catch (\PhpZip\Exception\ZipException $e) { // handle exception } finally { $zipFile->close(); } } }