/
home
/
sjslayjy
/
public_html
/
mosaram
/
app
/
Http
/
Controllers
/
Upload File
HOME
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Faker; use Auth; use App\User; use DB; use App\Exports\RetailerLedger; use Maatwebsite\Excel\Facades\Excel; use Carbon\Carbon; use DatePeriod; use DateInterval; use DateTime; use Illuminate\Support\Facades\Log; class ApiController extends Controller { public function checkApiAuth($data) { if ($data->api_token) { $user = \App\User::where('api_token', $data->api_token)->first(); if (!is_null($user)) { return true; } else { return false; } } else { return false; } } public function AuthId($data) { if ($data->api_token) { $user = \App\User::where('api_token', $data->api_token)->first(); if (!is_null($user)) { return $user; } else { return false; } } else { return false; } } public function login(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'email' => 'required|email', 'password' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { $creds = ['email' => $request->email, 'password' => $request->password]; if (Auth::attempt($creds)) { $auth_user = User::find(Auth::user()->id); $auth_user->api_token = bcrypt($internals->uuid); if ($request->firebase_token) { $auth_user->firebase_token = $request->firebase_token; } if ($request->device_details) { $auth_user->device_details = $request->device_details; } if ($request->version) { $auth_user->version = $request->version; } if ($request->device_id) { if($auth_user->device_id1 == null || $auth_user->device_id1 == $request->device_id1){ $auth_user->device_id1 = $request->device_id; } else if($auth_user->device_id2 == null){ $auth_user->device_id2 = $request->device_id; } else{ $response['flag'] = false; $response['message'] = "Your first and second port already consumed!"; } } $auth_user->save(); $response['flag'] = true; $response['user'] = $auth_user; } else { $response['flag'] = false; $response['message'] = "Invalid credentials."; } } return response()->json($response); } public function products(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $products = \App\Product::where('is_active', 1)->get(); if (count($products) > 0) { $response['flag'] = true; $response['products'] = $products; } else { $response['flag'] = false; $response['message'] = "Product not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function dealers(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $dealers = \App\Dealer::where('is_active', 1)->get(); if (count($dealers) > 0) { $response['flag'] = true; $response['dealers'] = $dealers; } else { $response['flag'] = false; $response['message'] = "Product not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function retailers(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $retailers = \App\Retailer::where('is_active', 1)->get(); if (count($retailers) > 0) { $response['flag'] = true; $response['retailers'] = $retailers; } else { $response['flag'] = false; $response['message'] = "Product not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function product_companies(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $product_companies = \App\ProductCompany::where('is_active', 1)->get(); if (count($product_companies) > 0) { $response['flag'] = true; $response['product_companies'] = $product_companies; } else { $response['flag'] = false; $response['message'] = "Product Company not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function destinations(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $destinations = \App\FreightList::all(); if (count($destinations) > 0) { $response['flag'] = true; $response['destinations'] = $destinations; } else { $response['flag'] = false; $response['message'] = "No Destination not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function units(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $units = \App\Unit::where('is_active', 1)->get(); if (count($units) > 0) { $response['flag'] = true; $response['units'] = $units; } else { $response['flag'] = false; $response['message'] = "Units not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function transporters(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $transporters = \App\Transporter::where('is_active', 1)->get(); if (count($transporters) > 0) { $response['flag'] = true; $response['transporters'] = $transporters; } else { $response['flag'] = false; $response['message'] = "Units not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function addTransporter(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'name' => 'required|unique:accounts,name', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { $transporter = new \App\Transporter(); $transporter->name = $request->name; $transporter->phone = $request->phone; $transporter->email = $request->email; if ($transporter->save()) { $response['flag'] = true; $response['message'] = "Transporter Added Successfully"; $response['transporter_id'] = $transporter->id; } else { $response['flag'] = false; $response['error'] = "Something Went Wrong"; } } return response()->json($response); } public function warehouses(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $warehouses = \App\Warehouse::where('is_active', 1)->get(); if (count($warehouses) > 0) { $response['flag'] = true; $response['warehouses'] = $warehouses; } else { $response['flag'] = false; $response['message'] = "Units not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function tokens(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if($this->checkApiAuth($request)){ if (1) { if ($request->date) { $date = $request->date; } else { $date = date('Y-m-d'); } $token = \App\Token::with('dealer', 'master_rake', 'product', 'product_company', 'unit', 'warehouse') //->whereDate('created_at', '=', date('Y-m-d')) ->whereDate('created_at', '>=', $date) ->orderBy('id', 'desc')->get(); if (!is_null($token)) { $response['flag'] = true; $response['tokens'] = $token; } else { $response['flag'] = false; $response['message'] = "Invalid Token"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function adminTokens(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if($this->checkApiAuth($request)){ if (1) { if ($request->date) { $date = $request->date; } else { $date = date('Y-m-d'); } $token = \App\Token::with('dealer', 'master_rake', 'product', 'product_company', 'unit', 'warehouse', 'from_warehouse') ->where('token_type', 2) ->whereDate('created_at', $date) ->orderBy('id', 'desc')->get(); if (!is_null($token)) { $response['flag'] = true; $response['tokens'] = $token; } else { $response['flag'] = false; $response['message'] = "Invalid Token"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function tokenList(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if($this->checkApiAuth($request)){ if (1) { $token = \App\Token::select('id', 'unique_id')->orderBy('id', 'desc')->get(); if (!is_null($token)) { $response['flag'] = true; $response['tokens'] = $token; } else { $response['flag'] = false; $response['message'] = "Invalid Token"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function tokenDetails(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'token_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $token = \App\Token::with('dealer', 'master_rake', 'product', 'product_company', 'unit', 'warehouse', 'transporter', 'from_warehouse', 'retailer')->where('id', $request->token_id)->first(); if (!is_null($token)) { $remaining_quantity = \App\ProductLoading::where('token_id', $request->token_id)->sum('quantity'); if (!$remaining_quantity) { $remaining_quantity = $token->quantity; } else { $remaining_quantity = $token->quantity - $remaining_quantity; } $response['flag'] = true; if ($token->to_type == 1) { $token->to = getModelById('Warehouse', $token->warehouse_id)->name; } elseif ($token->to_type == 2) { $token->to = getModelById('Retailer', $token->retailer_id)->name . "(" . getModelById('Retailer', $token->retailer_id)->address . ")"; } else { $token->to = getModelById('Dealer', $token->dealer_id)->name . "(" . getModelById('Dealer', $token->dealer_id)->address1 . ")"; } if ($token->token_type == 1) { $token->godown = getModelById('RakePoint', $token->master_rake->rake_point_id)->rake_point; } else { $token->godown = getModelById('Warehouse', $token->from_warehouse_id)->name; } // $token->date_of_generation = ('d/m/Y',strtotime($token->date_of_generation)); $token->product_company = getModelById('ProductCompany', $token->product_company_id)->brand_name; $token->token_quantity = $token->quantity . " " . getModelById('Unit', $token->unit_id)->unit; $token->godown_keeper = is_null(getModelById('User', $token->warehouse_keeper_id)) ? "" : getModelById('User', $token->warehouse_keeper_id)->name; $company = \App\Company::where('id', $token->company_id)->first(); $token->company = $company; $current_time = date('Y-m-d H:i:s'); $token_time = $token->created_at; $timediff = strtotime($current_time) - strtotime($token_time); $is_expired = false; if ($token->token_type == 1) { if ($timediff > 172800) { $is_expired = true; } } else { if ($timediff > 86400) { $is_expired = true; } } $response['token'] = $token; $response['remaining_quantity'] = $remaining_quantity; $response['is_expired'] = $is_expired; } else { $response['flag'] = false; $response['message'] = "Invalid Token"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function master_rakes(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { // if(1){ $currentDate = date('Y-m-d'); //$startDate = date('Y-m-d', strtotime('-30 days', strtotime(date('Y-m-d')))); $master_rakes = \App\MasterRake::where('is_active', 1)->orderBy('id', 'desc')->get(); // $master_rakes = \App\MasterRake::where('is_active', 1)->whereDate('created_at', '>=', $startDate)->whereDate('created_at', '<=', $currentDate)->orderBy('id', 'desc')->get(); if (count($master_rakes) > 0) { $response['flag'] = true; $response['master_rakes'] = $master_rakes; } else { $response['flag'] = false; $response['message'] = "No Rakes Found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function masterRakeDetails(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'master_rake_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { // if(1){ $master_rake = \App\MasterRake::where('id', $request->master_rake_id)->where('is_active', 1)->with('product_company', 'master_rake_products', 'rake_allotments')->first(); if (!is_null($master_rake)) { $response['flag'] = true; $response['master_rake'] = $master_rake; $distict_users = array_unique(\App\ProductLoading::where('master_rake_id', $request->master_rake_id)->pluck('user_id')->toArray()); $users = array(); foreach ($distict_users as $distict_user) { $user = new \stdClass; $user->id = $distict_user; $user->name = getModelById('User', $distict_user)->name; array_push($users, $user); } $response['users'] = $users; } else { $response['flag'] = false; $response['message'] = "Master Rake Not Found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function productLoading(Request $request) { $response = array(); if ($this->checkApiAuth($request)) { $validator = \Validator::make( $request->all(), array( 'loading_slip_type' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $validator->getMessageBag(); } else { $user = \App\User::where('api_token', $request->api_token)->first(); if ($request->loading_slip_type == 1) { $token_details = \App\Token::where('id', $request->regular_token_id)->first(); if ($token_details->token_type == 1) { $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'regular_token_id' => 'required', 'regular_quantity' => 'required|integer', 'regular_truck_number' => 'required', 'regular_master_rake_id' => 'required', 'regular_product_company_id' => 'required', 'regular_product_id' => 'required', 'regular_unit_id' => 'required', 'regular_dealer_id' => 'required', 'regular_transporter_id' => 'required', //'regular_freight' =>'required', 'regular_labour_name' => 'required', 'regular_labour_rate' => 'required', ) ); } else { $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'regular_token_id' => 'required', 'regular_quantity' => 'required|integer', 'regular_truck_number' => 'required', 'from_warehouse_id' => 'required', 'regular_product_company_id' => 'required', 'regular_product_id' => 'required', 'regular_unit_id' => 'required', 'regular_dealer_id' => 'required', 'regular_transporter_id' => 'required', //'regular_freight' =>'required', 'regular_labour_name' => 'required', 'regular_labour_rate' => 'required', ) ); } } else { $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'direct_master_rake_id' => 'required', 'direct_product_company_id' => 'required', 'direct_product_id' => 'required', 'direct_unit_id' => 'required', 'direct_quantity' => 'required|integer', 'direct_truck_number' => 'required', 'direct_labour_name' => 'required', 'direct_labour_rate' => 'required', 'direct_transporter_id' => 'required', //'direct_freight' =>'required', 'direct_warehouse_id' => 'required', ) ); } if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($request->loading_slip_type == 1) { $token = \App\Token::where('id', $request->regular_token_id)->first(); $product_loading = \App\ProductLoading::where('token_id', $request->regular_token_id)->sum('quantity'); $remaining_quantity = $token->quantity - $product_loading; if ($product_loading && $remaining_quantity < $request->regular_quantity) { $response['flag'] = false; $response['errors']['regular_quantity'] = "quantity Should not be greater than Token Quantity (" . $token->quantity . "). Remaining Quantity is " . $remaining_quantity; } else if ($request->regular_quantity > $token->quantity) { $response['flag'] = false; $response['errors']['regular_quantity'] = "quantity Should not be greater than Token Quantity (" . $token->quantity . ")"; } else { $product_loading = new \App\ProductLoading(); $product_loading->user_id = $user->id; $product_loading->loading_slip_type = $request->loading_slip_type; $product_loading->token_id = $request->regular_token_id; $product_loading->master_rake_id = $request->regular_master_rake_id; $product_loading->from_warehouse_id = $request->from_warehouse_id; if ($token->to_type == 2) { $product_loading->retailer_id = $token->retailer_id; $product_loading->retailer_name = getModelById('Retailer', $token->retailer_id)->name; } if ($token->to_type == 1) { $product_loading->is_approved = 0; } $product_loading->dealer_id = $request->regular_dealer_id; $product_loading->dealer_name = getModelById('Dealer', $request->regular_dealer_id)->name; $product_loading->product_company_id = $request->regular_product_company_id; $product_loading->product_company_name = getModelById('ProductCompany', $request->regular_product_company_id)->name; $product_loading->product_id = $request->regular_product_id; $product_loading->product_name = getModelById('Product', $request->regular_product_id)->name; $product_loading->quantity = $request->regular_quantity; $product_loading->mrp_rate = $token->mrp_rate; $product_loading->unit_id = $token->unit_id; $product_loading->unit_name = getModelById('Unit', $token->unit_id)->unit; $product_loading->transporter_id = $request->regular_transporter_id; if ($request->regular_transporter_id) { $product_loading->transporter_name = getModelById('Transporter', $request->regular_transporter_id)->name; } if ($request->regular_wagon_no) { $product_loading->wagon_number = $request->regular_wagon_no; } $product_loading->truck_number = $request->regular_truck_number; $product_loading->freight = $request->regular_freight; if ($product_loading->save()) { $labour_payment = new \App\LabourPayments(); $labour_payment->user_id = $user->id; $labour_payment->token_id = $request->regular_token_id; $labour_payment->master_rake_id = $request->regular_master_rake_id; $labour_payment->from_warehouse_id = $request->from_warehouse_id; $labour_payment->product_loading_id = $product_loading->id; $labour_payment->product_id = $request->regular_product_id; $labour_payment->product_name = getModelById('Product', $request->regular_product_id)->name; $labour_payment->quantity = $request->regular_quantity; $labour_payment->unit_id = $token->unit_id; $labour_payment->unit_name = getModelById('Unit', $token->unit_id)->unit; $labour_payment->labour_name = $request->regular_labour_name; $labour_payment->rate = $request->regular_labour_rate; $labour_payment->truck_number = $request->regular_truck_number; $product_loading->driver_number = $request->driver_number; $labour_payment->save(); /*-----push notification---*/ $recepients = \App\User::whereIn('role_id', array(1, 5, 6))->get(); if ($product_loading->loading_slip_type == 1) { $msg = "Product Loading (" . $product_loading->id . ") of " . $product_loading->product_name . " " . $product_loading->quantity . " " . $product_loading->unit_name; if (!is_null($product_loading->token_id)) { $msg .= " against Token " . getModelById('Token', $product_loading->token_id)->unique_id . " To "; if ($product_loading->loading_slip_type == 1) { $msg .= getModelById('Dealer', $product_loading->dealer_id)->name . "(" . getModelById('Dealer', $product_loading->dealer_id)->address1 . ")"; } else { $msg .= getModelById('Warehouse', $product_loading->warehouse_id)->name; } } else { if ($product_loading->loading_slip_type == 1) { $msg .= getModelById('Dealer', $product_loading->dealer_id)->name . "(" . getModelById('Dealer', $product_loading->dealer_id)->address1 . ")"; } else { $msg .= getModelById('Warehouse', $product_loading->warehouse_id)->name; } } } else { $msg = "Direct Product Loading " . $product_loading->id . " of " . $product_loading->product_name . " " . $product_loading->quantity . " " . $product_loading->unit_name . " To " . getModelById('Warehouse', $product_loading->warehouse_id)->name; } $msg .= " By " . $user->name; foreach ($recepients as $recepient) { if (!is_null($recepient->firebase_token) || $recepient->firebase_token != "") { if ($recepient->role_id == 1) { $type = "admin"; } else if ($recepient->role_id == 5) { $type = "logistic_manager"; } else if ($recepient->role_id == 6) { $type = "marketing_manager"; } NotificationController::notify($msg, $recepient->firebase_token, $type); } } /*-----push notification---*/ if ($token->token_type == 1) { $inventory = \App\Inventory::where('dealer_id', $request->regular_dealer_id) ->where('warehouse_id', 24) ->where('product_brand_id', $token->product_company_id) ->where('product_id', $request->regular_product_id) ->first(); if (!is_null($inventory)) { $inventory->quantity = $inventory->quantity - $request->regular_quantity; $inventory->save(); } else { $inventory = new \App\Inventory(); $inventory->dealer_id = $request->regular_dealer_id; $inventory->warehouse_id = 24; $inventory->product_brand_id = $token->product_company_id; $inventory->product_id = $request->regular_product_id; $inventory->quantity = 0 - $request->regular_quantity; $inventory->unit_id = $token->unit_id; $inventory->save(); } } if ($token->token_type == 2) { $inventory = \App\Inventory::where('dealer_id', $request->regular_dealer_id)->where('warehouse_id', $token->from_warehouse_id)->where('product_brand_id', $token->product_company_id)->where('product_id', $request->regular_product_id)->first(); if ($inventory) { $inventory->quantity = $inventory->quantity - $request->regular_quantity; $inventory->save(); } else { $inventory = new \App\Inventory(); $inventory->dealer_id = $request->regular_dealer_id; $inventory->warehouse_id = $request->from_warehouse_id; $inventory->product_brand_id = $token->product_company_id; $inventory->product_id = $request->regular_product_id; $inventory->unit_id = $token->unit_id; $inventory->quantity = 0 - $request->regular_quantity; $inventory->save(); } } $response['flag'] = true; $response['message'] = "Loading Slip Generated Successfully"; $response['loading_slip_id'] = $product_loading->id; } else { $response['flag'] = false; $response['error'] = "Something Went Wrong"; } } } else { $master_rake_product = \App\MasterRakeProduct::where('master_rake_id', $request->direct_master_rake_id)->where('product_id', $request->direct_product_id)->first(); if (is_null($master_rake_product)) { $response['flag'] = false; $error = new \stdClass(); $error->message = 'This Rake do not contains this Product'; $response['errors'][] = $error; } else { $product_loading = \App\ProductLoading::where('master_rake_id', $request->direct_master_rake_id)->where('product_id', $request->direct_product_id)->sum('quantity'); $remaining_quantity = ($master_rake_product->quantity + $master_rake_product->excess_quantity + 1000) - $product_loading; if ($product_loading && $remaining_quantity < $request->direct_quantity) { $response['flag'] = false; $error = new \stdClass(); $error->message = "quantity Should not be greater than Alloted Quantity (" . $master_rake_product->quantity . "). Remaining Quantity is " . $remaining_quantity; $response['errors'][] = $error; } else if (!$product_loading && $master_rake_product->quantity < $request->direct_quantity) { $response['flag'] = false; $error = new \stdClass(); $error->message = "quantity Should not be greater than Alloted Quantity (" . $master_rake_product->quantity . ")."; $response['errors'][] = $error; } else { $product_loading = new \App\ProductLoading(); $product_loading->user_id = $user->id; $product_loading->loading_slip_type = $request->loading_slip_type; $product_loading->master_rake_id = $request->direct_master_rake_id; $product_loading->product_company_id = $request->direct_product_company_id; $product_loading->product_company_name = getModelById('ProductCompany', $request->direct_product_company_id)->name; $product_loading->warehouse_id = $request->direct_warehouse_id; $product_loading->product_id = $request->direct_product_id; $product_loading->product_name = getModelById('Product', $request->direct_product_id)->name; $product_loading->quantity = $request->direct_quantity; $product_loading->unit_id = $request->direct_unit_id; $product_loading->unit_name = getModelById('Unit', $request->direct_unit_id)->unit; $product_loading->transporter_id = $request->direct_transporter_id; if ($request->direct_transporter_id) { $product_loading->transporter_name = getModelById('Transporter', $request->direct_transporter_id)->name; } if ($request->direct_wagon_no) { $product_loading->wagon_number = $request->direct_wagon_no; } $product_loading->truck_number = $request->direct_truck_number; $product_loading->freight = $request->direct_freight; $product_loading->is_approved = 0; if ($product_loading->save()) { $labour_payment = new \App\LabourPayments(); $labour_payment->user_id = $user->id; $labour_payment->master_rake_id = $request->direct_master_rake_id; $labour_payment->product_loading_id = $product_loading->id; $labour_payment->warehouse_id = $product_loading->warehouse_id; $labour_payment->product_id = $request->direct_product_id; $labour_payment->product_name = getModelById('Product', $request->direct_product_id)->name; $labour_payment->quantity = $request->direct_quantity; $labour_payment->unit_id = $request->direct_unit_id; $labour_payment->unit_name = getModelById('Unit', $request->direct_unit_id)->unit; $labour_payment->labour_name = $request->direct_labour_name; $labour_payment->rate = $request->direct_labour_rate; $labour_payment->truck_number = $request->direct_truck_number; $product_loading->driver_number = $request->driver_number; $labour_payment->save(); /*-----push notification---*/ $recepients = \App\User::whereIn('role_id', array(1, 5, 6))->get(); if ($product_loading->loading_slip_type == 1) { $msg = "Product Loading (" . $product_loading->id . ") of " . $product_loading->product_name . " " . $product_loading->quantity . " " . $product_loading->unit_name; if (!is_null($product_loading->token_id)) { $msg .= " against Token " . getModelById('Token', $product_loading->token_id)->unique_id . " To "; if ($product_loading->loading_slip_type == 1) { $msg .= getModelById('Dealer', $product_loading->dealer_id)->name . "(" . getModelById('Dealer', $product_loading->dealer_id)->address1 . ")"; } else { $msg .= getModelById('Warehouse', $product_loading->warehouse_id)->name; } } else { if ($product_loading->loading_slip_type == 1) { $msg .= getModelById('Dealer', $product_loading->dealer_id)->name . "(" . getModelById('Dealer', $product_loading->dealer_id)->address1 . ")"; } else { $msg .= getModelById('Warehouse', $product_loading->warehouse_id)->name; } } } else { $msg = "Direct Product Loading " . $product_loading->id . " of " . $product_loading->product_name . " " . $product_loading->quantity . " " . $product_loading->unit_name . " To " . getModelById('Warehouse', $product_loading->warehouse_id)->name; } $msg .= " By " . $user->name; foreach ($recepients as $recepient) { if (!is_null($recepient->firebase_token) || $recepient->firebase_token != "") { if ($recepient->role_id == 1) { $type = "admin"; } else if ($recepient->role_id == 5) { $type = "logistic_manager"; } else if ($recepient->role_id == 6) { $type = "marketing_manager"; } NotificationController::notify($msg, $recepient->firebase_token, $type); } } /*-----push notification---*/ //remove from Buffer STP $inventory = \App\Inventory::where('product_company_id', $request->direct_product_company_id)->where('warehouse_id', 24)->where('product_id', $request->direct_product_id)->where('product_brand_id', $request->direct_product_company_id)->first(); if (!is_null($inventory)) { $inventory->quantity = $inventory->quantity - $request->direct_quantity; $inventory->save(); } $response['flag'] = true; $response['message'] = "Loading Slip Generated Successfully"; $response['loading_slip_id'] = $product_loading->id; } else { $response['flag'] = false; $response['error'] = "Something Went Wrong"; } } } } } } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } return response()->json($response); } public function productUnloading(Request $request) { $response = array(); if ($this->checkApiAuth($request)) { $validator = \Validator::make( $request->all(), array( 'unloading_slip_type' => 'required', ), array( 'unloading_slip_type.required' => "अनलोडिंग स्लिप टाइप चुनें", ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // regular unloading if ($request->unloading_slip_type == 1) { $validator = \Validator::make( $request->all(), array( 'loading_slip_id' => 'required', 'warehouse_id' => 'required', 'quantity' => 'required|min:1', 'labour_name' => 'required', 'labour_rate' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($request->quantity != 0) { $product_loading = \App\ProductLoading::where('id', $request->loading_slip_id)->with('token')->first(); $user = \App\User::where('api_token', $request->api_token)->first(); if (!is_null($product_loading)) { if ($product_loading->recieved_quantity == 0) { $product_unloading = new \App\ProductUnloading(); $product_unloading->user_id = $user->id; $product_unloading->loading_slip_type = $product_loading->loading_slip_type; if (isset($product_loading->token)) { $product_unloading->token_id = $product_loading->token->id; } $product_unloading->product_loading_id = $product_loading->id; $product_unloading->master_rake_id = $product_loading->master_rake_id; // if($product_loading->loading_slip_type == 2){ $product_unloading->product_company_id = $product_loading->product_company_id; $product_unloading->product_company_name = getModelById('ProductCompany', $product_loading->product_company_id)->name; // } $product_unloading->warehouse_id = $request->warehouse_id; $product_unloading->product_id = $product_loading->product_id; $product_unloading->product_name = getModelById('Product', $product_loading->product_id)->name; $product_unloading->quantity = $request->quantity; $product_unloading->unit_id = $product_loading->unit_id; $product_unloading->unit_name = getModelById('Unit', $product_loading->unit_id)->unit; $product_unloading->transporter_id = $product_loading->transporter_id; if ($product_loading->transporter_id) { $product_unloading->transporter_name = getModelById('Transporter', $product_loading->transporter_id)->name; } $product_unloading->truck_number = $product_loading->truck_number; if ($product_loading->loading_slip_type == 1) { $product_unloading->dealer_id = $product_loading->dealer_id; $product_unloading->dealer_name = getModelById('Dealer', $product_loading->dealer_id)->name; } if ($product_unloading->save()) { $product_loading->is_approved = 1; $product_loading->recieved_quantity = $request->quantity; $product_loading->save(); $unloading_labour_payment = new \App\UnloadingLabourPayment(); $unloading_labour_payment->user_id = $user->id; if (isset($product_loading->token)) { $unloading_labour_payment->token_id = $product_loading->token->id; } $unloading_labour_payment->master_rake_id = $product_loading->master_rake_id; $unloading_labour_payment->product_unloading_id = $product_unloading->id; $unloading_labour_payment->warehouse_id = $request->warehouse_id; $unloading_labour_payment->product_id = $product_loading->product_id; $unloading_labour_payment->product_name = getModelById('Product', $product_loading->product_id)->name; $unloading_labour_payment->quantity = $request->quantity; $unloading_labour_payment->unit_id = $product_loading->unit_id; $unloading_labour_payment->unit_name = getModelById('Unit', $product_loading->unit_id)->unit; $unloading_labour_payment->labour_name = $request->labour_name; $unloading_labour_payment->rate = $request->labour_rate; $unloading_labour_payment->truck_number = $product_loading->truck_number; $unloading_labour_payment->save(); if (isset($product_loading->token)) { $token = \App\Token::where('id', $product_loading->token_id)->first(); $inventory = \App\Inventory::where('dealer_id', $product_loading->dealer_id)->where('warehouse_id', $request->warehouse_id)->where('product_brand_id', $product_loading->product_company_id)->where('product_id', $product_loading->product_id)->first(); if (!is_null($inventory)) { $inventory->quantity = $inventory->quantity + $request->quantity; $inventory->save(); } else { $inventory = new \App\Inventory(); $inventory->dealer_id = $product_loading->dealer_id; $inventory->warehouse_id = $request->warehouse_id; $inventory->product_brand_id = $product_loading->token->product_company_id; $inventory->product_id = $product_loading->product_id; $inventory->quantity = $request->quantity; $inventory->unit_id = $product_loading->token->unit_id; $inventory->save(); } if (!is_null($product_loading->token->warehouse_id)) { if ($product_loading->token->warehouse_id != $request->warehouse_id) { $token->warehouse_id = $request->warehouse_id; $token->save(); $product_loading->warehouse_id = $request->warehouse_id; $product_loading->save(); } } else { $token->warehouse_id = $request->warehouse_id; $token->save(); $product_loading->warehouse_id = $request->warehouse_id; if (!is_null($product_loading->retailer_id) || !is_null($product_loading->dealer_id)) { $product_loading->is_returned = 1; } $product_loading->save(); } } else if ($product_loading->loading_slip_type == 2) { $inventory = \App\Inventory::where('product_company_id', $product_loading->product_company_id)->where('warehouse_id', $request->warehouse_id)->where('product_brand_id', $product_loading->product_company_id)->where('product_id', $product_loading->product_id)->first(); if (!is_null($inventory)) { $inventory->quantity = $inventory->quantity + $request->quantity; $inventory->save(); } else { $inventory = new \App\Inventory(); $inventory->product_company_id = $product_loading->product_company_id; $inventory->warehouse_id = $request->warehouse_id; $inventory->product_brand_id = $product_loading->product_company_id; $inventory->product_id = $product_loading->product_id; $inventory->unit_id = $product_loading->unit_id; $inventory->quantity = $request->quantity; $inventory->save(); } if ($product_loading->warehouse_id != $request->warehouse_id) { $product_loading->warehouse_id = $request->warehouse_id; $product_loading->save(); } } $response['flag'] = true; $response['message'] = "अनलोडिंग सफलतापूर्वक हो चुकी है !"; $response['unloading_slip_id'] = $product_unloading->id; } else { $response['flag'] = false; $response['error'] = "Something Went Wrong"; } } else { $response['flag'] = false; $response['message'] = "ये लोडिंग पहले ही अनलोड हो चुकी है !"; } } else { $response['flag'] = false; $response['message'] = "Loading details not found"; } } else { $response['flag'] = false; $response['error'] = "आमद मात्रा 0 से ज्यादा होनी चाहिए !"; } } } else { // direct unloading $validator = \Validator::make( $request->all(), array( 'direct_invoice_number' => 'required', // 'direct_invoice_date' =>'required', 'direct_product_company_id' => 'required', 'direct_product_id' => 'required|min:1', 'direct_quantity' => 'required|min:1', 'direct_unit_id' => 'required', 'direct_warehouse_id' => 'required', 'direct_transporter_id' => 'required', 'direct_truck_number' => 'required', 'direct_labour_name' => 'required', 'direct_labour_rate' => 'required', ), array( 'direct_invoice_number.required' => 'कृपया इनवॉइस/चालान नम्बर डालें', // 'direct_invoice_date.required'=>'कृपया इनवॉइस डेट डालें', 'direct_product_company_id.required' => 'कृपया कंपनी चुनें', 'direct_product_id.required' => 'कृपया प्रोडक्ट चुनें', 'direct_quantity.required' => 'कृपया मात्रा डालें', 'direct_unit_id.required' => 'कृपया यूनिट चुनें', 'direct_warehouse_id.required' => 'कृपया वेयरहाउस चुनें', 'direct_transporter_id.required' => 'कृपया ट्रांसपोर्टर चुनें', 'direct_truck_number.required' => 'कृपया ट्रक नंबर डालें', 'direct_labour_name.required' => 'कृपया लेबर का नाम डालें', 'direct_labour_rate.required' => 'कृपया लेबर रेट डालें', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { $current_date = date('Y-m-d'); $date1 = strtr($request->direct_invoice_date, '/', '-'); // $invoiceDateArr = explode('/', $request->direct_invoice_date); // $timediff = strtotime($current_date) - strtotime($invoiceDateArr[2].'-'.$invoiceDateArr[1].'-'.$invoiceDateArr[0]); // if($timediff <= 432000){ if (1) { $user = \App\User::where('api_token', $request->api_token)->first(); $product_unloading = new \App\ProductUnloading(); $product_unloading->user_id = $user->id; $product_unloading->product_company_id = $request->direct_product_company_id; $product_unloading->product_company_name = getModelById('ProductCompany', $request->direct_product_company_id)->name; $product_unloading->warehouse_id = $request->direct_warehouse_id; $product_unloading->product_id = $request->direct_product_id; $product_unloading->product_name = getModelById('Product', $request->direct_product_id)->name; $product_unloading->quantity = $request->direct_quantity; $product_unloading->unit_id = $request->direct_unit_id; $product_unloading->unit_name = getModelById('Unit', $request->direct_unit_id)->unit; $product_unloading->transporter_id = $request->direct_transporter_id; $product_unloading->freight = $request->direct_freight; $product_unloading->invoice_callan_number = $request->direct_invoice_number; if ($request->direct_invoice_date) { $product_unloading->invoice_date = date('Y-m-d', strtotime($date1)); } if ($request->direct_transporter_id) { $product_unloading->transporter_name = getModelById('Transporter', $request->direct_transporter_id)->name; } $product_unloading->truck_number = $request->direct_truck_number; if ($product_unloading->save()) { $unloading_labour_payment = new \App\UnloadingLabourPayment(); $unloading_labour_payment->user_id = $user->id; $unloading_labour_payment->product_unloading_id = $product_unloading->id; $unloading_labour_payment->warehouse_id = $request->direct_warehouse_id; $unloading_labour_payment->product_id = $request->direct_product_id; $unloading_labour_payment->product_name = getModelById('Product', $request->direct_product_id)->name; $unloading_labour_payment->quantity = $request->direct_quantity; $unloading_labour_payment->unit_id = $request->direct_unit_id; $unloading_labour_payment->unit_name = getModelById('Unit', $request->direct_unit_id)->unit; $unloading_labour_payment->labour_name = $request->direct_labour_name; $unloading_labour_payment->rate = $request->direct_labour_rate; $unloading_labour_payment->truck_number = $request->direct_truck_number; $unloading_labour_payment->save(); $inventory = \App\Inventory::where('product_company_id', $request->direct_product_company_id)->where('warehouse_id', $request->direct_warehouse_id)->where('product_brand_id', $request->direct_product_company_id)->where('product_id', $request->direct_product_id)->first(); if (!is_null($inventory)) { $inventory->quantity = $inventory->quantity + $request->direct_quantity; $inventory->save(); } else { $inventory = new \App\Inventory(); $inventory->product_company_id = $request->direct_product_company_id; $inventory->warehouse_id = $request->direct_warehouse_id; $inventory->product_brand_id = $request->direct_product_company_id; $inventory->product_id = $request->direct_product_id; $inventory->unit_id = $request->direct_unit_id; $inventory->quantity = $request->direct_quantity; $inventory->save(); } $response['flag'] = true; $response['message'] = "अनलोडिंग सफलतापूर्वक हो चुकी है !"; $response['unloading_slip_id'] = $product_unloading->id; } else { $response['flag'] = false; $response['message'] = "Something Went Wrong"; } } else { $response['flag'] = false; $response['message'] = "इनवॉइस डेट 5 से अधिक पुरानी नहीं होनी चाहिए $timediff"; } } } } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } return response()->json($response); } public function DirectlabourPaymentSlips(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $date = date('Y-m-d'); if ($request->date) { $date = $request->date; } $labour_slips = \App\DirectLabourPayment::orderBy('id', 'desc')->with('master_rake', 'warehouse')->get(); $response['flag'] = true; $response['labour_slips'] = $labour_slips; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function labourSlips(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if($this->checkApiAuth($request)){ if (1) { $date = date('Y-m-d'); if ($request->date) { $date = $request->date; } $labour_slips = \App\LabourPayments::orderBy('id', 'desc')->whereDate('created_at', '=', date('Y-m-d'))->get(); $response['flag'] = true; $response['labour_slips'] = $labour_slips; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function loadingSlips(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if($this->checkApiAuth($request)){ if (1) { $last_date = date('Y-m-d', strtotime('-5 days')); $loading_slips = \App\ProductLoading::with('token', 'master_rake', 'product:id,name,hindi_name', 'warehouse:id,name,hindi_name', 'labour_payment', 'from_warehouse:id,name,hindi_name', 'retailer:id,name,hindi_name,address', 'userinfo') ->whereDate('created_at', '>=', $last_date) ->orderBy('id', 'desc') ->get(); $response['flag'] = true; $response['loading_slips'] = $loading_slips; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function loadingSlipList(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if($this->checkApiAuth($request)){ if (1) { $date = date('Y-m-d'); if ($request->date) { $date = $request->date; } $open_rake_ids = \App\MasterRake::where('is_closed', 0)->pluck('id'); if (!is_null($open_rake_ids)) { $loading_slips = \App\ProductLoading::select('id', 'transporter_name')->orderBy('id', 'desc') ->whereDate('created_at', '>=', '2019-09-15') ->get(); $response['loading_slips'] = $loading_slips; } $response['flag'] = true; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function unloadingSlips(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $date = date('Y-m-d'); if ($request->date) { $date = $request->date; } $unloading_slips = \App\ProductUnloading::with('token', 'master_rake', 'warehouse', 'unloading_labour_payment')->orderBy('id', 'desc') ->get(); $response['flag'] = true; $response['unloading_slips'] = $unloading_slips; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function loadingSlipDetails(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'loading_slip_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $loading_slip = \App\ProductLoading::with('token', 'master_rake', 'warehouse', 'product', 'labour_payment', 'from_warehouse', 'retailer')->where('id', $request->loading_slip_id)->first(); if (!is_null($loading_slip)) { $response['flag'] = true; if ($loading_slip->loading_slip_type == 1) { $company = \App\Company::where('id', $loading_slip->token->company_id)->first(); $company = $company; } else { $company = null; } $loading_slip->product_company_name = getModelById('ProductCompany', $loading_slip->product_company_id)->brand_name; $loading_slip->slip_generator_name = getModelById('User', $loading_slip->user_id)->name; $loading_slip->company = $company; $response['loading_slip'] = $loading_slip; } else { $response['flag'] = false; $response['message'] = "Invalid Loading Slip Id"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function unloadingSlipDetails(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'unloading_slip_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $loading_slip = \App\ProductUnloading::with('token', 'master_rake', 'product', 'unloading_labour_payment', 'warehouse')->where('id', $request->unloading_slip_id)->first(); if (!is_null($loading_slip)) { $response['flag'] = true; if ($loading_slip->loading_slip_type == 1) { $company = \App\Company::where('id', $loading_slip->token->company_id)->first(); $company = $company; } else { $company = null; } $loading_slip->company = $company; $response['unloading_slip'] = $loading_slip; } else { $response['flag'] = false; $response['message'] = "Invalid Unloading Slip Id"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function labourSlipDetails(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'labour_payment_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $labour_payment = \App\LabourPayments::with('token', 'master_rake', 'product')->where('id', $request->labour_payment_id)->first(); if (!is_null($labour_payment)) { $response['flag'] = true; $response['labour_payment'] = $labour_payment; } else { $response['flag'] = false; $response['message'] = "Invalid Labour Payment Id"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function payLabour(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'labour_payment_id' => 'required', 'paid_amount' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $labour_payment = \App\LabourPayments::with('token', 'master_rake', 'product')->where('id', $request->labour_payment_id)->first(); if (!is_null($labour_payment)) { if ($labour_payment->is_paid) { $response['flag'] = true; $response['message'] = "Payment Already done for this slip"; } else { $user = \App\User::where('api_token', $request->api_token)->first(); $labour_payment->is_paid = 1; $labour_payment->paid_by = $user->id; $labour_payment->paid_amount = $labour_payment->paid_amount + $request->paid_amount; $labour_payment->payment_date = date('Y-m-d H:i:s'); if ($labour_payment->save()) { $response['flag'] = true; $response['message'] = "Payment Details Saved Successfully"; } else { $response['flag'] = false; $response['message'] = "Invalid Labour Slip"; } } } else { $response['flag'] = false; $response['message'] = "Invalid Labour Slip"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } /* * Function to pay freight */ public function payFreight(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'loading_slip_id' => 'required', 'paid_amount' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $product_loading = \App\ProductLoading::find($request->loading_slip_id); if (!is_null($product_loading)) { if ($product_loading->is_freight_paid) { $response['flag'] = true; $response['message'] = "Payment Already done for this slip"; } else { $user = \App\User::where('api_token', $request->api_token)->first(); $product_loading->qr_scan_count = 2; $product_loading->processing_step = 2; $product_loading->is_freight_paid = 1; $product_loading->freight_paid_amount = $request->paid_amount; $product_loading->freight_pay_date = date('Y-m-d H:i:s'); $product_loading->freight_paid_by = $user->id; if ($product_loading->save()) { $response['flag'] = true; $response['message'] = "Freight Paid Successfully."; } else { $response['flag'] = false; $response['error'] = "Something Went Wrong"; } } } else { $response['flag'] = false; $response['message'] = "Loading Slip Not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function directLabourPayment(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'labour_name' => 'required', 'amount' => 'required', 'description' => 'required' ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { if ($request->master_rake_id || $request->warehouse_id) { $user = \App\User::where('api_token', $request->api_token)->first(); $directlabour = new \App\DirectLabourPayment(); $directlabour->master_rake_id = $request->master_rake_id; $directlabour->warehouse_id = $request->warehouse_id; $directlabour->labour_name = $request->labour_name; $directlabour->amount = $request->amount; $directlabour->description = $request->description; $directlabour->user_id = $user->id; if ($directlabour->save()) { $response['flag'] = true; $response['message'] = "Payment Generated Successfully"; $direct_labour_payment = \App\DirectLabourPayment::with('master_rake', 'warehouse')->where('id', $directlabour->id)->first(); $direct_labour_payment->slip_generator_name = getModelById('User', $direct_labour_payment->user_id)->name; $response['direct_labour_payment'] = $direct_labour_payment; } else { $response['flag'] = false; $response['errors'] = "Something Went Wrong"; } } else { $response['flag'] = false; $response['errors'] = "Please Select Rake or Warehouse first"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function directLabourPaymentSlipDetails(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'labour_payment_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $direct_labour_payment = \App\DirectLabourPayment::with('master_rake', 'warehouse')->where('id', $request->labour_payment_id)->first(); if (!is_null($direct_labour_payment)) { $response['flag'] = true; $direct_labour_payment->slip_generator_name = getModelById('User', $direct_labour_payment->user_id)->name; $response['direct_labour_payment'] = $direct_labour_payment; } else { $response['flag'] = false; $response['message'] = "Invalid Labour Payment Id"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function applicationModules(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $date = date('Y-m-d'); if ($request->date) { $date = $request->date; } $modules = \App\ApplicationModule::where('is_active', 1)->get(); $response['flag'] = true; $response['modules'] = $modules; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function parseErrorResponse($errors) { $response = []; foreach ($errors->toArray() as $key => $value) { $obj = new \stdClass(); $obj->message = $value[0]; array_push($response, $obj); } return $response; } public function checkDealerSmSData(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { $companyDis = \App\CompanyDi::where('is_paid', 0)->get(); foreach ($companyDis as $companyDi) { $date_diff = date_diff($companyDi->due_date, date('Y-m-d')); if ($date_diff == 7) { $mobile_number = \App\Dealer::where('id', $companyDi->dealer_id)->first('mobile_number'); $message = "Your Payment is pending from " . $companyDi->due_date . " the amount is " . $companyDi->total . "Please pay it"; $response = SmsController::sendSms($mobile_number, $message); $response['flag'] = true; $response['message'] = $response; } else { $response['flag'] = false; $response['message'] = 'Something Went Wrong'; } } } return response()->json($response); } public function wagonUnloading(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'master_rake_id' => 'required', 'wagon_number' => 'required', 'product_id' => 'required', 'quantity' => 'required', 'labour_name' => 'required', 'labour_rate' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $master_rake = \App\MasterRake::where('id', $request->master_rake_id)->first(); if (!$master_rake->is_closed) { $user = \App\User::where('api_token', $request->api_token)->first(); $wagon_unloading = new \App\WagonUnloading(); $wagon_unloading->master_rake_id = $request->master_rake_id; $wagon_unloading->product_id = $request->product_id; $wagon_unloading->quantity = $request->quantity; $wagon_unloading->wagon_number = $request->wagon_number; $wagon_unloading->labour_name = $request->labour_name; $wagon_unloading->wagon_rate = $request->labour_rate; $wagon_unloading->unloaded_by = $user->id; if ($wagon_unloading->save()) { $response['flag'] = true; $response['message'] = "unloaded successfully"; $response['wagon_unloading_id'] = $wagon_unloading->id; } else { $response['flag'] = false; $response['message'] = "Something Went Wrong"; } } else { $response['flag'] = false; $response['message'] = "रेक बंद हो चुकी है "; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function wagonUnloadings(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if($this->checkApiAuth($request)){ if (1) { $query = \App\WagonUnloading::query(); if ($request->master_rake_id) { $query->where('master_rake_id', $request->master_rake_id); } $wagon_unloadings = $query->select('id', 'master_rake_id', 'product_id', 'wagon_number', 'labour_name', 'wagon_rate', 'quantity', 'unloaded_by', 'is_paid', 'paid_amount')->with('master_rake:id,name', 'product:id,name,hindi_name', 'user:id,name')->get(); $response['flag'] = true; $response['wagon_unloadings'] = $wagon_unloadings; if (count($wagon_unloadings) == 0) { $response['error_image'] = url("assets/mobile/empty_result.png"); } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function wagonUnloadingDetails(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'wagon_unloading_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $wagon_loading = \App\WagonUnloading::with('master_rake', 'product')->where('id', $request->wagon_unloading_id)->first(); if (!is_null($wagon_loading)) { $response['flag'] = true; $wagon_loading->slip_generator_name = getModelById('User', $wagon_loading->unloaded_by)->name; $response['wagon_loading'] = $wagon_loading; } else { $response['flag'] = false; $response['message'] = "Invalid Id"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function rakeTotalTokenLoading(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( // 'api_token' =>'required', 'master_rake_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { $products = array(); $updated_companies = array(); $companies = \App\Company::where('is_active', 1)->get(); foreach ($companies as $company) { $product_ids = \App\MasterRakeProduct::where('master_rake_id', $request->master_rake_id)->pluck('product_id'); $count_data = array(); foreach ($product_ids as $product_id) { $product = array(); $tokens = \App\Token::where('master_rake_id', $request->master_rake_id) ->where('company_id', $company->id) ->where('product_id', $product_id) ->sum('quantity'); $token_ids = \App\Token::where('master_rake_id', $request->master_rake_id) ->where('product_id', $product_id) ->where('company_id', $company->id) ->pluck('id'); $loadings = \App\ProductLoading::where('master_rake_id', $request->master_rake_id) ->whereIn('token_id', $token_ids) ->sum('quantity'); $product['product'] = getModelById('Product', $product_id)->name; $product['product_id'] = $product_id; $product['total_token_quantity'] = $tokens; $product['total_loading_quantity'] = $loadings; array_push($count_data, $product); array_push($products, getModelById('Product', $product_id)->name); } $company->count_data = $count_data; array_push($updated_companies, $company); } $response['flag'] = true; $response['companies'] = $updated_companies; $response['products'] = array_unique($products); } return response()->json($response); } public function getRakeTokens(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'master_rake_id' => 'required', 'company_id' => 'required', 'product_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { $tokens = \App\Token::where('master_rake_id', $request->master_rake_id)->where('company_id', $request->company_id)->where('product_id', $request->product_id)->with('dealer:id,name,address1,hindi_name,hindi_address1', 'product:id,name,hindi_name', 'unit:id,unit,hindi_unit', 'transporter:id,name,hindi_name', 'retailer:id,name,hindi_name')->get(); $response['flag'] = true; $response['tokens'] = $tokens; } return response()->json($response); } public function getRakeLoadings(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'master_rake_id' => 'required', 'company_id' => 'required', 'product_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { $token_ids = \App\Token::where('master_rake_id', $request->master_rake_id)->where('company_id', $request->company_id)->where('product_id', $request->product_id)->pluck('id'); $loadings = \App\ProductLoading::whereIn('token_id', $token_ids)->select('id', 'loading_slip_type', 'product_company_id', 'product_company_name', 'product_id', 'quantity', 'unit_id', 'retailer_id', 'dealer_id', 'warehouse_id', 'is_approved', 'recieved_quantity', 'transporter_id', 'truck_number', 'driver_number')->with('retailer:id,hindi_name,hindi_address', 'dealer:id,hindi_name,hindi_address1', 'product:id,hindi_name', 'unit:id,hindi_unit', 'warehouse:id,name,hindi_name', 'transporter:id,hindi_name', 'product_company:id,hindi_name,hindi_brand_name')->get(); $response['flag'] = true; $response['loadings'] = $loadings; if (count($loadings) == 0) { $response['error_image'] = url("assets/mobile/empty_result.png"); } } return response()->json($response); } public function warehouseTotalTokenLoading(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'from_warehouse_id' => 'required', 'date' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { $products = array(); $updated_companies = array(); $companies = \App\Company::where('is_active', 1)->get(); foreach ($companies as $company) { $product_ids = \App\Token::whereDate('created_at', '=', date('Y-m-d', strtotime($request->date)))->where('from_warehouse_id', $request->from_warehouse_id)->pluck('product_id')->toArray(); $count_data = array(); if (count($product_ids)) { $product_ids = array_unique($product_ids); foreach ($product_ids as $product_id) { $product = array(); $tokens = \App\Token::whereDate('created_at', '=', date('Y-m-d', strtotime($request->date)))->where('from_warehouse_id', $request->from_warehouse_id) ->where('company_id', $company->id) ->where('product_id', $product_id) ->sum('quantity'); $token_ids = \App\Token::whereDate('created_at', '=', date('Y-m-d', strtotime($request->date)))->where('from_warehouse_id', $request->from_warehouse_id) ->where('product_id', $product_id) ->where('company_id', $company->id) ->pluck('id'); $loadings = \App\ProductLoading::whereDate('created_at', '=', date('Y-m-d', strtotime($request->date)))->where('from_warehouse_id', $request->from_warehouse_id) ->whereIn('token_id', $token_ids) ->sum('quantity'); $product['product'] = getModelById('Product', $product_id)->name; $product['product_id'] = $product_id; $product['total_token_quantity'] = $tokens; $product['total_loading_quantity'] = $loadings; array_push($count_data, $product); array_push($products, getModelById('Product', $product_id)->name); } } $company->count_data = $count_data; array_push($updated_companies, $company); } $response['flag'] = true; $response['companies'] = $updated_companies; $response['products'] = array_unique($products); } return response()->json($response); } public function getWarehouseTokens(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'from_warehouse_id' => 'required', 'company_id' => 'required', 'product_id' => 'required', 'date' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { $tokens = \App\Token::whereDate('created_at', '=', date('Y-m-d', strtotime($request->date)))->where('from_warehouse_id', $request->from_warehouse_id)->where('company_id', $request->company_id)->where('product_id', $request->product_id)->with('dealer:id,name,address1,hindi_name,hindi_address1', 'product:id,name,hindi_name', 'unit:id,unit,hindi_unit', 'transporter:id,name,hindi_name', 'retailer:id,name,hindi_name')->get(); $response['flag'] = true; $response['tokens'] = $tokens; } return response()->json($response); } public function getWarehouseLoadings(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'from_warehouse_id' => 'required', 'company_id' => 'required', 'product_id' => 'required', 'date' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { $token_ids = \App\Token::whereDate('created_at', '=', date('Y-m-d', strtotime($request->date)))->where('from_warehouse_id', $request->from_warehouse_id)->where('company_id', $request->company_id)->where('product_id', $request->product_id)->pluck('id'); $loadings = \App\ProductLoading::whereIn('token_id', $token_ids)->select('id', 'loading_slip_type', 'product_company_id', 'product_company_name', 'product_id', 'quantity', 'unit_id', 'retailer_id', 'dealer_id', 'warehouse_id', 'is_approved', 'recieved_quantity', 'transporter_id', 'truck_number', 'driver_number')->with('retailer:id,hindi_name,hindi_address', 'dealer:id,hindi_name,hindi_address1', 'product:id,hindi_name', 'unit:id,hindi_unit', 'warehouse:id,name,hindi_name', 'transporter:id,hindi_name', 'product_company:id,hindi_name,hindi_brand_name')->get(); $response['flag'] = true; $response['loadings'] = $loadings; if (count($loadings) == 0) { $response['error_image'] = url("assets/mobile/empty_result.png"); } } return response()->json($response); } public function rakeTokens(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'master_rake_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { $totals = 0; $rake_tokens = array(); $query = \App\Token::query(); $query->where('token_type', 1)->where('master_rake_id', $request->master_rake_id)->select('id', 'unique_id', 'warehouse_id', 'retailer_id', 'dealer_id', 'rate', 'product_company_id', 'account_from_id', 'product_id', 'quantity', 'unit_id', 'transporter_id')->with('dealer:id,hindi_name,hindi_address1', 'product:id,hindi_name', 'product_company:id,hindi_brand_name', 'unit:id,hindi_unit', 'warehouse:id,hindi_name', 'transporter:id,hindi_name', 'retailer:id,hindi_name,hindi_name'); if ($request->date) { $query->whereDate('created_at', '=', date('Y-m-d', strtotime($request->date))); } if ($request->product_id) { $query->where('product_id', $request->product_id); } if ($request->user_id) { $query->where('warehouse_keeper_id', $request->user_id); } $tokens = $query->orderBy('id', 'desc')->get(); if (count($tokens)) { foreach ($tokens as $token) { $token->total_loadings = \App\ProductLoading::where('token_id', $token->id)->sum('quantity'); $totals = $totals + $token->total_loadings; array_push($rake_tokens, $token); } } $response['flag'] = true; $response['tokens'] = $rake_tokens; $response['totals'] = $totals; if (count($rake_tokens) == 0) { $response['error_image'] = url("assets/mobile/empty_result.png"); } } return response()->json($response); } public function warehouseTokens(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'warehouse_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { $warehouse_tokens = array(); $query = \App\Token::query(); $query->where('token_type', 2)->where('from_warehouse_id', $request->warehouse_id)->select('id', 'unique_id', 'warehouse_id', 'retailer_id', 'dealer_id', 'rate', 'product_company_id', 'account_from_id', 'product_id', 'quantity', 'unit_id', 'transporter_id')->with('dealer:id,hindi_name,hindi_address1', 'product:id,hindi_name', 'product_company:id,hindi_brand_name', 'unit:id,hindi_unit', 'warehouse:id,hindi_name', 'transporter:id,hindi_name', 'retailer:id,hindi_name,hindi_name'); if ($request->date) { $query->whereDate('created_at', '=', date('Y-m-d', strtotime($request->date))); } $tokens = $query->orderBy('id', 'desc')->get(); if (count($tokens)) { foreach ($tokens as $token) { $total_loadings = \App\ProductLoading::where('token_id', $token->id)->sum('quantity'); $token->total_loadings = $total_loadings; if (!is_null($total_loadings)) { array_push($warehouse_tokens, $token); } } } $response['flag'] = true; $response['tokens'] = $warehouse_tokens; if (count($warehouse_tokens) == 0) { $response['error_image'] = url("assets/mobile/empty_result.png"); } } return response()->json($response); } public function tokenLoadings(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'token_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { $loadings = \App\ProductLoading::where('token_id', $request->token_id)->with('dealer:id,hindi_name,hindi_address1', 'product:id,hindi_name', 'unit:id,hindi_unit', 'warehouse:id,hindi_name', 'transporter:id,hindi_name', 'retailer:id,hindi_name,hindi_name')->get(); $response['flag'] = true; $response['loadings'] = $loadings; if (count($loadings) == 0) { $response['error_image'] = url("assets/mobile/empty_result.png"); } } return response()->json($response); } public function rakeUnloadings(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'master_rake_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { $loadings = \App\ProductLoading::whereNotNull('warehouse_id')->where('master_rake_id', $request->master_rake_id)->select('id', 'loading_slip_type', 'product_company_id', 'product_company_name', 'product_id', 'quantity', 'unit_id', 'dealer_id', 'warehouse_id', 'is_approved', 'recieved_quantity', 'transporter_id', 'truck_number', 'driver_number')->with('dealer:id,hindi_name,hindi_address1', 'product:id,hindi_name', 'unit:id,hindi_unit', 'warehouse:id,name,hindi_name', 'transporter:id,hindi_name', 'product_company:id,hindi_name,hindi_brand_name')->get(); $response['flag'] = true; $response['loadings'] = $loadings; if (count($loadings) == 0) { $response['error_image'] = url("assets/mobile/empty_result.png"); } } return response()->json($response); } public function standardization(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'warehouse_id' => 'required', 'open_product_brand_id' => 'required', 'closed_product_brand_id' => 'required', 'open_product_id' => 'required', 'closed_product_id' => 'required', 'open_quantity' => 'required', 'packed_quantity' => 'required', 'labour_name' => 'required', 'labour_rate' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $open_query = \App\Inventory::query(); if ($request->dealer_id) { $open_query->where('dealer_id', $request->dealer_id); } else { $open_query->where('product_company_id', $request->product_company_id); } $open_inventory = $open_query->where('warehouse_id', $request->warehouse_id)->where('product_brand_id', $request->open_product_brand_id)->where('product_id', $request->open_product_id)->first(); if ($open_inventory) { $user = \App\User::where('api_token', $request->api_token)->first(); $standardization = new \App\Standardization(); $standardization->warehouse_id = $request->warehouse_id; $standardization->dealer_id = $request->dealer_id; $standardization->product_company_id = $request->product_company_id; if ($request->product_company_id) { $standardization->open_product_brand_id = $request->product_company_id; } else { $standardization->open_product_brand_id = $request->open_product_brand_id; } $standardization->closed_product_brand_id = $request->closed_product_brand_id; $standardization->open_product_id = $request->open_product_id; $standardization->closed_product_id = $request->closed_product_id; $standardization->open_quantity = $request->open_quantity; $standardization->packed_quantity = $request->packed_quantity; if ($request->shooping_quantity) { $standardization->shooping_quantity = $request->shooping_quantity; } $standardization->labour_name = $request->labour_name; $standardization->labour_rate = $request->labour_rate; $standardization->user_id = $user->id; if ($standardization->save()) { $open_inventory->quantity = $open_inventory->quantity - $request->open_quantity; $open_inventory->save(); $close_stock_query = \App\Inventory::query(); if ($request->dealer_id) { $close_stock_query->where('dealer_id', $request->dealer_id); } else { $close_stock_query->where('product_company_id', $request->product_company_id); } $close_stock_inventory = $close_stock_query->where('warehouse_id', $request->warehouse_id)->where('product_brand_id', $request->closed_product_brand_id)->where('product_id', $request->closed_product_id)->first(); if (!is_null($close_stock_inventory)) { $close_stock_inventory->quantity = $close_stock_inventory->quantity + $request->packed_quantity; $close_stock_inventory->save(); } else { $inventory = new \App\Inventory; if ($request->dealer_id) { $inventory->dealer_id = $request->dealer_id; } else { $inventory->product_company_id = $request->product_company_id; } $inventory->product_brand_id = $request->closed_product_brand_id; $inventory->product_id = $request->closed_product_id; $inventory->warehouse_id = $request->warehouse_id; $inventory->quantity = $request->packed_quantity; $inventory->unit_id = $open_inventory->unit_id; $inventory->save(); } /*------------update Excess-------------*/ if (isset($request->shooping_quantity) && $request->shooping_quantity > 0) { $excess = ($request->packed_quantity + $request->shooping_quantity) - $request->open_quantity; if ($excess > 0) { $excess_inventory = new \App\OtherInventory; $excess_inventory->type = "Excess"; $excess_inventory->dealer_id = 114; $excess_inventory->product_brand_id = $request->closed_product_brand_id; $excess_inventory->product_id = $request->closed_product_id; $excess_inventory->warehouse_id = $request->warehouse_id; $excess_inventory->quantity = $excess; $excess_inventory->unit_id = $open_inventory->unit_id; $excess_inventory->save(); } } /*------------update Excess-------------*/ /*------------update sweeping-------------*/ if ($request->shooping_quantity > 0) { $temp_excess = ($request->packed_quantity + $request->shooping_quantity) - $request->open_quantity; $remining_sweeping = $request->shooping_quantity - $temp_excess; $other_inventory = \App\OtherInventory::where('type', 'sweeping')->where('warehouse_id', $request->warehouse_id)->where('product_brand_id', $request->closed_product_brand_id)->where('product_id', $request->closed_product_id)->first(); if (!is_null($other_inventory)) { $other_inventory->quantity = $other_inventory->quantity + $remining_sweeping; $other_inventory->save(); } else { $other_inventory = new \App\OtherInventory; $other_inventory->type = "sweeping"; if ($request->dealer_id) { $other_inventory->dealer_id = $request->dealer_id; } else { $other_inventory->product_company_id = $request->product_company_id; } $other_inventory->product_brand_id = $request->closed_product_brand_id; $other_inventory->product_id = $request->closed_product_id; $other_inventory->warehouse_id = $request->warehouse_id; $other_inventory->quantity = $remining_sweeping; $other_inventory->unit_id = $open_inventory->unit_id; $other_inventory->save(); } } /*------------update sweeping-------------*/ $response['flag'] = true; $response['message'] = "Done successfully"; $response['standardization_id'] = $standardization->id; } else { $response['flag'] = false; $response['message'] = "Something Went Wrong"; } } else { $response['flag'] = false; $response['message'] = "Selected Party do not have Selected product of selected brand in selected warehouse"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function standardizationDetails(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'standardization_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $standardization = \App\Standardization::with('product', 'warehouse', 'dealer', 'product_company', 'product_brand')->where('id', $request->standardization_id)->first(); if (!is_null($standardization)) { $response['flag'] = true; $response['standardization'] = $standardization; } else { $response['flag'] = false; $response['message'] = "Invalid Id"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function invoice_types(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $invoice_types = \App\InvoiceType::where('is_active', 1)->get(); if (!is_null($invoice_types)) { $response['flag'] = true; $response['invoice_types'] = $invoice_types; } else { $response['flag'] = false; $response['message'] = "No Invoice types"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function loadingSlipInvoices(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { if (isset($request->invoice_type)) { $loading_slip_invoices = \App\LoadingSlipInvoice::select('id', 'invoice_number')->where('invoice_type', $request->invoice_type)->orderBy('id', 'desc')->get(); } else { $loading_slip_invoices = \App\LoadingSlipInvoice::select('id', 'invoice_number')->orderBy('id', 'desc')->get(); } if (!is_null($loading_slip_invoices)) { $response['flag'] = true; $response['loading_slip_invoices'] = $loading_slip_invoices; } else { $response['flag'] = false; $response['message'] = "Invalid Invoice"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function loadingSlipInvoiceDetails(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'invoice_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if($this->checkApiAuth($request)){ if (1) { $invoice = \App\LoadingSlipInvoice::where('id', $request->invoice_id)->select('id', 'invoice_number', 'retailer_id', 'retailer_name', 'dealer_id', 'product_id', 'loading_slip_id')->with('dealer:id,name,hindi_name,address1,hindi_address1', 'product:id,name,hindi_name', 'product_loading:id,transporter_id,unit_id,product_company_id') // ->where('is_paid',1) ->first(); if (!is_null($invoice)) { $response['flag'] = true; $response['invoice'] = $invoice; } else { $response['flag'] = false; $response['message'] = "Invalid Invoice"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function receivedReturnedProduct(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'invoice_number' => 'required', 'retailer_id' => 'required', 'dealer_id' => 'required', 'product_brand_id' => 'required', 'product_id' => 'required', 'returned_quantity' => 'required', 'unit_id' => 'required', 'warehouse_id' => 'required', 'vehicle_number' => 'required', 'labour_name' => 'required', 'labour_rate' => 'required', ), array( 'invoice_number.required' => 'इनवॉइस नंबर चुनें ', 'retailer_id.required' => 'रिटेलर चुनें', 'dealer_id.required' => 'डीलर चुनें', 'product_brand_id.required' => 'प्रोडक्ट ब्रांड चुनें', 'product_id.required' => 'प्रोडक्ट चुनें', 'returned_quantity.required' => 'वापस मात्रा डालें', 'unit_id.required' => 'यूनिट चुनें ', 'warehouse_id.required' => 'वेयरहाउस चुनें', 'vehicle_number.required' => 'ट्रक नंबर डालें', 'labour_name.required' => 'लेबर नाम डालें', 'labour_rate.required' => 'लेबर रेट डालें', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($request->returned_quantity > 0) { $invoice = \App\LoadingSlipInvoice::where('id', $request->invoice_number)->select('id', 'invoice_number', 'retailer_id', 'retailer_name', 'dealer_id', 'product_id', 'loading_slip_id', 'rate', 'secondary_discount', 'freight_discount')->with('product_loading:id,quantity,transporter_id,unit_id,product_company_id') ->first(); if ($request->returned_quantity <= $invoice->product_loading->quantity) { $user = \App\User::where('api_token', $request->api_token)->first(); if (!is_null($user)) { $return_product = new \App\ReturnedProduct(); $return_product->invoice_number = $invoice->invoice_number; $return_product->retailer_id = $request->retailer_id; $return_product->dealer_id = $request->dealer_id; $return_product->product_company_id = $request->product_company_id; $return_product->product_brand_id = $request->product_brand_id; $return_product->product_id = $request->product_id; $return_product->returned_quantity = $request->returned_quantity; $return_product->warehouse_id = $request->warehouse_id; $return_product->vehicle_number = $request->vehicle_number; $return_product->transporter_id = $request->transporter_id; $return_product->unit_id = $invoice->product_loading->unit_id; $return_product->freight = $request->freight; $return_product->labour_name = $request->labour_name; $return_product->labour_rate = $request->labour_rate; $return_product->user_id = $user->id; if ($return_product->save()) { $inventory = \App\Inventory::where('dealer_id', $request->dealer_id)->where('warehouse_id', $request->warehouse_id)->where('product_brand_id', $request->product_brand_id)->where('product_id', $request->product_id)->first(); if (!is_null($inventory)) { $inventory->quantity = $inventory->quantity + $request->returned_quantity; $inventory->save(); } else { $inventory = new \App\Inventory(); $inventory->dealer_id = $request->dealer_id; $inventory->warehouse_id = $request->warehouse_id; $inventory->product_brand_id = $request->product_brand_id; $inventory->product_id = $request->product_id; $inventory->unit_id = $request->unit_id; $inventory->quantity = $request->returned_quantity; $inventory->save(); } $perticular = "Returned Product Credit Against Invoice " . $invoice->invoice_number . " ( " . $invoice->product->name . " ) qty ( " . $request->returned_quantity . " ) "; $rate = $invoice->rate - $invoice->freight_discount; if ($invoice->secondary_discount != "") { $rate = $rate - $invoice->secondary_discount; } // echo $rate; // exit; $return_amount = $request->returned_quantity * $rate; $ledger = new \App\PartyInvoiceLedger(); $ledger->retailer_id = $invoice->retailer_id; $ledger->dealer_id = $invoice->dealer_id; $ledger->particular = $perticular; $ledger->type = 'credit'; $ledger->credit = $return_amount; $ledger->debit = 0; $ledger->against = $invoice->invoice_number; if ($ledger->save()) { $loading_slip_invoice = \App\LoadingSlipInvoice::find($request->invoice_number); if (date('m') <= 3) { $financial_year = (date('Y') - 1) . '-' . date('Y'); } else { $financial_year = date('Y') . '-' . (date('Y') + 1); } $session = \App\Session::where('session', $financial_year)->where('is_active', 1)->first(); if ($loading_slip_invoice == null) { $loading_slip_invoice->is_cn = 1; $loading_slip_invoice->cn_qty = $request->returned_quantity; } else { $loading_slip_invoice->is_cn = 1; $loading_slip_invoice->cn_qty = $loading_slip_invoice->cn_qty + $request->returned_quantity; } $loading_slip_invoice->save(); $credit_history = \DB::table('credit_note_history')->where('dealer_id', $request->dealer_id)->where('session', $session->id)->orderBy('id', 'desc')->first(); if ($credit_history != null) { $series = $credit_history->series + 1; } else { $series = 1; } $cnLastId = new \App\CreditNoteHistory(); $cnLastId->dealer_id = $request->dealer_id; $cnLastId->retailer_id = $request->retailer_id; $cnLastId->loading_slip_invoice_id = $request->invoice_number; $cnLastId->return_id = $return_product->id; $cnLastId->return_qty = $request->returned_quantity; $cnLastId->qty = $invoice->product_loading->quantity; $cnLastId->credit_note_amount = $return_amount; $cnLastId->date = date('Y-m-d'); $cnLastId->series = $series; $cnLastId->session = $session->id; $cnLastId->user_id = $this->AuthId($request)->id; $cnLastId->save(); $ledger->cn_against = $cnLastId->id; $ledger->particular = $perticular . ' #' . $series; // $ledger->particular = 'credit note series-'.$series; $ledger->save(); if ($loading_slip_invoice->secondary_discount > 0) { // $sdPerticular = "Returned Product Credit Against Invoice ".$invoice->invoice_number." ( ".$invoice->product->name." ) qty ( ".$request->returned_quantity." ) "; $sdReturn_amount = $request->returned_quantity * $invoice->secondary_discount; $cnLastId->sd_credit_note_amount = $sdReturn_amount; $cnLastId->save(); $sdLedger = new \App\SecondaryDiscountPartyLedger(); $sdLedger->transaction_date = date('Y-m-d'); $sdLedger->retailer_id = $invoice->retailer_id; $sdLedger->dealer_id = $invoice->dealer_id; $sdLedger->particular = $perticular; $sdLedger->credit = $sdReturn_amount; $sdLedger->debit = 0; $sdLedger->against = $invoice->invoice_number; $sdLedger->cn_against = $cnLastId->id; $sdLedger->particular = $perticular . ' #' . $series; $sdLedger->save(); } } else { $response['flag'] = false; $response['message'] = "Something Went Wrong"; } $response['flag'] = true; $response['message'] = "Done successfully"; $response['return_id'] = $return_product->id; } else { $response['flag'] = false; $response['message'] = "Something Went Wrong"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } else { $response['flag'] = false; $response['message'] = "रिटर्न्ड मात्रा लोडिंग मात्रा (" . $invoice->product_loading->quantity . " ) से ज्यादा नहीं होनी चाहिए"; } } else { $response['flag'] = false; $response['message'] = "वापस मात्रा 0 से ज्यादा होनी चाहिए "; } } return response()->json($response); } public function receivedReturnedProduct_old(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'invoice_number' => 'required', 'retailer_id' => 'required', 'dealer_id' => 'required', 'product_brand_id' => 'required', 'product_id' => 'required', 'returned_quantity' => 'required', 'unit_id' => 'required', 'warehouse_id' => 'required', 'vehicle_number' => 'required', 'labour_name' => 'required', 'labour_rate' => 'required', ), array( 'invoice_number.required' => 'इनवॉइस नंबर चुनें ', 'retailer_id.required' => 'रिटेलर चुनें', 'dealer_id.required' => 'डीलर चुनें', 'product_brand_id.required' => 'प्रोडक्ट ब्रांड चुनें', 'product_id.required' => 'प्रोडक्ट चुनें', 'returned_quantity.required' => 'वापस मात्रा डालें', 'unit_id.required' => 'यूनिट चुनें ', 'warehouse_id.required' => 'वेयरहाउस चुनें', 'vehicle_number.required' => 'ट्रक नंबर डालें', 'labour_name.required' => 'लेबर नाम डालें', 'labour_rate.required' => 'लेबर रेट डालें', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($request->returned_quantity > 0) { $invoice = \App\LoadingSlipInvoice::where('id', $request->invoice_number)->select('id', 'invoice_number', 'retailer_id', 'retailer_name', 'dealer_id', 'product_id', 'loading_slip_id', 'rate')->with('product_loading:id,quantity,transporter_id,unit_id,product_company_id') ->first(); if ($request->returned_quantity <= $invoice->product_loading->quantity) { $user = \App\User::where('api_token', $request->api_token)->first(); if (!is_null($user)) { $return_product = new \App\ReturnedProduct(); $return_product->invoice_number = $invoice->invoice_number; $return_product->retailer_id = $request->retailer_id; $return_product->dealer_id = $request->dealer_id; $return_product->product_company_id = $request->product_company_id; $return_product->product_brand_id = $request->product_brand_id; $return_product->product_id = $request->product_id; $return_product->returned_quantity = $request->returned_quantity; $return_product->warehouse_id = $request->warehouse_id; $return_product->vehicle_number = $request->vehicle_number; $return_product->transporter_id = $request->transporter_id; $return_product->unit_id = $invoice->product_loading->unit_id; $return_product->freight = $request->freight; $return_product->labour_name = $request->labour_name; $return_product->labour_rate = $request->labour_rate; $return_product->user_id = $user->id; if ($return_product->save()) { $inventory = \App\Inventory::where('dealer_id', $request->dealer_id)->where('warehouse_id', $request->warehouse_id)->where('product_brand_id', $request->product_brand_id)->where('product_id', $request->product_id)->first(); if (!is_null($inventory)) { $inventory->quantity = $inventory->quantity + $request->returned_quantity; $inventory->save(); } else { $inventory = new \App\Inventory(); $inventory->dealer_id = $request->dealer_id; $inventory->warehouse_id = $request->warehouse_id; $inventory->product_brand_id = $request->product_brand_id; $inventory->product_id = $request->product_id; $inventory->unit_id = $request->unit_id; $inventory->quantity = $request->returned_quantity; $inventory->save(); } $ledger = \App\PartyInvoiceLedger::where('retailer_id', $invoice->retailer_id)->orderBy('id', 'desc')->first(); $perticular = "Returned Product Credit Against Invoice " . $invoice->invoice_number . " ( " . $invoice->product->name . " ) qty ( " . $request->returned_quantity . " ) "; $return_amount = $request->returned_quantity * $invoice->rate; if (!is_null($ledger)) { $balance = $ledger->balance; $ledger = new \App\PartyInvoiceLedger(); $ledger->retailer_id = $invoice->retailer_id; $ledger->particular = $perticular; $ledger->credit = $return_amount; $ledger->debit = 0; $ledger->balance = $balance - $return_amount; $ledger->against = $invoice->invoice_number; $ledger->save(); } else { $ledger = new \App\PartyInvoiceLedger(); $ledger->retailer_id = $invoice->retailer_id; $ledger->particular = $perticular; $ledger->credit = $return_amount; $ledger->debit = 0; $ledger->balance = $return_amount; $ledger->against = $invoice->invoice_number; $ledger->save(); } $response['flag'] = true; $response['message'] = "Done successfully"; $response['return_id'] = $return_product->id; } else { $response['flag'] = false; $response['message'] = "Something Went Wrong"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } else { $response['flag'] = false; $response['message'] = "रिटर्न्ड मात्रा लोडिंग मात्रा (" . $invoice->product_loading->quantity . " ) से ज्यादा नहीं होनी चाहिए"; } } else { $response['flag'] = false; $response['message'] = "वापस मात्रा 0 से ज्यादा होनी चाहिए "; } } return response()->json($response); } public function returnedProductDetails(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'return_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $returned_product = \App\ReturnedProduct::with('retailer', 'dealer', 'warehouse', 'product', 'product_brand', 'product_company')->where('id', $request->return_id)->first(); if (!is_null($returned_product)) { $response['flag'] = true; $response['returned_product'] = $returned_product; } else { $response['flag'] = false; $response['message'] = "Invalid Id"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function rakeFinancialDetails(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'master_rake_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $user = \App\User::where('api_token', $request->api_token)->first(); $master_rake = \App\MasterRake::where('id', $request->master_rake_id)->first(); if (!is_null($master_rake)) { $totals = array(); $total_tokens['name'] = $user->role_id == 1 ? "Total Token" : 'टोटल टोकन '; $total_tokens['key'] = 'tokens'; $total_tokens['total'] = \App\Token::where('master_rake_id', $request->master_rake_id)->sum('quantity'); $total_loadings['name'] = $user->role_id == 1 ? "Total Loadings" : 'टोटल लोडिंग्स'; $total_loadings['key'] = 'loadings'; $total_loadings['total'] = \App\ProductLoading::where('master_rake_id', $request->master_rake_id)->sum('quantity'); $total_direct_labour_payments = array(); $total_direct_labour_payments['icon'] = URL('/assets/mobile/rupee.png'); $total_direct_labour_payments['name'] = $user->role_id == 1 ? "Total Direct Labour Payment" : 'टोटल डायरेक्ट लेबर पेमेंट्स'; $total_direct_labour_payments['key'] = 'direct_labour_payments'; $total_direct_labour_payments['total'] = \App\DirectLabourPayment::where('master_rake_id', $request->master_rake_id)->sum('amount'); $total_wagon_unloadings = array(); $total_wagon_unloadings['icon'] = URL('/assets/mobile/rupee.png'); $total_wagon_unloadings['name'] = $user->role_id == 1 ? "Total wagon unloading" : "टोटल वैगन अनलोडिंग"; $total_wagon_unloadings['key'] = "wagon_unloadings"; $total_wagon_unloadings['total'] = \App\WagonUnloading::where('master_rake_id', $request->master_rake_id)->sum('paid_amount'); $total_freight_payments = array(); $total_freight_payments['icon'] = URL('/assets/mobile/rupee.png'); $total_freight_payments['name'] = $user->role_id == 1 ? "Total Freight Payment" : "टोटल फ्रेट पेमेंट्स"; $total_freight_payments['key'] = "freight_payments"; $total_freight_payments['total'] = \App\ProductLoading::where('master_rake_id', $request->master_rake_id)->sum('freight_paid_amount'); $total_labour_payments = array(); $total_labour_payments['icon'] = URL('/assets/mobile/rupee.png'); $total_labour_payments['name'] = $user->role_id == 1 ? "Total Labour Payment" : "टोटल लेबर पेमेंट्स"; $total_labour_payments['key'] = "labour_payments"; $total_labour_payments['total'] = \App\LabourPayments::where('master_rake_id', $request->master_rake_id)->sum('paid_amount'); $total_unloading_labour_payments = array(); $total_unloading_labour_payments['icon'] = URL('/assets/mobile/rupee.png'); $total_unloading_labour_payments['name'] = $user->role_id == 1 ? "Unloading Labour Payment" : "अनलोडिंग लेबर पेमेंट्स"; $total_unloading_labour_payments['key'] = "unloading_labour_payments"; $total_unloading_labour_payments['total'] = \App\UnloadingLabourPayment::where('master_rake_id', $request->master_rake_id)->sum('paid_amount'); $total_rr = array(); $total_rr['icon'] = URL('/assets/mobile/wagon.png'); $total_rr['name'] = $user->role_id == 1 ? "RR Quantity" : "RR मात्रा"; $total_rake_quantity = \App\MasterRakeProduct::where('master_rake_id', $master_rake->id)->sum('quantity'); $total_rake_shortage = \App\MasterRakeProduct::where('master_rake_id', $master_rake->id)->sum('shortage_from_company'); $total_product_loadings = \App\ProductLoading::where('master_rake_id', $master_rake->id) ->whereNull('from_warehouse_id') ->sum('quantity'); $excess_shortage = $total_product_loadings - ($total_rake_quantity - $total_rake_shortage); $total_rr['total'] = $total_rake_quantity; $total_wharfage = array(); $total_wharfage['icon'] = URL('/assets/mobile/rupee.png'); $total_wharfage['name'] = $user->role_id == 1 ? "wharfage Charges" : "व्हारफेज चार्जेज "; $total_wharfage['total'] = is_null($master_rake->wharfage) ? 0 : $master_rake->wharfage; $total_demurrage = array(); $total_demurrage['icon'] = URL('/assets/mobile/rupee.png'); $total_demurrage['name'] = $user->role_id == 1 ? "demurrage Charges" : "डेम्रेज चार्जेज"; $total_demurrage['total'] = is_null($master_rake->demurrage) ? 0 : $master_rake->demurrage; $total_expense['name'] = "Total"; $total_expense['icon'] = URL('/assets/mobile/rupee.png'); $total_expense['total'] = $total_direct_labour_payments['total'] + $total_wagon_unloadings['total'] + $total_freight_payments['total'] + $total_labour_payments['total'] + $total_unloading_labour_payments['total'] + $total_wharfage['total'] + $total_demurrage['total'];; array_push($totals, $total_expense); array_push($totals, $total_tokens); array_push($totals, $total_loadings); array_push($totals, $total_direct_labour_payments); array_push($totals, $total_wagon_unloadings); array_push($totals, $total_freight_payments); array_push($totals, $total_labour_payments); array_push($totals, $total_unloading_labour_payments); array_push($totals, $total_rr); array_push($totals, $total_wharfage); array_push($totals, $total_demurrage); $response['flag'] = true; $response['master_rake'] = $master_rake; $response['totals'] = $totals; $response['user'] = $user; } else { $response['flag'] = false; $response['message'] = "Invalid Rake Id"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function rakeSummaryDetails(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'master_rake_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $user = \App\User::where('api_token', $request->api_token)->first(); $master_rake = \App\MasterRake::where('id', $request->master_rake_id)->first(); if (!is_null($master_rake)) { $totals = array(); $totals['total_tokens']['name'] = $user->role_id == 1 ? "Total Token" : 'टोटल टोकन '; $totals['total_tokens']['key'] = 'tokens'; $totals['total_tokens']['total'] = \App\Token::where('master_rake_id', $request->master_rake_id)->sum('quantity'); $totals['total_loadings']['name'] = $user->role_id == 1 ? "Total Loadings" : 'टोटल लोडिंग्स'; $totals['total_loadings']['key'] = 'loadings'; $totals['total_loadings']['total'] = \App\ProductLoading::where('master_rake_id', $request->master_rake_id)->sum('quantity'); $totals['total_direct_labour_payments']['icon'] = URL('/assets/mobile/rupee.png'); $totals['total_direct_labour_payments']['name'] = $user->role_id == 1 ? "Total Direct Labour Payment" : 'टोटल डायरेक्ट लेबर पेमेंट्स'; $totals['total_direct_labour_payments']['key'] = 'direct_labour_payments'; $totals['total_direct_labour_payments']['total'] = \App\DirectLabourPayment::where('master_rake_id', $request->master_rake_id)->sum('amount'); $totals['total_wagon_unloadings']['icon'] = URL('/assets/mobile/rupee.png'); $totals['total_wagon_unloadings']['name'] = $user->role_id == 1 ? "Total wagon unloading" : "टोटल वैगन अनलोडिंग"; $totals['total_wagon_unloadings']['key'] = "wagon_unloadings"; $totals['total_wagon_unloadings']['total'] = \App\WagonUnloading::where('master_rake_id', $request->master_rake_id)->sum('paid_amount'); $totals['total_freight_payments']['icon'] = URL('/assets/mobile/rupee.png'); $totals['total_freight_payments']['name'] = $user->role_id == 1 ? "Total Freight Payment" : "टोटल फ्रेट पेमेंट्स"; $totals['total_freight_payments']['key'] = "freight_payments"; $totals['total_freight_payments']['total'] = \App\ProductLoading::where('master_rake_id', $request->master_rake_id)->sum('freight_paid_amount'); $totals['total_labour_payments']['icon'] = URL('/assets/mobile/rupee.png'); $totals['total_labour_payments']['name'] = $user->role_id == 1 ? "Total Labour Payment" : "टोटल लेबर पेमेंट्स"; $totals['total_labour_payments']['key'] = "labour_payments"; $totals['total_labour_payments']['total'] = \App\LabourPayments::where('master_rake_id', $request->master_rake_id)->sum('paid_amount'); $totals['total_unloading_labour_payments']['icon'] = URL('/assets/mobile/rupee.png'); $totals['total_unloading_labour_payments']['name'] = $user->role_id == 1 ? "Unloading Labour Payment" : "अनलोडिंग लेबर पेमेंट्स"; $totals['total_unloading_labour_payments']['key'] = "unloading_labour_payments"; $totals['total_unloading_labour_payments']['total'] = \App\UnloadingLabourPayment::where('master_rake_id', $request->master_rake_id)->sum('paid_amount'); $totals['total_rr']['icon'] = URL('/assets/mobile/wagon.png'); $totals['total_rr']['name'] = $user->role_id == 1 ? "RR Quantity" : "RR मात्रा"; $total_rake_quantity = \App\MasterRakeProduct::where('master_rake_id', $master_rake->id)->sum('quantity'); $total_rake_shortage = \App\MasterRakeProduct::where('master_rake_id', $master_rake->id)->sum('shortage_from_company'); $total_product_loadings = \App\ProductLoading::where('master_rake_id', $master_rake->id) ->whereNull('from_warehouse_id') ->sum('quantity'); $excess_shortage = $total_product_loadings - ($total_rake_quantity - $total_rake_shortage); $totals['total_rr']['total'] = $total_rake_quantity; $totals['total_wharfage']['icon'] = URL('/assets/mobile/rupee.png'); $totals['total_wharfage']['name'] = $user->role_id == 1 ? "wharfage Charges" : "व्हारफेज चार्जेज "; $totals['total_wharfage']['total'] = is_null($master_rake->wharfage) ? 0 : $master_rake->wharfage; $totals['total_demurrage']['icon'] = URL('/assets/mobile/rupee.png'); $totals['total_demurrage']['name'] = $user->role_id == 1 ? "demurrage Charges" : "डेम्रेज चार्जेज"; $totals['total_demurrage']['total'] = is_null($master_rake->demurrage) ? 0 : $master_rake->demurrage; //$total_expense['name'] = "Total"; //$total_expense['icon'] = URL('/assets/mobile/rupee.png'); //$total_expense['total'] = $total_direct_labour_payments['total'] + $total_wagon_unloadings['total'] + $total_freight_payments['total'] + $total_labour_payments['total'] + $total_unloading_labour_payments['total'] + $total_wharfage['total'] + $total_demurrage['total'];; $response['flag'] = true; $response['master_rake'] = $master_rake; $response['totals'] = $totals; $response['user'] = $user; } else { $response['flag'] = false; $response['message'] = "Invalid Rake Id"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function newRakeSummaryDetails(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'master_rake_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $user = \App\User::where('api_token', $request->api_token)->first(); $master_rake = \App\MasterRake::where('id', $request->master_rake_id)->first(); if (!is_null($master_rake)) { $total_rake_quantity = \App\MasterRakeProduct::where('master_rake_id', $master_rake->id)->sum('quantity'); $total_tokens = \App\Token::where('master_rake_id', $request->master_rake_id)->sum('quantity'); $rake = \App\MasterRake::where('is_active', 1)->where('id', $request->master_rake_id)->with('master_rake_products')->first(); $total_direct_labour_payments = \App\DirectLabourPayment::where('master_rake_id', $request->master_rake_id)->sum('amount'); $total_wagon_unloadings = \App\WagonUnloading::where('master_rake_id', $request->master_rake_id)->sum('paid_amount'); $total_labour_payments = \App\LabourPayments::where('master_rake_id', $request->master_rake_id)->sum('paid_amount'); $total_unloading_labour_payments = \App\UnloadingLabourPayment::where('master_rake_id', $request->master_rake_id)->sum('paid_amount'); $total_freight_payments = \App\ProductLoading::where('master_rake_id', $request->master_rake_id)->sum('freight_paid_amount'); $total_expenses = $total_direct_labour_payments + $total_labour_payments + $total_unloading_labour_payments + $total_wagon_unloadings + $total_freight_payments + $rake->demurrage + $rake->wharfage; $total_loaded_bags = \App\ProductLoading::where('master_rake_id', $request->master_rake_id)->sum('quantity'); $stock_moved_to_warehouse = \App\ProductLoading::where('master_rake_id', $request->master_rake_id)->where('loading_slip_type', 2)->sum('quantity'); $total_loading_done = \DB::table('product_unloadings')->where('master_rake_id', $request->master_rake_id)->sum('quantity'); $total_sale_quantity = \App\Token::where('master_rake_id', $request->master_rake_id)->where('to_type', 2)->sum('quantity'); $total_sale_price = \App\Token::select('rate')->where('master_rake_id', $request->master_rake_id)->where('to_type', 2)->groupBy('rate')->get(); $count_sale = \App\Token::where('master_rake_id', $request->master_rake_id)->where('to_type', 2)->count('rate'); $tSalePrice = 0; foreach ($total_sale_price as $value) { $tSalePrice = $tSalePrice + $value->rate; } if (count($total_sale_price) != 0 && $tSalePrice != 0) { // $average_rate_of_sale = $total_sale_price/$total_sale_quantity; $average_rate_of_sale = $tSalePrice / count($total_sale_price); } else { $average_rate_of_sale = 0; } $totals = array(); $totals['total_rr']['name'] = $user->role_id == 1 ? "Total RR" : 'टोटल RR '; $totals['total_rr']['key'] = 'total_rr'; $totals['total_rr']['total'] = $total_rake_quantity; $totals['total_tokens']['name'] = $user->role_id == 1 ? "Total Token" : 'टोटल टोकन '; $totals['total_tokens']['key'] = 'total_tokens'; $totals['total_tokens']['total'] = $total_tokens; $totals['total_loaded_bags']['name'] = $user->role_id == 1 ? "Total Loaded Bags" : 'लोडेड बैग'; $totals['total_loaded_bags']['key'] = 'total_loaded_bags'; $totals['total_loaded_bags']['total'] = $total_loaded_bags; $totals['total_loading_done']['name'] = $user->role_id == 1 ? "Total Loading Done" : 'लोडिंग हो चुकी है'; $totals['total_loading_done']['key'] = 'total_loading_done'; $totals['total_loading_done']['total'] = $total_loading_done; $totals['stock_moved_to_warehouse']['name'] = $user->role_id == 1 ? "Stock Moved to warehouse (direct)" : 'वेयरहाउस पहुँचा हुआ स्टॉक (डायरेक्ट)'; $totals['stock_moved_to_warehouse']['key'] = 'stock_moved_to_warehouse'; $totals['stock_moved_to_warehouse']['total'] = $stock_moved_to_warehouse; $totals['average_rate_of_sale']['name'] = $user->role_id == 1 ? "Average Rate of Sale" : 'बिक्री की औसत दर'; $totals['average_rate_of_sale']['key'] = 'average_rate_of_sale'; $totals['average_rate_of_sale']['total'] = round($average_rate_of_sale, 2); $totals['total_expenses']['name'] = $user->role_id == 1 ? "Total Expenses" : 'कुल खर्च'; $totals['total_expenses']['key'] = 'total_expenses'; $totals['total_expenses']['total'] = $total_expenses; $response['flag'] = true; $response['totals'] = $totals; } else { $response['flag'] = false; $response['message'] = "Invalid Rake Id"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function tokenListRake(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'master_rake_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { // $tokens = \DB::table('tokens')->join('transporters', 'transporters.id', 'tokens.transporter_id')->join('users', 'users.id', 'tokens.user_id')->join('products', 'products.id', 'tokens.product_id')->join('retailers', 'retailers.id', 'tokens.retailer_id')->where('tokens.is_active', 1)->where('tokens.master_rake_id', $request->master_rake_id)->select('tokens.*', 'retailers.name as retailer_name', 'products.name as product_name', 'users.name as token_generate_by', 'transporters.name as transporter_name')->get(); $tokens = \App\Token::where('master_rake_id', $request->master_rake_id)->get(); $response['flag'] = true; $response['tokens'] = $tokens; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function rakeFinancialReport(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'master_rake_id' => 'required', 'type' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if($this->checkApiAuth($request)){ if (1) { $master_rake = \App\MasterRake::where('id', $request->master_rake_id)->first(); if (!is_null($master_rake)) { if ($request->type == "direct_labour_payments") { $response['direct_labour_payments'] = \App\DirectLabourPayment::where('master_rake_id', $request->master_rake_id)->select('labour_name', 'description', 'is_paid', 'paid_amount')->get(); } else if ($request->type == "wagon_unloadings") { $response['wagon_unloadings'] = \App\WagonUnloading::where('master_rake_id', $request->master_rake_id)->select('wagon_number', 'quantity', 'labour_name', 'wagon_rate', 'is_paid', 'paid_amount')->get(); } else if ($request->type == "freight_payments") { $response['freight_payments'] = \App\ProductLoading::where('master_rake_id', $request->master_rake_id)->select('transporter_name', 'truck_number', 'product_name', 'product_id', 'recieved_quantity', 'unit_name', 'is_freight_paid', 'freight_paid_amount')->with('product:id,hindi_name')->get(); } else if ($request->type == "labour_payments") { $response['labour_payments'] = \App\LabourPayments::where('master_rake_id', $request->master_rake_id)->select('labour_name', 'product_name', 'product_id', 'quantity', 'unit_name', 'is_paid', 'paid_amount')->with('product:id,hindi_name')->get(); } else if ($request->type == "unloading_labour_payments") { $response['unloading_labour_payments'] = \App\UnloadingLabourPayment::where('master_rake_id', $request->master_rake_id)->select('labour_name', 'product_name', 'product_id', 'quantity', 'unit_name', 'is_paid', 'paid_amount')->with('product:id,hindi_name')->get(); } } else { $response['flag'] = false; $response['message'] = "Invalid Rake Id"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function warehouseFinancialDetails(Request $request) { $response = array(); $internals = Faker\Factory::create('en_US'); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'warehouse_id' => 'required', 'date' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if($this->checkApiAuth($request)){ if (1) { $warehouse = \App\Warehouse::where('id', $request->warehouse_id)->first(); if (!is_null($warehouse)) { $totals = array(); $total_direct_labour_payments = array(); $total_direct_labour_payments['icon'] = URL('/assets/mobile/rupee.png'); $total_direct_labour_payments['name'] = 'टोटल डायरेक्ट लेबर पेमेंट्स'; $total_direct_labour_payments['key'] = 'direct_labour_payments'; $total_direct_labour_payments['total'] = \App\DirectLabourPayment::where('warehouse_id', $request->warehouse_id)->where('is_paid', 1)->whereDate('created_at', '=', date('Y-m-d', strtotime($request->date)))->sum('amount'); $total_freight_payments = array(); $total_freight_payments['icon'] = URL('/assets/mobile/rupee.png'); $total_freight_payments['name'] = "टोटल फ्रेट पेमेंट्स"; $total_freight_payments['key'] = 'freight_payments'; $total_freight_payments['total'] = \App\ProductLoading::where('from_warehouse_id', $request->warehouse_id)->where('is_freight_paid', 1)->whereDate('created_at', '=', date('Y-m-d', strtotime($request->date)))->sum('freight_paid_amount'); $total_labour_payments = array(); $total_labour_payments['icon'] = URL('/assets/mobile/rupee.png'); $total_labour_payments['name'] = "टोटल लेबर पेमेंट्स"; $total_labour_payments['key'] = 'labour_payments'; $total_labour_payments['total'] = \App\LabourPayments::where('from_warehouse_id', $request->warehouse_id)->where('is_paid', 1)->whereDate('created_at', '=', date('Y-m-d', strtotime($request->date)))->sum('paid_amount'); $total_unloading_labour_payments = array(); $total_unloading_labour_payments['icon'] = URL('/assets/mobile/rupee.png'); $total_unloading_labour_payments['name'] = "अनलोडिंग लेबर पेमेंट्स"; $total_unloading_labour_payments['key'] = 'unloading_labour_payments'; $total_unloading_labour_payments['total'] = \App\UnloadingLabourPayment::where('warehouse_id', $request->warehouse_id)->where('is_paid', 1)->whereDate('created_at', '=', date('Y-m-d', strtotime($request->date)))->sum('paid_amount'); array_push($totals, $total_direct_labour_payments); array_push($totals, $total_freight_payments); array_push($totals, $total_labour_payments); array_push($totals, $total_unloading_labour_payments); $response['flag'] = true; $response['warehouse'] = $warehouse; $response['totals'] = $totals; } else { $response['flag'] = false; $response['message'] = "Invalid Warehouse Id"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function warehouseFinancialReport(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'warehouse_id' => 'required', 'type' => 'required', 'date' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if($this->checkApiAuth($request)){ if (1) { $warehouse = \App\Warehouse::where('id', $request->warehouse_id)->first(); if (!is_null($warehouse)) { if ($request->type == "direct_labour_payments") { $response['direct_labour_payments'] = \App\DirectLabourPayment::where('warehouse_id', $request->warehouse_id)->select('labour_name', 'description', 'is_paid', 'paid_amount')->whereDate('created_at', '=', date('Y-m-d', strtotime($request->date)))->get(); } else if ($request->type == "freight_payments") { $response['freight_payments'] = \App\ProductLoading::where('from_warehouse_id', $request->warehouse_id)->where('is_freight_paid', 1)->select('transporter_name', 'truck_number', 'product_name', 'product_id', 'recieved_quantity', 'unit_name', 'freight_paid_amount')->whereDate('created_at', '=', date('Y-m-d', strtotime($request->date)))->with('product:id,hindi_name')->get(); } else if ($request->type == "labour_payments") { $response['labour_payments'] = \App\LabourPayments::where('from_warehouse_id', $request->warehouse_id)->whereDate('created_at', '=', date('Y-m-d', strtotime($request->date)))->select('labour_name', 'product_name', 'product_id', 'quantity', 'unit_name', 'is_paid', 'paid_amount')->with('product:id,hindi_name')->get(); } else if ($request->type == "unloading_labour_payments") { $response['unloading_labour_payments'] = \App\UnloadingLabourPayment::where('warehouse_id', $request->warehouse_id)->whereDate('created_at', '=', date('Y-m-d', strtotime($request->date)))->select('labour_name', 'product_name', 'product_id', 'quantity', 'unit_name', 'is_paid', 'paid_amount')->with('product:id,hindi_name')->get(); } } else { $response['flag'] = false; $response['message'] = "Invalid Rake Id"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function WarehouseTransferLoading(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'product_brand_id' => 'required', 'product_id' => 'required', 'quantity' => 'required', 'unit_id' => 'required', 'from_warehouse_id' => 'required', 'to_warehouse_id' => 'required|different:from_warehouse_id', 'transporter_id' => 'required', 'truck_number' => 'required', // 'freight' =>'required', 'labour_name' => 'required', 'labour_rate' => 'required', ), array( 'api_token.required' => 'सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें ', 'product_brand_id.required' => 'प्रोडक्ट ब्रांड चुनें', 'product_id.required' => 'प्रोडक्ट चुनें', 'quantity.required' => 'वापस मात्रा डालें', 'unit_id.required' => 'यूनिट चुनें ', 'from_warehouse_id.required' => 'फ्रॉम वेयरहाउस चुनें', 'to_warehouse_id.required' => 'टू वेयरहाउस चुनें', 'to_warehouse_id.different' => 'दोनों गोडाउन अलग अलग होना चाहिए', 'transporter_id.required' => 'ट्रांसपोर्टर चुनें', 'truck_number.required' => 'ट्रक नंबर डालें', // 'freight.required'=>'फ्रेट डालें', 'labour_name.required' => 'लेबर नाम डालें', 'labour_rate.required' => 'लेबर रेट डालें', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $query = \App\Inventory::where('warehouse_id', $request->from_warehouse_id) ->where('product_id', $request->product_id) ->where('product_brand_id', $request->product_brand_id) ->where('unit_id', $request->unit_id); $inventory = $query->first(); $total_stock = $query->sum('quantity'); if (!is_null($inventory)) { if ($total_stock >= $request->quantity) { $user = \App\User::where('api_token', $request->api_token)->first(); $warehouse_transfer_loading = new \App\WarehouseTransferLoading(); $warehouse_transfer_loading->product_brand_id = $request->product_brand_id; $warehouse_transfer_loading->product_id = $request->product_id; $warehouse_transfer_loading->quantity = $request->quantity; $warehouse_transfer_loading->unit_id = $request->unit_id; $warehouse_transfer_loading->from_warehouse_id = $request->from_warehouse_id; $warehouse_transfer_loading->to_warehouse_id = $request->to_warehouse_id; $warehouse_transfer_loading->transporter_id = $request->transporter_id; $warehouse_transfer_loading->freight = $request->freight ?? 0.00; $warehouse_transfer_loading->truck_number = $request->truck_number; $warehouse_transfer_loading->labour_name = $request->labour_name; $warehouse_transfer_loading->labour_rate = $request->labour_rate; $warehouse_transfer_loading->user_id = $user->id; if ($warehouse_transfer_loading->save()) { $response['flag'] = true; $response['message'] = "ट्रांसफर सफलतापूर्वक हो चूका है"; $response['warehouse_transfer_loading_id'] = $warehouse_transfer_loading->id; } else { $response['flag'] = false; $response['message'] = "कुछ गलत हुआ है ! कृपया दुबारा से प्रयास करें "; } } else { $response['flag'] = false; $response['message'] = "डाली गयी मात्रा (" . $request->quantity . ") गोडाउन स्टॉक (" . $total_stock . ") से ज्यादा नहीं होनी चाहिए "; } } else { $response['flag'] = false; $response['message'] = getModelById('Warehouse', $request->from_warehouse_id)->hindi_name . " में " . getModelById('Product', $request->product_id)->hindi_name . " का कोई स्टॉक नहीं है "; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function WarehouseTransferLoadingList(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if($this->checkApiAuth($request)){ if (1) { $warehouse_transfer_loadings = \App\WarehouseTransferLoading::select("id")->get(); $response['flag'] = true; $response['warehouse_transfer_loadings'] = $warehouse_transfer_loadings; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function warehouseTransferLoadings(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'loading_warehouse_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if($this->checkApiAuth($request)){ if (1) { $query = \App\WarehouseTransferLoading::query(); if ($request->unloading_warehouse_id) { $query->where('to_warehouse_id', $request->unloading_warehouse_id); } if ($request->date) { $query->whereDate('created_at', '=', date('Y-m-d', strtotime($request->date))); } $warehouse_transfer_unloadings = $query->where('from_warehouse_id', $request->loading_warehouse_id)->select("id", "from_warehouse_id", "to_warehouse_id", "product_brand_id", "product_id", "quantity", "unit_id", "transporter_id", "truck_number", "labour_name", "labour_rate", "is_approved")->with('product:id,name,hindi_name', 'unit:id,unit', 'from_warehouse:id,hindi_name', 'to_warehouse:id,hindi_name', 'transporter:id,hindi_name', 'product_brand:id,hindi_brand_name')->get(); $response['flag'] = true; $response['warehouse_transfer_unloadings'] = $warehouse_transfer_unloadings; if (count($warehouse_transfer_unloadings) == 0) { $response['error_image'] = url("assets/mobile/empty_result.png"); } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function warehouseTransferLoadingDetails(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'warehouse_transfer_loading_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if($this->checkApiAuth($request)){ if (1) { $warehouse_transfer_loading = \App\WarehouseTransferLoading::where('id', $request->warehouse_transfer_loading_id)->select("id", "from_warehouse_id", "to_warehouse_id", "product_brand_id", "product_id", "quantity", "unit_id", "transporter_id", "truck_number", "labour_name", "labour_rate")->with('product:id,name,hindi_name', 'unit:id,unit', 'from_warehouse:id,hindi_name', 'to_warehouse:id,hindi_name', 'transporter:id,hindi_name', 'product_brand:id,hindi_brand_name')->first(); if (!is_null($warehouse_transfer_loading)) { $response['flag'] = true; $response['warehouse_transfer'] = $warehouse_transfer_loading; } else { $response['flag'] = false; $response['message'] = "ऐसा कोई ट्रांसफर नहीं हुआ है|"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function WarehouseTransferUnloading(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'product_brand_id' => 'required', 'warehouse_transfer_loading_id' => 'required', 'product_id' => 'required', 'quantity' => 'required', 'unit_id' => 'required', 'from_warehouse_id' => 'required', 'to_warehouse_id' => 'required|different:from_warehouse_id', 'labour_name' => 'required', 'labour_rate' => 'required', ), array( 'api_token.required' => 'सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें ', 'warehouse_transfer_loading_id.required' => 'वेयरहाउस लोडिंग चुनें', 'product_brand_id.required' => 'प्रोडक्ट ब्रांड चुनें', 'product_id.required' => 'प्रोडक्ट चुनें', 'quantity.required' => 'वापस मात्रा डालें', 'unit_id.required' => 'यूनिट चुनें ', 'from_warehouse_id.required' => 'फ्रॉम वेयरहाउस चुनें', 'to_warehouse_id.required' => 'टू वेयरहाउस चुनें', 'to_warehouse_id.different' => 'दोनों गोडाउन अलग अलग होना चाहिए', 'labour_name.required' => 'लेबर नाम डालें', 'labour_rate.required' => 'लेबर रेट डालें', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $warehouse_transfer_loading = \App\WarehouseTransferLoading::where('id', $request->warehouse_transfer_loading_id)->first(); if (!is_null($warehouse_transfer_loading)) { if ($warehouse_transfer_loading->is_approved == 0) { $query = \App\Inventory::where('warehouse_id', $warehouse_transfer_loading->from_warehouse_id) ->where('product_id', $warehouse_transfer_loading->product_id) ->where('product_brand_id', $warehouse_transfer_loading->product_brand_id) ->where('unit_id', $warehouse_transfer_loading->unit_id); $inventory = $query->first(); $total_stock = $query->sum('quantity'); if (!is_null($inventory)) { if ($total_stock >= $request->quantity) { $higher_source_stock = \App\Inventory::where('warehouse_id', $warehouse_transfer_loading->from_warehouse_id) ->where('product_id', $warehouse_transfer_loading->product_id) ->where('product_brand_id', $warehouse_transfer_loading->product_brand_id) ->where('unit_id', $warehouse_transfer_loading->unit_id) ->where('quantity', '>=', $request->quantity) ->first(); if (!is_null($higher_source_stock)) { $higher_source_stock->quantity = $higher_source_stock->quantity - $request->quantity; $higher_source_stock->save(); $destination_stock_query = \App\Inventory::query(); $destination_stock_query->where('warehouse_id', $request->to_warehouse_id) ->where('product_id', $warehouse_transfer_loading->product_id) ->where('product_brand_id', $warehouse_transfer_loading->product_brand_id) ->where('unit_id', $warehouse_transfer_loading->unit_id) ->where('quantity', '>=', $request->quantity); if (!is_null($higher_source_stock->dealer_id)) { $destination_stock_query->where('dealer_id', $higher_source_stock->dealer_id); } else { $destination_stock_query->where('product_company_id', $higher_source_stock->product_company_id); } $destination_stock = $destination_stock_query->first(); if (!is_null($destination_stock)) { $destination_stock->quantity = $destination_stock->quantity + $request->quantity; $destination_stock->save(); } else { $destination_stock = new \App\Inventory(); if (!is_null($higher_source_stock->dealer_id)) { $destination_stock->dealer_id = $higher_source_stock->dealer_id; } else { $destination_stock->product_company_id = $higher_source_stock->product_company_id; } $destination_stock->warehouse_id = $request->to_warehouse_id; $destination_stock->product_brand_id = $warehouse_transfer_loading->product_brand_id; $destination_stock->product_id = $warehouse_transfer_loading->product_id; $destination_stock->quantity = $request->quantity; $destination_stock->unit_id = $warehouse_transfer_loading->unit_id; $destination_stock->save(); } } else { $stocks = \App\Inventory::where('warehouse_id', $warehouse_transfer_loading->from_warehouse_id) ->where('product_id', $warehouse_transfer_loading->product_id) ->where('product_brand_id', $warehouse_transfer_loading->product_brand_id) ->where('unit_id', $warehouse_transfer_loading->unit_id) ->where('quantity', '>', 0) ->orderBy('quantity', 'desc') ->get(); $remaining_quantity = $request->quantity; $i = 0; foreach ($stocks as $stock) { if ($remaining_quantity > 0) { if ($i == 0) { $transfer_quantity = $stock->quantity; } else { $transfer_quantity = $remaining_quantity; } $stock->quantity = $stock->quantity - $transfer_quantity; $stock->save(); $destination_stock_query = \App\Inventory::query(); $destination_stock_query->where('warehouse_id', $request->to_warehouse_id) ->where('product_id', $warehouse_transfer_loading->product_id) ->where('product_brand_id', $warehouse_transfer_loading->product_brand_id) ->where('unit_id', $warehouse_transfer_loading->unit_id) ->where('quantity', '>=', $transfer_quantity); if (!is_null($stock->dealer_id)) { $destination_stock_query->where('dealer_id', $stock->dealer_id); } else { $destination_stock_query->where('product_company_id', $stock->product_company_id); } $destination_stock = $destination_stock_query->first(); if (!is_null($destination_stock)) { $destination_stock->quantity = $destination_stock->quantity + $transfer_quantity; $destination_stock->save(); $remaining_quantity = $remaining_quantity - $transfer_quantity; } else { $destination_stock = new \App\Inventory(); if (!is_null($stock->dealer_id)) { $destination_stock->dealer_id = $stock->dealer_id; } else { $destination_stock->product_company_id = $stock->product_company_id; } $destination_stock->warehouse_id = $request->to_warehouse_id; $destination_stock->product_brand_id = $warehouse_transfer_loading->product_brand_id; $destination_stock->product_id = $warehouse_transfer_loading->product_id; $destination_stock->quantity = $transfer_quantity; $destination_stock->unit_id = $warehouse_transfer_loading->unit_id; $destination_stock->save(); $remaining_quantity = $remaining_quantity - $transfer_quantity; } } $i++; } } $user = \App\User::where('api_token', $request->api_token)->first(); $warehouse_transfer_unloading = new \App\WarehouseTransferUnloading(); $warehouse_transfer_unloading->warehouse_transfer_loading_id = $warehouse_transfer_loading->id; $warehouse_transfer_unloading->product_brand_id = $warehouse_transfer_loading->product_brand_id; $warehouse_transfer_unloading->product_id = $warehouse_transfer_loading->product_id; $warehouse_transfer_unloading->quantity = $request->quantity; $warehouse_transfer_unloading->unit_id = $warehouse_transfer_loading->unit_id; $warehouse_transfer_unloading->from_warehouse_id = $warehouse_transfer_loading->from_warehouse_id; $warehouse_transfer_unloading->to_warehouse_id = $request->to_warehouse_id; $warehouse_transfer_unloading->labour_name = $request->labour_name; $warehouse_transfer_unloading->labour_rate = $request->labour_rate; $warehouse_transfer_unloading->user_id = $user->id; if ($warehouse_transfer_unloading->save()) { $warehouse_transfer_loading->is_approved = 1; $warehouse_transfer_loading->received_quantity = $request->quantity; $warehouse_transfer_loading->save(); $response['flag'] = true; $response['message'] = "ट्रांसफर सफलतापूर्वक हो चूका है"; $response['warehouse_transfer_unloading_id'] = $warehouse_transfer_unloading->id; } else { $response['flag'] = false; $response['message'] = "कुछ गलत हुआ है ! कृपया दुबारा से प्रयास करें "; } } else { $response['flag'] = false; $response['message'] = "डाली गयी मात्रा (" . $request->quantity . ") गोडाउन स्टॉक (" . $total_stock . ") से ज्यादा नहीं होनी चाहिए "; } } else { $response['flag'] = false; $response['message'] = getModelById('Warehouse', $request->from_warehouse_id)->hindi_name . " में " . getModelById('Product', $request->product_id)->hindi_name . " का कोई स्टॉक नहीं है "; } } else { $response['flag'] = false; $response['message'] = "यह लोडिंग पहले ही अनलोड हो चुकी है"; } } else { $response['flag'] = false; $response['message'] = "कृपया सही वेयरहाउस लोडिंग चुनें"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function warehouseTransferUnloadingDetails(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'warehouse_transfer_unloading_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if($this->checkApiAuth($request)){ if (1) { $warehouse_transfer_unloading = \App\WarehouseTransferUnloading::where('id', $request->warehouse_transfer_unloading_id)->select("id", "warehouse_transfer_loading_id", "from_warehouse_id", "to_warehouse_id", "product_brand_id", "product_id", "quantity", "unit_id", "labour_name", "labour_rate")->with('warehouse_transfer_loading:id,truck_number', 'product:id,name,hindi_name', 'unit:id,unit', 'from_warehouse:id,hindi_name', 'to_warehouse:id,hindi_name', 'product_brand:id,hindi_brand_name')->first(); if (!is_null($warehouse_transfer_unloading)) { $response['flag'] = true; $response['warehouse_transfer'] = $warehouse_transfer_unloading; } else { $response['flag'] = false; $response['message'] = "ऐसा कोई ट्रांसफर नहीं हुआ है|"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function warehouseTransferUnloadingList(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if($this->checkApiAuth($request)){ if (1) { $warehouse_transfer_unloading = \App\WarehouseTransferUnloading::select("id")->get(); $response['flag'] = true; $response['warehouse_transfer'] = $warehouse_transfer_unloading; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function warehouseTransferUnloadings(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'unloading_warehouse_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if($this->checkApiAuth($request)){ if (1) { $query = \App\WarehouseTransferUnloading::query(); if ($request->loading_warehouse_id) { $query->where('from_warehouse_id', $request->loading_warehouse_id); } if ($request->date) { $query->whereDate('created_at', '=', date('Y-m-d', strtotime($request->date))); } $warehouse_transfer_unloadings = $query->where('to_warehouse_id', $request->unloading_warehouse_id)->select("id", "warehouse_transfer_loading_id", "from_warehouse_id", "to_warehouse_id", "product_brand_id", "product_id", "quantity", "unit_id", "labour_name", "labour_rate")->with('warehouse_transfer_loading:id,truck_number', 'product:id,name,hindi_name', 'unit:id,unit', 'from_warehouse:id,hindi_name', 'to_warehouse:id,hindi_name', 'product_brand:id,hindi_brand_name')->get(); $response['flag'] = true; $response['warehouse_transfer_unloadings'] = $warehouse_transfer_unloadings; if (count($warehouse_transfer_unloadings) == 0) { $response['error_image'] = url("assets/mobile/empty_result.png"); } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function dailySalesPurchase(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { // if(1){ if ($request->date) { $date = date('Y-m-d', strtotime($request->date)); } else { $date = date('Y-m-d'); } $sales = array(); //$unique_sale_parties = \App\LoadingSlipInvoice::select('dealer_id')->whereDate('created_at', '=', date('Y-m-d', strtotime($date)))->orderBy('id', 'desc')->get(); $unique_sale_parties = \App\LoadingSlipInvoice::whereDate('created_at', '=', date('Y-m-d', strtotime($date)))->orderBy('id', 'desc')->get(); if (!is_null($unique_sale_parties)) { foreach ($unique_sale_parties as $unique_sale_party) { //$unique_products = \App\LoadingSlipInvoice::select('product_id')->where('dealer_id', $unique_sale_party->dealer_id)->whereDate('created_at', '=', date('Y-m-d', strtotime($date)))->orderBy('id', 'desc')->distinct()->get(); //foreach ($unique_products as $unique_product) { $tempArr = array(); $tempArr['party'] = getModelById('Dealer', $unique_sale_party->dealer_id)->name; $tempArr['product'] = getModelById('Product', $unique_sale_party->product_id)->name; $tempArr['total_sale_quantity'] = "$unique_sale_party->quantity"; $tempArr['total_sale_amount'] = $unique_sale_party->total; $tempArr['rate'] = $unique_sale_party->rate; $tempArr['unit'] = "$unique_sale_party->unit"; $tempArr['retailer'] = "$unique_sale_party->retailer_name"; //$tempArr['total_sale_quantity'] = \App\LoadingSlipInvoice::where('product_id', $unique_product->product_id)->where('dealer_id', $unique_sale_party->dealer_id)->whereDate('created_at', '=', date('Y-m-d', strtotime($date)))->orderBy('id', 'desc')->quantity; //$tempArr['total_sale_amount'] = \App\LoadingSlipInvoice::where('product_id', $unique_product->product_id)->where('dealer_id', $unique_sale_party->dealer_id)->whereDate('created_at', '=', date('Y-m-d', strtotime($date)))->orderBy('id', 'desc')->total; array_push($sales, $tempArr); //} } } $purchase = array(); $unique_purchase_c_companies = \App\CompanyDi::select('product_company_id')->whereDate('invoice_date', '=', date('Y-m-d', strtotime($date)))->orderBy('id', 'desc')->distinct()->get(); $unique_purchase_w_companies = \App\WarehouseDi::select('product_company_id')->where('transfer_type', 1)->whereDate('invoice_date', '=', date('Y-m-d', strtotime($date)))->orderBy('id', 'desc')->distinct()->get(); $unique_purchase_w_parties = \App\WarehouseDi::select('from_dealer_id')->where('transfer_type', 2)->whereDate('invoice_date', '=', date('Y-m-d', strtotime($date)))->orderBy('id', 'desc')->distinct()->get(); if (!is_null($unique_purchase_c_companies)) { foreach ($unique_purchase_c_companies as $unique_purchase_c_company) { $unique_products = \App\CompanyDi::select('product_id')->where('product_company_id', $unique_purchase_c_company->product_company_id)->whereDate('invoice_date', '=', date('Y-m-d', strtotime($date)))->orderBy('id', 'desc')->distinct()->get(); foreach ($unique_products as $unique_product) { $tempArr = array(); $tempArr['party'] = getModelById('ProductCompany', $unique_purchase_c_company->product_company_id)->name; $tempArr['product'] = getModelById('Product', $unique_product->product_id)->name; $tempArr['total_purchase_quantity'] = \App\CompanyDi::where('product_id', $unique_product->product_id)->where('product_company_id', $unique_purchase_c_company->product_company_id)->whereDate('invoice_date', '=', date('Y-m-d', strtotime($date)))->orderBy('id', 'desc')->sum('quantity'); $tempArr['total_purchase_amount'] = \App\CompanyDi::where('product_id', $unique_product->product_id)->where('product_company_id', $unique_purchase_c_company->product_company_id)->whereDate('invoice_date', '=', date('Y-m-d', strtotime($date)))->orderBy('id', 'desc')->sum('total'); array_push($purchase, $tempArr); } } } if (!is_null($unique_purchase_w_companies)) { foreach ($unique_purchase_w_companies as $unique_company) { $unique_products = \App\WarehouseDi::select('product_id')->where('product_company_id', $unique_company->product_company_id)->whereDate('invoice_date', '=', date('Y-m-d'))->orderBy('id', 'desc')->distinct()->get(); foreach ($unique_products as $unique_product) { $tempArr = array(); $tempArr['party'] = getModelById('ProductCompany', $unique_company->product_company_id)->name; $tempArr['product'] = getModelById('Product', $unique_product->product_id)->name; $tempArr['total_purchase_quantity'] = \App\WarehouseDi::where('product_id', $unique_product->product_id)->where('product_company_id', $unique_company->product_company_id)->whereDate('invoice_date', '=', date('Y-m-d'))->orderBy('id', 'desc')->sum('quantity'); $tempArr['total_purchase_amount'] = \App\WarehouseDi::where('product_id', $unique_product->product_id)->where('product_company_id', $unique_company->product_company_id)->whereDate('invoice_date', '=', date('Y-m-d'))->orderBy('id', 'desc')->sum('total'); array_push($purchase, $tempArr); } } } if (!is_null($unique_purchase_w_parties)) { foreach ($unique_purchase_w_parties as $unique_purchase_party) { $unique_products = \App\WarehouseDi::select('product_id')->where('from_dealer_id', $unique_purchase_party->from_dealer_id)->whereDate('invoice_date', '=', date('Y-m-d'))->orderBy('id', 'desc')->distinct()->get(); foreach ($unique_products as $unique_product) { $tempArr = array(); $tempArr['party'] = getModelById('Dealer', $unique_purchase_party->from_dealer_id)->name; $tempArr['product'] = getModelById('Product', $unique_product->product_id)->name; $tempArr['total_purchase_quantity'] = \App\WarehouseDi::where('product_id', $unique_product->product_id)->where('from_dealer_id', $unique_purchase_party->from_dealer_id)->whereDate('invoice_date', '=', date('Y-m-d'))->orderBy('id', 'desc')->sum('quantity'); $tempArr['total_purchase_amount'] = \App\WarehouseDi::where('product_id', $unique_product->product_id)->where('from_dealer_id', $unique_purchase_party->from_dealer_id)->whereDate('invoice_date', '=', date('Y-m-d'))->orderBy('id', 'desc')->sum('total'); array_push($purchase, $tempArr); } } } $response['flag'] = true; $response['sales'] = $sales; $response['purchase'] = $purchase; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function dailyWarehousePaymentReports(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if($this->checkApiAuth($request)){ if (1) { $daily_expense_reports = \App\DailyWarehouseExpenseReport::with('user:id,name')->get(); $response['flag'] = true; $response['daily_expense_reports'] = $daily_expense_reports; if (count($daily_expense_reports) == 0) { $response['error_image'] = url("assets/mobile/empty_result.png"); } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function rakePaymentReports(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if($this->checkApiAuth($request)){ if (1) { $daily_expense_reports = \App\RakeExpenseReport::with('user:id,name')->get(); $response['flag'] = true; $response['daily_expense_reports'] = $daily_expense_reports; if (count($daily_expense_reports) == 0) { $response['error_image'] = url("assets/mobile/empty_result.png"); } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function getPartyStock(Request $request) { try { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'status' => 'required', 'id' => 'required' ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if(1){ if ($this->checkApiAuth($request)) { $cement_product_ids = \App\Product::where('product_category_id', 2)->pluck('id'); // dd($cement_product_ids); $response['cement_stock'] = \App\Inventory::where($request->status, $request->id)->whereIn('product_id', $cement_product_ids)->sum('quantity'); $response['dap_stock'] = \App\Inventory::where($request->status, $request->id)->where('product_id', 4)->sum('quantity'); $response['npk_stock'] = \App\Inventory::where($request->status, $request->id)->whereIn('product_id', array(9, 10, 11, 12, 65, 103))->sum('quantity'); $response['urea_stock'] = \App\Inventory::where($request->status, $request->id)->where('product_id', 19)->sum('quantity'); $response['mop_stock'] = \App\Inventory::where($request->status, $request->id)->where('product_id', 7)->sum('quantity'); } return response()->json($response); } } //catch exception catch (Exception $e) { echo 'Message: ' . $e->getMessage(); } } public function approveRakeExpenseReport(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'report_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if($this->checkApiAuth($request)){ if (1) { $daily_expense_report = \App\RakeExpenseReport::where('id', $request->report_id)->first(); if (!is_null($daily_expense_report)) { $daily_expense_report->first_approval = 0; $daily_expense_report->second_approval = 0; $daily_expense_report->third_approval = 0; $daily_expense_report->final_approval = 1; $daily_expense_report->save(); if ($daily_expense_report->save()) { $response['flag'] = true; $response['message'] = "Report Approved Successfully "; } else { $response['flag'] = false; $response['message'] = "Something Went Wrong "; } } else { $response['flag'] = false; $response['message'] = "Report Not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function approveDailyWarehouseExpenseReport(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'report_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if($this->checkApiAuth($request)){ if (1) { $daily_expense_report = \App\DailyWarehouseExpenseReport::where('id', $request->report_id)->first(); if (!is_null($daily_expense_report)) { $daily_expense_report->first_approval = 0; $daily_expense_report->second_approval = 0; $daily_expense_report->third_approval = 0; $daily_expense_report->final_approval = 1; $daily_expense_report->save(); if ($daily_expense_report->save()) { $response['flag'] = true; $response['message'] = "Report Approved Successfully "; } else { $response['flag'] = false; $response['message'] = "Something Went Wrong "; } } else { $response['flag'] = false; $response['message'] = "Report Not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function rejectReport(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'type' => 'required', 'report_id' => 'required', 'reason' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['message'] = "Required parameters missing"; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $user = \App\User::where('api_token', $request->api_token)->first(); $report_rejections = \App\ReportRejection::where('report_id', $request->report_id)->first(); if (is_null($report_rejections)) { $report_rejections = new \App\ReportRejection(); $report_rejections->type = $request->type; $report_rejections->report_id = $request->report_id; $report_rejections->reason = $request->reason; $report_rejections->rejected_by = $user->id; if ($report_rejections->save()) { if ($request->type == "daily-warehouse-payments") { $daily_expense_report = \App\DailyWarehouseExpenseReport::where('id', $request->report_id)->first(); if (!is_null($daily_expense_report)) { $daily_expense_report->first_approval = 0; $daily_expense_report->second_approval = 0; $daily_expense_report->third_approval = 0; $daily_expense_report->final_approval = 0; $daily_expense_report->save(); } } else if ($request->type == "rake-payments") { $daily_expense_report = \App\RakeExpenseReport::where('id', $request->report_id)->first(); if (!is_null($daily_expense_report)) { $daily_expense_report->first_approval = 0; $daily_expense_report->second_approval = 0; $daily_expense_report->third_approval = 0; $daily_expense_report->final_approval = 0; $daily_expense_report->save(); } } $response['flag'] = true; $response['message'] = "Report Rejected Successfully "; } else { $response['flag'] = false; $response['message'] = "Something Went Wrong "; } } else { $response['flag'] = false; $response['message'] = "Already Rejected"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function expenseListRake(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'master_rake_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $rake = \App\MasterRake::where('is_active', 1)->where('id', $request->master_rake_id)->with('master_rake_products')->first(); $total_direct_labour_payments = \App\DirectLabourPayment::where('master_rake_id', $request->master_rake_id)->sum('amount'); $total_wagon_unloadings = \App\WagonUnloading::where('master_rake_id', $request->master_rake_id)->sum('paid_amount'); $total_freight_payments = \App\ProductLoading::where('master_rake_id', $request->master_rake_id)->sum('freight_paid_amount'); $total_labour_payments = \App\LabourPayments::where('master_rake_id', $request->master_rake_id)->sum('paid_amount'); $total_unloading_labour_payments = \App\UnloadingLabourPayment::where('master_rake_id', $request->master_rake_id)->sum('paid_amount'); $rate_id = $request->master_rake_id; $sql = "SELECT sum(freight_paid_amount) as warehouse_freight FROM `product_loadings` WHERE master_rake_id=" . $rate_id . " and warehouse_id != '' and is_freight_paid=1"; $sql1 = "SELECT sum(freight_paid_amount) as party_freight FROM `product_loadings` WHERE master_rake_id=" . $rate_id . " and retailer_id != '' and is_freight_paid=1"; $result = \DB::select($sql); $result1 = \DB::select($sql1); $total_expenses = $total_direct_labour_payments + $total_labour_payments + $total_unloading_labour_payments + $total_wagon_unloadings + $total_freight_payments + $rake->demurrage + $rake->wharfage; $data['direct_expence'] = $total_direct_labour_payments; $data['freight_payments'] = $total_freight_payments; $data['wagon_unloadings'] = $total_wagon_unloadings; $data['warehouse_unloading_labour_payments'] = $total_unloading_labour_payments; $data['labour_payments'] = $total_labour_payments; $data['demurrage_charges'] = $rake->demurrage; $data['wharfage_charges'] = $rake->wharfage; $data['party_freight'] = $result1[0]->party_freight; $data['warehouse_freight'] = $result[0]->warehouse_freight; $data['total'] = $total_expenses; $response['flag'] = true; $response['data'] = $data; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function freightPaymentInfo(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'master_rake_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $rate_id = $request->master_rake_id; $sql = "SELECT sum(freight_paid_amount) as warehouse_freight FROM `product_loadings` WHERE master_rake_id=" . $rate_id . " and warehouse_id != '' and is_freight_paid=1"; $sql1 = "SELECT sum(freight_paid_amount) as party_freight FROM `product_loadings` WHERE master_rake_id=" . $rate_id . " and retailer_id != '' and is_freight_paid=1"; $result = \DB::select($sql); $result1 = \DB::select($sql1); $data['party_freight'] = $result1[0]->party_freight; $data['warehouse_freight'] = $result[0]->warehouse_freight; $response['flag'] = true; $response['data'] = $data; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function tokenCompanyWise(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'company_id' => 'required', 'report_date' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $sql = "select company_id,date_of_generation,sum(product_total_qty) as total_qty,sum(total_price) as price, (t1.total_price/t1.product_total_qty) as avg_rate_of_sale,t1.product_category_id, cat.category, comp.name from (SELECT tokens.company_id, tokens.product_id,tokens.date_of_generation, sum(tokens.quantity) as product_total_qty,tokens.rate, sum(tokens.quantity*tokens.rate) as total_price, products.product_category_id FROM tokens join products on products.id=tokens.product_id WHERE tokens.date_of_generation='" . $request->report_date . "' and tokens.company_id=" . $request->company_id . " GROUP BY tokens.product_id) as t1 join product_categories as cat on cat.id = t1.product_category_id join companies as comp on comp.id = t1.company_id GROUP BY t1.product_category_id"; $tokens = \DB::select($sql); $data['tokens'] = $tokens; $response['flag'] = true; $response['data'] = $data; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function tokenOfCompanyProductsWise(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'company_id' => 'required', // 'product_category_id' => 'required', // 'report_date' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $cement_product_ids = \App\Product::where('product_category_id', 2)->pluck('id'); // $cement_product_ids=implode(',', $cement_product_ids); $cement_product_ids = json_encode($cement_product_ids, true); $cement_product_ids = json_decode($cement_product_ids, true); $cement_product_ids = implode(', ', $cement_product_ids); // print_r(implode(', ', $cement_product_ids)); // exit; if ($request->product_category_id && $request->report_date) { $sql = "select t1.*, (t1.total_price/t1.product_total_qty) as avg_rate_of_sale from (SELECT tokens.company_id, tokens.product_id,tokens.date_of_generation, sum(tokens.quantity) as product_total_qty,tokens.rate, sum(tokens.quantity*tokens.rate) as total_price, products.product_category_id, products.name FROM tokens join products on products.id=tokens.product_id WHERE tokens.date_of_generation='" . $request->report_date . "' and tokens.company_id= " . $request->company_id . " and products.product_category_id = " . $request->product_category_id . " GROUP BY tokens.product_id) as t1"; } else { if ($request->status == 'dealer_id') { $status = 'account_from_id'; } else { $status = $request->status; } $sql = "select t1.*, (t1.total_price/t1.product_total_qty) as avg_rate_of_sale from (SELECT tokens.company_id, tokens.product_id,tokens.date_of_generation, sum(tokens.quantity) as product_total_qty,tokens.rate, sum(tokens.quantity*tokens.rate) as total_price, products.product_category_id, products.name FROM tokens join products on products.id=tokens.product_id WHERE " . $status . "=" . $request->company_id . " and tokens.product_id IN(" . $cement_product_ids . ") GROUP BY tokens.product_id) as t1"; } $tokens = \DB::select($sql); $data['tokens'] = $tokens; $response['flag'] = true; $response['data'] = $data; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function dealer_list(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $data[0]['name'] = 'MRSD-STP'; $data[0]['company_id'] = '1'; $data[0]['dealer_id'] = '1'; $data[1]['name'] = 'MRSD-LRP'; $data[1]['company_id'] = '1'; $data[1]['dealer_id'] = '3'; $data[2]['name'] = 'MCPL-STP'; $data[2]['company_id'] = '1'; $data[2]['dealer_id'] = '30'; $data[3]['name'] = 'MCPL-DEVI KALI RD'; $data[3]['company_id'] = '1'; $data[3]['dealer_id'] = '31'; $data[4]['name'] = 'CFCL'; $data[4]['company_id'] = '2'; $data[4]['dealer_id'] = '4'; $data[5]['name'] = 'IPL'; $data[5]['company_id'] = '2'; $data[5]['dealer_id'] = '5'; $data[6]['name'] = 'PPL'; $data[6]['company_id'] = '2'; $data[6]['dealer_id'] = '6'; $response['flag'] = true; $response['data'] = $data; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function product_stock_list(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'company_id' => 'required', 'dealer_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { if ($request->company_id == 1) { $dealer_id = $request->dealer_id; $sql = "select t1.product_category_id as category_id, sum(t1.quantity) as stocks,t1.category as name, t1.product_id from (SELECT i.quantity, p.name,pc.category, p.product_category_id, i.product_id FROM inventories as i join products as p on p.id=i.product_id join product_categories as pc on pc.id = p.product_category_id WHERE i.dealer_id=" . $dealer_id . " ) as t1 GROUP by t1.product_category_id"; } else { $company_id = $request->dealer_id; $sql = "select t1.product_category_id as category_id, sum(t1.quantity) as stocks,t1.category as name, t1.product_id from (SELECT i.quantity, p.name,pc.category, p.product_category_id, i.product_id FROM inventories as i join products as p on p.id=i.product_id join product_categories as pc on pc.id = p.product_category_id WHERE i.product_company_id=" . $company_id . " ) as t1 GROUP by t1.product_category_id"; } $stocks = \DB::select($sql); $response['flag'] = true; $response['data'] = $stocks; $response['company_id'] = $request->company_id; $response['dealer_id'] = $request->dealer_id; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function company_product_stock_list(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'company_id' => 'required', 'dealer_id' => 'required', 'category_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $cat_id = $request->category_id; if ($request->company_id == 1) { $dealer_id = $request->dealer_id; $sql = "select t1.product_category_id as category_id, sum(t1.quantity) as stocks,t1.name , t1.product_id from (SELECT i.quantity,i.product_company_id, p.name,pc.category, p.product_category_id, i.product_id FROM inventories as i join products as p on p.id=i.product_id join product_categories as pc on pc.id = p.product_category_id WHERE i.dealer_id=" . $dealer_id . " and pc.id = " . $cat_id . ") as t1 GROUP by t1.product_id"; } else { $company_id = $request->dealer_id; $sql = "select t1.product_category_id as category_id, sum(t1.quantity) as stocks,t1.name , t1.product_id from (SELECT i.quantity,i.product_company_id, p.name,pc.category, p.product_category_id, i.product_id FROM inventories as i join products as p on p.id=i.product_id join product_categories as pc on pc.id = p.product_category_id WHERE i.product_company_id=" . $company_id . " and pc.id = " . $cat_id . ") as t1 GROUP by t1.product_id"; } $stocks = \DB::select($sql); $response['flag'] = true; $response['data'] = $stocks; $response['company_id'] = $request->company_id; $response['dealer_id'] = $request->dealer_id; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function company_product_in_warehouse(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'product_id' => 'required', 'category_id' => 'required', 'company_id' => 'required', 'dealer_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $product_id = $request->product_id; $cat_id = $request->category_id; if ($request->company_id == 1) { $dealer_id = $request->dealer_id; $sql = "select sum(t1.quantity) as stocks,t1.name , t1.product_id, t1.warehouse_id from (SELECT i.quantity,i.product_company_id,i.warehouse_id, w.name, p.product_category_id, i.product_id FROM inventories as i join products as p on p.id=i.product_id join warehouses as w on w.id = i.warehouse_id WHERE i.dealer_id=" . $dealer_id . " and p.product_category_id = " . $cat_id . " and i.product_id = " . $product_id . ") as t1 GROUP BY t1.warehouse_id"; } else { $company_id = $request->dealer_id; $sql = "select sum(t1.quantity) as stocks,t1.name , t1.product_id, t1.warehouse_id from (SELECT i.quantity,i.product_company_id,i.warehouse_id, w.name, p.product_category_id, i.product_id FROM inventories as i join products as p on p.id=i.product_id join warehouses as w on w.id = i.warehouse_id WHERE i.product_company_id=" . $company_id . " and p.product_category_id = " . $cat_id . " and i.product_id = " . $product_id . ") as t1 GROUP BY t1.warehouse_id"; } $stocks = \DB::select($sql); $response['flag'] = true; $response['data'] = $stocks; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } /*----------2nd Phase New Api 2022-------*/ /*---------Location -----------*/ public function save_latlng(Request $request) { // echo "hello"; // exit; $response = array(); $validator = \Validator::make( $request->all(), array( 'user_id' => 'required', 'lat' => 'required', 'lng' => 'required', 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $user = \App\User::where(['id' => $request->user_id, 'is_active' => 1])->first(); if (!is_null($user)) { $checkLatLng = \App\TrackUser::where('user_id', $request->user_id)->whereDate('created_at', date('Y-m-d'))->orderBy('id', 'desc')->first(); if (!is_null($checkLatLng)) { if (($checkLatLng->lat == $request->lat && $checkLatLng->lng == $request->lng) && ($checkLatLng->status == $request->gps_status)) { $checkLatLng->batterylevel = $request->bat_level; $checkLatLng->save(); $this->attendance_report($request->user_id, $request->lat, $request->lng); $response['flag'] = true; $response['success'] = 'done'; } elseif ($checkLatLng->status != $request->gps_status) { if ($request->lat != '0.0' && $request->lng != '0.0') { $track_user = new \App\TrackUser(); $track_user->user_id = $request->user_id; $track_user->lat = $request->lat; $track_user->lng = $request->lng; $track_user->batterylevel = $request->bat_level; $track_user->status = $request->gps_status; $track_user->save(); $this->attendance_report($request->user_id, $request->lat, $request->lng); $response['flag'] = true; $response['success'] = 'done'; } } else { $meter = $this->calculateDistanceBetweenTwoPoints($checkLatLng->lat, $checkLatLng->lng, $request->lat, $request->lng, 'MT'); if ($meter >= 10) { if ($request->lat != '0.0' && $request->lng != '0.0') { $track_user = new \App\TrackUser(); $track_user->user_id = $request->user_id; $track_user->lat = $request->lat; $track_user->lng = $request->lng; $track_user->batterylevel = $request->bat_level; $track_user->status = $request->gps_status; $track_user->save(); $this->attendance_report($request->user_id, $request->lat, $request->lng); $response['flag'] = true; $response['success'] = 'done'; } } if ($meter < 10) { if ($request->lat != '0.0' && $request->lng != '0.0') { $checkLatLng->batterylevel = $request->bat_level; $checkLatLng->updated_at = date('Y-m-d H:i:s'); $checkLatLng->save(); $this->attendance_report($request->user_id, $request->lat, $request->lng); $response['flag'] = true; $response['success'] = 'done'; } } } } else { if ($request->lat != '0.0' && $request->lng != '0.0') { $track_user = new \App\TrackUser(); $track_user->user_id = $request->user_id; $track_user->lat = $request->lat; $track_user->lng = $request->lng; $track_user->batterylevel = $request->bat_level; $track_user->status = $request->gps_status; $track_user->save(); $this->attendance_report($request->user_id, $request->lat, $request->lng); $response['flag'] = true; $response['success'] = 'done'; } //$this->get_callforajax(); } } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function attendance_report($userId, $lat, $lng) { $latlngs = DB::table('addlocation')->get()->toArray(); $todayDate = date('Y-m-d'); foreach($latlngs as $latlng){ $ltlg = explode(',',$latlng->ltlong); $radius = $this->calculateDistanceBetweenTwoPoints($ltlg[0],$ltlg[1], $lat, $lng, 'MT'); if ($radius < 60) { $checkAtt = \App\Location::where('user_id', $userId)->orderBy('id', 'desc')->first(); if (is_null($checkAtt)) { $attendance = new \App\Location(); $attendance->user_id = $userId; $attendance->location_id = $latlng->id; $attendance->in_time_lat = $lat; $attendance->in_time_lng = $lng; $attendance->in_time = date('Y-m-d H:i:s'); $attendance->in_count = 1; $attendance->in_time_location_range = $radius . " " . "Meter"; $attendance->attendance_status = 1; $attendance->save(); } else { if($checkAtt->out_count == 1) { $attendance = new \App\Location(); $attendance->user_id = $userId; $attendance->location_id = $latlng->id; $attendance->in_time_lat = $lat; $attendance->in_time_lng = $lng; $attendance->in_time = date('Y-m-d H:i:s'); $attendance->in_count = 1; $attendance->in_time_location_range = $radius . " " . "Meter"; $attendance->attendance_status = 1; $attendance->save(); }else{ $checkLatLng = \App\TrackUser::where('user_id', $userId)->whereDate('created_at','!=', date('Y-m-d'))->orderBy('id', 'desc')->first(); $checkdate = date('Y-m-d', strtotime($checkLatLng->created_at)); if($checkdate != date('Y-m-d')){ $checkAtts = \App\Location::where('user_id', $userId)->whereDate('created_at','!=', date('Y-m-d'))->orderBy('id', 'desc')->first(); $checkLatLngs = \App\TrackUser::where('user_id', $userId)->whereDate('created_at', date('Y-m-d',strtotime($checkAtts->in_time)))->orderBy('id', 'desc')->first(); if($checkAtts->out_count == 0){ $inTime1 = date_create($checkAtts->in_time); $outTime2 = date_create($checkLatLngs->created_at); $diff = date_diff($inTime1, $outTime2); $checkAtts->hours_stay = $diff->format("%H:%i:%s"); $checkAtts->out_time_lat = $checkLatLngs->lat; $checkAtts->out_time_lng = $checkLatLngs->lng; $checkAtts->out_time = $checkLatLngs->updated_at; $checkAtts->out_count = 1; $checkAtts->out_time_location_range = $radius . " " . "Meter"; $checkAtts->save(); } } if($checkAtt->out_count == 1) { $attendance = new \App\Location(); $attendance->user_id = $userId; $attendance->location_id = $latlng->id; $attendance->in_time_lat = $lat; $attendance->in_time_lng = $lng; $attendance->in_time = date('Y-m-d H:i:s'); $attendance->in_count = 1; $attendance->in_time_location_range = $radius . " " . "Meter"; $attendance->attendance_status = 1; $attendance->save(); } } } } if ($radius >= 60) { $checkAtt = \App\Location::where('user_id', $userId)->where('out_count', 0)->where('location_id',$latlng->id)->orderBy('id', 'desc')->first(); if (!is_null($checkAtt)) { $checkAtt->out_time_lat = $lat; $checkAtt->out_time_lng = $lng; $checkAtt->out_time = date("Y-m-d H:i:s"); $checkAtt->out_count = 1; $checkAtt->out_time_location_range = $radius . " " . "Meter"; $inTime1 = date_create($checkAtt->in_time); $outTime2 = date_create(date("Y-m-d H:i:s")); $diff = date_diff($inTime1, $outTime2); $checkAtt->hours_stay = $diff->format("%H:%i:%s"); $checkAtt->save(); } } } } public function offline_save_latlng(Request $request) { //logger($request->all()); $response = array(); $validator = \Validator::make( $request->all(), array( // 'user_id' => 'required', // 'lat' => 'required', // 'lng' => 'required', 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $offline_data = $request->offline_data; $parsedData = json_decode($offline_data, true); foreach ($parsedData['data'] as $record) { // echo 'User: ' . $record['User']; // echo 'Time: ' . $record['Time']; // echo 'Latitude: ' . $record['Lat']; // echo 'Longitude: ' . $record['Lon']; // echo 'GPS: ' . $record['GPS']; // echo 'Battery: ' . $record['Bat']; $user = \App\User::where(['id' => $record['User'], 'is_active' => 1])->first(); if (!is_null($user)) { $checkLatLng = \App\TrackUser::where('user_id', $record['User'])->orderBy('id', 'desc')->first(); if (!is_null($checkLatLng)) { if (($checkLatLng->lat == $record['Lat'] && $checkLatLng->lng == $record['Lon']) && ($checkLatLng->status == $record['GPS'])) { $checkLatLng->batterylevel = $record['Bat']; $checkLatLng->save(); $this->attendance_report($record['User'], $record['Lat'], $record['Lon']); } elseif ($checkLatLng->status != $record['GPS']) { if ($record['Lat'] != '0.0' && $record['Lon'] != '0.0') { $track_user = new \App\TrackUser(); $track_user->user_id = $record['User']; $track_user->lat = $record['Lat']; $track_user->lng = $record['Lon']; $track_user->batterylevel = $record['Bat']; $track_user->status = $record['GPS']; $track_user->offline_status = 1; $track_user->created_at = $record['Time']; $track_user->save(); $this->attendance_report($record['User'], $record['Lat'], $record['Lon']); } } else { $meter = $this->calculateDistanceBetweenTwoPoints($checkLatLng->lat, $checkLatLng->lng, $record['Lat'], $record['Lon'], 'MT'); if ($meter >= 2) { if ($record['Lat'] != '0.0' && $record['Lon'] != '0.0') { $track_user = new \App\TrackUser(); $track_user->user_id = $record['User']; $track_user->lat = $record['Lat']; $track_user->lng = $record['Lon']; $track_user->batterylevel = $record['Bat']; $track_user->status = $record['GPS']; $track_user->offline_status = 1; $track_user->created_at = $record['Time']; $track_user->save(); $this->attendance_report($record['User'], $record['Lat'], $record['Lon']); $response['flag'] = true; $response['success'] = 'done'; } } if ($meter < 2) { if ($record['Lat'] != '0.0' && $record['Lon'] != '0.0') { $checkLatLng->batterylevel = $record['Bat']; $checkLatLng->status = $record['GPS']; $checkLatLng->save(); $this->attendance_report($record['User'], $record['Lat'], $record['Lon']); $response['flag'] = true; $response['success'] = 'done'; } } } } else { if ($record['Lat'] != '0.0' && $record['Lon'] != '0.0') { $track_user = new \App\TrackUser(); $track_user->user_id = $record['User']; $track_user->lat = $record['Lat']; $track_user->lng = $record['Lon']; $track_user->batterylevel = $record['Bat']; $track_user->status = $record['GPS']; $track_user->offline_status = 1; $track_user->created_at = $record['Time']; $track_user->save(); $this->attendance_report($record['User'], $record['Lat'], $record['Lon']); $response['flag'] = true; $response['success'] = 'done'; } //$this->get_callforajax(); } } } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } /*---------End Location -----------*/ /*--------- Field Collection-------*/ public function addPaymentByCash(Request $request) { // dd($request->all()); $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'retailer_id' => 'required', 'dealer_id' => 'required', // 'mode' =>'required', 'amount' => 'required', 'multipals_cheque_no' => 'required', 'location' => 'required', // 'user_id' =>'required', //'transaction_date' =>'required', ) ); // dd($this->dateConverter($request->transaction_date)); // dd(\App\BankStatement::where('type','payment')->orderBy('id','desc')->first()); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $user = \App\User::where('api_token', $request->api_token)->first(); $dealer_id = $request->dealer_id; $retailer_id = $request->retailer_id; $mode = $request->mode; $amount = $request->amount; //$transaction_date = $request->transaction_date; $multipals_cheque_no = $request->multipals_cheque_no; $location = $request->location; $user_id = $user->id; $reference_no = $request->reference_no; $addPayment = new \App\ReceivePayment(); $addPayment->dealer_id = $dealer_id; $addPayment->retailer_id = $retailer_id; $addPayment->mode = $mode; $addPayment->amount = $amount; //$addPayment->transaction_date=$transaction_date; $addPayment->multipals_cheque_no = $multipals_cheque_no; $addPayment->reference_no = $reference_no; $addPayment->location = $location; $addPayment->user_id = $user_id; if ($addPayment->save()) { $response['flag'] = true; $response['message'] = "Payment Added Successfully !!"; } else { $response['flag'] = false; $response['message'] = "Something Wrong!!"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function addPaymentByCheque(Request $request) { // dd($request->all()); $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'retailer_id' => 'required', 'dealer_id' => 'required', // 'mode' =>'required', 'amount' => 'required', 'multipals_cheque_no' => 'required', 'location' => 'required', // 'user_id' =>'required', //'transaction_date' =>'required', ) ); // dd($this->dateConverter($request->transaction_date)); // dd(\App\BankStatement::where('type','payment')->orderBy('id','desc')->first()); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $user = \App\User::where('api_token', $request->api_token)->first(); $retailer_id = $request->retailer_id; $dealer_id = $request->dealer_id; $mode = $request->mode; $amount = $request->amount; $multipals_cheque_no = $request->multipals_cheque_no; $location = $request->location; //$transaction_date = $request->transaction_date; $user_id = $user->id; $reference_no = $request->reference_no; $addPayment = new \App\ReceivePayment(); $addPayment->retailer_id = $retailer_id; $addPayment->dealer_id = $dealer_id; $addPayment->mode = 'cheque'; //$addPayment->transaction_date=$transaction_date; $addPayment->amount = $amount; $addPayment->multipals_cheque_no = $multipals_cheque_no; $addPayment->location = $location; $addPayment->reference_no = $reference_no; $addPayment->user_id = $user_id; if ($addPayment->save()) { $response['flag'] = true; $response['message'] = "Payment Added Successfully !!"; } else { $response['flag'] = false; $response['message'] = "Something Wrong!!"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function partyLedgerClosing(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'retailer_id' => 'required', 'dealer_id' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $total_debit = \App\PartyInvoiceLedger::where('retailer_id', $request->retailer_id)->where('dealer_id', $request->dealer_id)->sum('debit'); $total_credit = \App\PartyInvoiceLedger::where('retailer_id', $request->retailer_id)->where('dealer_id', $request->dealer_id)->sum('credit'); $totalSD = \App\SecondaryDiscountPartyLedger::join('retailers', 'retailers.id', 'secondary_discount_party_ledgers.retailer_id') ->select('retailers.name as retailer_name', DB::raw('SUM(secondary_discount_party_ledgers.credit) as totalCredit'), DB::raw('SUM(secondary_discount_party_ledgers.debit) as totalDebit')) ->where('secondary_discount_party_ledgers.retailer_id', $request->retailer_id) ->where('secondary_discount_party_ledgers.dealer_id', $request->dealer_id) ->get(); $totalcredit = $total_credit + $totalSD[0]->totalCredit; $totaldebit = $total_debit + $totalSD[0]->totalDebit; $response['total_credit'] = $totalcredit; $response['total_debit'] = $totaldebit; $total_closing_balance = $totalcredit - $totaldebit; $response['closing_balance'] = abs($total_closing_balance); $response['type'] = ($total_closing_balance > 0) ? 'Credit' : 'Debit'; $response['message'] = "Party Closing Balance"; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function fieldCollectionList(Request $request) { //dd($request->all()); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $user_id = $this->AuthId($request)->id; $today = date('Y-m-d'); //$to=$request->to_date; //$from=$request->from_date; //if(!is_null($to) && !is_null($from)){ if ($user_id == 1) { if (!is_null($today)) { $ReceivePayment = \App\ReceivePayment::join('retailers', 'retailers.id', 'receive_payments.retailer_id')->join('dealers', 'dealers.id', 'receive_payments.dealer_id')->select('*', 'dealers.name as dealer_name', 'retailers.name as retailer_name')->whereDate('receive_payments.created_at', $today) ->get(); } else { $ReceivePayment = \App\ReceivePayment::join('retailers', 'retailers.id', 'receive_payments.retailer_id')->join('dealers', 'dealers.id', 'receive_payments.dealer_id')->select('*', 'dealers.name as dealer_name', 'retailers.name as retailer_name')->get(); } $response['flag'] = true; $response['message'] = 'ReceivePayment List'; $response['data'] = $ReceivePayment; } else { if (!is_null($today)) { $ReceivePayment = \App\ReceivePayment::join('retailers', 'retailers.id', 'receive_payments.retailer_id')->join('dealers', 'dealers.id', 'receive_payments.dealer_id')->select('*', 'dealers.name as dealer_name', 'retailers.name as retailer_name')->where('receive_payments.user_id', $user_id)->whereDate('receive_payments.created_at', $today)->get(); } else { $ReceivePayment = \App\ReceivePayment::join('retailers', 'retailers.id', 'receive_payments.retailer_id')->join('dealers', 'dealers.id', 'receive_payments.dealer_id')->select('*', 'dealers.name as dealer_name', 'retailers.name as retailer_name')->where('receive_payments.user_id', $user_id)->get(); } $response['flag'] = true; $response['message'] = 'ReceivePayment List'; $response['data'] = $ReceivePayment; } } else { $response['flag'] = false; $response['message'] = " Session Expired"; $response['is_token_expired'] = 1; } } return response()->json($response); } public function fieldCollectionDenomination(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required' ) ); if ($validator->fails()) { $response['flag'] = false; $response['message'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $user = \App\User::where('api_token', $request->api_token)->first(); $field_collections = \App\ReceivePayment::where([['user_id', '=', $user->id], ['mode', '=', 'Cash'], ['status', '=', '0'], [DB::raw("date(created_at)"), '=', date("Y-m-d")]])->get(); if (count($field_collections) > 0) { $currency = $field_collections[0]['multipals_cheque_no']; $currency_arr = array(); $currencyCount_arr = array(); $arr = json_decode($currency, true); $totalAmount = 0; foreach ($arr as $key => $arrV) { $currencyCount = 0; foreach ($field_collections as $field_collectionV) { $arr1 = json_decode($field_collectionV->multipals_cheque_no, true); $currencyCount += $arr1[$key]; } $totalAmount += ($key * $currencyCount); array_push($currency_arr, $key); array_push($currencyCount_arr, $currencyCount); } $Denomination = array_combine($currency_arr, $currencyCount_arr); $response['data'] = array('denomination' => $Denomination, 'total_amount' => $totalAmount); $response['flag'] = true; } else { $response['flag'] = false; $response['message'] = "Data not found"; } } else { $response['flag'] = false; $response['message'] = " Session Expired"; $response['is_token_expired'] = 1; } } return response()->json($response); } public function activeLedgerRetailerList(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $retailers = \App\PartyInvoiceLedger::join('retailers', 'retailers.id', 'party_invoice_ledgers.retailer_id')->groupBy('retailer_id')->select('name', 'hindi_name', 'address', 'retailers.id', DB::raw("SUM(credit)-SUM(debit) as closing"))->having(DB::raw("SUM(credit)-SUM(debit)"), '!=', 0)->get(); if (count($retailers) > 0) { $response['flag'] = true; $response['retailers'] = $retailers; } else { $response['flag'] = false; $response['message'] = "Retailer not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function routes(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $routes = \App\Route::where('is_active', 1)->get(); if (count($routes) > 0) { $response['flag'] = true; $response['routes'] = $routes; $response['day'] = [array( 1 => 'Monday', 2 => 'Tuesday', 3 => 'Wednesday', 4 => 'Thursday', 5 => 'Friday', 6 => 'Saturday', 7 => 'Sunday' )]; } else { $response['flag'] = false; $response['message'] = "Routes not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function groupList(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'route_id' => 'required' ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $user = \App\User::where('api_token', $request->api_token)->first(); if($user->id == 27){ $retailers = \App\Retailer::with('group')->where(array('route_id' => $request->route_id))->groupBy('group_id')->get(); $retailersWithoutGroups = \App\Retailer::where(array('route_id' => $request->route_id))->whereNull('group_id')->get(); }else{ $retailers = \App\Retailer::with('group')->where(array('route_id' => $request->route_id, 'marketing_manager_id' => $user->id))->groupBy('group_id')->get(); $retailersWithoutGroups = \App\Retailer::where(array('route_id' => $request->route_id, 'marketing_manager_id' => $user->id))->whereNull('group_id')->get(); } // $group=\App\Group::with('retailer')->get(); // $response['flag'] = true; // $response['data'] = $retailersWithoutGroups; // return response()->json($response); $groupDatalist = []; $n = 0; $m = 0; foreach ($retailers as $retailer) { // var_dump($retailer->group); // exit; if (!is_null($retailer->group)) { $partyInvloiceLedger = \App\PartyInvoiceLedger::join('retailers', 'retailers.id', 'party_invoice_ledgers.retailer_id')->join('groups', 'groups.id', 'retailers.group_id') ->select(DB::raw('SUM(party_invoice_ledgers.credit) as total_credit,SUM(party_invoice_ledgers.debit) as total_debit,groups.name as group_name ,groups.id as group_id ,groups.color'))->where('groups.id', $retailer->group->id)->where('retailers.route_id', $request->route_id)->first(); $totalSD = \App\SecondaryDiscountPartyLedger::join('retailers', 'retailers.id', 'secondary_discount_party_ledgers.retailer_id')->join('groups', 'groups.id', 'retailers.group_id') ->select('retailers.name as retailer_name', DB::raw('SUM(secondary_discount_party_ledgers.credit) as totalCredit'), DB::raw('SUM(secondary_discount_party_ledgers.debit) as totalDebit')) ->where('groups.id', $retailer->group->id)->where('retailers.route_id', $request->route_id) ->get(); // $totalcredit = $partyInvloiceLedger->total_credit + $totalSD[0]->totalCredit; // $totaldebit = $partyInvloiceLedger->total_debit + $totalSD[0]->totalDebit; $creditsd = $totalSD[0]->totalCredit; $debitsd = $totalSD[0]->totalDebit; $creditparty = $partyInvloiceLedger->total_credit; $debitparty = $partyInvloiceLedger->total_debit; $totalcredit = $creditparty + $creditsd; $totaldebit = $debitparty + $debitsd; // if (($partyInvloiceLedger->total_credit == $partyInvloiceLedger->total_debit)) { // $groupDatalist['group_flag'] = false; // $groupDatalist['group_details'][$n]['group'] = true; // $groupDatalist['group_details'][$n]['group_id'] = ""; // $groupDatalist['group_details'][$n]['group_name'] = ''; // $groupDatalist['group_details'][$n]['totalAmount'] = ""; // $groupDatalist['group_details'][$n]['color'] = ""; // } else { $groupDatalist['group_flag'] = true; $groupDatalist['group_details'][$n]['group'] = true; $groupDatalist['group_details'][$n]['group_id'] = $retailer->group->id; $groupDatalist['group_details'][$n]['group_name'] = $retailer->group->name; // $groupDatalist['group_details'][$n]['g'] = $partyInvloiceLedger ; $groupDatalist['group_details'][$n]['totalAmount'] = ($totalcredit - $totaldebit); $groupDatalist['group_details'][$n]['color'] = $retailer->group->color; //} } else { $groupDatalist['group_flag'] = false; $groupDatalist['group_details'][$n]['group'] = true; $groupDatalist['group_details'][$n]['group_id'] = ""; $groupDatalist['group_details'][$n]['group_name'] = ''; $groupDatalist['group_details'][$n]['totalAmount'] = ""; $groupDatalist['group_details'][$n]['color'] = ""; } $n++; } foreach ($retailersWithoutGroups as $retailersWithoutGroup) { $partyInvloiceLedgerWithoutGroupCredit = \App\PartyInvoiceLedger::join('retailers', 'retailers.id', 'party_invoice_ledgers.retailer_id') ->where('party_invoice_ledgers.retailer_id', $retailersWithoutGroup->id)->where('retailers.route_id', $request->route_id)->sum('party_invoice_ledgers.credit'); $partyInvloiceLedgerWithoutGroupDebit = \App\PartyInvoiceLedger::join('retailers', 'retailers.id', 'party_invoice_ledgers.retailer_id') ->where('party_invoice_ledgers.retailer_id', $retailersWithoutGroup->id)->where('retailers.route_id', $request->route_id)->sum('party_invoice_ledgers.debit'); $totalSD = \App\SecondaryDiscountPartyLedger::join('retailers', 'retailers.id', 'secondary_discount_party_ledgers.retailer_id') ->select('retailers.name as retailer_name', DB::raw('SUM(secondary_discount_party_ledgers.credit) as totalCredit'), DB::raw('SUM(secondary_discount_party_ledgers.debit) as totalDebit')) ->where('secondary_discount_party_ledgers.retailer_id', $retailersWithoutGroup->id) ->get(); $creditsd = $totalSD[0]->totalCredit; $debitsd = $totalSD[0]->totalDebit; $creditparty = $partyInvloiceLedgerWithoutGroupCredit; $debitparty = $partyInvloiceLedgerWithoutGroupDebit; $totalWithoutGroupcredit = $creditparty + $creditsd; $totalWithoutGroupdebit = $debitparty + $debitsd; // $response['flag'] = true; // $response['data'] = $partyInvloiceLedgerWithoutGroup; // return response()->json($response); // if (($partyInvloiceLedgerWithoutGroupCredit != $partyInvloiceLedgerWithoutGroupDebit)) { $groupDatalist['retailer_flag'] = true; $groupDatalist['retailer_details'][$m]['group'] = false; $groupDatalist['retailer_details'][$m]['retailer_id'] = $retailersWithoutGroup->id; $groupDatalist['retailer_details'][$m]['retailer_name'] = $retailersWithoutGroup->name; $groupDatalist['retailer_details'][$m]['retailer_address'] = $retailersWithoutGroup->address; $groupDatalist['retailer_details'][$m]['totalAmount'] = ($totalWithoutGroupcredit - $totalWithoutGroupdebit); // } // else { // $groupDatalist['retailer_flag'] = false; // $groupDatalist['retailer_details'][$m]['group'] = false; // $groupDatalist['retailer_details'][$m]['retailer_id'] = ""; // $groupDatalist['retailer_details'][$m]['retailer_name'] = ""; // $groupDatalist['retailer_details'][$m]['retailer_address'] = ""; // $groupDatalist['retailer_details'][$m]['totalAmount'] = ""; // } $m++; } if (count($groupDatalist) > 0) { $response['flag'] = true; $response['data'] = $groupDatalist; } else { $response['flag'] = false; $response['message'] = "Group List not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function groupRetailerList(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'group_id' => 'required' ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $retailers = \App\Retailer::where('retailers.group_id', $request->group_id)->where('retailers.route_id', $request->route_id)->get(); $arr1 = array(); if (!empty($retailers)) { $arr = array(); foreach ($retailers as $retailers_value) { //array_push($arr, $retailers_value->id); // } // if (!empty($arr)) { $value = $retailers_value->id; $partyInvloiceLedger = DB::table('party_invoice_ledgers')->join('retailers', 'retailers.id', 'party_invoice_ledgers.retailer_id')->join('groups', 'groups.id', 'retailers.group_id')->select('retailers.id', 'retailers.name as retailer_name', 'retailers.address as address', DB::raw('SUM(party_invoice_ledgers.credit) as total_credit,SUM(party_invoice_ledgers.debit) as total_debit,groups.name as group_name ,groups.id as group_id'))->where('party_invoice_ledgers.retailer_id', $value)->groupBy('party_invoice_ledgers.retailer_id')->havingRaw('total_credit != total_debit')->get(); $totalSD = \App\SecondaryDiscountPartyLedger::join('retailers', 'retailers.id', 'secondary_discount_party_ledgers.retailer_id') ->select('retailers.name as retailer_name', DB::raw('SUM(secondary_discount_party_ledgers.credit) as totalCredit'), DB::raw('SUM(secondary_discount_party_ledgers.debit) as totalDebit')) ->where('secondary_discount_party_ledgers.retailer_id', $value) ->get(); $creditsd = $totalSD[0]->totalCredit; $debitsd = $totalSD[0]->totalDebit; // $sd = $creditsd - $debitsd; // $ds = $sd; if (($partyInvloiceLedger[0]->total_credit - $partyInvloiceLedger[0]->total_debit) < 0) { $party['party'] = $partyInvloiceLedger; } else { $party['party'] = []; } if (($creditsd - $debitsd) < 0) { $party['sd'] = $creditsd - $debitsd; } else { $party['sd'] = 0; } array_push($arr1, $party); } } if (count($arr1) > 0) { $response['flag'] = true; $response['data'] = $arr1; } else { $response['flag'] = false; $response['message'] = "Retailer List not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function groupwisePartyDebitersList(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'group_id' => 'required' ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $groups = DB::table('party_invoice_ledgers') ->join('retailers', 'retailers.id', 'party_invoice_ledgers.retailer_id') ->join('groups', 'groups.id', 'retailers.group_id') ->select('retailers.id as retailer_id', 'retailers.group_id as group_id', 'retailers.name as retailer_name', 'groups.name as group_name', DB::raw('SUM(party_invoice_ledgers.credit) as totalCredit'), DB::raw('SUM(party_invoice_ledgers.debit) as totalDebit')) ->where('retailers.group_id', $request->group_id) ->groupBy(['groups.id']) ->get(); if (count($groups) > 0) { $response['flag'] = true; $response['data'] = $groups; } else { $response['flag'] = false; $response['message'] = "Retailer List not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function retailerLedgerList(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'retailer_id' => 'required' ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $totalMain = DB::table('party_invoice_ledgers') ->join('retailers', 'retailers.id', 'party_invoice_ledgers.retailer_id') ->select('retailers.name as retailer_name', DB::raw('SUM(party_invoice_ledgers.credit) as totalCredit'), DB::raw('SUM(party_invoice_ledgers.debit) as totalDebit')) ->where('party_invoice_ledgers.retailer_id', $request->retailer_id) ->get(); $totalSD = \App\SecondaryDiscountPartyLedger::join('retailers', 'retailers.id', 'secondary_discount_party_ledgers.retailer_id') ->select('retailers.name as retailer_name', DB::raw('SUM(secondary_discount_party_ledgers.credit) as totalCredit'), DB::raw('SUM(secondary_discount_party_ledgers.debit) as totalDebit')) ->where('secondary_discount_party_ledgers.retailer_id', $request->retailer_id) ->get(); if (count($totalMain) > 0) { $retailer_name = $totalMain[0]->retailer_name; } elseif (count($totalSD) > 0) { $retailer_name = $totalSD[0]->retailer_name; } $total = [ 'retailer_name' => $retailer_name, 'totalCredit' => $totalMain[0]->totalCredit + $totalSD[0]->totalCredit, 'totalDebit' => $totalMain[0]->totalDebit + $totalSD[0]->totalDebit ]; $ledgerOpening = \DB::select('select `particular` as rate,`particular` as quantity, `retailer_id`, `particular`, `type`, `credit`, `debit`, `against`, DATE_FORMAT(created_at, "%Y-%m-%d") as transaction_date from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and `particular`="opening balance" order by `created_at` asc'); $ledgerWithSeries = \DB::select('select `party_invoice_ledgers`.`particular` as rate,`party_invoice_ledgers`.`particular` as quantity ,`party_invoice_ledgers`.`retailer_id`, `party_invoice_ledgers`.`particular`, `party_invoice_ledgers`.`type`, `party_invoice_ledgers`.`credit`, `party_invoice_ledgers`.`debit`, `party_invoice_ledgers`.`against`, `bank_statements`.`transaction_date` from `party_invoice_ledgers` join `bank_statements` on `bank_statements`.`series` = `party_invoice_ledgers`.`against` and bank_statements.type=LOWER(party_invoice_ledgers.type) where `party_invoice_ledgers`.`retailer_id` = ' . $request->retailer_id . ' order by `bank_statements`.`transaction_date` asc'); $ledgerInvoice = \DB::select('select `loading_slip_invoices`.`rate`, `loading_slip_invoices`.`quantity`, `party_invoice_ledgers`.`retailer_id`, `party_invoice_ledgers`.`particular`, `party_invoice_ledgers`.`type`, `party_invoice_ledgers`.`credit`, `party_invoice_ledgers`.`debit`, `party_invoice_ledgers`.`against`, DATE_FORMAT(`party_invoice_ledgers`.created_at, "%Y-%m-%d") as transaction_date from `party_invoice_ledgers` join `loading_slip_invoices` on `loading_slip_invoices`.`invoice_number`=`party_invoice_ledgers`.`against` where `party_invoice_ledgers`.`retailer_id` = ' . $request->retailer_id . ' and `party_invoice_ledgers`.`type` is null order by `party_invoice_ledgers`.`created_at` asc'); $generalEntry = \DB::select('select `particular` as rate,`particular` as quantity, `retailer_id`, `particular`, `type`, `credit`, `debit`, journal_series as against, `transaction_date` from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and `type` = "Journal Entry" order by `transaction_date` asc'); $CnEntry = \DB::select('select `particular` as rate,`particular` as quantity,`retailer_id`, `particular`, `type`, `credit`, `debit`, cn_against as against, DATE_FORMAT(created_at, "%Y-%m-%d") as transaction_date from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and cn_against!="" order by `created_at` asc'); $secondary_discount = DB::select('select `loading_slip_invoices`.`secondary_discount` as rate,`loading_slip_invoices`.`quantity`, `secondary_discount_party_ledgers`.`retailer_id`, `secondary_discount_party_ledgers`.`particular`, `secondary_discount_party_ledgers`.`type`, `secondary_discount_party_ledgers`.`credit`, `secondary_discount_party_ledgers`.`debit`, `secondary_discount_party_ledgers`.`against`, `secondary_discount_party_ledgers`.`transaction_date` from `secondary_discount_party_ledgers` join `loading_slip_invoices` on `loading_slip_invoices`.`invoice_number`=`secondary_discount_party_ledgers`.`against` where `secondary_discount_party_ledgers`.`retailer_id` =' . $request->retailer_id . ' order by `secondary_discount_party_ledgers`.`transaction_date` asc'); $ledgers = array_merge(array_merge($ledgerWithSeries, $ledgerInvoice, $ledgerOpening, $CnEntry), $generalEntry, $secondary_discount); array_multisort(array_column($ledgers, 'transaction_date'), SORT_DESC, $ledgers); //$opening_balance = 0; // if (count($totalMain) > 0) { // if (count($totalSD) > 0) { if (count($total) > 0) { $response['flag'] = true; $response['total'] = [$total]; // $response['total'] = $totalMain; // $response['total'] = $totalSD; $response['ledger_detail'] = $ledgers; } else { $response['flag'] = false; $response['message'] = "List not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें"; $response['is_token_expired'] = true; } } return response()->json($response); } public function retailerLedgerExcel(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'retailer_id' => 'required' ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $retailer_id = $request->retailer_id; //$data=Excel::download(new RetailerLedger($retailer_id), 'retailerLedger.xlsx'); $path = 'retailer_excel/' . time() . '-' . $retailer_id . '.xlsx'; // echo url('public/retailer_excel'.'/'.$path); // exit(); $data = Excel::store(new RetailerLedger($retailer_id), $path, 'real_public'); if (!empty($data)) { $response['flag'] = true; $response['excel_path'] = url($path); } else { $response['flag'] = false; $response['message'] = "Something went wrong"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें"; $response['is_token_expired'] = true; } } return response()->json($response); } public function retailerInvoiceList(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'retailer_id' => 'required' ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $retailer_id = $request->retailer_id; $invoices = \App\LoadingSlipInvoice::where('remaining_amount', '>', 0)->where('retailer_id', $retailer_id)->get(); if (!empty($invoices)) { $response['flag'] = true; $response['invoices'] = $invoices; } else { $response['flag'] = false; $response['message'] = "Something went wrong"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें"; $response['is_token_expired'] = true; } } return response()->json($response); } /*------- End Field Collection------*/ /*------- Expense Voucher------*/ public function expenseMasterList(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'id' => 'required' ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $expenses = \DB::table('expenses')->where('is_active', 1)->where('brand_id', $request->id)->get(); if (count($expenses) > 0) { $response['flag'] = true; $response['expenses'] = $expenses; } else { $response['flag'] = false; $response['message'] = "Expense not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function brandMasterList(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'id' => 'required' ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $brand = \DB::table('brands')->where('is_active', 1)->where('company_id', $request->id)->get(); if (count($brand) > 0) { $response['flag'] = true; $response['expenses'] = $brand; } else { $response['flag'] = false; $response['message'] = "Brands not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function bank_accounts(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $bank_accounts = \App\BankAccount::where('is_active', 1)->with('bank')->get(); if (count($bank_accounts) > 0) { $response['flag'] = true; $response['bank_accounts'] = $bank_accounts; } else { $response['flag'] = false; $response['message'] = "Bank Acounts not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function bank_branches(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $bank_accounts = \App\BankAccount::where('is_active', 1)->pluck('bank_branch'); if (count($bank_accounts) > 0) { $response['flag'] = true; $response['bank_accounts'] = $bank_accounts; } else { $response['flag'] = false; $response['message'] = "Bank Acounts not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function recepient_user_list(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'id' => 'required' ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { if ($request->id == 2) { $recepients = \App\User::whereIn('id', array(62, 63))->select('id', 'name')->get(); } else if ($request->id == 5) { $recepients = \App\User::whereIn('id', array(18, 20, 14, 44, 26, 47, 13, 36, 46, 45, 11, 48, 49))->select('id', 'name')->get(); } else if ($request->id == 8) { $recepients = \App\User::whereIn('id', array(18, 20, 14, 44, 26, 47, 13, 36, 46, 45, 22))->select('id', 'name')->get(); } else if ($request->id == 9) { $recepients = \App\User::whereIn('id', array(18, 20, 14, 44, 26, 47, 13, 36, 46, 45, 43, 50, 51))->select('id', 'name')->get(); } else if ($request->id == 10) { $recepients = \App\User::whereIn('id', array(18, 20, 14, 44, 26, 47, 13, 36, 46, 45, 52, 53))->select('id', 'name')->get(); } else if ($request->id == 11) { $recepients = \App\User::whereIn('id', array(18, 20, 14, 44, 26, 47, 13, 36, 46, 45, 54, 55))->select('id', 'name')->get(); } else if ($request->id == 12) { $recepients = \App\User::whereIn('id', array(18, 20, 14, 44, 26, 47, 13, 36, 46, 45, 34))->select('id', 'name')->get(); } else if ($request->id == 3) { $recepients = \App\User::whereIn('id', array(62, 63))->select('id', 'name')->get(); } else if ($request->id == 4) { $recepients = \App\User::whereIn('id', array(62, 63))->select('id', 'name')->get(); } else if ($request->id == 14) { $recepients = \App\User::whereIn('id', array(65))->select('id', 'name')->get(); } else if ($request->id == 6) { $recepients = \App\User::whereIn('id', array(18, 20, 14, 44, 26, 47, 13, 36, 46, 45, 11, 48, 49))->select('id', 'name')->get(); } else if ($request->id == 7) { $recepients = \App\User::whereIn('id', array(18, 20, 14, 44, 26, 47, 13, 36, 46, 45, 11, 48, 49))->select('id', 'name')->get(); } else if ($request->id == 13) { $recepients = \App\User::whereIn('id', array(64))->select('id', 'name')->get(); } else if ($request->id == 1) { $recepients = \App\User::whereIn('id', array(18, 20, 14, 44, 26, 47, 13, 36, 46, 45, 43))->select('id', 'name')->get(); } else if ($request->id == 15) { $recepients = \App\User::whereIn('id', array(18,26,36, 46, 45,71,76))->select('id', 'name')->get(); } if (count($recepients) > 0) { $response['flag'] = true; $response['data'] = $recepients; } else { $response['flag'] = false; $response['message'] = "Users not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function created_user_list(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { if (count($created_users) > 0) { $response['flag'] = true; $response['data'] = $created_users; } else { $response['flag'] = false; $response['message'] = "Users not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function firm_list(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { if ($request->api_token) { $company = \App\User::where('api_token', $request->api_token)->first(); if ($company->company_id == 1 || $company->company_id == 7 || $company->company_id == 9 || $company->company_id == 11) { if ($company->company_id == 1) { $dealers = \App\Company::whereIn('id', array(2, 3, 5, 8))->select('id', 'name')->get(); } else if ($company->company_id == 7) { $dealers = \App\Company::whereIn('id', array(7))->select('id', 'name')->get(); } else if ($company->company_id == 9) { $dealers = \App\Company::whereIn('id', array(9))->select('id', 'name')->get(); } else if ($company->company_id == 11) { $dealers = \App\Company::whereIn('id', array(11))->select('id', 'name')->get(); } } else { $response['flag'] = false; $response['message'] = "You Are Not Allowed Make Vouche"; } } if (count($dealers) > 0) { $response['flag'] = true; $response['data'] = $dealers; } else { $response['flag'] = false; $response['message'] = "Dealers not found"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function add_expense_voucher(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'dealer_id' => 'required', 'type' => 'required', 'amount' => 'required', //'mode' =>'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $user = \DB::table('users')->where('api_token', $request->api_token)->first(); $expense_voucher = new \App\ExpenseVoucher(); $expense_voucher->dealer_id = $request->dealer_id; $expense_voucher->bank_id = 3; $expense_voucher->amount = $request->amount; $expense_voucher->mode = 'cash'; $expense_voucher->type = $request->type; $expense_voucher->approve_status = 0; $expense_voucher->paid_date = 0; $expense_voucher->paid_id = 0; $expense_voucher->paid_status = 0; $expense_voucher->user_name = $user->name; $expense_voucher->user_id = $user->id; $expense_voucher->received_by = $request->received_by; $expense_voucher->brand = $request->brand; $expense_voucher->created_by = $user->id; $expense_voucher->remark = $request->remark; //if($request->mode=='Cash'){ //} $expense_voucher->generate_user_id = $this->AuthId($request)->id; // $expense_voucher->bank_id=$request->bank_id; if ($expense_voucher->save()) { $response['flag'] = true; $response['message'] = "वाउचर सफलतापूर्वक बन गया|"; $response['id'] = $expense_voucher->id; $response['type'] = $request->type; } else { $response['flag'] = false; $response['message'] = "हमें खेद है वाउचर नहीं बनाया जा सका|"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function get_expenses(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $today = date('Y-m-d'); //echo $today; // $to=$request->to_date; // $from=$request->from_date; $user = \App\User::where('api_token', $request->api_token)->first(); if ($user->id == 32 || $user->id == 67 || $user->id == 1) { if (!is_null($today)) { //$getdata= \App\ExpenseVoucher::whereDate('transaction_date','>=', $today->subDays( 2 ))->get(); $getdata = \App\ExpenseVoucher::with('dealer:id,name', 'recived_user:id,name', 'created_user:id,name', 'paid_user:id,name', 'expense:id,name')->select('id', 'dealer_id', 'amount', 'received_by', 'created_by', 'type', 'created_at', 'transaction_date', 'approve_status', 'approve_user_id', 'approve_date', 'paid_status', 'remark', 'approve_remark', 'status')->whereDate('created_at', $today)->where('dealer_id', '!=', 9)->orderBy('id', 'DESC')->get(); $yesterday = \App\ExpenseVoucher::with('dealer:id,name', 'recived_user:id,name', 'created_user:id,name', 'paid_user:id,name', 'expense:id,name')->select('id', 'dealer_id', 'amount', 'received_by', 'created_by', 'type', 'created_at', 'transaction_date', 'approve_status', 'approve_user_id', 'approve_date', 'paid_status', 'remark', 'approve_remark', 'status')->whereRaw('date(created_at) = date((NOW() - INTERVAL 1 DAY))')->where('dealer_id', '!=', 9)->orderBy('id', 'DESC')->get(); $day_before_yesterday = \App\ExpenseVoucher::with('dealer:id,name', 'recived_user:id,name', 'created_user:id,name', 'paid_user:id,name', 'expense:id,name')->select('id', 'dealer_id', 'amount', 'received_by', 'created_by', 'type', 'created_at', 'transaction_date', 'approve_status', 'approve_user_id', 'approve_date', 'paid_status', 'remark', 'approve_remark', 'status')->where('approve_status', '=', 0)->where('dealer_id', '!=', 9)->orderBy('id', 'DESC')->get(); } else { $getdata = \App\ExpenseVoucher::with('dealer:id,name', 'recived_user:id,name', 'created_user:id,name', 'paid_user:id,name', 'expense:id,name')->select('id', 'dealer_id', 'amount', 'received_by', 'created_by', 'type', 'created_at', 'transaction_date', 'approve_status', 'approve_user_id', 'approve_date', 'paid_status', 'remark', 'approve_remark', 'status')->where('dealer_id', '!=', 9)->orderBy('id', 'DESC')->get(); } foreach ($getdata as $getdatas) { $data = \App\ExpenseVoucher::where('id', $getdatas->id)->first(); $data->status = 1; $data->save(); } foreach ($yesterday as $yesterdays) { $data = \App\ExpenseVoucher::where('id', $yesterdays->id)->first(); $data->status = 1; $data->save(); } foreach ($day_before_yesterday as $day_before_yesterdays) { $data = \App\ExpenseVoucher::where('id', $day_before_yesterdays->id)->first(); $data->status = 1; $data->save(); } $response['flag'] = true; $response['today'] = $getdata; $response['yesterday'] = $yesterday; $response['day_before_yesterday'] = $day_before_yesterday; } elseif ($user->id == 68 || $user->id == 69) { if (!is_null($today)) { //$getdata= \App\ExpenseVoucher::whereDate('transaction_date','>=', $today->subDays( 2 ))->get(); $getdata = \App\ExpenseVoucher::with('dealer:id,name', 'recived_user:id,name', 'created_user:id,name', 'paid_user:id,name', 'expense:id,name')->select('id', 'dealer_id', 'amount', 'received_by', 'created_by', 'type', 'created_at', 'transaction_date', 'approve_status', 'approve_user_id', 'approve_date', 'paid_status', 'remark', 'approve_remark', 'status')->whereIn('brand', ['4', '2', '3'])->whereDate('created_at', $today)->where('dealer_id', '=', 9)->orderBy('id', 'DESC')->get(); $yesterday = \App\ExpenseVoucher::with('dealer:id,name', 'recived_user:id,name', 'created_user:id,name', 'paid_user:id,name', 'expense:id,name')->select('id', 'dealer_id', 'amount', 'received_by', 'created_by', 'type', 'created_at', 'transaction_date', 'approve_status', 'approve_user_id', 'approve_date', 'paid_status', 'remark', 'approve_remark', 'status')->whereIn('brand', ['4', '2', '3'])->whereRaw('date(created_at) = date((NOW() - INTERVAL 1 DAY))')->where('dealer_id', '=', 9)->orderBy('id', 'DESC')->get(); $day_before_yesterday = \App\ExpenseVoucher::with('dealer:id,name', 'recived_user:id,name', 'created_user:id,name', 'paid_user:id,name', 'expense:id,name')->select('id', 'dealer_id', 'amount', 'received_by', 'created_by', 'type', 'created_at', 'transaction_date', 'approve_status', 'approve_user_id', 'approve_date', 'paid_status', 'remark', 'approve_remark', 'status')->whereIn('brand', ['4', '2', '3'])->where('approve_status', '=', 0)->where('dealer_id', '=', 9)->orderBy('id', 'DESC')->get(); } else { $getdata = \App\ExpenseVoucher::with('dealer:id,name', 'recived_user:id,name', 'created_user:id,name', 'paid_user:id,name', 'expense:id,name')->select('id', 'dealer_id', 'amount', 'received_by', 'created_by', 'type', 'created_at', 'transaction_date', 'approve_status', 'approve_user_id', 'approve_date', 'paid_status', 'remark', 'approve_remark', 'status')->where('brand', '=', 2)->orwhere('brand', '=', 3)->orwhere('brand', '=', 4)->where('dealer_id', '=', 9)->orderBy('id', 'DESC')->get(); } foreach ($getdata as $getdatas) { $data = \App\ExpenseVoucher::where('id', $getdatas->id)->first(); $data->status = 1; $data->save(); } foreach ($yesterday as $yesterdays) { $data = \App\ExpenseVoucher::where('id', $yesterdays->id)->first(); $data->status = 1; $data->save(); } foreach ($day_before_yesterday as $day_before_yesterdays) { $data = \App\ExpenseVoucher::where('id', $day_before_yesterdays->id)->first(); $data->status = 1; $data->save(); } $response['flag'] = true; $response['today'] = $getdata; $response['yesterday'] = $yesterday; $response['day_before_yesterday'] = $day_before_yesterday; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function set_status_expenses(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $today = date('Y-m-d'); //echo $today; // $to=$request->to_date; // $from=$request->from_date; if (!is_null($today)) { //$getdata= \App\ExpenseVoucher::whereDate('transaction_date','>=', $today->subDays( 2 ))->get(); $getdata = \App\ExpenseVoucher::select('id', 'status')->whereDate('created_at', $today)->orderBy('id', 'DESC')->get(); $yesterday = \App\ExpenseVoucher::select('id', 'status')->whereRaw('date(created_at) = date((NOW() - INTERVAL 1 DAY))')->orderBy('id', 'DESC')->get(); $day_before_yesterday = \App\ExpenseVoucher::select('id', 'status')->whereRaw('date(created_at) = date((NOW() - INTERVAL 2 DAY))')->orderBy('id', 'DESC')->get(); } else { $getdata = \App\ExpenseVoucher::select('id', 'status')->orderBy('id', 'DESC')->get(); } //if(count($getdata) > 0){ $response['flag'] = true; $response['today'] = $getdata; $response['yesterday'] = $yesterday; $response['day_before_yesterday'] = $day_before_yesterday; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function update_expense_voucher(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'id' => 'required', 'status' => 'required' ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $switch = 0; $now = date('Y-m-d H:i:s'); $user = \App\User::where('api_token', $request->api_token)->first(); if ($user->id == 32 || $user->id == 67 || $user->id == 1 || $user->id == 68 || $user->id == 69) { $status = $request->status; $approved_remark = $request->approve_remark; if (date('m') <= 3) { $financial_year = (date('Y') - 1) . '-' . date('Y'); } else { $financial_year = date('Y') . '-' . (date('Y') + 1); } $session = \App\Session::where('session', $financial_year)->where('is_active', 1)->first(); $expense_voucher = \App\ExpenseVoucher::find($request->id); if ($status == 1) { $switch = 1; $expense_voucher->approve_status = 1; $expense_voucher->approve_date = $now; $expense_voucher->approve_user_id = $user->id; $expense_voucher->approve_remark = $approved_remark; } if ($status == 2) { $switch = 2; $expense_voucher->approve_status = 2; $expense_voucher->approve_date = $now; $expense_voucher->approve_user_id = $user->id; $expense_voucher->approve_remark = $approved_remark; } if ($status == 3) { $switch = 3; $expense_voucher->transaction_date = $now; $expense_voucher->paid_status = 1; $expense_voucher->paid_id = $user->id; $expense_voucher->paid_date = $now; } if ($expense_voucher->save()) { if ($switch == 1) { $response['flag'] = true; $response['status'] = 1; $response['message'] = "वाउचर सफलतापूर्वक मंजूर कर लिया गया हैं|"; } if ($switch == 2) { $response['flag'] = true; $response['status'] = 2; $response['message'] = "वाउचर रद्द|"; } if ($switch == 3) { if ($expense_voucher->mode == 'cash') { $bank_id = 3; $mode = 'Cash'; } if ($expense_voucher->mode == 'cheque') { $bank_id = 2; $mode = 'Cheque'; } $expenseLedger = new \App\ExpenseLedger(); $expenseLedger->bank_id = $bank_id; $expenseLedger->dealer_id = $expense_voucher->dealer_id; $expenseLedger->type_id = $expense_voucher->type; $expenseLedger->mode = $mode; $expenseLedger->amount = $expense_voucher->amount; $expenseLedger->date = date('Y-m-d'); $expenseLedger->session_id = $session->id; $expenseLedger->voucher_id = $request->id; //$expenseLedger->against=$request->id; $expenseLedger->user_id = $user->id; $expenseLedger->save(); $bankStatement = new \App\BankStatement(); $bankStatement->bank_id = $bank_id; $series = \App\BankStatement::where('type', 'payment')->orderBy('id', 'desc')->first(); if (!is_null($series)) { $ser = $series->series + 1; } else { $ser = 1; } $bankStatement->type = 'payment'; $bankStatement->status = 'credit'; $bankStatement->series = $ser; $bankStatement->dealer_id = $expense_voucher->dealer_id; $bankStatement->mode = $mode; $bankStatement->particular = 'Expense- ' . getModelById('Expense', $expense_voucher->type)->name . ' (' . $expense_voucher->remark . ')'; $bankStatement->amount = $expense_voucher->amount; $bankStatement->history = null; $bankStatement->transaction_date = date('Y-m-d'); $bankStatement->save(); $expenseLedger->against = $ser; $expenseLedger->save(); $response['flag'] = true; $response['status'] = 1; $response['message'] = "वाउचर का भुगतान सफलतापूर्वक किया गया |"; } } else { $response['flag'] = false; $response['status'] = 2; $response['message'] = "वाउचर मंजूर नहीं किया गया हैं|"; } } else { $response['flag'] = true; $response['status'] = 2; $response['message'] = "वाउचर मंजूर नहीं किया गया हैं|"; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } /*------- End Expense Voucher------*/ public function fertilizerProduct() { $response = array(); $products = \App\Product::where('product_category_id', 1)->where('is_active', 1)->get(); if (count($products) > 0) { $response['flag'] = true; $response['products'] = $products; } else { $response['flag'] = false; $response['message'] = "Products not found"; } return response()->json($response); } //--------------------------flutter api ----------------------------------------------// public function flutter_get_expenses(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { // $date = Carbon::now()->subDays(3); $date = date('Y-m-d', strtotime("-3 days")); //echo $today; // $to=$request->to_date; // $from=$request->from_date; $user = \App\User::where('api_token', $request->api_token)->first(); if ($user->id == 32 || $user->id == 67 || $user->id == 1) { if (!is_null($date)) { // \DB::enableQueryLog(); //$getdata= \App\ExpenseVoucher::whereDate('transaction_date','>=', $today->subDays( 2 ))->get(); $getdata = \App\ExpenseVoucher::with('dealer:id,name', 'recived_user:id,name', 'created_user:id,name', 'paid_user:id,name', 'expense:id,name')->select('id', 'dealer_id', 'amount', 'received_by', 'created_by', 'type', \DB::raw('date(created_at) as created_at'), 'transaction_date', 'approve_status', 'approve_user_id', 'approve_date', 'paid_status', 'remark', 'approve_remark', 'status')->where('dealer_id', '!=', 9)->where('created_at', '>=', $date)->orderBy('id', 'DESC')->get(); // foreach($getdata as $key=>$value) { // echo $value->created_at." date : "; // } // exit; // print_r($getdata); // dd(\DB::getQueryLog($getdata)); } else { $getdata = \App\ExpenseVoucher::with('dealer:id,name', 'recived_user:id,name', 'created_user:id,name', 'paid_user:id,name', 'expense:id,name')->select('id', 'dealer_id', 'amount', 'received_by', \DB::raw('date(created_at) as created_at'), 'type', 'created_br', 'transaction_date', 'approve_status', 'approve_user_id', 'approve_date', 'paid_status', 'remark', 'approve_remark', 'status')->where('dealer_id', '!=', 9)->orderBy('id', 'DESC')->get(); } foreach ($getdata as $getdatas) { $data = \App\ExpenseVoucher::where('id', $getdatas->id)->first(); $data->status = 1; $data->save(); } $todays = array(); $getresponse = array(); // dd($getdata); foreach ($getdata as $getdatas) { // dd($getdatas->created_at); $getresponse['id'] = $getdatas->id; $getresponse['dealer_id'] = $getdatas->dealer_id; $getresponse['dealer_name'] = getModelById('Company', $getdatas->dealer_id)->name; $getresponse['amount'] = $getdatas->amount; $getresponse['received_by'] = $getdatas->received_by; $getresponse['received_name'] = getModelById('User', $getdatas->received_by)->name; $getresponse['created_by'] = $getdatas->created_by; $getresponse['created_by_name'] = getModelById('User', $getdatas->created_by)->name; $getresponse['expense'] = getModelById('Expense', $getdatas->type)->name; $getresponse['created_at'] = $getdatas->created_at->format('Y-m-d'); $getresponse['transaction_date'] = $getdatas->transaction_date; $getresponse['approve_status'] = $getdatas->approve_status; $getresponse['approve_date'] = $getdatas->approve_date; $getresponse['approve_user_id'] = $getdatas->approve_user_id; $getresponse['approve_user_name'] = getModelById('User', $getdatas->approve_user_id)->name; if ($getdatas->paid_status == 0) { $getresponse['paid_status'] = "Unpaid"; } else { $getresponse['paid_status'] = "Paid " . ($getdatas->transaction_date); } $getresponse['remark'] = $getdatas->remark; $getresponse['approve_remark'] = $getdatas->approve_remark; $getresponse['status'] = $getdatas->status; array_push($todays, $getresponse); } $datas['tokens'] = $todays; $response['flag'] = true; $response['data'] = $datas; } elseif ($user->id == 68 || $user->id == 69) { if (!is_null($date)) { //$getdata= \App\ExpenseVoucher::whereDate('transaction_date','>=', $today->subDays( 2 ))->get(); $getdata = \App\ExpenseVoucher::with('dealer:id,name', 'recived_user:id,name', 'created_user:id,name', 'paid_user:id,name', 'expense:id,name')->select('id', 'dealer_id', 'amount', 'received_by', 'created_by', 'type', \DB::raw('date(created_at) as created_at'), 'transaction_date', 'approve_status', 'approve_user_id', 'approve_date', 'paid_status', 'remark', 'approve_remark', 'status')->whereIn('brand', ['4', '2', '3'])->where('dealer_id', '=', 9)->orderBy('id', 'DESC')->get(); } else { $getdata = \App\ExpenseVoucher::with('dealer:id,name', 'recived_user:id,name', 'created_user:id,name', 'paid_user:id,name', 'expense:id,name')->select('id', 'dealer_id', 'amount', 'received_by', 'created_by', 'type', \DB::raw('date(created_at) as created_at'), 'transaction_date', 'approve_status', 'approve_user_id', 'approve_date', 'paid_status', 'remark', 'approve_remark', 'status')->where('brand', '=', 2)->orwhere('brand', '=', 3)->orwhere('brand', '=', 4)->where('dealer_id', '=', 9)->orderBy('id', 'DESC')->get(); } foreach ($getdata as $getdatas) { $data = \App\ExpenseVoucher::where('id', $getdatas->id)->first(); $data->status = 1; $data->save(); } $todays = array(); $getresponse = array(); foreach ($getdata as $getdatas) { $getresponse['id'] = $getdatas->id; $getresponse['dealer_id'] = $getdatas->dealer_id; $getresponse['dealer_name'] = getModelById('Company', $getdatas->dealer_id)->name; $getresponse['amount'] = $getdatas->amount; $getresponse['received_by'] = $getdatas->received_by; $getresponse['received_name'] = getModelById('User', $getdatas->received_by)->name; $getresponse['created_by'] = $getdatas->created_by; $getresponse['created_by_name'] = getModelById('User', $getdatas->created_by)->name; $getresponse['expense'] = getModelById('Expense', $getdatas->type)->name; $getresponse['created_at'] = $getdatas->created_at->format('Y-m-d'); $getresponse['transaction_date'] = $getdatas->transaction_date; $getresponse['approve_status'] = $getdatas->approve_status; $getresponse['approve_date'] = $getdatas->approve_date; $getresponse['approve_user_id'] = $getdatas->approve_user_id; $getresponse['approve_user_name'] = getModelById('User', $getdatas->approve_user_id)->name; $getresponse['paid_status'] = $getdatas->paid_status; $getresponse['remark'] = $getdatas->remark; $getresponse['approve_remark'] = $getdatas->approve_remark; $getresponse['status'] = $getdatas->status; array_push($todays, $getresponse); } $datas['tokens'] = $todays; $response['flag'] = true; $response['data'] = $datas; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } // dd($todays); return response()->json($response); } public function flutterGetPartyStock(Request $request) { try { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', //'status' => 'required', //'id' => 'required' ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if(1){ if ($this->checkApiAuth($request)) { $cement_product_ids = \App\Product::select('id')->where('product_category_id', 2)->get()->toArray(); // dd($cement_product_ids); // \DB::enableQueryLog(); $response['cement_stock'] = \App\Inventory::whereIn('product_id', $cement_product_ids)->sum('quantity'); $response['dap_stock'] = \App\Inventory::where('product_id', 4)->sum('quantity'); $response['npk_stock'] = \App\Inventory::whereIn('product_id', array(9, 10, 11, 12, 65, 103))->sum('quantity'); $response['urea_stock'] = \App\Inventory::where('product_id', 19)->sum('quantity'); $response['mop_stock'] = \App\Inventory::where('product_id', 7)->sum('quantity'); //->whereIn('dealer_id', [1, 3, 30, 31]) } return response()->json($response); } } //catch exception catch (Exception $e) { echo 'Message: ' . $e->getMessage(); } } public function flutterTokenOfCompanyProductsWise(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', //'company_id' => 'required', 'product_category_id' => 'required', // 'report_date' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $other_dealer = array(); $company_stock = array(); $cement_product_ids = \App\Product::select('id')->where('product_category_id', 2)->get()->toArray(); $dealerArr = array(1,30, 31); if ($request->product_category_id == 2) { $stockData = \DB::table('inventories')->select(\DB::raw('sum(inventories.quantity) as total_stock'), 'inventories.dealer_id as company_id')->join('products', 'products.id', 'inventories.product_id')->where('products.product_category_id', $request->product_category_id)->whereIn('inventories.dealer_id', $dealerArr)->groupBy('inventories.dealer_id')->get()->toArray(); $other_dealer['total_stock'] = \App\Inventory::whereIn('product_id', $cement_product_ids)->whereNotIn('dealer_id', $dealerArr)->sum('quantity'); $other_dealer['company_id'] = "other_dealer"; array_push($stockData, $other_dealer); $company_stock['total_stock'] = \App\Inventory::whereIn('product_id', $cement_product_ids)->whereNotNull('product_company_id')->sum('quantity'); $company_stock['company_id'] = "company"; array_push($stockData, $company_stock); $stockData = json_decode(json_encode($stockData), true); } else { $other_dealer = array(); $company_stock = array(); if ($request->product_category_id == "npk") { $productArr = array(9, 10, 11, 12, 65, 103); $stockData = \DB::table('inventories')->select(\DB::raw('sum(inventories.quantity) as total_stock'), 'inventories.dealer_id as company_id')->join('products', 'products.id', 'inventories.product_id')->whereIn('products.id', $productArr)->whereIn('inventories.dealer_id', $dealerArr)->groupBy('inventories.dealer_id')->get()->toArray(); $other_dealer['total_stock'] = \App\Inventory::whereIn('product_id', $productArr)->whereNotIn('dealer_id', $dealerArr)->sum('quantity'); $other_dealer['company_id'] = "other_dealer"; array_push($stockData, $other_dealer); $company_stock['total_stock'] = \App\Inventory::whereIn('product_id', $productArr)->whereNotNull('product_company_id')->sum('quantity'); $company_stock['company_id'] = "company"; array_push($stockData, $company_stock); $stockData = json_decode(json_encode($stockData), true); } else { $other_dealer = array(); $company_stock = array(); $productArr = $request->product_category_id; $stockData = \DB::table('inventories')->select(\DB::raw('sum(inventories.quantity) as total_stock'), 'inventories.dealer_id as company_id')->join('products', 'products.id', 'inventories.product_id')->where('products.id', $productArr)->whereIn('inventories.dealer_id', $dealerArr)->groupBy('inventories.dealer_id')->get()->toArray(); $other_dealer['total_stock'] = \App\Inventory::where('product_id', $productArr)->whereNotIn('dealer_id', $dealerArr)->sum('quantity'); $other_dealer['company_id'] = "other_dealer"; array_push($stockData, $other_dealer); $company_stock['total_stock'] = \App\Inventory::where('product_id', $productArr)->whereNotNull('product_company_id')->sum('quantity'); $company_stock['company_id'] = "company"; array_push($stockData, $company_stock); $stockData = json_decode(json_encode($stockData), true); } } foreach ($stockData as $key => $value) { if ($value['company_id'] == 1) { $stockData[$key]['company_name'] = 'MRSD-STP'; } // else if ($value['company_id'] == 3) { // $stockData[$key]['company_name'] = 'MRSD-LRP'; // } else if ($value['company_id'] == 30) { $stockData[$key]['company_name'] = 'MCPL-STP'; } else if ($value['company_id'] == 31) { $stockData[$key]['company_name'] = 'MCPL-DEVI KALI RD'; } else if ($value['company_id'] == "other_dealer") { $stockData[$key]['company_name'] = 'OTHER DEALER STOCK'; } else if ($value['company_id'] == "company") { $stockData[$key]['company_name'] = 'COMPANY STOCK'; } } $response['flag'] = true; $response['data']['tokens'] = $stockData; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function FlutterDealerWise(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', //'company_id' => 'required', 'product_category_id' => 'required', // 'report_date' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $other_dealer = array(); $company_stock = array(); $cement_product_ids = \App\Product::select('id')->where('product_category_id', 2)->get()->toArray(); $dealerArr = array(1,30, 31); if ($request->product_category_id == 2) { $stockData = \DB::table('inventories')->select(\DB::raw('sum(inventories.quantity) as total_stock'), 'inventories.dealer_id as company_id')->join('products', 'products.id', 'inventories.product_id')->where('products.product_category_id', $request->product_category_id)->whereNotIn('inventories.dealer_id', $dealerArr)->groupBy('inventories.dealer_id')->get()->toArray(); $stockData = json_decode(json_encode($stockData), true); } else { if ($request->product_category_id == "npk") { $productArr = array(9, 10, 11, 12, 65, 103); $stockData = \DB::table('inventories')->select(\DB::raw('sum(inventories.quantity) as total_stock'), 'inventories.dealer_id as company_id')->join('products', 'products.id', 'inventories.product_id')->whereIn('products.id', $productArr)->whereNotIn('inventories.dealer_id', $dealerArr)->groupBy('inventories.dealer_id')->get()->toArray(); $stockData = json_decode(json_encode($stockData), true); } else { $productArr = $request->product_category_id; $stockData = \DB::table('inventories')->select(\DB::raw('sum(inventories.quantity) as total_stock'), 'inventories.dealer_id as company_id')->join('products', 'products.id', 'inventories.product_id')->where('products.id', $productArr)->whereNotIn('inventories.dealer_id', $dealerArr)->groupBy('inventories.dealer_id')->get()->toArray(); $stockData = json_decode(json_encode($stockData), true); } } foreach ($stockData as $key => $value) { $stockData[$key]['company_name'] = getModelById('Dealer', $value['company_id'])->name . ' (' . getModelById('Dealer', $value['company_id'])->address1 . ')'; } $response['flag'] = true; $response['data']['tokens'] = $stockData; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function flutterCompanyProductsWise(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'company_id' => 'required', // 'product_category_id' => 'required', // 'report_date' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { if ($request->product_category_id == 2) { $cement_product_ids = \App\Product::where('product_category_id', $request->product_category_id)->pluck('id'); $cement_product_ids = json_encode($cement_product_ids, true); $cement_product_ids = json_decode($cement_product_ids, true); $cement_product_ids = implode(', ', $cement_product_ids); if ($request->company_id == "company") { $sql = "select t1.* from (SELECT inventories.product_id, sum(inventories.quantity) as product_total_qty, product_companies.name as brand_name, product_companies.id as brand_id FROM inventories join products on products.id=inventories.product_id join product_companies on product_companies.id=inventories.product_company_id WHERE product_company_id IS NOT NULL and inventories.product_id IN(" . $cement_product_ids . ") GROUP BY inventories.product_brand_id) as t1"; } else { $sql = "select t1.* from (SELECT inventories.product_id, sum(inventories.quantity) as product_total_qty, product_companies.name as brand_name, product_companies.id as brand_id FROM inventories join products on products.id=inventories.product_id join product_companies on product_companies.id=inventories.product_brand_id WHERE dealer_id = " . $request->company_id . " and inventories.product_id IN(" . $cement_product_ids . ") GROUP BY inventories.product_brand_id) as t1"; } } else { if ($request->product_category_id == "npk") { $productArr = "9, 10, 11, 12, 65, 103"; if ($request->company_id == "company") { $sql = "select t1.* from (SELECT inventories.product_id, sum(inventories.quantity) as product_total_qty, product_companies.name as brand_name , product_companies.id as brand_id FROM inventories join products on products.id=inventories.product_id join product_companies on product_companies.id=inventories.product_company_id WHERE product_company_id IS NOT NULL and inventories.product_id IN(" . $productArr . ") GROUP BY inventories.product_brand_id) as t1"; } else { $sql = "select t1.* from (SELECT inventories.product_id, sum(inventories.quantity) as product_total_qty, product_companies.name as brand_name , product_companies.id as brand_id FROM inventories join products on products.id=inventories.product_id join product_companies on product_companies.id=inventories.product_brand_id WHERE dealer_id = " . $request->company_id . " and inventories.product_id IN(" . $productArr . ") GROUP BY inventories.product_brand_id) as t1"; } } else { if ($request->company_id == "company") { $productArr = $request->product_category_id; $sql = "select t1.* from (SELECT inventories.product_id, sum(inventories.quantity) as product_total_qty, product_companies.name as brand_name, product_companies.id as brand_id FROM inventories join products on products.id=inventories.product_id join product_companies on product_companies.id=inventories.product_brand_id WHERE product_company_id IS NOT NULL and inventories.product_id = " . $productArr . " GROUP BY inventories.product_brand_id) as t1"; } else { $productArr = $request->product_category_id; $sql = "select t1.* from (SELECT inventories.product_id, sum(inventories.quantity) as product_total_qty, product_companies.name as brand_name, product_companies.id as brand_id FROM inventories join products on products.id=inventories.product_id join product_companies on product_companies.id=inventories.product_brand_id WHERE dealer_id = " . $request->company_id . " and inventories.product_id IN(" . $productArr . ") GROUP BY inventories.product_brand_id) as t1"; } } } $tokens = \DB::select($sql); $data['tokens'] = $tokens; $response['flag'] = true; $response['data'] = $data; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function flutterProductsWise(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'company_id' => 'required', // 'product_category_id' => 'required', // 'report_date' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { if ($request->product_category_id == 2) { $cement_product_ids = \App\Product::where('product_category_id', $request->product_category_id)->pluck('id'); $cement_product_ids = json_encode($cement_product_ids, true); $cement_product_ids = json_decode($cement_product_ids, true); $cement_product_ids = implode(', ', $cement_product_ids); if ($request->company_id == "company") { $sql = "select t1.* from (SELECT inventories.product_id, sum(inventories.quantity) as product_total_qty, products.name as brand_name FROM inventories join products on products.id=inventories.product_id join product_companies on product_companies.id=inventories.product_company_id WHERE product_company_id IS NOT NULL and inventories.product_id IN(" . $cement_product_ids . ") and inventories.product_brand_id = " . $request->product_brand_id . " GROUP BY inventories.product_id) as t1"; } else { $sql = "select t1.* from (SELECT inventories.product_id, sum(inventories.quantity) as product_total_qty, products.name as brand_name FROM inventories join products on products.id=inventories.product_id join product_companies on product_companies.id=inventories.product_brand_id WHERE dealer_id = " . $request->company_id . " and inventories.product_id IN(" . $cement_product_ids . ") and inventories.product_brand_id = " . $request->product_brand_id . " GROUP BY inventories.product_id ) as t1"; } } else { if ($request->product_category_id == "npk") { $productArr = "9, 10, 11, 12, 65, 103"; if ($request->company_id == "company") { $sql = "select t1.* from (SELECT inventories.product_id, sum(inventories.quantity) as product_total_qty, products.name as brand_name FROM inventories join products on products.id=inventories.product_id join product_companies on product_companies.id=inventories.product_company_id WHERE product_company_id IS NOT NULL and inventories.product_id IN(" . $productArr . ") and inventories.product_brand_id = " . $request->product_brand_id . " GROUP BY inventories.product_id) as t1"; } else { $sql = "select t1.* from (SELECT inventories.product_id, sum(inventories.quantity) as product_total_qty, products.name as brand_name FROM inventories join products on products.id=inventories.product_id join product_companies on product_companies.id=inventories.product_brand_id WHERE dealer_id = " . $request->company_id . " and inventories.product_id IN(" . $productArr . ") and inventories.product_brand_id = " . $request->product_brand_id . " GROUP BY inventories.product_id) as t1"; } } else { if ($request->company_id == "company") { $productArr = $request->product_category_id; $sql = "select t1.* from (SELECT inventories.product_id, sum(inventories.quantity) as product_total_qty, products.name as brand_name FROM inventories join products on products.id=inventories.product_id join product_companies on product_companies.id=inventories.product_brand_id WHERE product_company_id IS NOT NULL and inventories.product_id = " . $productArr . " and inventories.product_brand_id = " . $request->product_brand_id . " GROUP BY inventories.product_id) as t1"; } else { $productArr = $request->product_category_id; $sql = "select t1.* from (SELECT inventories.product_id, sum(inventories.quantity) as product_total_qty, products.name as brand_name FROM inventories join products on products.id=inventories.product_id join product_companies on product_companies.id=inventories.product_brand_id WHERE dealer_id = " . $request->company_id . " and inventories.product_id IN(" . $productArr . ") and inventories.product_brand_id = " . $request->product_brand_id . " GROUP BY inventories.product_id) as t1"; } } } $tokens = \DB::select($sql); $data['tokens'] = $tokens; $response['flag'] = true; $response['data'] = $data; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } /*---------------------------sale purchase api -------------------------------*/ public function salePurchaseTotal(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', //'company_id' => 'required', // 'product_category_id' => 'required', // 'report_date' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $cement_product_ids = \App\Product::select('id')->where('product_category_id', 2)->get()->toArray(); // dd($cement_product_ids); // \DB::enableQueryLog(); $sales['cement_stock'] = \App\LoadingSlipInvoice::whereIn('product_id', $cement_product_ids)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $sales['dap_stock'] = \App\LoadingSlipInvoice::where('product_id', 4)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $sales['npk_stock'] = \App\LoadingSlipInvoice::whereIn('product_id', array(9, 10, 11, 12, 65, 103))->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $sales['urea_stock'] = \App\LoadingSlipInvoice::where('product_id', 19)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $sales['mop_stock'] = \App\LoadingSlipInvoice::where('product_id', 7)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); //->whereIn('dealer_id', [1, 3, 30, 31]) $cement_stock_company = \App\CompanyDi::whereIn('product_id', $cement_product_ids)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $dap_stock_company = \App\CompanyDi::where('product_id', 4)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $npk_stock_company = \App\CompanyDi::whereIn('product_id', array(9, 10, 11, 12, 65, 103))->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $urea_stock_company = \App\CompanyDi::where('product_id', 19)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $mop_stock_company = \App\CompanyDi::where('product_id', 7)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $cement_stock_warehouse = \App\WarehouseDi::whereIn('product_id', $cement_product_ids)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $dap_stock_warehouse = \App\WarehouseDi::where('product_id', 4)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $npk_stock_warehouse = \App\WarehouseDi::whereIn('product_id', array(9, 10, 11, 12, 65, 103))->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $urea_stock_warehouse = \App\WarehouseDi::where('product_id', 19)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $mop_stock_warehouse = \App\WarehouseDi::where('product_id', 7)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $cement = $cement_stock_company + $cement_stock_warehouse; $dap = $dap_stock_company + $dap_stock_warehouse; $npk = $npk_stock_company + $npk_stock_warehouse; $urea = $urea_stock_company + $urea_stock_warehouse; $mop = $mop_stock_company + $mop_stock_warehouse; $purchase['cement_stock'] = "$cement"; $purchase['dap_stock'] = "$dap"; $purchase['npk_stock'] = "$npk"; $purchase['urea_stock'] = "$urea"; $purchase['mop_stock'] = "$mop"; if ($sales['cement_stock'] > 0 || $sales['dap_stock'] > 0 || $sales['npk_stock'] > 0 || $sales['urea_stock'] > 0 || $sales['mop_stock'] > 0) { $response['flag'] = true; $response['sales'] = $sales; } if ($cement > 0 || $dap > 0 || $npk > 0 || $urea > 0 || $mop > 0 ) { $response['flag'] = true; $response['purchase'] = $purchase; } } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } return response()->json($response); } } public function salepurchase(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', //'company_id' => 'required', 'product_category_id' => 'required', // 'report_date' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $other_dealer = array(); $company_stock = array(); $cement_product_ids = \App\Product::select('id')->where('product_category_id', 2)->get()->toArray(); $dealerArr = array(1,30, 31); if ($request->product_category_id == 2) { if ($request->type == "sales") { $stockData = \DB::table('loading_slip_invoices')->select(\DB::raw('sum(loading_slip_invoices.quantity) as total_stock'), 'loading_slip_invoices.dealer_id as company_id')->join('products', 'products.id', 'loading_slip_invoices.product_id')->where('products.product_category_id', $request->product_category_id)->whereIn('loading_slip_invoices.dealer_id', $dealerArr)->whereBetween('loading_slip_invoices.created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->groupBy('loading_slip_invoices.dealer_id')->get()->toArray(); $other_dealer['total_stock'] = \App\LoadingSlipInvoice::whereIn('product_id', $cement_product_ids)->whereNotIn('dealer_id', $dealerArr)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $other_dealer['company_id'] = "other_dealer"; array_push($stockData, $other_dealer); $stockData = json_decode(json_encode($stockData), true); } if ($request->type == "purchase") { $stockData = array(); $finalArr = array(); $stockDatacompany1 = \App\CompanyDi::whereIn('product_id', $cement_product_ids)->where('dealer_id', 1)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $stockDatawarehouse1 = \App\WarehouseDi::whereIn('product_id', $cement_product_ids)->where('dealer_id', 1)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $finalArr['total_stock'] = $stockDatacompany1 + $stockDatawarehouse1; $finalArr['company_id'] = "1"; array_push($stockData, $finalArr); // $stockDatacompany3 = \App\CompanyDi::whereIn('product_id', $cement_product_ids)->where('dealer_id', 3)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); // $stockDatawarehouse3 = \App\WarehouseDi::whereIn('product_id', $cement_product_ids)->where('dealer_id', 3)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); // $stockData3['total_stock'] = $stockDatacompany3 + $stockDatawarehouse3; // $stockData3['company_id'] = "3"; // array_push($stockData, $stockData3); $stockDatacompany30 = \App\CompanyDi::whereIn('product_id', $cement_product_ids)->where('dealer_id', 30)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $stockDatawarehouse30 = \App\WarehouseDi::whereIn('product_id', $cement_product_ids)->where('dealer_id', 30)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $stockData30['total_stock'] = $stockDatacompany30 + $stockDatawarehouse30; $stockData30['company_id'] = "30"; array_push($stockData, $stockData30); $stockDatacompany31 = \App\CompanyDi::whereIn('product_id', $cement_product_ids)->where('dealer_id', 31)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $stockDatawarehouse31 = \App\WarehouseDi::whereIn('product_id', $cement_product_ids)->where('dealer_id', 31)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $stockData31['total_stock'] = $stockDatacompany31 + $stockDatawarehouse31; $stockData31['company_id'] = "31"; array_push($stockData, $stockData31); $other_dealercompany['total_stock'] = \App\CompanyDi::whereIn('product_id', $cement_product_ids)->whereNotIn('dealer_id', $dealerArr)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $other_dealerwarehouse['total_stock'] = \App\WarehouseDi::whereIn('product_id', $cement_product_ids)->whereNotIn('dealer_id', $dealerArr)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $other_dealer['total_stock'] = $other_dealercompany['total_stock'] + $other_dealerwarehouse['total_stock']; $other_dealer['company_id'] = "other_dealer"; array_push($stockData, $other_dealer); $stockData = json_decode(json_encode($stockData), true); } // $company_stock['total_stock'] = \App\Inventory::whereIn('product_id', $cement_product_ids)->whereNotNull('product_company_id')->sum('quantity'); // $company_stock['company_id'] = "company"; // array_push($stockData, $company_stock); } else { $other_dealer = array(); $company_stock = array(); if ($request->product_category_id == "npk") { $productArr = array(9, 10, 11, 12, 65, 103); if ($request->type == "sales") { $productArr = array(9, 10, 11, 12, 65, 103); $stockData = \DB::table('loading_slip_invoices')->select(\DB::raw('sum(loading_slip_invoices.quantity) as total_stock'), 'loading_slip_invoices.dealer_id as company_id')->join('products', 'products.id', 'loading_slip_invoices.product_id')->whereIn('products.id', $productArr)->whereIn('loading_slip_invoices.dealer_id', $dealerArr)->whereBetween('loading_slip_invoices.created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->groupBy('loading_slip_invoices.dealer_id')->get()->toArray(); $other_dealer['total_stock'] = \App\LoadingSlipInvoice::whereIn('product_id', $productArr)->whereNotIn('dealer_id', $dealerArr)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $other_dealer['company_id'] = "other_dealer"; array_push($stockData, $other_dealer); } if ($request->type == "purchase") { $productArr = array(9, 10, 11, 12, 65, 103); $finalArr = array(); $stockData = array(); $stockDatacompany1 = \App\CompanyDi::whereIn('product_id', $productArr)->where('dealer_id', 1)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $stockDatawarehouse1 = \App\WarehouseDi::whereIn('product_id', $productArr)->where('dealer_id', 1)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $finalArr['total_stock'] = $stockDatacompany1 + $stockDatawarehouse1; $finalArr['company_id'] = "1"; array_push($stockData, $finalArr); // $stockDatacompany3 = \App\CompanyDi::whereIn('product_id', $productArr)->where('dealer_id', 3)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); // $stockDatawarehouse3 = \App\WarehouseDi::whereIn('product_id', $productArr)->where('dealer_id', 3)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); // $stockData3['total_stock'] = $stockDatacompany3 + $stockDatawarehouse3; // $stockData3['company_id'] = "3"; // array_push($stockData, $stockData3); $stockDatacompany30 = \App\CompanyDi::whereIn('product_id', $productArr)->where('dealer_id', 30)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $stockDatawarehouse30 = \App\WarehouseDi::whereIn('product_id', $productArr)->where('dealer_id', 30)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $stockData30['total_stock'] = $stockDatacompany30 + $stockDatawarehouse30; $stockData30['company_id'] = "30"; array_push($stockData, $stockData30); $stockDatacompany31 = \App\CompanyDi::whereIn('product_id', $productArr)->where('dealer_id', 31)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $stockDatawarehouse31 = \App\WarehouseDi::whereIn('product_id', $productArr)->where('dealer_id', 31)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $stockData31['total_stock'] = $stockDatacompany31 + $stockDatawarehouse31; $stockData31['company_id'] = "31"; array_push($stockData, $stockData31); $other_dealercompany['total_stock'] = \App\CompanyDi::whereIn('product_id', $productArr)->whereNotIn('dealer_id', $dealerArr)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $other_dealerwarehouse['total_stock'] = \App\WarehouseDi::whereIn('product_id', $productArr)->whereNotIn('dealer_id', $dealerArr)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $other_dealer['total_stock'] = $other_dealercompany['total_stock'] + $other_dealerwarehouse['total_stock']; $other_dealer['company_id'] = "other_dealer"; array_push($stockData, $other_dealer); } // $company_stock['total_stock'] = \App\Inventory::whereIn('product_id', $productArr)->whereNotNull('product_company_id')->sum('quantity'); // $company_stock['company_id'] = "company"; // array_push($stockData, $company_stock); $stockData = json_decode(json_encode($stockData), true); } else { $other_dealer = array(); $company_stock = array(); $productArr = $request->product_category_id; if ($request->type == "sales") { $stockData = \DB::table('loading_slip_invoices')->select(\DB::raw('sum(loading_slip_invoices.quantity) as total_stock'), 'loading_slip_invoices.dealer_id as company_id')->join('products', 'products.id', 'loading_slip_invoices.product_id')->where('products.id', $productArr)->whereIn('loading_slip_invoices.dealer_id', $dealerArr)->whereBetween('loading_slip_invoices.created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->groupBy('loading_slip_invoices.dealer_id')->get()->toArray(); $other_dealer['total_stock'] = \App\LoadingSlipInvoice::where('product_id', $productArr)->whereNotIn('dealer_id', $dealerArr)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $other_dealer['company_id'] = "other_dealer"; array_push($stockData, $other_dealer); } if ($request->type == "purchase") { $finalArr = array(); $stockData = array(); $productArr = $request->product_category_id; $stockDatacompany1 = \App\CompanyDi::where('product_id', $productArr)->where('dealer_id', 1)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $stockDatawarehouse1 = \App\WarehouseDi::where('product_id', $productArr)->where('dealer_id', 1)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $finalArr['total_stock'] = $stockDatacompany1 + $stockDatawarehouse1; $finalArr['company_id'] = "1"; array_push($stockData, $finalArr); // $stockDatacompany3 = \App\CompanyDi::where('product_id', $productArr)->where('dealer_id', 3)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); // $stockDatawarehouse3 = \App\WarehouseDi::where('product_id', $productArr)->where('dealer_id', 3)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); // $stockData3['total_stock'] = $stockDatacompany3 + $stockDatawarehouse3; // $stockData3['company_id'] = "3"; // array_push($stockData, $stockData3); $stockDatacompany30 = \App\CompanyDi::where('product_id', $productArr)->where('dealer_id', 30)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $stockDatawarehouse30 = \App\WarehouseDi::where('product_id', $productArr)->where('dealer_id', 30)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $stockData30['total_stock'] = $stockDatacompany30 + $stockDatawarehouse30; $stockData30['company_id'] = "30"; array_push($stockData, $stockData30); $stockDatacompany31 = \App\CompanyDi::where('product_id', $productArr)->where('dealer_id', 31)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $stockDatawarehouse31 = \App\WarehouseDi::where('product_id', $productArr)->where('dealer_id', 31)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $stockData31['total_stock'] = $stockDatacompany31 + $stockDatawarehouse31; $stockData31['company_id'] = "31"; array_push($stockData, $stockData31); $other_dealercompany['total_stock'] = \App\CompanyDi::where('product_id', $productArr)->whereNotIn('dealer_id', $dealerArr)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $other_dealerwarehouse['total_stock'] = \App\WarehouseDi::where('product_id', $productArr)->whereNotIn('dealer_id', $dealerArr)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $other_dealer['total_stock'] = $other_dealercompany['total_stock'] + $other_dealerwarehouse['total_stock']; $other_dealer['company_id'] = "other_dealer"; array_push($stockData, $other_dealer); } // $company_stock['total_stock'] = \App\Inventory::where('product_id', $productArr)->whereNotNull('product_company_id')->sum('quantity'); // $company_stock['company_id'] = "company"; // array_push($stockData, $company_stock); $stockData = json_decode(json_encode($stockData), true); } } foreach ($stockData as $key => $value) { if ($value['company_id'] == 1) { $stockData[$key]['company_name'] = 'MRSD-STP'; } // else if ($value['company_id'] == 3) { // $stockData[$key]['company_name'] = 'MRSD-LRP'; // } else if ($value['company_id'] == 30) { $stockData[$key]['company_name'] = 'MCPL-STP'; } else if ($value['company_id'] == 31) { $stockData[$key]['company_name'] = 'MCPL-DEVI KALI RD'; } else if ($value['company_id'] == "other_dealer") { $stockData[$key]['company_name'] = 'OTHER DEALER'; } // else if ($value['company_id'] == "company") { // $stockData[$key]['company_name'] = 'COMPANY STOCK'; // } } $response['flag'] = true; $response['data']['tokens'] = $stockData; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function salePurchaseProductsWise(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', //'company_id' => 'required', // 'product_category_id' => 'required', // 'report_date' => 'required', // 'purchase_type' =>'required' ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $result = 0; $results = 0; $result1 = 0; $tokens =array(); $start_date = date('Y-m-d', strtotime($request->date)); $end_date = date('Y-m-d', strtotime($request->todate)); if ($request->product_category_id == 2) { $cement_product_ids = \App\Product::where('product_category_id', $request->product_category_id)->pluck('id'); $cement_product_ids = json_encode($cement_product_ids, true); $cement_product_ids = json_decode($cement_product_ids, true); $cement_product_ids = implode(', ', $cement_product_ids); if ($request->type == "sales") { if ($request->company_id == "company") { $results = "select t1.* from (SELECT loading_slip_invoices.product_id, loading_slip_invoices.quantity as product_total_qty, loading_slip_invoices.created_at as date, loading_slip_invoices.freight_discount as freight_discount, loading_slip_invoices.secondary_discount as secondary_discount , product_companies.name as brand_name, products.name as product_name, loading_slip_invoices.retailer_name, dealers.name as dealersale, loading_slip_invoices.rate, loading_slip_invoices.total FROM loading_slip_invoices join dealers on dealers.id=loading_slip_invoices.dealer_id join products on products.id=loading_slip_invoices.product_id join product_companies on product_companies.id=loading_slip_invoices.product_company_id WHERE loading_slip_invoices.product_id IN(" . $cement_product_ids . ") AND loading_slip_invoices.created_at BETWEEN '$start_date' AND '$end_date' ORDER BY loading_slip_invoices.id DESC) as t1"; } else { $results = "select t1.* from (SELECT loading_slip_invoices.product_id, loading_slip_invoices.quantity as product_total_qty, loading_slip_invoices.created_at as date, loading_slip_invoices.freight_discount as freight_discount, loading_slip_invoices.secondary_discount as secondary_discount , products.name as product_name, loading_slip_invoices.retailer_name, dealers.name as dealersale, loading_slip_invoices.rate, loading_slip_invoices.total FROM loading_slip_invoices join dealers on dealers.id=loading_slip_invoices.dealer_id join product_loadings on product_loadings.id=loading_slip_invoices.loading_slip_id join products on products.id=loading_slip_invoices.product_id WHERE loading_slip_invoices.dealer_id = '$request->company_id' AND loading_slip_invoices.product_id IN(" . $cement_product_ids . ") and product_loadings.product_company_id = " . $request->product_brand_id . " and loading_slip_invoices.created_at BETWEEN '$start_date' AND '$end_date' ORDER BY loading_slip_invoices.id DESC) as t1"; } } if ($request->type == "purchase") { if ($request->company_id == "company") { if($request->purchase_type == "warehouse"){ $result = "select t1.* from (SELECT warehouse_dis.product_id, warehouse_dis.total,warehouse_dis.igst, warehouse_dis.rate, d.name as from_dealer, pc.name as product_company_id, fd.name as dealer_name, warehouse_dis.quantity as product_total_qty, warehouse_dis.created_at as date, products.name as product_name FROM warehouse_dis join dealers as fd on warehouse_dis.dealer_id=fd.id join product_companies as pc on warehouse_dis.product_company_id=pc.id left join dealers as d on warehouse_dis.from_dealer_id=d.id left join products on products.id=warehouse_dis.product_id WHERE product_company_id IS NOT NULL and warehouse_dis.product_id IN(" . $cement_product_ids . ") and warehouse_dis.product_brand_id = " . $request->product_brand_id . " and warehouse_dis.created_at BETWEEN '$start_date' AND '$end_date' ORDER BY warehouse_dis.id DESC) as t1"; } if($request->purchase_type == "company") { $result1 = "select t1.* from (SELECT company_dis.product_id, d.name as dealer_name, pc.name as product_company_id, company_dis.rate, company_dis.total, company_dis.igst, company_dis.quantity as product_total_qty, company_dis.created_at as date, product_companies.name as brand_name, p.name as product_name FROM company_dis join dealers as d on company_dis.dealer_id=d.id join product_companies as pc on company_dis.product_company_id=pc.id join products as p on p.id=company_dis.product_id join product_companies on product_companies.id=company_dis.product_company_id WHERE product_company_id IS NOT NULL and company_dis.product_id IN(" . $cement_product_ids . ") and company_dis.product_company_id = " . $request->product_brand_id . " and company_dis.created_at BETWEEN '$start_date' AND '$end_date' ORDER BY company_dis.id DESC) as t1"; } } else { if($request->purchase_type == "warehouse"){ $result = "select t1.* from (SELECT warehouse_dis.product_id, pc.name as product_company_id, warehouse_dis.total, warehouse_dis.rate, warehouse_dis.igst, d.name as from_dealer, fd.name as dealer_name, warehouse_dis.quantity as product_total_qty, warehouse_dis.created_at as date, products.name as product_name FROM warehouse_dis left join dealers as fd on warehouse_dis.dealer_id=fd.id join product_companies as pc on warehouse_dis.product_company_id=pc.id left join dealers as d on warehouse_dis.from_dealer_id=d.id join products on products.id=warehouse_dis.product_id WHERE dealer_id = " . $request->company_id . " and warehouse_dis.product_id IN(" . $cement_product_ids . ") and warehouse_dis.product_brand_id = " . $request->product_brand_id . " and warehouse_dis.created_at BETWEEN '$start_date' AND '$end_date' ORDER BY warehouse_dis.id DESC) as t1"; } if($request->purchase_type == "company") { $result1 = "select t1.* from (SELECT company_dis.product_id, d.name as dealer_name, company_dis.rate, pc.name as product_company_id, company_dis.total, company_dis.igst, company_dis.quantity as product_total_qty, company_dis.created_at as date, p.name as product_name FROM company_dis join dealers as d on company_dis.dealer_id=d.id join product_companies as pc on company_dis.product_company_id=pc.id join products as p on p.id=company_dis.product_id WHERE dealer_id = " . $request->company_id . " and company_dis.product_company_id = " . $request->product_brand_id . " and company_dis.product_company_id = " . $request->product_brand_id . " and company_dis.product_id IN(" . $cement_product_ids . ") and company_dis.created_at BETWEEN '$start_date' AND '$end_date' ORDER BY company_dis.id DESC) as t1"; } } } } else { if ($request->type == "sales") { if ($request->product_category_id == "npk") { $productArr = "9, 10, 11, 12, 65, 103"; if ($request->company_id == "company") { $results = "select t1.* from (SELECT loading_slip_invoices.product_id, loading_slip_invoices.quantity as product_total_qty, loading_slip_invoices.created_at as date, loading_slip_invoices.freight_discount as freight_discount, loading_slip_invoices.secondary_discount as secondary_discount , product_companies.name as brand_name, products.name as product_name, loading_slip_invoices.retailer_name, dealers.name as dealersale, loading_slip_invoices.rate, loading_slip_invoices.total FROM loading_slip_invoices join dealers on dealers.id=loading_slip_invoices.dealer_id join products on products.id=loading_slip_invoices.product_id join product_companies on product_companies.id=loading_slip_invoices.product_company_id WHERE loading_slip_invoices.product_id IN(" . $productArr . ") and loading_slip_invoices.created_at BETWEEN '$start_date' AND '$end_date' ORDER BY loading_slip_invoices.id DESC) as t1"; } else { $results = "select t1.* from (SELECT loading_slip_invoices.product_id, loading_slip_invoices.quantity as product_total_qty, loading_slip_invoices.created_at as date, loading_slip_invoices.freight_discount as freight_discount, loading_slip_invoices.secondary_discount as secondary_discount , products.name as product_name, loading_slip_invoices.retailer_name, dealers.name as dealersale, loading_slip_invoices.rate, loading_slip_invoices.total FROM loading_slip_invoices join dealers on dealers.id=loading_slip_invoices.dealer_id join product_loadings on product_loadings.id=loading_slip_invoices.loading_slip_id join products on products.id=loading_slip_invoices.product_id WHERE loading_slip_invoices.dealer_id = '$request->company_id' AND loading_slip_invoices.product_id IN(" . $productArr . ") and product_loadings.product_company_id = " . $request->product_brand_id . " and loading_slip_invoices.created_at BETWEEN '$start_date' AND '$end_date' ORDER BY loading_slip_invoices.id DESC) as t1"; } } else { if ($request->company_id == "company") { $productAr = $request->product_category_id; $results = "select t1.* from (SELECT loading_slip_invoices.product_id, loading_slip_invoices.quantity as product_total_qty, loading_slip_invoices.created_at as date, loading_slip_invoices.freight_discount as freight_discount, loading_slip_invoices.secondary_discount as secondary_discount , product_companies.name as brand_name, products.name as product_name, loading_slip_invoices.retailer_name, dealers.name as dealersale, loading_slip_invoices.rate, loading_slip_invoices.total FROM loading_slip_invoices join dealers on dealers.id=loading_slip_invoices.dealer_id join products on products.id=loading_slip_invoices.product_id join product_companies on product_companies.id=loading_slip_invoices.product_id WHERE loading_slip_invoices.product_id = '$productAr' and loading_slip_invoices.created_at BETWEEN '$start_date' AND '$end_date' ORDER BY loading_slip_invoices.id DESC) as t1"; } else { $productAr = $request->product_category_id; $results = "select t1.* from (SELECT loading_slip_invoices.product_id, loading_slip_invoices.quantity as product_total_qty, loading_slip_invoices.created_at as date, loading_slip_invoices.freight_discount as freight_discount, loading_slip_invoices.secondary_discount as secondary_discount , products.name as product_name, loading_slip_invoices.retailer_name, dealers.name as dealersale, loading_slip_invoices.rate, loading_slip_invoices.total FROM loading_slip_invoices join dealers on dealers.id=loading_slip_invoices.dealer_id join product_loadings on product_loadings.id=loading_slip_invoices.loading_slip_id join products on products.id=loading_slip_invoices.product_id WHERE loading_slip_invoices.dealer_id = '$request->company_id' AND loading_slip_invoices.product_id IN(" . $productAr . ") and product_loadings.product_company_id = " . $request->product_brand_id . " and loading_slip_invoices.created_at BETWEEN '$start_date' AND '$end_date' ORDER BY loading_slip_invoices.id DESC) as t1"; } } } if ($request->type == "purchase") { if ($request->product_category_id == "npk") { $productArr = "9, 10, 11, 12, 65, 103"; if ($request->company_id == "company") { if($request->purchase_type == "warehouse"){ $result = "select t1.* from (SELECT warehouse_dis.product_id, pc.name as product_company_id, warehouse_dis.total, warehouse_dis.rate, warehouse_dis.igst, d.name as from_dealer, fd.name as dealer_name, warehouse_dis.quantity as product_total_qty, warehouse_dis.created_at as date, products.name as product_name FROM warehouse_dis left join dealers as fd on warehouse_dis.dealer_id=fd.id left join product_companies as pc on warehouse_dis.product_company_id=pc.id left join dealers as d on warehouse_dis.from_dealer_id=d.id left join products on products.id=warehouse_dis.product_id WHERE product_company_id IS NOT NULL and warehouse_dis.product_id IN(" . $productArr . ") and warehouse_dis.product_brand_id = " . $request->product_brand_id . " and warehouse_dis.created_at BETWEEN '$start_date' AND '$end_date' ORDER BY warehouse_dis.id DESC) as t1"; } if($request->purchase_type == "company") { $result1 = "select t1.* from (SELECT company_dis.product_id, d.name as dealer_name, company_dis.rate, company_dis.igst, pc.name as product_company_id, company_dis.total, company_dis.quantity as product_total_qty, company_dis.created_at as date, p.name as product_name FROM company_dis join dealers as d on company_dis.dealer_id=d.id join product_companies as pc on company_dis.product_company_id=pc.id join products as p on p.id=company_dis.product_id WHERE product_company_id IS NOT NULL and company_dis.product_id IN(" . $productArr . ") and company_dis.product_company_id = " . $request->product_brand_id . " and company_dis.created_at BETWEEN '$start_date' AND '$end_date' ORDER BY company_dis.id DESC) as t1"; } } else { if($request->purchase_type == "warehouse"){ $result = "select t1.* from (SELECT warehouse_dis.product_id, pc.name as product_company_id, warehouse_dis.total, warehouse_dis.rate, warehouse_dis.igst, d.name as from_dealer, fd.name as dealer_name, warehouse_dis.quantity as product_total_qty, warehouse_dis.created_at as date, products.name as product_name FROM warehouse_dis left join dealers as fd on warehouse_dis.dealer_id=fd.id join product_companies as pc on warehouse_dis.product_company_id=pc.id left join dealers as d on warehouse_dis.from_dealer_id=d.id left join products on products.id=warehouse_dis.product_id WHERE dealer_id = " . $request->company_id . " and warehouse_dis.product_id IN(" . $productArr . ") and warehouse_dis.product_brand_id = " . $request->product_brand_id . " and warehouse_dis.created_at BETWEEN '$start_date' AND '$end_date' ORDER BY warehouse_dis.id DESC) as t1"; } if($request->purchase_type == "company"){$result1 = "select t1.* from (SELECT company_dis.product_id, d.name as dealer_name, pc.name as product_company_id, company_dis.rate, company_dis.igst, company_dis.total, company_dis.quantity as product_total_qty, company_dis.created_at as date, p.name as product_name FROM company_dis join dealers as d on company_dis.dealer_id=d.id join product_companies as pc on company_dis.product_company_id=pc.id join products as p on p.id=company_dis.product_id WHERE dealer_id = " . $request->company_id . " and company_dis.product_id IN(" . $productArr . ") and company_dis.product_company_id = " . $request->product_brand_id . " and company_dis.created_at BETWEEN '$start_date' AND '$end_date' ORDER BY company_dis.id DESC) as t1"; } } } else { if ($request->company_id == "company") { $productArr = $request->product_category_id; if($request->purchase_type == "warehouse"){ $result = "select t1.* from (SELECT warehouse_dis.product_id, pc.name as product_company_id, warehouse_dis.total, warehouse_dis.rate, warehouse_dis.igst, d.name as from_dealer, fd.name as dealer_name, warehouse_dis.quantity as product_total_qty, warehouse_dis.created_at as date, products.name as product_name FROM warehouse_dis left join dealers as fd on warehouse_dis.dealer_id=fd.id join product_companies as pc on warehouse_dis.product_company_id=pc.id left join dealers as d on warehouse_dis.from_dealer_id=d.id left join products on products.id=warehouse_dis.product_id WHERE product_company_id IS NOT NULL and warehouse_dis.product_id = " . $productArr . " and warehouse_dis.product_brand_id = " . $request->product_brand_id . " and warehouse_dis.created_at BETWEEN '$start_date' AND '$end_date' ORDER BY warehouse_dis.id DESC) as t1"; } if($request->purchase_type == "company"){ $result1 = "select t1.* from (SELECT company_dis.product_id, d.name as dealer_name, pc.name as product_company_id, company_dis.rate, company_dis.igst, company_dis.total, company_dis.quantity as product_total_qty, company_dis.created_at as date, p.name as product_name FROM company_dis join dealers as d on company_dis.dealer_id=d.id join product_companies as pc on company_dis.product_company_id=pc.id join products as p on p.id=company_dis.product_id WHERE product_company_id IS NOT NULL and company_dis.product_id = " . $productArr . " and company_dis.created_at BETWEEN '$start_date' AND '$end_date' ORDER BY company_dis.id DESC) as t1"; } } else { $productArr = $request->product_category_id; if($request->purchase_type == "warehouse"){ $result = "select t1.* from (SELECT warehouse_dis.product_id, pc.name as product_company_id, warehouse_dis.total, warehouse_dis.rate, warehouse_dis.igst, d.name as from_dealer, fd.name as dealer_name, warehouse_dis.quantity as product_total_qty, warehouse_dis.created_at as date, products.name as product_name FROM warehouse_dis left join dealers as fd on warehouse_dis.dealer_id=fd.id join product_companies as pc on warehouse_dis.product_company_id=pc.id left join dealers as d on warehouse_dis.from_dealer_id=d.id left join products on products.id=warehouse_dis.product_id WHERE dealer_id = " . $request->company_id . " and warehouse_dis.product_id = " . $productArr . " and warehouse_dis.product_brand_id = " . $request->product_brand_id . " and warehouse_dis.created_at BETWEEN '$start_date' AND '$end_date' ORDER BY warehouse_dis.id DESC) as t1"; } if($request->purchase_type == "company"){ $result1 = "select t1.* from (SELECT company_dis.product_id, d.name as dealer_name, pc.name as product_company_id, company_dis.rate, company_dis.igst, company_dis.total, company_dis.quantity as product_total_qty, company_dis.created_at as date, p.name as product_name FROM company_dis join dealers as d on company_dis.dealer_id=d.id join product_companies as pc on company_dis.product_company_id=pc.id join products as p on p.id=company_dis.product_id WHERE dealer_id = " . $request->company_id . " and company_dis.product_id = " . $productArr . " and company_dis.product_company_id = " . $request->product_brand_id . " and company_dis.created_at BETWEEN '$start_date' AND '$end_date' ORDER BY company_dis.id DESC) as t1"; } } } } } if(!empty($results)){ $sql = \DB::select($results); $tokens = $sql; } if(!empty($result)){ $sql0 = \DB::select($result); $tokens = $sql0; } if(!empty($result1)){ $sql1 = \DB::select($result1); $tokens = $sql1; } $data['tokens'] = $tokens; $response['flag'] = true; $response['data'] = $data; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function salePurchasebrandsWise(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'company_id' => 'required', 'product_category_id' => 'required', // 'report_date' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $sql = 0; $sql1 = 0; $sql0 = 0; $result = 0; $results = 0; $result1 = 0; $start_date = date('Y-m-d', strtotime($request->date)); $end_date = date('Y-m-d', strtotime($request->todate)); if ($request->product_category_id == 2) { $cement_product_ids = \App\Product::where('product_category_id', $request->product_category_id)->pluck('id'); $cement_product_ids = json_encode($cement_product_ids, true); $cement_product_ids = json_decode($cement_product_ids, true); $cement_product_ids = implode(', ', $cement_product_ids); if ($request->type == "sales") { $results = "select t1.* from (SELECT sum(loading_slip_invoices.quantity) as product_total_qty, product_loadings.product_company_name as brand_name, product_loadings.product_company_id as brand_id FROM loading_slip_invoices join products on products.id=loading_slip_invoices.product_id join product_loadings on product_loadings.id=loading_slip_invoices.loading_slip_id join product_companies on product_companies.id=loading_slip_invoices.company_id WHERE loading_slip_invoices.dealer_id = " . $request->company_id . " and loading_slip_invoices.product_id IN(" . $cement_product_ids . ") and loading_slip_invoices.created_at BETWEEN '$start_date' AND '$end_date' GROUP BY product_loadings.product_company_id) as t1"; } if ($request->type == "purchase") { $result = "select t1.* from (SELECT warehouse_dis.product_id, sum(warehouse_dis.quantity) as product_total_qty, product_companies.name as brand_name, product_companies.id as brand_id FROM warehouse_dis join products on products.id=warehouse_dis.product_id join product_companies on product_companies.id=warehouse_dis.product_brand_id WHERE dealer_id = " . $request->company_id . " and warehouse_dis.product_id IN(" . $cement_product_ids . ") and warehouse_dis.created_at BETWEEN '$start_date' AND '$end_date' GROUP BY warehouse_dis.product_brand_id) as t1"; $result1 = "select t1.* from (SELECT company_dis.product_id, sum(company_dis.quantity) as product_total_qty, product_companies.name as brand_name, product_companies.id as brand_id FROM company_dis join products on products.id=company_dis.product_id join product_companies on product_companies.id=company_dis.product_company_id WHERE dealer_id = " . $request->company_id . " and company_dis.product_id IN(" . $cement_product_ids . ") and company_dis.created_at BETWEEN '$start_date' AND '$end_date' GROUP BY company_dis.product_company_id) as t1"; } } else { if ($request->type == "sales") { if ($request->product_category_id == "npk") { $productArr = "9, 10, 11, 12, 65, 103"; $results = "select t1.* from (SELECT sum(loading_slip_invoices.quantity) as product_total_qty, product_loadings.product_company_name as brand_name, product_loadings.product_company_id as brand_id FROM loading_slip_invoices join products on products.id=loading_slip_invoices.product_id join product_loadings on product_loadings.id=loading_slip_invoices.loading_slip_id join product_companies on product_companies.id=loading_slip_invoices.company_id WHERE loading_slip_invoices.dealer_id = " . $request->company_id . " and product_loadings.product_id IN(" . $productArr . ") and loading_slip_invoices.created_at BETWEEN '$start_date' AND '$end_date' GROUP BY product_loadings.product_company_id) as t1"; } else { $productArr = $request->product_category_id; $results = "select t1.* from (SELECT sum(loading_slip_invoices.quantity) as product_total_qty, product_loadings.product_company_name as brand_name, product_loadings.product_company_id as brand_id FROM loading_slip_invoices join products on products.id=loading_slip_invoices.product_id join product_loadings on product_loadings.id=loading_slip_invoices.loading_slip_id join product_companies on product_companies.id=loading_slip_invoices.company_id WHERE loading_slip_invoices.dealer_id = " . $request->company_id . " and product_loadings.product_id IN(" . $productArr . ") and loading_slip_invoices.created_at BETWEEN '$start_date' AND '$end_date' GROUP BY product_loadings.product_company_id) as t1"; } } if ($request->type == "purchase") { if ($request->product_category_id == "npk") { $productArr = "9, 10, 11, 12, 65, 103"; $result = "select t1.* from (SELECT warehouse_dis.product_id, sum(warehouse_dis.quantity) as product_total_qty, product_companies.name as brand_name, product_companies.id as brand_id FROM warehouse_dis join products on products.id=warehouse_dis.product_id join product_companies on product_companies.id=warehouse_dis.product_brand_id WHERE dealer_id = " . $request->company_id . " and warehouse_dis.product_id IN(" . $productArr . ") and warehouse_dis.created_at BETWEEN '$start_date' AND '$end_date' GROUP BY warehouse_dis.product_brand_id) as t1"; $result1 = "select t1.* from (SELECT company_dis.product_id, sum(company_dis.quantity) as product_total_qty, product_companies.name as brand_name, product_companies.id as brand_id FROM company_dis join products on products.id=company_dis.product_id join product_companies on product_companies.id=company_dis.product_company_id WHERE dealer_id = " . $request->company_id . " and company_dis.product_id IN(" . $productArr . ") and company_dis.created_at BETWEEN '$start_date' AND '$end_date' GROUP BY company_dis.product_company_id) as t1"; } else { $productArr = $request->product_category_id; $result = "select t1.* from (SELECT warehouse_dis.product_id, sum(warehouse_dis.quantity) as product_total_qty, product_companies.name as brand_name, product_companies.id as brand_id FROM warehouse_dis join products on products.id=warehouse_dis.product_id join product_companies on product_companies.id=warehouse_dis.product_brand_id WHERE dealer_id = " . $request->company_id . " and warehouse_dis.product_id IN(" . $productArr . ") and warehouse_dis.created_at BETWEEN '$start_date' AND '$end_date' GROUP BY warehouse_dis.product_brand_id) as t1"; $result1 = "select t1.* from (SELECT company_dis.product_id, sum(company_dis.quantity) as product_total_qty, product_companies.name as brand_name, product_companies.id as brand_id FROM company_dis join products on products.id=company_dis.product_id join product_companies on product_companies.id=company_dis.product_company_id WHERE dealer_id = " . $request->company_id . " and company_dis.product_id IN(" . $productArr . ") and company_dis.created_at BETWEEN '$start_date' AND '$end_date' GROUP BY company_dis.product_company_id) as t1"; } } } $tokens = array(); if (!empty($results)) { $sql = \DB::select($results); $tokens['sale'] = $sql; } if (!empty($result)) { $sql0 = \DB::select($result); $tokens['purchase_warehouse_di'] = $sql0; } if (!empty($result1)) { $sql1 = \DB::select($result1); $tokens['purchase_company_di'] = $sql1; } $data['tokens'] = $tokens; $response['flag'] = true; $response['data'] = $data; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function otherDealerWise(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', //'company_id' => 'required', 'product_category_id' => 'required', // 'report_date' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $other_dealer = array(); $company_stock = array(); $cement_product_ids = \App\Product::select('id')->where('product_category_id', 2)->get()->toArray(); $dealerArr = array(1,30, 31); if($request->type == "sales"){ if ($request->product_category_id == 2) { $stockData = \DB::table('loading_slip_invoices')->select(\DB::raw('sum(loading_slip_invoices.quantity) as total_stock'), 'loading_slip_invoices.dealer_id as company_id')->join('products', 'products.id', 'loading_slip_invoices.product_id')->where('products.product_category_id', $request->product_category_id)->whereBetween('loading_slip_invoices.created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->whereNotIn('loading_slip_invoices.dealer_id', $dealerArr)->groupBy('loading_slip_invoices.dealer_id')->get()->toArray(); $stockData = json_decode(json_encode($stockData), true); } else { if ($request->product_category_id == "npk") { $productArr = array(9, 10, 11, 12, 65, 103); $stockData = \DB::table('loading_slip_invoices')->select(\DB::raw('sum(loading_slip_invoices.quantity) as total_stock'), 'loading_slip_invoices.dealer_id as company_id')->join('products', 'products.id', 'loading_slip_invoices.product_id')->whereIn('products.id', $productArr)->whereBetween('loading_slip_invoices.created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->whereNotIn('loading_slip_invoices.dealer_id', $dealerArr)->groupBy('loading_slip_invoices.dealer_id')->get()->toArray(); $stockData = json_decode(json_encode($stockData), true); } else { $productArr = $request->product_category_id; $stockData = \DB::table('loading_slip_invoices')->select(\DB::raw('sum(loading_slip_invoices.quantity) as total_stock'), 'loading_slip_invoices.dealer_id as company_id')->join('products', 'products.id', 'loading_slip_invoices.product_id')->where('products.id', $productArr)->whereBetween('loading_slip_invoices.created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->whereNotIn('loading_slip_invoices.dealer_id', $dealerArr)->groupBy('loading_slip_invoices.dealer_id')->get()->toArray(); $stockData = json_decode(json_encode($stockData), true); } } } if($request->type == "purchase"){ if ($request->product_category_id == 2) { $stockData['warehouse_di'] = \DB::table('warehouse_dis')->select(\DB::raw('sum(warehouse_dis.quantity) as total_stock'), 'warehouse_dis.dealer_id as company_id')->join('products', 'products.id', 'warehouse_dis.product_id')->where('products.product_category_id', $request->product_category_id)->whereBetween('warehouse_dis.created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->whereNotIn('warehouse_dis.dealer_id', $dealerArr)->groupBy('warehouse_dis.dealer_id')->get()->toArray(); $stockData['company_di'] = \DB::table('company_dis')->select(\DB::raw('sum(company_dis.quantity) as total_stock'), 'company_dis.dealer_id as company_id')->join('products', 'products.id', 'company_dis.product_id')->where('products.product_category_id', $request->product_category_id)->whereBetween('company_dis.created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->whereNotIn('company_dis.dealer_id', $dealerArr)->groupBy('company_dis.dealer_id')->get()->toArray(); $stockData = json_decode(json_encode($stockData), true); } else { if ($request->product_category_id == "npk") { $productArr = array(9, 10, 11, 12, 65, 103); $stockData['warehouse_di'] = \DB::table('warehouse_dis')->select(\DB::raw('sum(warehouse_dis.quantity) as total_stock'), 'warehouse_dis.dealer_id as company_id')->join('products', 'products.id', 'warehouse_dis.product_id')->whereIn('products.id', $productArr)->whereBetween('warehouse_dis.created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->whereNotIn('warehouse_dis.dealer_id', $dealerArr)->groupBy('warehouse_dis.dealer_id')->get()->toArray(); $stockData['company_di'] = \DB::table('company_dis')->select(\DB::raw('sum(company_dis.quantity) as total_stock'), 'company_dis.dealer_id as company_id')->join('products', 'products.id', 'company_dis.product_id')->whereIn('products.id', $productArr)->whereBetween('company_dis.created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->whereNotIn('company_dis.dealer_id', $dealerArr)->groupBy('company_dis.dealer_id')->get()->toArray(); $stockData = json_decode(json_encode($stockData), true); } else { $productArr = $request->product_category_id; $stockData['warehouse_di'] = \DB::table('warehouse_dis')->select(\DB::raw('sum(warehouse_dis.quantity) as total_stock'), 'warehouse_dis.dealer_id as company_id')->join('products', 'products.id', 'warehouse_dis.product_id')->where('products.id', $productArr)->whereBetween('warehouse_dis.created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->whereNotIn('warehouse_dis.dealer_id', $dealerArr)->groupBy('warehouse_dis.dealer_id')->get()->toArray(); $stockData['company_di'] = \DB::table('company_dis')->select(\DB::raw('sum(company_dis.quantity) as total_stock'), 'company_dis.dealer_id as company_id')->join('products', 'products.id', 'company_dis.product_id')->where('products.id', $productArr)->whereBetween('company_dis.created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->whereNotIn('company_dis.dealer_id', $dealerArr)->groupBy('company_dis.dealer_id')->get()->toArray(); $stockData = json_decode(json_encode($stockData), true); } } } if($request->type == "sales"){ foreach ($stockData as $key => $value) { $stockData[$key]['company_name'] = getModelById('Dealer', $value['company_id'])->name . ' (' . getModelById('Dealer', $value['company_id'])->address1 . ')'; } } if($request->type == "purchase"){ // dd($stockData); foreach ($stockData as $key1=>$stockdatas) { foreach($stockdatas as $key => $value ){ $stockData[$key1][$key]['company_name'] = getModelById('Dealer', $value['company_id'])->name . ' (' . getModelById('Dealer', $value['company_id'])->address1 . ')'; // $stockData[$key]['company_name'] = getModelById('Dealer', $value['company_id'])->name . ' (' . getModelById('Dealer', $value['company_id'])->address1 . ')'; } } } $response['flag'] = true; $response['data']['tokens'] = $stockData; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function tokenTotal(Request $request) { try { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', //'status' => 'required', //'id' => 'required' ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { // if(1){ if ($this->checkApiAuth($request)) { $cement_product_ids = \App\Product::select('id')->where('product_category_id', 2)->get()->toArray(); // dd($cement_product_ids); // \DB::enableQueryLog(); $response['cement_stock'] = \App\Token::whereIn('product_id', $cement_product_ids)->whereNotNull('account_from_id')->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $response['dap_stock'] = \App\Token::where('product_id', 4)->whereNotNull('account_from_id')->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $response['npk_stock'] = \App\Token::whereIn('product_id', array(9, 10, 11, 12, 65, 103))->whereNotNull('dealer_id')->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $response['urea_stock'] = \App\Token::where('product_id', 19)->whereNotNull('account_from_id')->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); $response['mop_stock'] = \App\Token::where('product_id', 7)->whereNotNull('account_from_id')->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->sum('quantity'); //->whereIn('dealer_id', [1, 3, 30, 31]) } return response()->json($response); } } //catch exception catch (Exception $e) { echo 'Message: ' . $e->getMessage(); } } public function tokenCompanyQuantity(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', //'company_id' => 'required', 'product_category_id' => 'required', // 'report_date' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $other_dealer = array(); $company_stock = array(); $cement_product_ids = \App\Product::select('id')->where('product_category_id', 2)->get()->toArray(); $dealerArr = array(1,30, 31); if ($request->product_category_id == 2) { $stockData = \DB::table('tokens')->select(\DB::raw('sum(tokens.quantity) as total_stock'), 'tokens.account_from_id as company_id')->join('products', 'products.id', 'tokens.product_id')->whereBetween('tokens.created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->whereIn('tokens.product_id', $cement_product_ids)->whereIn('tokens.account_from_id', $dealerArr)->groupBy('tokens.account_from_id')->get()->toArray(); $other_dealer['total_stock'] = \App\Token::whereIn('product_id', $cement_product_ids)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->whereNotIn('account_from_id', $dealerArr)->sum('quantity'); $other_dealer['company_id'] = "other_dealer"; array_push($stockData, $other_dealer); // $company_stock['total_stock'] = \App\Token::whereIn('product_id', $cement_product_ids)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->whereNotNull('product_company_id')->sum('quantity'); // $company_stock['company_id'] = "company"; // array_push($stockData, $company_stock); $stockData = json_decode(json_encode($stockData), true); } else { $other_dealer = array(); $company_stock = array(); if ($request->product_category_id == "npk") { $productArr = array(9, 10, 11, 12, 65, 103); $stockData = \DB::table('tokens')->select(\DB::raw('sum(tokens.quantity) as total_stock'), 'tokens.account_from_id as company_id')->join('products', 'products.id', 'tokens.product_id')->whereBetween('tokens.created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->whereIn('tokens.product_id', $productArr)->whereIn('tokens.account_from_id', $dealerArr)->groupBy('tokens.account_from_id')->get()->toArray(); $other_dealer['total_stock'] = \App\Token::whereIn('product_id', $productArr)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->whereNotIn('account_from_id', $dealerArr)->sum('quantity'); $other_dealer['company_id'] = "other_dealer"; array_push($stockData, $other_dealer); // $company_stock['total_stock'] = \App\Token::whereIn('product_id', $productArr)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->whereNotNull('product_company_id')->sum('quantity'); // $company_stock['company_id'] = "company"; // array_push($stockData, $company_stock); $stockData = json_decode(json_encode($stockData), true); } else { $other_dealer = array(); $company_stock = array(); $productArr = $request->product_category_id; $stockData = \DB::table('tokens')->select(\DB::raw('sum(tokens.quantity) as total_stock'), 'tokens.account_from_id as company_id')->join('products', 'products.id', 'tokens.product_id')->whereBetween('tokens.created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->where('products.id', $productArr)->whereIn('tokens.account_from_id', $dealerArr)->groupBy('tokens.account_from_id')->get()->toArray(); $other_dealer['total_stock'] = \App\Token::where('product_id', $productArr)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->whereNotIn('account_from_id', $dealerArr)->sum('quantity'); $other_dealer['company_id'] = "other_dealer"; array_push($stockData, $other_dealer); // $company_stock['total_stock'] = \App\Token::where('product_id', $productArr)->whereBetween('created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->whereNotNull('product_company_id')->sum('quantity'); // $company_stock['company_id'] = "company"; // array_push($stockData, $company_stock); $stockData = json_decode(json_encode($stockData), true); } } foreach ($stockData as $key => $value) { if ($value['company_id'] == 1) { $stockData[$key]['company_name'] = 'MRSD-STP'; } // else if ($value['company_id'] == 3) { // $stockData[$key]['company_name'] = 'MRSD-LRP'; // } else if ($value['company_id'] == 30) { $stockData[$key]['company_name'] = 'MCPL-STP'; } else if ($value['company_id'] == 31) { $stockData[$key]['company_name'] = 'MCPL-DEVI KALI RD'; } else if ($value['company_id'] == "other_dealer") { $stockData[$key]['company_name'] = 'OTHER DEALER'; } // else if ($value['company_id'] == "company") { // $stockData[$key]['company_name'] = 'COMPANY STOCK'; // } } $response['flag'] = true; $response['data']['tokens'] = $stockData; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function TokenBrandsWise(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'company_id' => 'required', // 'product_category_id' => 'required', // 'report_date' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $start_date = date('Y-m-d', strtotime($request->date)); $end_date = date('Y-m-d', strtotime($request->todate)); if ($request->product_category_id == 2) { $cement_product_ids = \App\Product::where('product_category_id', $request->product_category_id)->pluck('id'); $cement_product_ids = json_encode($cement_product_ids, true); $cement_product_ids = json_decode($cement_product_ids, true); $cement_product_ids = implode(', ', $cement_product_ids); if ($request->company_id == "company") { $sql = "select t1.* from (SELECT tokens.product_id, sum(tokens.quantity) as product_total_qty, product_companies.name as brand_name, product_companies.id as brand_id FROM tokens join products on products.id=tokens.product_id join product_companies on product_companies.id=tokens.product_company_id WHERE product_company_id IS NOT NULL and tokens.product_id IN(" . $cement_product_ids . ") and tokens.created_at BETWEEN '$start_date' AND '$end_date' GROUP BY tokens.product_company_id) as t1"; } else { $sql = "select t1.* from (SELECT tokens.product_id, sum(tokens.quantity) as product_total_qty, product_companies.name as brand_name, product_companies.id as brand_id FROM tokens join products on products.id=tokens.product_id join product_companies on product_companies.id=tokens.product_company_id WHERE account_from_id = " . $request->company_id . " and tokens.product_id IN(" . $cement_product_ids . ") and tokens.created_at BETWEEN '$start_date' AND '$end_date' GROUP BY tokens.product_company_id) as t1"; } } else { if ($request->product_category_id == "npk") { $productArr = "9, 10, 11, 12, 65, 103"; if ($request->company_id == "company") { $sql = "select t1.* from (SELECT tokens.product_id, sum(tokens.quantity) as product_total_qty, product_companies.name as brand_name , product_companies.id as brand_id FROM tokens join products on products.id=tokens.product_id join product_companies on product_companies.id=tokens.product_company_id WHERE product_company_id IS NOT NULL and tokens.product_id IN(" . $productArr . ") and tokens.created_at BETWEEN '$start_date' AND '$end_date' GROUP BY tokens.product_company_id) as t1"; } else { $sql = "select t1.* from (SELECT tokens.product_id, sum(tokens.quantity) as product_total_qty, product_companies.name as brand_name , product_companies.id as brand_id FROM tokens join products on products.id=tokens.product_id join product_companies on product_companies.id=tokens.product_company_id WHERE account_from_id = " . $request->company_id . " and tokens.product_id IN(" . $productArr . ") and tokens.created_at BETWEEN '$start_date' AND '$end_date' GROUP BY tokens.product_company_id) as t1"; } } else { if ($request->company_id == "company") { $productArr = $request->product_category_id; $sql = "select t1.* from (SELECT tokens.product_id, sum(tokens.quantity) as product_total_qty, product_companies.name as brand_name, product_companies.id as brand_id FROM tokens join products on products.id=tokens.product_id join product_companies on product_companies.id=tokens.product_company_id WHERE product_company_id IS NOT NULL and tokens.product_id = " . $productArr . " and tokens.created_at BETWEEN '$start_date' AND '$end_date' GROUP BY tokens.product_company_id) as t1"; } else { $productArr = $request->product_category_id; $sql = "select t1.* from (SELECT tokens.product_id, sum(tokens.quantity) as product_total_qty, product_companies.name as brand_name, product_companies.id as brand_id FROM tokens join products on products.id=tokens.product_id join product_companies on product_companies.id=tokens.product_company_id WHERE account_from_id = " . $request->company_id . " and tokens.product_id = " . $productArr . " and tokens.created_at BETWEEN '$start_date' AND '$end_date' GROUP BY tokens.product_company_id) as t1"; } } } $tokens = \DB::select($sql); $data['tokens'] = $tokens; $response['flag'] = true; $response['data'] = $data; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function TokenProductsWise(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', 'company_id' => 'required', // 'product_category_id' => 'required', // 'report_date' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $start_date = date('Y-m-d', strtotime($request->date)); $end_date = date('Y-m-d', strtotime($request->todate)); if ($request->product_category_id == 2) { $cement_product_ids = \App\Product::where('product_category_id', $request->product_category_id)->pluck('id'); $cement_product_ids = json_encode($cement_product_ids, true); $cement_product_ids = json_decode($cement_product_ids, true); $cement_product_ids = implode(', ', $cement_product_ids); if ($request->company_id == "company") { $sql = "select t1.* from (SELECT tokens.product_id, tokens.quantity as product_total_qty,tokens.unique_id as token_id, tokens.created_at as date, tokens.master_rake_id as rake_id, products.name as brand_name, w.name as warehouse, r.name as retailer, tokens.token_type, sell_id.name as sell_admin, purchase_id.name as purchase_admin FROM tokens left join warehouses as w on tokens.from_warehouse_id=w.id left join retailers as r ON tokens.retailer_id=r.id left join dealers as sell_id ON tokens.dealer_id=sell_id.id left join dealers as purchase_id ON tokens.account_from_id=purchase_id.id join products on products.id=tokens.product_id left join product_companies on product_companies.id=tokens.product_company_id WHERE product_company_id IS NOT NULL and tokens.product_id IN(" . $cement_product_ids . ") and tokens.product_company_id = " . $request->product_brand_id . " and tokens.created_at BETWEEN '$start_date' AND '$end_date') as t1 ORDER BY t1.date DESC"; } else { $sql = "select t1.* from (SELECT tokens.product_id, tokens.quantity as product_total_qty,tokens.unique_id as token_id, tokens.created_at as date, products.name as brand_name, w.name as warehouse, r.name as retailer, tokens.token_type, sell_id.name as sell_admin, purchase_id.name as purchase_admin FROM tokens left join warehouses as w on tokens.from_warehouse_id=w.id left join retailers as r ON tokens.retailer_id=r.id left join dealers as sell_id ON tokens.dealer_id=sell_id.id left join dealers as purchase_id ON tokens.account_from_id=purchase_id.id join products on products.id=tokens.product_id left join product_companies on product_companies.id=tokens.product_company_id WHERE account_from_id = " . $request->company_id . " and tokens.product_id IN(" . $cement_product_ids . ") and tokens.product_company_id = " . $request->product_brand_id . " and tokens.created_at BETWEEN '$start_date' AND '$end_date') as t1 ORDER BY t1.date DESC"; } } else { if ($request->product_category_id == "npk") { $productArr = "9, 10, 11, 12, 65, 103"; if ($request->company_id == "company") { $sql = "select t1.* from (SELECT tokens.product_id, tokens.quantity as product_total_qty,tokens.unique_id as token_id, tokens.created_at as date, products.name as brand_name, w.name as warehouse, r.name as retailer, tokens.token_type, sell_id.name as sell_admin, purchase_id.name as purchase_admin FROM tokens left join warehouses as w on tokens.from_warehouse_id=w.id left join retailers as r ON tokens.retailer_id=r.id left join dealers as sell_id ON tokens.account_from_id=sell_id.id left join dealers as purchase_id ON tokens.account_from_id=purchase_id.id join products on products.id=tokens.product_id left join product_companies on product_companies.id=tokens.product_company_id WHERE product_company_id IS NOT NULL and tokens.product_id IN(" . $productArr . ") and tokens.product_company_id = " . $request->product_brand_id . " and tokens.created_at BETWEEN '$start_date' AND '$end_date') as t1 ORDER BY t1.date DESC"; } else { $sql = "select t1.* from (SELECT tokens.product_id, tokens.quantity as product_total_qty,tokens.unique_id as token_id, tokens.created_at as date, products.name as brand_name, w.name as warehouse, r.name as retailer, tokens.token_type, sell_id.name as sell_admin, purchase_id.name as purchase_admin FROM tokens left join warehouses as w on tokens.from_warehouse_id=w.id left join retailers as r ON tokens.retailer_id=r.id left join dealers as sell_id ON tokens.dealer_id=sell_id.id left join dealers as purchase_id ON tokens.account_from_id=purchase_id.id join products on products.id=tokens.product_id left join product_companies on product_companies.id=tokens.product_company_id WHERE account_from_id = " . $request->company_id . " and tokens.product_id IN(" . $productArr . ") and tokens.product_company_id = " . $request->product_brand_id . " and tokens.created_at BETWEEN '$start_date' AND '$end_date') as t1 ORDER BY t1.date DESC"; } } else { if ($request->company_id == "company") { $productArr = $request->product_category_id; $sql = "select t1.* from (SELECT tokens.product_id, tokens.quantity as product_total_qty,tokens.unique_id as token_id, tokens.created_at as date, products.name as brand_name, w.name as warehouse, r.name as retailer, tokens.token_type, sell_id.name as sell_admin, purchase_id.name as purchase_admin FROM tokens left join warehouses as w on tokens.from_warehouse_id=w.id left join retailers as r ON tokens.retailer_id=r.id left join dealers as sell_id ON tokens.dealer_id=sell_id.id left join dealers as purchase_id ON tokens.account_from_id=purchase_id.id join products on products.id=tokens.product_id left join product_companies on product_companies.id=tokens.product_company_id WHERE product_company_id IS NOT NULL and tokens.product_id = " . $productArr . " and tokens.product_company_id = " . $request->product_brand_id . " and tokens.created_at BETWEEN '$start_date' AND '$end_date') as t1 ORDER BY t1.date DESC"; } else { $productArr = $request->product_category_id; $sql = "select t1.* from (SELECT tokens.product_id, tokens.quantity as product_total_qty,tokens.unique_id as token_id, tokens.created_at as date, products.name as brand_name, w.name as warehouse, r.name as retailer, tokens.token_type, sell_id.name as sell_admin, purchase_id.name as purchase_admin FROM tokens left join warehouses as w on tokens.from_warehouse_id=w.id left join retailers as r ON tokens.retailer_id=r.id left join dealers as sell_id ON tokens.dealer_id=sell_id.id left join dealers as purchase_id ON tokens.account_from_id=purchase_id.id left join products on products.id=tokens.product_id join product_companies on product_companies.id=tokens.product_company_id WHERE account_from_id = " . $request->company_id . " and tokens.product_id = " . $productArr . " and tokens.product_company_id = " . $request->product_brand_id . " and tokens.created_at BETWEEN '$start_date' AND '$end_date') as t1 ORDER BY t1.date DESC"; } } } $tokens = \DB::select($sql); $data['tokens'] = $tokens; $response['flag'] = true; $response['data'] = $data; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function tokenOtherDealerWise(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required', //'company_id' => 'required', 'product_category_id' => 'required', // 'report_date' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $other_dealer = array(); $company_stock = array(); $cement_product_ids = \App\Product::select('id')->where('product_category_id', 2)->get()->toArray(); $dealerArr = array(1,30, 31); if ($request->product_category_id == 2) { $stockData = \DB::table('tokens')->select(\DB::raw('sum(tokens.quantity) as total_stock'), 'tokens.account_from_id as company_id')->join('products', 'products.id', 'tokens.product_id')->whereBetween('tokens.created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->where('products.product_category_id', $request->product_category_id)->whereNotIn('tokens.account_from_id', $dealerArr)->groupBy('tokens.account_from_id')->get()->toArray(); $stockData = json_decode(json_encode($stockData), true); } else { if ($request->product_category_id == "npk") { $productArr = array(9, 10, 11, 12, 65, 103); $stockData = \DB::table('tokens')->select(\DB::raw('sum(tokens.quantity) as total_stock'), 'tokens.account_from_id as company_id')->join('products', 'products.id', 'tokens.product_id')->whereBetween('tokens.created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->whereIn('products.id', $productArr)->whereNotIn('tokens.account_from_id', $dealerArr)->groupBy('tokens.account_from_id')->get()->toArray(); $stockData = json_decode(json_encode($stockData), true); } else { $productArr = $request->product_category_id; $stockData = \DB::table('tokens')->select(\DB::raw('sum(tokens.quantity) as total_stock'), 'tokens.account_from_id as company_id')->join('products', 'products.id', 'tokens.product_id')->whereBetween('tokens.created_at', [date('Y-m-d', strtotime($request->date)), date('Y-m-d', strtotime($request->todate))])->where('products.id', $productArr)->whereNotIn('tokens.account_from_id', $dealerArr)->groupBy('tokens.account_from_id')->get()->toArray(); $stockData = json_decode(json_encode($stockData), true); } } foreach ($stockData as $key => $value) { $stockData[$key]['company_name'] = getModelById('Dealer', $value['company_id'])->name . ' (' . getModelById('Dealer', $value['company_id'])->address1 . ')'; } $response['flag'] = true; $response['data']['tokens'] = $stockData; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } public function trackcandidate(Request $request) { $response = array(); $root = $request->root; $name = $request->name; $add = $request->add; $ltlong = $request->ltlong; $filenameWithExt = $request->file('image')->getClientOriginalName(); $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME); $extension = $request->file('image')->getClientOriginalExtension(); $fileNameToStore = $filename . '_' . time() . '.' . $extension; $destinationPath = public_path() . '/image'; $path = $request->file('image')->move($destinationPath, $fileNameToStore); $imagecandidate = str_replace(public_path(), '', $path); $respon = \DB::table('addlocation')->insert( array( 'root' => $root, 'name' => $name, 'add' => $add, 'ltlong' => $ltlong, 'image' => $imagecandidate ) ); if ($respon) { $response['flag'] = true; $response['message'] = "Successfully Insert"; } else { $response['flag'] = false; $response['message'] = "Not Insert Location"; } return response()->json($response); } /* ------------------------------------- track person ------------------------------*/ // public function getusers() // { // $id = $_POST['id']; // $loc1 = array(); // $mydate = date("Y-m-d", strtotime($_POST['mydate'])); // //echo $mydate; // $fromdate = date("Y-m-d", strtotime($_POST['fromdate'])); // $getuser = "SELECT * FROM track_users where user_id='$id' and DATE(created_at) >= '$mydate' AND // DATE(created_at) <= '$fromdate'"; // $results = DB::select($getuser); // $geojson = array( // 'type' => 'FeatureCollection', // 'features' => array() // ); // foreach ($results as $loc) { // // print_r($loc->id); // $feature = array('query' => $getuser, 'type' => 'Feature', 'geometry' => array('type' => 'Point', 'coordinates' => array($loc->lat, $loc->lng)), 'properties' => $loc->id); // array_push($geojson['features'], $feature); // } // header('Content-type: application/json'); // echo json_encode($geojson, JSON_NUMERIC_CHECK); // } /*--------------------------attendance api-----------------------------*/ public function get_callforajax(Request $request) { date_default_timezone_set("Asia/Calcutta"); header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: GET, POST'); header("Access-Control-Allow-Headers: X-Requested-With"); $loc1=array(); // return view('home'); $sql="SELECT t1.* FROM track_users t1 WHERE t1.id = (SELECT t2.id FROM track_users t2 WHERE t2.user_id = t1.user_id ORDER BY t2.id DESC LIMIT 1) and DATE(`created_at`) = CURDATE()"; // $sql="SELECT * FROM track_users where DATE(created_at)=CURDATE()"; //$results = DB::select($sql); $locations = DB::select($sql); //echo json_decode($locations,true); $i=1; foreach($locations as $loc) { // echo $loc->city; $sql="SELECT * FROM users where id='$loc->user_id'"; $results = DB::select($sql); $loc1[]=array('name'=>$results[0]->name,'lat'=>$loc->lat,'lng'=>$loc->lng,'idd'=>$i); $i++; //Attendance Mark Code. $datas = \DB::table('addlocation')->get(); foreach($datas as $locate){ $coordinates = $locate->ltlong; $parts = explode(',', $coordinates); $latitude = $parts[0]; $longitude = $parts[1]; $warehouse1=$this->calculateDistanceBetweenTwoPoints($latitude,$longitude,$loc->lat,$loc->lng,'FT',true,5); if($warehouse1<=150) { $date=date('Y-m-d'); $date1=date('Y-m-d h:i:s'); $date2=date('h:i:s'); $date3=date('h'); $sql="SELECT * FROM location where user_id='$loc->user_id' and in_time LIKE '%$date%'"; $results = DB::select($sql); if(empty($results)) { DB::table('location')->insert( array('user_id' => "$loc->user_id", 'lat' => "$loc->lat", 'lng' => "$loc->lng", 'in_time' =>$date1,'location_range'=>"$warehouse1",'type'=>'P','hours_stay'=>'00:00') ); } else{ if($date3>=10 && $date3<=19){ // DB::statement("update location set "); $time=$results[0]->hours_stay; $endTime = strtotime("+5 minutes", strtotime($time)); // DB::update("update location set hours_stay='$endTime' set where user_id='$loc->user_id' and in_time LIKE '%$date%'"); } } } } } echo json_encode($loc1); } public function calculateDistanceBetweenTwoPoints($latitudeOne='', $longitudeOne='', $latitudeTwo='', $longitudeTwo='',$distanceUnit ='',$round=false,$decimalPoints='') { if (empty($decimalPoints)) { $decimalPoints = '3'; } if (empty($distanceUnit)) { $distanceUnit = 'KM'; } $distanceUnit = strtolower($distanceUnit); $pointDifference = $longitudeOne - $longitudeTwo; $toSin = (sin(deg2rad($latitudeOne)) * sin(deg2rad($latitudeTwo))) + (cos(deg2rad($latitudeOne)) * cos(deg2rad($latitudeTwo)) * cos(deg2rad($pointDifference))); $toAcos = acos($toSin); $toRad2Deg = rad2deg($toAcos); $toMiles = $toRad2Deg * 60 * 1.1515; $toKilometers = $toMiles * 1.609344; $toNauticalMiles = $toMiles * 0.8684; $toMeters = $toKilometers * 1000; $toFeets = $toMiles * 5280; $toYards = $toFeets / 3; switch (strtoupper($distanceUnit)) { case 'ML'://miles $toMiles = ($round == true ? round($toMiles) : round($toMiles, $decimalPoints)); return $toMiles; break; case 'KM'://Kilometers $toKilometers = ($round == true ? round($toKilometers) : round($toKilometers, $decimalPoints)); return $toKilometers; break; case 'MT'://Meters $toMeters = ($round == true ? round($toMeters) : round($toMeters, $decimalPoints)); return $toMeters; break; case 'FT'://feets $toFeets = ($round == true ? round($toFeets) : round($toFeets, $decimalPoints)); return $toFeets; break; case 'YD'://yards $toYards = ($round == true ? round($toYards) : round($toYards, $decimalPoints)); return $toYards; break; case 'NM'://Nautical miles $toNauticalMiles = ($round == true ? round($toNauticalMiles) : round($toNauticalMiles, $decimalPoints)); return $toNauticalMiles; break; } } public function getusers(){ $id=$_POST['id']; $loc1=array(); $mydate=date("Y-m-d", strtotime($_POST['mydate'])); //echo $mydate; $fromdate=date("Y-m-d", strtotime($_POST['fromdate'])); $getuser="SELECT * FROM track_users where user_id='$id' and DATE(created_at) >= '$mydate' AND DATE(created_at) <= '$fromdate'"; $results = DB::select($getuser); $geojson = array( 'type' => 'FeatureCollection', 'features' => array() ); foreach($results as $loc) { // print_r($loc->id); $feature = array('query'=>$getuser,'type' => 'Feature','geometry' => array('type' => 'Point','coordinates' => array($loc->lat, $loc->lng)),'properties' => $loc->id); array_push($geojson['features'], $feature); } header('Content-type: application/json'); echo json_encode($geojson, JSON_NUMERIC_CHECK); } public function gpsStatus(Request $request){ date_default_timezone_set("Asia/Kolkata"); $id=$_POST['userid']; $status=$_POST['status']; $bat=$_POST['bat']; $date=date('Y-m-d h:i:s'); $statusofapi= DB::table('gps_status')->insert( array('user_id' =>$id, 'status' => $status, 'created_at' => $date, 'batterylevel'=>$bat, 'updated_at' =>$date) ); if($statusofapi){ echo json_encode(array('msg'=>'Saved','status'=>1)); } else { echo json_encode(array('msg'=>'Error','status'=>0)); } } public function timeduration(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'api_token' => 'required' ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { if ($this->checkApiAuth($request)) { $user = \App\User::where('api_token', $data->api_token)->first(); $response['flag'] = true; $response['data'] = $user; } else { $response['flag'] = false; $response['message'] = "सेशन एक्सपायर हो चूका है| कृपया दुबारा लॉगिन करें "; $response['is_token_expired'] = true; } } return response()->json($response); } //--------------Voice Serarch based Report APIs--------------------------- public function getStockData($speechText) { if (!$speechText) { return response()->json([ 'error' => 'Speech text is required.', ], 400); } $voice = $speechText; // \Log::info('Speech Text: ' . $speechText); $keywords = explode(' ', $voice); $stockData = []; foreach ($keywords as $name) { $inventories = DB::table('inventories') ->join('products', 'inventories.product_id', '=', 'products.id') ->join('warehouses', 'warehouses.id', '=', 'inventories.warehouse_id') ->where('products.name', 'like', '%' . $name . '%') ->whereDate('inventories.created_at','>=', Carbon::now()->subDays(90)) ->select('inventories.*', 'products.name as product_name', 'warehouses.name as warehouse_name', 'inventories.dealer_id') ->get(); foreach ($inventories as $inventory) { $dealer = DB::table('dealers')->find($inventory->dealer_id); $dealerName = $dealer ? $dealer->name : 'NA'; $stockData[] = [ 'dealer' => $dealerName, 'product' => $inventory->product_name, 'warehouse' => $inventory->warehouse_name, 'quantity' => $inventory->quantity, ]; } } return response()->json([ 'stock_count' => count($stockData), 'data' => $stockData, ]); } public function getSaleData($speechText) { if (!$speechText) { return response()->json([ 'error' => 'Speech text is required.', ], 400); } $keywords = explode(' ', $speechText); $saleData = []; $totalCount = 0; foreach ($keywords as $name) { $loadingSlipInvoices = DB::table('loading_slip_invoices') ->join('products', 'loading_slip_invoices.product_id', '=', 'products.id') ->where('products.name', 'like', $name . '%') ->whereDate('loading_slip_invoices.created_at', '>=', Carbon::now()->subDays(90)) ->select('loading_slip_invoices.*', 'products.name as product_name') ->get(); foreach ($loadingSlipInvoices as $value) { $dealer = DB::table('dealers')->find($value->dealer_id); $dealerName = $dealer ? $dealer->name : 'NA'; $saleData[] = [ 'dealer' => $dealerName, 'product' => $value->product_name, 'invoice_type' => $value->invoice_type, 'invoice_number' => $value->invoice_number, 'quantity' => $value->quantity, 'rate' => $value->rate, 'freight_discount' => $value->freight_discount, ]; $totalCount++; } } return response()->json([ 'sale_count' => $totalCount, 'data' => $saleData, ]); } public function getPurchaseData($speechText) { if (!$speechText) { return response()->json([ 'error' => 'Speech text is required.', ], 400); } $keywords = explode(' ', $speechText); $purchaseData = []; $totalCount = 0; foreach ($keywords as $name) { $products = DB::table('products') ->select('products.id', 'products.name') ->where('products.name', 'like', $name . '%') ->get(); foreach ($products as $product) { $totalPurchaseQuantity = \App\CompanyDi::where('product_id', $product->id)->sum('quantity'); $totalPurchaseAmount = \App\CompanyDi::where('product_id', $product->id)->sum('total'); $purchaseData[] = [ 'product' => $product->name, 'total_quantity' => $totalPurchaseQuantity, 'total_amount' => $totalPurchaseAmount, ]; $totalCount++; } } return response()->json([ 'purchase_count' => $totalCount, 'data' => $purchaseData, ]); } public function getInvoiceData($speechText) { if (!$speechText) { return response()->json([ 'error' => 'Speech text is required.', ], 400); } $keywords = explode(' ', $speechText); $invoiceData = []; $totalCount = 0; foreach ($keywords as $name) { $products = DB::table('products') ->select('products.id', 'products.name') ->where('products.name', 'like', $name . '%') ->get(); foreach ($products as $product) { $invoices = \App\LoadingSlipInvoice::with('product_loading') ->where('product_id', $product->id) ->whereDate('created_at', '>=', Carbon::now()->subDays(90)) ->get(); foreach ($invoices as $invoice) { $invoiceData[] = [ 'invoice_number' => $invoice->invoice_number, 'retailer_name' => $invoice->retailer_name, 'amount' => $invoice->total, 'product_name' => $product->name, ]; $totalCount++; } } } return response()->json([ 'invoice_count' => $totalCount, 'data' => $invoiceData, ]); } public function getPieChartData($speechText) { if (empty($speechText)) { return response()->json(['error' => 'Speech text is required.'], 400); } $keywords = explode(' ', $speechText); $response = [ 'stockTotal' => 0, 'saleTotal' => 0, 'purchaseTotal' => 0, 'tokenTotal' => 0, 'invoiceTotal' => 0, ]; foreach ($keywords as $name) { $response['stockTotal'] += DB::table('inventories') ->join('products', 'inventories.product_id', '=', 'products.id') ->where('products.name', 'like', $name . '%') ->whereDate('inventories.created_at', '>=', now()->subDays(90)) ->count(); $response['saleTotal'] += DB::table('loading_slip_invoices') ->join('products', 'loading_slip_invoices.product_id', '=', 'products.id') ->where('products.name', 'like', $name . '%') ->whereDate('loading_slip_invoices.created_at', '>=', now()->subDays(90)) ->count(); $productIds = DB::table('products') ->where('name', 'like', $name . '%') ->pluck('id'); if ($productIds->isNotEmpty()) { $response['purchaseTotal'] += \App\CompanyDi::whereIn('product_id', $productIds) ->whereDate('created_at', '>=', now()->subDays(90)) ->exists() ? 1 : 0; $response['tokenTotal'] += \App\Token::whereIn('product_id', $productIds) ->where('quantity', '>', 0) ->whereDate('created_at', '>=', now()->subDays(90)) ->count(); $response['invoiceTotal'] += \App\LoadingSlipInvoice::whereIn('product_id', $productIds) ->whereDate('created_at', '>=', now()->subDays(90)) ->count(); } break; } return response()->json($response); } public function rakePaymentReport(Request $request) { $data = [ 'master_rakes' => \App\MasterRake::where('is_active', 1)->get(), 'users' => \App\User::where('is_active', 1)->get(), ]; if ($request->isMethod('post')) { $validator = \Validator::make($request->all(), [ 'master_rake_id' => 'required', ]); if ($validator->fails()) { return response()->json([ 'error' => $validator->errors(), 'message' => 'Validation failed. master_rake_id is required.', ], 400); } $data['master_rake_id'] = $request->master_rake_id; if ($request->paid_by) { $data['paid_by'] = $request->paid_by; $data['total_freight_payments'] = \App\ProductLoading::where('master_rake_id', $request->master_rake_id) ->where('freight_paid_by', $request->paid_by) ->sum('freight_paid_amount'); $data['total_labour_payments'] = \App\LabourPayments::where('master_rake_id', $request->master_rake_id) ->where('paid_by', $request->paid_by) ->sum('paid_amount'); $data['total_direct_labour_payments'] = \App\DirectLabourPayment::where('master_rake_id', $request->master_rake_id) ->where('paid_by', $request->paid_by) ->sum('amount'); $data['total_wagon_unloadings'] = \App\WagonUnloading::where('master_rake_id', $request->master_rake_id) ->where('paid_by', $request->paid_by) ->sum('paid_amount'); $data['total_unloading_labour_payments'] = \App\UnloadingLabourPayment::where('master_rake_id', $request->master_rake_id) ->where('paid_by', $request->paid_by) ->sum('paid_amount'); $data['total'] = $data['total_freight_payments'] + $data['total_labour_payments'] + $data['total_direct_labour_payments'] + $data['total_wagon_unloadings'] + $data['total_unloading_labour_payments']; } elseif (($request->date_to != $request->date_from) && $request->date_from) { $date_from = $request->date_from; $date_to = $request->date_to; $data['total_freight_payments'] = \App\ProductLoading::where('master_rake_id', $request->master_rake_id) ->whereBetween('freight_pay_date', [$date_from, $date_to]) ->sum('freight_paid_amount'); $data['total_labour_payments'] = \App\LabourPayments::where('master_rake_id', $request->master_rake_id) ->whereBetween('payment_date', [$date_from, $date_to]) ->sum('paid_amount'); $data['total_direct_labour_payments'] = \App\DirectLabourPayment::where('master_rake_id', $request->master_rake_id) ->whereBetween('payment_date', [$date_from, $date_to]) ->sum('amount'); $data['total_wagon_unloadings'] = \App\WagonUnloading::where('master_rake_id', $request->master_rake_id) ->whereBetween('payment_date', [$date_from, $date_to]) ->sum('paid_amount'); $data['total_unloading_labour_payments'] = \App\UnloadingLabourPayment::where('master_rake_id', $request->master_rake_id) ->whereBetween('payment_date', [$date_from, $date_to]) ->sum('paid_amount'); $data['date_to'] = $date_to; $data['date_from'] = $date_from; $data['total'] = $data['total_freight_payments'] + $data['total_labour_payments'] + $data['total_direct_labour_payments'] + $data['total_wagon_unloadings'] + $data['total_unloading_labour_payments']; } elseif ($request->date_from) { $date_from = $request->date_from; $data['total_freight_payments'] = \App\ProductLoading::where('master_rake_id', $request->master_rake_id) ->where('freight_pay_date', date('Y-m-d', strtotime($date_from))) ->sum('freight_paid_amount'); $data['total_labour_payments'] = \App\LabourPayments::where('master_rake_id', $request->master_rake_id) ->whereDate('payment_date', date('Y-m-d', strtotime($date_from))) ->sum('paid_amount'); $data['total_direct_labour_payments'] = \App\DirectLabourPayment::where('master_rake_id', $request->master_rake_id) ->whereDate('payment_date', date('Y-m-d', strtotime($date_from))) ->sum('amount'); $data['total_wagon_unloadings'] = \App\WagonUnloading::where('master_rake_id', $request->master_rake_id) ->whereDate('payment_date', date('Y-m-d', strtotime($date_from))) ->sum('paid_amount'); $data['total_unloading_labour_payments'] = \App\UnloadingLabourPayment::where('master_rake_id', $request->master_rake_id) ->whereDate('payment_date', date('Y-m-d', strtotime($date_from))) ->sum('paid_amount'); $data['date_from'] = $date_from; $data['date_to'] = ''; $data['total'] = $data['total_freight_payments'] + $data['total_labour_payments'] + $data['total_direct_labour_payments'] + $data['total_wagon_unloadings'] + $data['total_unloading_labour_payments']; } else { $data['total_freight_payments'] = \App\ProductLoading::where('master_rake_id', $request->master_rake_id) ->sum('freight_paid_amount'); $data['total_labour_payments'] = \App\LabourPayments::where('master_rake_id', $request->master_rake_id) ->sum('paid_amount'); $data['total_direct_labour_payments'] = \App\DirectLabourPayment::where('master_rake_id', $request->master_rake_id) ->sum('amount'); $data['total_wagon_unloadings'] = \App\WagonUnloading::where('master_rake_id', $request->master_rake_id) ->sum('paid_amount'); $data['total_unloading_labour_payments'] = \App\UnloadingLabourPayment::where('master_rake_id', $request->master_rake_id) ->sum('paid_amount'); $data['total'] = $data['total_freight_payments'] + $data['total_labour_payments'] + $data['total_direct_labour_payments'] + $data['total_wagon_unloadings'] + $data['total_unloading_labour_payments']; } } return response()->json($data); } public function freightReport(Request $request) { $data = []; $data['users'] = \App\User::where('is_active', 1)->get(); $data['master_rakes'] = \App\MasterRake::where('is_active', 1)->get(); if ($request->isMethod('post')) { $validator = \Validator::make($request->all(), [ 'date' => 'nullable|date', 'paid_by' => 'nullable|integer', 'master_rake_id' => 'required|nullable|integer', ]); if ($validator->fails()) { return response()->json([ 'status' => 'error', 'message' => $validator->errors(), ], 400); } $query = \App\ProductLoading::query(); if ($request->date) { $query->whereDate('updated_at', '=', date('Y-m-d', strtotime($request->date))); $data['date'] = $request->date; } if ($request->paid_by) { $query->where('freight_paid_by', $request->paid_by); $data['paid_by'] = $request->paid_by; } if ($request->master_rake_id) { $query->where('master_rake_id', $request->master_rake_id); $data['master_rake_id'] = $request->master_rake_id; } $product_loadings = $query->where('freight', '>', 0) ->with('token') ->get(); $data['product_loadings'] = $product_loadings; } else { $data['labour_payments'] = []; } return response()->json([ 'status' => 'success', 'data' => $data, ]); } public function labourPaymentReport(Request $request) { $data = []; $data['master_rakes'] = \App\MasterRake::where('is_active', 1)->get(); if ($request->isMethod('post')) { $validator = \Validator::make($request->all(), [ 'master_rake_id' => 'required|integer', ]); if ($validator->fails()) { return response()->json([ 'status' => 'error', 'message' => $validator->errors(), ], 400); } $labour_payments = \App\LabourPayments::where('master_rake_id', $request->master_rake_id) ->with('token') ->get(); $data['labour_payments'] = $labour_payments; $data['master_rake_id'] = $request->master_rake_id; } else { $data['labour_payments'] = []; } return response()->json([ 'status' => 'success', 'data' => $data, ]); } public function directLabourPaymentReport(Request $request) { $labour_payments = \App\DirectLabourPayment::all(); $data = [ 'direct_labour_payments' => $labour_payments, ]; return response()->json([ 'status' => 'success', 'data' => $data, ]); } public function rakeReport(Request $request) { $data = []; $data['master_rakes'] = \App\MasterRake::where('is_active', 1)->get(); if ($request->isMethod('post')) { $validator = \Validator::make($request->all(), [ 'master_rake_id' => 'required|integer|exists:master_rakes,id', ]); if ($validator->fails()) { return response()->json([ 'status' => 'error', 'message' => $validator->errors(), ], 400); } $masterRakeId = $request->master_rake_id; $data['rake'] = \App\MasterRake::where('is_active', 1) ->where('id', $masterRakeId) ->with('master_rake_products') ->first(); $data['master_rake_id'] = $masterRakeId; $data['total_direct_labour_payments'] = \App\DirectLabourPayment::where('master_rake_id', $masterRakeId) ->where('is_paid', 1) ->sum('amount'); $data['direct_labour_payments'] = \App\DirectLabourPayment::where('master_rake_id', $masterRakeId) ->where('is_paid', 1) ->get(); $data['total_wagon_unloadings'] = \App\WagonUnloading::where('master_rake_id', $masterRakeId) ->where('is_paid', 1) ->sum('paid_amount'); $data['wagon_unloadings'] = \App\WagonUnloading::where('master_rake_id', $masterRakeId) ->where('is_paid', 1) ->get(); $data['total_freight_payments'] = \App\ProductLoading::where('master_rake_id', $masterRakeId) ->where('is_freight_paid', 1) ->sum('freight_paid_amount'); $data['total_tokens'] = \App\Token::where('master_rake_id', $masterRakeId)->count('id'); $data['total_loadings'] = \App\ProductLoading::where('master_rake_id', $masterRakeId)->count('id'); $data['freight_payments'] = \App\ProductLoading::where('master_rake_id', $masterRakeId) ->where('is_freight_paid', 1) ->get(); $data['total_labour_payments'] = \App\LabourPayments::where('master_rake_id', $masterRakeId) ->where('is_paid', 1) ->sum('paid_amount'); $data['labour_payments'] = \App\LabourPayments::where('master_rake_id', $masterRakeId) ->where('is_paid', 1) ->get(); $data['total_unloading_labour_payments'] = \App\UnloadingLabourPayment::where('master_rake_id', $masterRakeId) ->where('is_paid', 1) ->sum('paid_amount'); $data['unloading_labour_payments'] = \App\UnloadingLabourPayment::where('master_rake_id', $masterRakeId) ->where('is_paid', 1) ->get(); } return response()->json([ 'status' => 'success', 'data' => $data, ]); } }