HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/vhost/disk-apps/agile-selling-wpb/app/Http/Controllers/Api/POSApiController.php
<?php

namespace App\Http\Controllers\api;

use App\BoItemManualPrice;
use DB;
use App\User;
use App\Brand;
use App\Order;
use App\Product;
use App\Parameter;
use App\ProductImage;
use App\Subcategories;
use App\TopTenProduct;
use App\BoShippingType;
use App\BoSpecialPrice;
use App\UserInformation;
use App\ProductAttribute;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class POSApiController extends Controller
{
    public function allProducts(Request $request){
        try {
            $parameter = Parameter::where('id',1)->select('sucursal_products')->first();
            $listProducts = DB::table('subcategory_products')
                            ->join('products', 'subcategory_products.product_id', '=', 'products.id', 'left')
                            ->where('products.active', true)
                            ->orderBy('products.order','asc');
            if ($parameter->sucursal_products) {
              $user_id = Auth::user()->id;
              $sucursal = UserInformation::where('user_id', $user_id)->select('current_sucursal_id')->first();
              $listProducts = $listProducts->where('products.sucursal_id',$sucursal->current_sucursal_id)->get();
            }else{
              $listProducts = $listProducts->get();
            }
            if ($listProducts) {
              foreach ($listProducts as $product) {
                $brand = null;
                if ($product->brand_id) {
                  $brand = Brand::where('id',$product->brand_id)->first();
                }
                $product->brand = $brand;
                $product->product_attributes = ProductAttribute::where('product_id', $product->id)->with('attribute')->get()->groupBy('attribute_id')->toArray();
                $product->product_images = ProductImage::where('product_id', $product->id)->get();
                $product->subcategory = Subcategories::where('id', $product->subcategory_id)->first();
                $product->bo_special_prices = BoSpecialPrice::where('product_id', $product->id)->get();
                $product->bo_item_manual_prices = BoItemManualPrice::where('product_id', $product->id)->get();
              }
            }

            $data = array('status' => 'success', 'data' => $listProducts);
            return response()->json($data, 200);
        } catch (\Throwable $th) {
            $data = array('status' => 'error', 'data' => null, 'message' => $th->getMessage());
            return response()->json($data, 400);
        }
    }

    public function allUsers(Request $request)
    {
        try {
            $globalBd = new TopTenProduct();
            $globalBd->setConnection("global-db");

            $users = User::where([['rol_id', 4],['active', true]])->with('addresses','boPriceList', 'boPaymentTermType', 'boContactEmployeesUsers', 'boSpecialPrices')->get();
            foreach ($users as $user) {
                $user->top_ten_products = $globalBd->where('user_id', $user->id)->orderBy('quantity', 'DESC')->get();
            }
            // topTenProducts - top_ten_products
            $data = array('status' => 'success', 'data' => $users);
            return response()->json($data, 200);
        } catch (\Throwable $th) {
            $data = array('status' => 'error', 'data' => null, 'message' => $th->getMessage());
            return response()->json($data, 400);
        }
    }

    public function allOrders(Request $request)
    {
        try {
            $orders = Order::where('order_type_id', 5)
                            ->with(['orderState','address','deliveryMan','orderProducts','boShippingType','client'])
                            ->offset($request["offset"])
                            ->take($request["take"])
                            ->orderBy('creation_date', 'desc')
                            ->get();
            $data = array('status' => 'success', 'data' => $orders);
            return response()->json($data, 200);
        } catch (\Throwable $th) {
            $data = array('status' => 'error', 'data' => null, 'message' => $th->getMessage());
            return response()->json($data, 400);
        }
    }

    public function getOrderTypes(Request $request)
    {
        try {
            $types = BoShippingType::where('active', true)->get();
            $data = array('status' => 'success', 'data' => $types);
            return response()->json($data, 200);
        } catch (\Throwable $th) {
            $data = array('status' => 'error', 'data' => null, 'message' => $th->getMessage());
            return response()->json($data, 400);
        }
    }
}