/**
  * 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]);
 }
 /** To be removed when I've added a fkey constraint cascade onDelete **/
 private function delete_record(DistPlan $distPlan)
 {
     // Get all childrens
     foreach ($distPlan->details as $det) {
         $det->delete();
     }
     // Then, remove distribution plan
     $distPlan->delete();
 }
 /**
  * Show comparisons between plan and realization
  *
  * @param int $distRealizationID
  * @param int $distPlanID
  * @return Response
  */
 public function compare($distRealizationID, $distPlanID)
 {
     $dist_real = DistRealize::with('details.agent')->find($distRealizationID);
     // Check if distPlanID match with $distRealizationID
     if ($dist_real->distribution_plan_id != $distPlanID) {
         return redirect()->back()->with('errMsg', 'Mismatched IDs!');
     }
     $dist_plan = DistPlan::with('details.agent')->find($distPlanID);
     // Try to combine both of them based on agent_id
     $agent_plan = $dist_plan->details->keyBy('agent_id');
     $agent_real = $dist_real->details->keyBy('agent_id');
     return view('circulation/distribution-realization-compare', ['agent_plan' => $agent_plan, 'agent_real' => $agent_real]);
 }