Пример #1
0
 /**
  * Display the specified resource.
  *
  * @param  int  $id
  * @return Response
  */
 public function show($id)
 {
     $returnItems = ReturnItem::with('edition', 'magazine', 'invoiceDetail.invoice', 'agent')->find($id);
     return view('circulation/return-view', ['returnItem' => $returnItems]);
 }
 /**
  * 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);
 }