/
home
/
old_henza
/
server
/
app
/
Http
/
Controllers
/
Customer
/
File Upload :
llllll
Current File: /home/old_henza/server/app/Http/Controllers/Customer/OrderController.php
<?php namespace App\Http\Controllers\Customer; use App\DB\Te\TableEngine; use App\Http\Controllers\Controller; use App\Models\ConfigStatic; use App\Models\Major\Promotion; use App\Models\Order; use App\Models\OrderActivity; use App\Models\Product; use App\Models\ProductVariant; use App\Models\PromotionProduct; use App\Models\SingleCustomer\PromotionUsed; use App\Models\SingleCustomerOrderItems; use App\Models\SingleCustomerOrders; use App\Models\Sms; use App\Models\TransactionOrder; use App\Models\WebServiceGold; use Carbon\Carbon; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Validator; use Rooyesh\Wallet\interfaces\TransactionInterface; use Rooyesh\Wallet\interfaces\WalletInterface; use Rooyesh\Wallet\models\Transaction; use Rooyesh\Wallet\models\Wallet; class OrderController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(Request $request) { $records = SingleCustomerOrders::where('user_id_id',auth()->user()->id)->with(['items'])->orderBy('id','desc'); TableEngine::create($records, $request->all()) ->initSearch([]) ->select([ '*', SingleCustomerOrders::$SELECT_UAJ, SingleCustomerOrders::$SELECT_CAJ ])->export($records); return response()->json($records); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { // } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\jsonResponse */ public function store(Request $request) { $data = $request->all(); $count = []; $validator = Validator::make($request->all(), [ 'order_items' => 'required', 'shipping_address' => 'required' ]); if ($validator->fails()) { return response()->json($validator->errors(),422); } $items = $request->order_items; $customer = Auth::user(); //ثبت در جدول order $order = $this->createSingleCustomerOrders($data, auth()->id()); foreach ($items as $item) { $product = Product::findOrFail($item['product_id']); if (!in_array($item['product_id'],$count)){ $product->increment('count_order'); $count[] = $item['product_id']; } //ثبت orderItem $order_item = $this->createSingleCustomerOrderItems($product->title,$item,$order->id,auth()->id()); $variant = ProductVariant::find($item['variant_id']); $variant->update(['qty'=>$variant->qty - $item['qty']]); Sms::Send('09124871664','هنزا(وبسایت)'."\n".'تعداد '.$item['qty'].' عدد توسط سفارش '.$order->id.'از محصول با کد '.$product->product_code.' کم شد.'); } if($order->use_wallet){ $wallet = Wallet::where('owner',auth()->id())->first(); $gold = $wallet->gold; $price_gold = $gold * $order->gold_price_original; $all = $price_gold + $wallet->credit; $wallet_interface = new WalletInterface(auth()->id(), 1); if ($all <= $order->price){ if ($wallet->gold >0){ $this->createTransactionInterface($wallet_interface,$wallet->gold,1,false,$order); } if($wallet->credit > 0){ $this->createTransactionInterface($wallet_interface,$wallet->credit,2,false,$order); } }else{ if ($wallet->gold >0 ){ if ($wallet->gold <= $order->total_weight){ $this->createTransactionInterface($wallet_interface,$wallet->gold,1,false,$order); if ($gold < $order->total_weight){ $dis = $all - $order->price; $this->createTransactionInterface($wallet_interface,$dis,2,false,$order); } }else{ $this->createTransactionInterface($wallet_interface,$order->total_weight,1,false,$order); } } } $tr = Transaction::where('order_id',$order->id)->where('customer_id',auth()->id())->where('value','<',0)->get(); $amount = 0; if ( count($tr) >0){ foreach ($tr as $item){ if ($item->transaction_type == 1){ $amount = abs($item->value) * $order->gold_price_original; } if ($item->transaction_type == 2){ $amount = $amount + abs($item->value); } } $payment = $order->price - $amount; $order->update(['payment_after_wallet'=>$payment]); } } //ثبت وضعیت های سفارش و ثبت در جدول transaction_order وارسال پیامک به مشتری $this->afterCreateOrder(auth()->id(),$order,1); Sms::Send('09128081392','هنزا(وبسایت)'."\n".'سفارش جدید با شماره '.$order->id.' به مبلغ '.$order->total_price.'تومان '); Sms::Send('09124406015','هنزا(وبسایت)'."\n".'سفارش جدید با شماره '.$order->id.' به مبلغ '.$order->total_price.'تومان '); // Sms::Send('09126449907','هنزا(وبسایت)'."\n".'سفارش جدید با شماره '.$order->id.' به مبلغ '.$order->total_price.'تومان '); Sms::Send('09124406015','هنزا(وبسایت)'."\n".'سفارش جدید با شماره '.$order->id.' به مبلغ '.$order->total_price.'تومان '); Sms::Send('09363366849','هنزا(وبسایت)'."\n".'سفارش جدید با شماره '.$order->id.' به مبلغ '.$order->total_price.'تومان '); return response()->json(true); } public function cancel($order_id){ //برای این که اگر از ولت استفاده کرده بود مقادیر به ولت برکردد $transaction = Transaction::where('order_id',$order_id)->get(); $order = Order::findOrFail($order_id); if (count($transaction)>0){ $wallet_interface = new WalletInterface(auth()->id(), 1); foreach ($transaction as $item){ $this->createTransactionInterface($wallet_interface,$item->value,$item->transaction_type,true,$order); } } } /** * 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) { // } public function report(){ $id = auth('api-singleCustomer')->id(); $all = SingleCustomerOrders::where('user_id_id' , $id)->sum('total_price'); $count = SingleCustomerOrders::where('user_id_id' , $id)->count(); $month = SingleCustomerOrders::where('user_id_id' , $id)->where('created_at', Carbon::now()->subMonths(30))->sum('total_price'); return response()->json([ 'id'=>$id, 'sum'=>$all, 'total_orders'=>$count, 'sum_last_30_days'=>$month ]); } public function generateReportOrder($id) { $order = SingleCustomerOrders::with(['items', 'customer','branch'])->select(['*', Order::$SELECT_UAJ, Order::$SELECT_CAJ, Order::$SELECT_STS_STR, Order::$SELECT_COLOR, Order::$SELECT_IMG])->findOrFail($id); if ((!is_null($order->shipping_address) && count($order->shipping_address) == 0) || is_null($order->shipping_address)){ $shipping_address['reciver_phone'] = null; $shipping_address['reciver_fixed_phone'] = null; $shipping_address['zipcode'] = null; $shipping_address['province'] = null; $shipping_address['address'] = null; $shipping_address['city'] = null; $order->shipping_address = $shipping_address; } $pdf = \niklasravnsborg\LaravelPdf\Facades\Pdf::loadView('pdfSingle', ['order' => $order]); $file_name = 'report_' . $order->id . '.pdf'; if (!\Illuminate\Support\Facades\File::exists(public_path('storage/single'))) { \Illuminate\Support\Facades\File::makeDirectory(public_path('storage/single')); } $pdf->save(public_path('storage/single' . '/' . $file_name)); return response()->download(public_path('storage/single' . '/' . $file_name)); } public function getPromotion(Request $request) { $promotion = Promotion::web()->get()->toArray(); return response()->json($promotion); } }
Copyright ©2k19 -
Hexid
|
Tex7ure