Ejemplo n.º 1
0
 /**
  * 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]);
 }
 /**
  * Store a newly created resource in storage.
  *
  * @param StoreInvoiceRequest request
  * @return Response
  */
 public function store(StoreInvoiceRequest $request)
 {
     $input = $request->only('agent_id', 'edition_id', 'issue_date');
     // Validate requests before invoice was created. Such as:
     //   - Agent has not yet billed
     //   - Deliveries exist
     //
     //
     //get distribution plan executed for this agent and particular
     //  magazine for this date
     $distReal = DistReal::with('details.agent', 'edition.magazine')->where('edition_id', '=', $input['edition_id'])->first();
     // only get distRealDet where agent_id matches
     foreach ($distReal->details as $det) {
         if ((int) $det->agent_id == (int) $input['agent_id']) {
             $distRealDet = $det;
             break;
         }
     }
     //get necessary delivery for this month
     $deliveries = Delivery::with('distRealizationDet.distributionRealization.edition')->whereRaw('dist_realization_det_id = ? AND in_invoice_quota = 0', [$distRealDet->id])->get();
     //get returns for this month and 3 months ago with the same magazine
     $ed = Edition::with('magazine')->find((int) $input['edition_id']);
     //generate necessary data to go to invoice
     //generate data to create invoice
     $inv['num'] = $this->createInvNumber();
     $inv['number'] = "{$distReal->edition->magazine->id}/" . str_pad($inv['num'], 5, 0, STR_PAD_LEFT);
     $inv['agent_id'] = $input['agent_id'];
     $issueDate = strtotime($input['issue_date']);
     $inv['issue_date'] = date('Y-m-d', $issueDate);
     //MySQL format
     $dueDate = strtotime('last friday of this month');
     $inv['due_date'] = date('Y-m-d', $dueDate);
     $inv['edition_id'] = (int) $input['edition_id'];
     // Invoice OK
     $newInv = InvoiceQuota::firstOrCreate($inv);
     // generate invoice delivery details
     foreach ($deliveries as $delv) {
         $toDB['delivery_id'] = $delv->id;
         $toDB['invoice_quota_id'] = $newInv->id;
         // Calculating price here
         // Current edition price, multiplied by quota amount
         $toDB['total'] = $delv->distRealizationDet->distributionRealization->edition->price * $delv->quota;
         $toDB['edition_id'] = $input['edition_id'];
         $newInvDelivery = InvDelivery::firstOrCreate($toDB);
         //Update in_invoice to true
         $delv->in_invoice_quota = 1;
         $delv->save();
     }
     $msg = "Done! New Invoice# : {$newInv->number}";
     return redirect("invoice/invoice")->with('message', $msg);
 }
Ejemplo n.º 3
0
 /**
  * Return date should be 3 months due.
  *
  * Return date is based on `distributionPlan`.`publish_date`.
  * All distributionPlan should have 1-to-1 relationship
  * with edition. Therefore, if it not exist, return will fail.
  *
  * @Return null
  */
 private function validateReturnDate($input, $edition_id)
 {
     $distReal = DistReal::where('edition_id', '=', $edition_id)->firstOrFail();
     $distDate = new \DateTime($distReal->publish_date);
     $distDate->add(new \DateInterval('P3M'));
     $retrDate = new \DateTime($input['date']);
     // Then, compare dates
     if ($retrDate > $distDate) {
         throw new \Exception('Item return has passed 3 months overdue!');
     }
 }
 /**
  * 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]);
 }
 /**
  * Store a newly created resource in storage.
  *
  * @param Request request
  * @return Response
  */
 public function store(StoreInvoiceRequest $request)
 {
     $input = $request->only('agent_id', 'edition_id', 'issue_date');
     // Validate requests before invoice was created. Such as:
     //   - Agent has not yet billed
     //   - Deliveries exist
     //
     //
     //get distribution plan executed for this agent and particular
     //  magazine for this date
     $distReal = DistReal::with('details.agent', 'edition.magazine')->where('edition_id', '=', $input['edition_id'])->first();
     // only get distRealDet where agent_id matches
     foreach ($distReal->details as $det) {
         if ((int) $det->agent_id == (int) $input['agent_id']) {
             $distRealDet = $det;
             break;
         }
     }
     //get necessary delivery for this month
     $deliveries = Delivery::with('distRealizationDet.distributionRealization.edition')->whereRaw('dist_realization_det_id = ? AND in_invoice_consign = 0', [$distRealDet->id])->get();
     //get returns for this month and 3 months ago with the same magazine
     $ed = Edition::with('magazine')->find((int) $input['edition_id']);
     // just use scopeInvoice (see: http://laravel.com/docs/5.0/eloquent#query-scopes)
     $returns = ReturnItem::with('distRealizationDet.distributionRealization.edition')->Invoice((int) $input['agent_id'], (int) $ed->magazine->id)->get();
     //generate necessary data to go to invoice
     //generate data to create invoice
     $inv['num'] = $this->createInvNumber();
     $inv['number'] = "{$distReal->edition->magazine->id}/" . str_pad($inv['num'], 5, 0, STR_PAD_LEFT);
     $inv['agent_id'] = $input['agent_id'];
     $issueDate = strtotime($input['issue_date']);
     $inv['issue_date'] = date('Y-m-d', $issueDate);
     //MySQL format
     $dueDate = strtotime('last friday of this month');
     $inv['due_date'] = date('Y-m-d', $dueDate);
     $inv['edition_id'] = (int) $input['edition_id'];
     // Invoice OK
     $newInv = InvoiceConsign::firstOrCreate($inv);
     // generate invoice delivery details
     foreach ($deliveries as $delv) {
         $toDB['delivery_id'] = $delv->id;
         $toDB['invoice_consign_id'] = $newInv->id;
         // Calculating price here
         // Current delivery price, multiplied by consign
         $toDB['total'] = $delv->distRealizationDet->distributionRealization->edition->price * $delv->consigned;
         $toDB['edition_id'] = $input['edition_id'];
         $newInvDelivery = InvDelivery::firstOrCreate($toDB);
         //Update in_invoice to true
         $delv->in_invoice_consign = 1;
         $delv->save();
     }
     // Because all details will have the same edition
     $thisMonthPrice = $delv->distRealizationDet->distributionRealization->edition->price;
     unset($toDB);
     foreach ($returns as $ret) {
         $toDB['return_item_id'] = $ret->id;
         $toDB['invoice_consign_id'] = $newInv->id;
         // Calculating price here. We will calculate discount based on:
         //   thisMonthPrice * total return - returnMonthPrice * total return
         $returnMonthPrice = $ret->distRealizationDet->distributionRealization->edition->price;
         $toDB['discount'] = $ret->total * ($thisMonthPrice - $returnMonthPrice);
         // of course, total will  use thisMonthPrice, even though returned
         // item may have different price.
         $toDB['total'] = $thisMonthPrice * $ret->total;
         $toDB['edition_id'] = $input['edition_id'];
         $newInvDelivery = InvReturn::firstOrCreate($toDB);
         //Update in_invoice
         $ret->in_invoice = 1;
         $ret->save();
     }
     $msg = "Done! New Invoice# : {$newInv->number}";
     return redirect("invoice/invoice")->with('message', $msg);
 }