/**
  * 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);
 }
Пример #2
0
 /**
  * Store a newly created resource in storage.
  *
  * Because I'm using separate request validator, all
  * actions done here will be limited to database input
  *
  * @param StoreReturnItemRequest $request
  * @return Response
  */
 public function store(StoreReturnItemRequest $request)
 {
     // Then, store necessary request
     $input = $request->only('agent_id', 'number', 'edition_id', 'total', 'date');
     $issueDate = strtotime($input['date']);
     $input['date'] = date('Y-m-d', $issueDate);
     //Validate if there are any Distribution plan
     $distRealDetID = array();
     try {
         // Validate for each edition returned
         foreach ($input['edition_id'] as $key => $edition_id) {
             $distRealDetID[] = $this->validateReturnItem($input, $edition_id, $input['total'][$key]);
             $this->validateReturnDate($input, $edition_id);
         }
     } catch (\Exception $e) {
         return redirect()->back()->withErrors($e->getMessage())->withInput();
     } catch (ModelNotFoundException $e) {
         // If we can't find data
         return redirect()->back()->withErrors('No deliveries were made!');
     }
     //Compose what to enter to database
     foreach ($input['edition_id'] as $key => $edition_id) {
         $ed = Edition::with('magazine')->find($edition_id);
         $toDB['dist_realization_det_id'] = $distRealDetID[$key];
         $toDB['agent_id'] = $input['agent_id'];
         $toDB['edition_id'] = $edition_id;
         $toDB['magazine_id'] = $ed->magazine->id;
         $toDB['date'] = $input['date'];
         $toDB['total'] = $input['total'][$key];
         $toDB['num'] = (int) $input['number'];
         $toDB['number'] = "{$edition_id}/" . str_pad($toDB['num'], 6, 0, STR_PAD_LEFT);
         $newReturn = ReturnItem::firstOrCreate($toDB);
     }
     $msg = "Done! New Return # : {$newReturn->number}";
     return redirect("circulation/return")->with('message', $msg);
 }
 /**
  * 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);
 }
Пример #4
0
 /**
  * Return form view to create report
  *
  * @return Response
  */
 public function getCreateDistRealization()
 {
     $magazines = Magazine::all();
     $editions = Edition::with('magazine')->orderBy('magazine_id')->get();
     return view('report/form-dist-realization', ['magazines' => $magazines, 'editions' => $editions]);
 }
Пример #5
0
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function destroy($id)
 {
     try {
         $edition = Edition::findOrFail($id);
         $edition->delete();
         //Set result message as flashdata
         $execMsg = "Deletion successful!";
     } catch (ModelNotFoundException $e) {
         //In case of failure on deletion/finding data, set errMsg
         $execMsg = "Cannot delete record. Data not found.";
         return redirect('masterdata/edition')->with('errMsg', $execMsg);
     }
     return redirect('masterdata/edition')->with('message', $execMsg);
 }
 /**
  * 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);
 }