/** * Process form request to create report * * This will generate report for distribution * realization. * * @param Request $request * @return Response */ public function postDistRealization(Request $request) { $this->validate($request, ['edition_id' => 'required|numeric']); /** // Prepare data for report $DistPlan = DistPlan::with('details.agent') ->where('edition_id', '=', $request->edition_id) ->first(); $DistReal = DistRealization::with('details.agent') ->where('edition_id', '=', $request->edition_id) ->first(); */ $DistPlan = DistPlan::where('edition_id', '=', $request->edition_id)->first(); $DistReal = DistRealization::with('edition.magazine')->where('edition_id', '=', $request->edition_id)->first(); // if empty, don't render any result if (!$DistPlan or !$DistReal) { $msg = "Tidak ditemukan perencanaan atau realisasi"; return redirect('report/create-dist-realization')->with('errMsg', $msg); } $DistPlanDet = DistPlanDet::with('agent.agent_category')->where('distribution_plan_id', '=', $DistPlan->id)->get(); $DistRealDet = DistRealDet::with('agent.agent_category')->where('distribution_realization_id', '=', $DistReal->id)->get(); // Change structure so that $DistPlanDet // and $DistRealDet are keyed by agent_id $agent_DistPlanDet = $DistPlanDet->keyBy('agent_id'); $agent_DistRealDet = $DistRealDet->keyBy('agent_id'); // Get agent details $keys = $agent_DistPlanDet->keys(); $agents = Agent::with('agent_category')->whereIn('id', $keys->all())->orderBy('agent_category_id', 'asc')->get(); // Now, return agents aggregat return view('report/preview-dist-realization', ['distReal' => $DistReal, 'distPlanDet' => $agent_DistPlanDet, 'distRealDet' => $agent_DistRealDet, 'agents' => $agents]); }
/** * Process store request based on previous distribution plan * * @param Request $request * @return Response */ public function createFromPrev(Request $request) { $this->validate($request, ['dist_plan_id' => 'required|numeric', 'magazine_id' => 'required|numeric', 'edition_code' => 'required']); // Go through validation steps try { // Check if edition code exist or distribution plan for previous edition exist $edition = Edition::where('edition_code', '=', $request->input('edition_code'))->where('magazine_id', '=', $request->input('magazine_id'))->firstOrFail(); $distPlan = DistPlan::with('details')->findOrFail($request->input('dist_plan_id')); // Check if distribution plan is already exist or not $existing = DistPlan::where('edition_id', '=', $edition->id)->first(); if ($existing) { throw new \Exception("Distribution plan has already been made!"); } } catch (ModelNotFoundException $e) { $execMsg = "Distribution plan is not found! / Edition Code mismatch!"; return redirect('circulation/distribution-plan')->with('errMsg', $execMsg); } catch (\Exception $e) { return redirect('circulation/distribution-plan')->with('errMsg', $e->getMessage()); } // Create new distribution. Unfilled value will be // set to default. Date will be set to today (Please // check php timezone for possible date mismatch) $distPlanNew = new DistPlan(); $distPlanNew->edition_id = $edition->id; $distPlanNew->print = $distPlan->print; $distPlanNew->gratis = $distPlan->gratis; $distPlanNew->distributed = $distPlan->distributed; $distPlanNew->stock = $distPlan->stock; $distPlanNew->publish_date = date('Y-m-d'); $distPlanNew->print_number = 1; $distPlanNew->save(); // generate exact data for dist_realize_details from dist_plan_details foreach ($distPlan->details as $distPlanDet) { $distPlanDetNew = new DistPlanDet(); $distPlanDetNew->distribution_plan_id = $distPlanNew->id; $distPlanDetNew->agent_id = $distPlanDet->agent_id; $distPlanDetNew->consigned = $distPlanDet->consigned; $distPlanDetNew->gratis = $distPlanDet->gratis; $distPlanDetNew->quota = $distPlanDet->quota; $distPlanDetNew->save(); } $msg = "Done! New distribution plan with id={$distPlanNew->id}"; return redirect('circulation/distribution-plan')->with('message', $msg); }
/** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($distPlanID, $detailsID) { try { $detail = DistPlanDet::findOrFail($detailsID); $detail->delete(); $execMsg = "Delete successful!"; } catch (ModelNotFoundException $e) { $execMsg = "Cannot delete record. Data not found."; return redirect("circulation/distribution-plan/{$distPlanID}")->with('errMsg', $execMsg); } return redirect("circulation/distribution-plan/{$distPlanID}")->with('message', $execMsg); }