Files
sifatbaho-php/app/Http/Controllers/AutoController.php
husanjon 6c985c1b29 Fix DB schema mismatches from backup import
- purpose/concern/region/district tables use uz/ru/cr instead of name
- auto_orders/estate_orders use purpose_id not purpose
- Add ownerName/owner computed fields from owner_first/last_name
- Fix appraiserChart to use order_members instead of appraiser_id
- Fix DebitController to join appraiser and order relations
- Fix role queries to be case-insensitive (DB has mixed case)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-06 06:05:09 +05:00

148 lines
7.1 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class AutoController extends Controller
{
private function ordersQuery()
{
return DB::table('auto_orders as o')
->leftJoin('purposes as p', 'p.id', '=', 'o.purpose_id')
->leftJoin('concerns as c', 'c.id', '=', 'o.car_category')
->selectRaw('o.*, o.ordered_customer as customer, p as "purposeOne"')
->selectRaw('o.*, o.ordered_customer as customer')
->addSelect([
'purpose_uz' => DB::table('purposes')->select('uz')->whereColumn('id', 'o.purpose_id'),
'purpose_ru' => DB::table('purposes')->select('ru')->whereColumn('id', 'o.purpose_id'),
'purpose_cr' => DB::table('purposes')->select('cr')->whereColumn('id', 'o.purpose_id'),
'concern_name' => DB::table('concerns')->select('name')->whereColumn('id', 'o.car_category'),
]);
}
public function index(Request $request)
{
$filters = $request->only(['search', 'period', 'status', 'appraiser', 'purpose_id', 'car_category']);
$size = $request->get('size', 20);
$query = DB::table('auto_orders as o')
->selectRaw('o.*, o.ordered_customer as customer, CONCAT_WS(\' \', o.owner_last_name, o.owner_first_name, o.owner_patronymic) as "ownerName"')
->addSelect([
'purpose_uz' => DB::table('purposes')->select('uz')->whereColumn('id', 'o.purpose_id'),
'purpose_ru' => DB::table('purposes')->select('ru')->whereColumn('id', 'o.purpose_id'),
'purpose_cr' => DB::table('purposes')->select('cr')->whereColumn('id', 'o.purpose_id'),
'concern_uz' => DB::table('concerns')->select('uz')->whereColumn('id', 'o.car_category'),
'concern_ru' => DB::table('concerns')->select('ru')->whereColumn('id', 'o.car_category'),
'concern_cr' => DB::table('concerns')->select('cr')->whereColumn('id', 'o.car_category'),
])
->orderByDesc('o.id');
if (!empty($filters['search'])) {
$s = '%' . $filters['search'] . '%';
$query->where(function ($q) use ($s) {
$q->where('o.number', 'like', $s)->orWhere('o.ordered_customer', 'like', $s);
});
}
if (!empty($filters['status'])) $query->where('o.status', $filters['status']);
if (!empty($filters['purpose_id'])) $query->where('o.purpose_id', $filters['purpose_id']);
if (!empty($filters['car_category'])) $query->where('o.car_category', $filters['car_category']);
$rawOrders = $query->paginate($size)->withQueryString();
$orders = $rawOrders->through(function ($o) {
$o->purposeOne = (object)['uz' => $o->purpose_uz, 'ru' => $o->purpose_ru, 'cr' => $o->purpose_cr];
$o->concernOne = (object)['uz' => $o->concern_uz, 'ru' => $o->concern_ru, 'cr' => $o->concern_cr];
return $o;
});
$stats = DB::table('auto_orders')->selectRaw('status, count(*) as cnt')->groupBy('status')->pluck('cnt', 'status');
$purposeCases = DB::table('purposes')->get();
$concerns = DB::table('concerns')->get();
return view('auto.index', compact('orders', 'stats', 'filters', 'size', 'purposeCases', 'concerns'));
}
public function create()
{
$concerns = DB::table('concerns')->get();
$purposes = DB::table('purposes')->get();
$regions = DB::table('regions')->get();
$appraisers = DB::table('users')->whereRaw('LOWER(role) = ?', ['appraiser'])->where('status', 'active')->get();
return view('auto.create', compact('concerns', 'purposes', 'regions', 'appraisers'));
}
public function store(Request $request)
{
return redirect()->route('auto.index');
}
public function edit($id)
{
$order = DB::table('auto_orders')->find($id);
$concerns = DB::table('concerns')->get();
$purposes = DB::table('purposes')->get();
$regions = DB::table('regions')->get();
return view('auto.edit', compact('order', 'concerns', 'purposes', 'regions'));
}
public function update(Request $request, $id)
{
return redirect()->route('auto.index');
}
public function show($order)
{
$order = DB::table('auto_orders as o')
->selectRaw('o.*, o.ordered_customer as customer, CONCAT_WS(\' \', o.owner_last_name, o.owner_first_name, o.owner_patronymic) as "ownerName"')
->addSelect([
'purpose_uz' => DB::table('purposes')->select('uz')->whereColumn('id', 'o.purpose_id'),
'purpose_ru' => DB::table('purposes')->select('ru')->whereColumn('id', 'o.purpose_id'),
'purpose_cr' => DB::table('purposes')->select('cr')->whereColumn('id', 'o.purpose_id'),
'concern_uz' => DB::table('concerns')->select('uz')->whereColumn('id', 'o.car_category'),
'concern_ru' => DB::table('concerns')->select('ru')->whereColumn('id', 'o.car_category'),
'concern_cr' => DB::table('concerns')->select('cr')->whereColumn('id', 'o.car_category'),
])
->where('o.id', $order)->first();
if ($order) {
$order->purposeOne = (object)['uz' => $order->purpose_uz, 'ru' => $order->purpose_ru, 'cr' => $order->purpose_cr];
$order->concernOne = (object)['uz' => $order->concern_uz, 'ru' => $order->concern_ru, 'cr' => $order->concern_cr];
}
$members = DB::table('order_members')->where('order_id', $order->id ?? 0)->where('order_type', 'auto_')->get();
$appraisers = DB::table('users')->whereRaw('LOWER(role) = ?', ['appraiser'])->where('status', 'active')->get();
$isAppraisers = $members->where('user_id', auth()->id())->count() > 0;
$files = DB::table('files')->where('order_id', $order->id ?? 0)->where('order_type', 'auto_')->get();
return view('auto.show', compact('order', 'members', 'appraisers', 'isAppraisers', 'files'));
}
public function showActivities($order)
{
$order = DB::table('auto_orders')->find($order);
$actions = DB::table('tracking_actions')->where('order_id', $order->id ?? 0)->latest()->get();
return view('auto.show-activities', compact('order', 'actions'));
}
public function showTeam($order)
{
$order = DB::table('auto_orders')->find($order);
$members = DB::table('order_members')->where('order_id', $order->id ?? 0)->where('order_type', 'auto_')->get();
$appraisers = DB::table('users')->whereRaw('LOWER(role) = ?', ['appraiser'])->where('status', 'active')->get();
return view('auto.show-team', compact('order', 'members', 'appraisers'));
}
public function showDocuments($order)
{
$order = DB::table('auto_orders')->find($order);
$files = DB::table('files')->where('order_id', $order->id ?? 0)->where('order_type', 'auto_')->get();
return view('auto.show-documents', compact('order', 'files'));
}
public function orderClone($id)
{
return redirect()->route('auto.index');
}
}