/**
  * 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);
 }
Esempio n. 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);
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function update($id, Request $request)
 {
     $input = $request->only('edition_code', 'magazine_id', 'price', 'cover', 'main_article');
     try {
         $edition = Edition::with('magazine')->findOrFail($id);
     } catch (ModelNotFoundException $e) {
         $execMsg = "Cannot show magazine. Error on ID={$id}";
         return redirect('masterdata/edition/')->with('errMsg', $execMsg);
     }
     $edition->edition_code = $input['edition_code'];
     $edition->magazine_id = $input['magazine_id'];
     $edition->cover = $input['cover'];
     $edition->price = $input['price'];
     $edition->main_article = $input['main_article'];
     $edition->save();
     $msg = "Update successful!";
     return redirect('masterdata/edition')->with('message', $msg);
 }
Esempio n. 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]);
 }
 /**
  * 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);
 }