<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Http\Facades\DB;
use Auth;
use App\Models\Order;
use App\Models\Order_item;
use App\Models\Shipping;
use App\Models\GeneralSettings;
use App\Models\EmailTemplates;
use Validator;
use Datatables;
use Mail;
use PDF;
class OrderController extends Controller
{
//call constructor
public function __construct(){
$this->middleware('auth:admin');
}
public function index(){
$data = array();
return view('admin.orders');
}
//Get orders data
public function ordersData($type){
$data = Order::all();
if($type == 'pending'){
$data = Order::where('status','=','pending')->get();
}
elseif($type == 'processing') {
$data = Order::where('status','=','processing')->get();
}
elseif($type == 'completed') {
$data = Order::where('status','=','completed')->get();
}
elseif($type == 'declined') {
$data = Order::where('status','=','declined')->get();
}
else{
$data = Order::orderBy('id','desc')->get();
}
return Datatables::of($data)
->addIndexColumn()
->addColumn('order_number', function($row){
$order_number = mb_strlen(strip_tags($row->order_number),'utf-8') > 50 ? mb_substr(strip_tags($row->order_number),0,50,'utf-8').'...' : strip_tags($row->order_number);
return '<a href="'.route('admin.order_show', $row->id).'"><b>#'.$order_number.'</b></a>';
})
->addColumn('created_at', function($row){
return $row->created_at;
})
->addColumn('billing', function($row){
return $row->customer_name.'<br>'.$row->customer_address.',<br>'.$row->customer_city.','.$row->customer_country.','.$row->customer_zip;
})
->addColumn('status', function($row){
$status = "";
if($row->status == "completed"){
$status .= '<span class="kt-badge kt-badge--success kt-badge--inline kt-badge--lg">Completed</span>';
}else if($row->status == "processing"){
$status .= '<span class="kt-badge kt-badge--warning kt-badge--inline kt-badge--lg">Processing</span>';
}else if($row->status == "pending"){
$status .= '<span class="kt-badge kt-badge--grey kt-badge--inline kt-badge--lg">Pending</span>';
}else if($row->status == "shipped"){
$status .= '<span class="kt-badge kt-badge--info kt-badge--inline kt-badge--lg">Shipped</span>';
}else if($row->status == "declined"){
$status .= '<span class="kt-badge kt-badge--danger kt-badge--inline kt-badge--lg">Declined</span>';
}else if($row->status == "confirmed"){
$status .= '<span class="kt-badge kt-badge--success kt-badge--inline kt-badge--lg">Confirmed</span>';
}else if($row->status == "refunded"){
$status .= '<span class="kt-badge kt-badge--grey kt-badge--inline kt-badge--lg">Refunded</span>';
}else{
}
return $status;
})
->addColumn('total_qty', function($row){
return $row->total_qty;
})
->addColumn('total_amount', function($row){
return $row->currency_sign.' '.number_format($row->total_amount, 2);
})
->addColumn('action', function($row){
return '<a class="btn btn-sm btn-warning" href="'.route('admin.order_show', $row->id).'"><i class="la la-eye"></i> View</a>';
})
->rawColumns(['index', 'order_number', 'customer_name', 'created_at', 'billing', 'total_amount', 'total_qty', 'status', 'action'])
->make(true);
}
//pending orders
public function pendingOrders()
{
return view('admin.orders_pending');
}
//processing orders
public function processingOrders()
{
return view('admin.orders_processing');
}
//completed orders
public function completedOrders()
{
return view('admin.orders_completed');
}
//cancelled orders
public function declinedOrders()
{
return view('admin.orders_declined');
}
//Get order details
public function orderShow($id){
if(!Order::where('id',$id)->exists())
{
return redirect()->route('admin.dashboard')->with('unsuccess',__('Sorry the page does not exist.'));
}
$order_details = Order::findOrFail($id);
$order_id = Order::select('order_number')->findOrFail($id)->order_number;
$order_items = Order_item::where('order_id', $order_id)->get();
//print_r($order_items);
return view('admin.order_details',compact('order_details','order_items'));
}
//Get order invoice details
public function invoiceShow($id){
if(!Order::where('id',$id)->exists())
{
return redirect()->route('admin.dashboard')->with('unsuccess',__('Sorry the page does not exist.'));
}
$order_details = Order::findOrFail($id);
$order_id = Order::select('order_number')->findOrFail($id)->order_number;
$order_items = Order_item::where('order_id', $order_id)->get();
$gs = GeneralSettings::findOrFail(1);
return view('admin.order_invoice',compact('order_details','order_items', 'gs'));
}
//Get order invoice details
public function invoicePrint($id){
if(!Order::where('id',$id)->exists())
{
return redirect()->route('admin.dashboard')->with('unsuccess',__('Sorry the page does not exist.'));
}
$order_details = Order::findOrFail($id);
$order_id = Order::select('order_number')->findOrFail($id)->order_number;
$order_items = Order_item::where('order_id', $order_id)->get();
$gs = GeneralSettings::findOrFail(1);
return view('admin.order_invoice_print',compact('order_details','order_items', 'gs'));
}
//update order status
public function updateOrder(Request $request)
{
//define rule
$rules = [
'status' => "required",
'payment_status' => "required",
];
//run validation
$validator = Validator::make($request->all(), $rules);
if($validator->fails()){
return response()->json(array(
"errors" => $validator->getMessageBag()->toArray(),
"status" => false,
"message" => "There were error. Please try again!"
));
}
$mail_data["order_status"] = $request->status;
$mail_data["payment_status"] = $request->payment_status;
/*get email template*/
/*email template enabled*/
if(EmailTemplates::where('email_type','order_status')->where('status', 1)->exists())
{
$email_template = EmailTemplates::where('email_type','order_status')->first();
$mail_data["title"] = $email_template->email_subject;
$email_body = $email_template->email_body;
}else{
$mail_data["title"] = 'Order Status Changed';
$email_body = 'You order is {order_status}.<br><b>Order Number:</b> {order_number}<br><b>Order Date:</b> {order_date}<br><b>Payment Status:</b> {payment_status}<br><b>Order Status:</b> {order_status}';
}
/*get order details*/
$order_details = Order::findOrFail($request->id);
$order_id = Order::select('order_number')->findOrFail($request->id)->order_number;
$order_items = Order_item::where('order_id', $order_id)->get();
$mail_data["email"] = $order_details->customer_email;
//$mail_data["email"] = 'daljit@htlogics.com';
/* Replace Variable in Template */
$email_body = str_replace('{user}', $order_details->customer_name, $email_body);
$email_body = str_replace('{order_status}', $request->status, $email_body);
$email_body = str_replace('{payment_status}', $request->payment_status, $email_body);
$email_body = str_replace('{order_number}', $order_details->order_number, $email_body);
$email_body = str_replace('{order_date}', $order_details->created_at, $email_body);
if($order_details->shipping_address){
$email_body = str_replace('{shipping_address}', $order_details->shipping_address.', '.$order_details->shipping_city.', '.$order_details->shipping_country.', '.$order_details->shipping_zip, $email_body);
}else{
$email_body = str_replace('{shipping_address}', $order_details->customer_address.', '.$order_details->customer_city.', '.$order_details->customer_country.', '.$order_details->customer_zip, $email_body);
}
if($order_details->shipping_phone){
$email_body = str_replace('{mobile}', $order_details->shipping_phone, $email_body);
}else{
$email_body = str_replace('{mobile}', $order_details->customer_phone, $email_body);
}
//get items code start
$subtotal = 0;
$tax = 0;
$items = '<h3 class="kt-card-heading" style="background:#eee;padding:10px;margin-top:30px;margin-bottom: 0px;">Products Ordered</h3>
<div class="table-responsive-sm">
<table class="table table-borderless invoice-products" style="width: 100%;line-height: 40px;">
<tbody>
<tr>
<th style="text-align: left;line-height: 40px;" width="40%">Name</th>
<th width="20%" style="text-align: left;line-height: 40px;">Qty</th>
<th width="20%" style="text-align: left;line-height: 40px;">Price</th>
<th width="20%" style="text-align: right;line-height: 40px;">Total</th>
</tr>';
if(count($order_items)>0){
foreach($order_items as $order_item){
$items .= '<tr>
<td style="line-height: 20px;">'.$order_item['item'].'</th>
<td style="line-height: 20px;" width="20%"><b>Qty</b>:'. $order_item['qty'].'</th>
<td style="line-height: 20px;" width="20%">'. $order_details->currency_sign.' '.$order_item['price'].'</th>';
$total_product_cost = $order_item['price']*$order_item['qty'];
$items .= '<td width="20%" style="text-align: right;line-height: 20px;">'.$order_details->currency_sign.' '.$total_product_cost.'</td>
</tr>';
}
}
$items .='</tbody><tfoot style="text-align: right;"><tr>
<th colspan="3">Subtotal</th>
<td>'.$order_details->currency_sign.' '.round($subtotal, 2).'</td>
</tr>';
if($order_details->shipping_cost != 0){
$price = round($order_details->shipping_cost,2);
if(Shipping::where('cost','=',$price)->count() > 0){
$items .= '<tr>
<th colspan="3">Shipping Charge'.Shipping::where('cost','=',$price)->first()->method_name.'</th>
<td>'.$order_details->currency_sign.' '.round($order_details->shipping_cost , 2).'</td>
</tr>';
}
}
if($order_details->packing_cost != 0):
$items .= '<tr>
<th colspan="3">'.$order_details->package_name.'</th>
<td>'.$order_details->currency_sign.' '.round($order_details->packing_cost , 2).'</td>
</tr>';
endif;
if($order_details->tax != 0):
$items .= '<tr>
<th colspan="3">TAX('.$order_details->currency_sign.')</th>';
$tax = ($subtotal / 100) * $order_details->tax;
$items .= '<td>'.round($tax, 2).'</td>
</tr>';
endif;
if($order_details->coupon_discount != null):
$items .= '<tr>
<th colspan="3">Coupon Discount</th>
<td>-'. $order_details->currency_sign.' '.round($order_details->coupon_discount, 2).'</td>
</tr>';
endif;
$items .= '<tr>
<td colspan="2"></td>
<th>Total</th>
<td>'.$order_details->currency_sign.' '.round($order_details->pay_amount, 2).'
</td>
</tr>
</tfoot>
</table>
</div>';
$email_body = str_replace('{items}', $items, $email_body);
/*pdf data start*/
$order_details = Order::findOrFail($request->id);
$order_id = Order::select('order_number')->findOrFail($request->id)->order_number;
$order_items = Order_item::where('order_id', $order_id)->get();
$gs = GeneralSettings::findOrFail(1);
$opciones_ssl=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$img_path = 'public/images/logo/' . $gs->email_logo;
$extencion = pathinfo($img_path, PATHINFO_EXTENSION);
$data = file_get_contents($img_path, false, stream_context_create($opciones_ssl));
$img_base_64 = base64_encode($data);
$path_img = 'data:image/' . $extencion . ';base64,' . $img_base_64;
$mail_data['pdf'] = PDF::loadView('admin.invoice_pdf',compact('order_details','order_items', 'gs', 'path_img'));
$mail_data['order_id'] = $order_id;
/*pdf data end*/
$mail_message = "";
/*send email*/
if($request->send_mail){
if($request->status == 'completed'){
Mail::send('mail.email_template', ['email_template' => $email_body, 'mail_data' => $mail_data], function($message)use($mail_data){
$message->to([$mail_data["email"]])->subject($mail_data["title"])->attachData($mail_data["pdf"]->output(), 'order_invoice_'.$mail_data["order_id"].'.pdf');
});
$mail_message = "Order mail sent with invoice attached.";
}else{
Mail::send('mail.email_template', ['email_template' => $email_body, 'mail_data' => $mail_data], function($message)use($mail_data){
$message->to([$mail_data["email"]])->subject($mail_data["title"]);
});
$mail_message = "Order mail sent.";
}
}
$data = Order::find($request->id);
$data->status = $request->status;
$data->payment_status = $request->payment_status;
if($data->save()){
return response()->json(array(
"status" => true,
"redirect" => '',
"message" => "Order status has been updated successfully. ".$mail_message
));
}
/*--- if unsuccessful, then show error ---*/
return response()->json(array(
"errors" => $validator->getMessageBag()->toArray(),
"status" => false,
"message" => "<div class='alert alert-danger'>There were error. Please try again!</div>"
));
}
function invoice_pdf($id){
if(!Order::where('id',$id)->exists())
{
return redirect()->route('admin.dashboard')->with('unsuccess',__('Sorry the page does not exist.'));
}
$order_details = Order::findOrFail($id);
$order_id = Order::select('order_number')->findOrFail($id)->order_number;
$order_items = Order_item::where('order_id', $order_id)->get();
$gs = GeneralSettings::findOrFail(1);
$opciones_ssl=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$img_path = 'public/images/logo/' . $gs->email_logo;
$extencion = pathinfo($img_path, PATHINFO_EXTENSION);
$data = file_get_contents($img_path, false, stream_context_create($opciones_ssl));
$img_base_64 = base64_encode($data);
$path_img = 'data:image/' . $extencion . ';base64,' . $img_base_64;
$pdf = PDF::loadView('admin.invoice_pdf',compact('order_details','order_items', 'gs', 'path_img'));
//$content = $pdf->download()->getOriginalContent();
//return Storage::put('public/invoice'.$id.'.pdf',$content) ;
return $pdf->download('order_invoice_'.$order_id.'.pdf');
}
/*function examplepdf(){
$data = array();
$data['name'] = "hello";
$pdf = PDF::loadView('admin.invoice_pdf',$data);
//$content = $pdf->download()->getOriginalContent();
//return Storage::put('public/invoice'.$id.'.pdf',$content) ;
return $pdf->donwload('order_invoice.pdf');
}*/
}
|