public function quote_items() { $items = []; foreach ($this->quantities() as $qty) { $items[] = QuoteItem::where('quote_id', '=', $this->id)->where('quantity', '=', $qty)->first(); } return $items; //return $this->hasMany('App\QuoteItem'); }
/** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update($id) { // Separate out the incoming data $all = Input::all(); $qri_input = array_where($all, function ($key, $value) { return starts_with($key, 'qri'); }); $qri_id_storing = array(); foreach ($qri_input['qri_id'] as $item) { array_push($qri_id_storing, $item); } $qr_input = array_where($all, function ($key, $value) { return !starts_with($key, 'qri') && $key != '_method' && $key != 'customer'; }); // invert qri_input array $input = $qri_input; $keys = array_keys($input); $first_key = $keys[0]; $count = count($input[$first_key]); $output = []; foreach (range(0, $count - 1) as $i) { foreach ($keys as $key) { $output[$i][$key] = $input[$key][$i]; } } $qri_input = $output; // remove qri_ prefix from array keys $prefix = "qri_"; $output = []; foreach ($qri_input as $qri) { foreach (array_keys($qri) as $k) { if (starts_with($k, $prefix)) { $nk = substr($k, strlen($prefix)); $qri[$nk] = $qri[$k]; unset($qri[$k]); } } $output[] = $qri; } $qri_input = $output; // process inverted qri_input array foreach ($qri_input as $qri) { $qri_id = array_pull($qri, 'id'); $qri["description"] = $qr_input["title"]; if ($qri_id == "") { // create new qri // first check if anything has been entered if ($qri["quantity"] != "" && $qri["quantity"] > 0) { QuoteRequestItem::create($qri); } } else { // update old qri $item = QuoteRequestItem::find($qri_id); if ($qri["quantity"] == "" || is_numeric($qri["quantity"])) { $item->update($qri); } } } // Get stored quote lines for delete checking $quotes_lines = QuoteRequestItem::where('quote_request_id', $id)->get(); $quote_request = QuoteRequest::find($id); $quotes = $quote_request->quotes; // At first delete quote items related to chosen item foreach ($quotes as $quote) { foreach ($quotes_lines as $quotes_line) { if ($quotes_line->quantity == 0 || $quotes_line->quantity == '') { $quote_items = QuoteItem::where('quote_id', $quote->id)->where('qri_id', $quotes_line->id); $quote_items->delete(); } } } // Delete chosen items foreach ($quotes_lines as $quotes_line) { if ($quotes_line->quantity == 0 || $quotes_line->quantity == '') { $quote_line_delete = QuoteRequestItem::find($quotes_line->id); $quote_line_delete->delete(); } } // process other input (quote request data) $q = QuoteRequest::find($id); $q->update($qr_input); // Update status $q->status = 1; $q->save(); // Storing artwork image if (Input::hasFile('artwork')) { if (Input::file('artwork')->isValid()) { $extension = Input::file('artwork')->getClientOriginalExtension(); $file_name = $id . '.' . $extension; $destination_path = 'uploads/artworks'; Input::file('artwork')->move($destination_path, $file_name); // Make a thumbnail picture $thumbnail = Image::make($destination_path . '/' . $file_name); $thumbnail->resize(55, null, function ($constraint) { $constraint->aspectRatio(); }); $thumbnail->save('uploads/thumbnails/' . $file_name); $q->artwork_image = $file_name; $q->save(); } } // delete Quote PDF if exists $path = 'quotes/' . $id . '.pdf'; if (file_exists($path)) { unlink($path); } // Reset chosen supplier for quotes $q->quote_id = 0; $q->save(); return redirect()->route('quote_requests.edit', $id)->with('message', 'Quote Request has been Updated'); }
/** * Show the history page for particular customers. * * @param int $id * @return Response */ public function history($id) { $message = Session::get('message'); $customer = Customer::find($id); $quotes = $customer->quotes; $array = array(); $i = 0; foreach ($quotes as $quote) { $array[$i]['quote_number'] = $quote->id; $array[$i]['title'] = $quote->title; $array[$i]['description'] = $quote->summary; $array[$i]['artwork_image'] = $quote->artwork_image; $array[$i]['request_date'] = $quote->request_date; $array[$i]['expiry_date'] = $quote->expiry_date; // Quotes if (isset($quote->get_quote->id)) { $array[$i]['supplier_id'] = $quote->get_quote->supplier->id; $array[$i]['supplier_name'] = $quote->get_quote->supplier->supplier_name; } else { $array[$i]['supplier_id'] = ''; $array[$i]['supplier_name'] = ''; } //Jobs if (isset($quote->job->id) && $quote->quote_id != 0) { $array[$i]['job_number'] = $quote->id; $array[$i]['job_sell'] = $quote->job->job_item->total; $array[$i]['quantity'] = $quote->job->job_item->quantity; $array[$i]['request_date'] = $quote->job->updated_at->format('d/m/Y'); $query = QuoteItem::where('qri_id', $quote->job->quote_request_items_id)->where('quote_id', $quote->get_quote->id)->first(); $array[$i]['job_cost'] = $query->total_buy_cost; } else { $array[$i]['job_number'] = ''; $array[$i]['job_cost'] = ''; $array[$i]['job_sell'] = ''; $array[$i]['quantity'] = ''; } //TODO: add status value after Workflow module done $array[$i]['status'] = 'invoiced'; $i++; } return view('customers.history', compact('customer', 'array', 'message')); }
public function post_evaluate(\Illuminate\Http\Request $request, $qr_id) { // Validate form $this->validate($request, ['quote_id' => 'required'], $messages = array('quote_id.required' => 'You should choose a Supplier for creating a Quote')); $quote_request = QuoteRequest::find($qr_id); $quantities = $quote_request->first_quote()->quantities(); $input = request::all(); $quote_id = $input['quote_id']; $quote = Quote::find($quote_id); //echo "<pre>"; //echo "Selecting Quote ID $quote_id for Quote Request $qr_id\n"; foreach ($quote_request->qris as $qri) { $qty = $qri["quantity"]; $qi = QuoteItem::where("quantity", "=", $qty)->where("quote_id", "=", $quote_id)->first(); //print("QRI: " . $qri["quantity"] . ": " . $qri["price"]."\n"); if ($qi == null) { //print("Could not find quote item for Quantity $qty\n"); $qri["price"] = 0; $qri["gst"] = 0; $qri["total"] = 0; $qri["unit_price"] = 0; $qri->save(); } else { $qri["price"] = $qi["total_net"]; $qri["gst"] = $qri["price"] * 0.1; $qri["total"] = $qri["price"] + $qri["gst"]; $qri["unit_price"] = $qri["total"] / $qri["quantity"]; $qri->save(); } } $quote_request = QuoteRequest::find($qr_id); $quote_request->quote_id = $input['quote_id']; $quote_request->save(); // delete Quote PDF if exists $path = 'quotes/' . $qr_id . '.pdf'; if (file_exists($path)) { unlink($path); } return redirect('evaluate/' . $qr_id); }