public function synchronizeSaleOld(array $params) { $locations = array(); $user = Auth::user(); if(Auth::user()->locations->count() === 0){ $locations = Location::where([ 'client_id' => $user->client_id, 'location_id' => null, ])->get()->pluck('id')->toArray(); } else{ $locations = $user->locations->pluck('id')->toArray(); } $locationService = new LocationService(); $locations = $locationService->getSubLocations($locations); if(count($params) <= 0 || (count($params) == 1 && isset($params['sale_id'])) || (count($params) == 2 && isset($params['sale_id']) && isset($params['society_id']))){ $sales = null; if(isset($params['sale_id']) && $params['sale_id'] !== null && isset($params['society_id']) && $params['society_id'] !== null){ // societies.growers.tickets.bales $sales = Sale::with([ 'societies' => function($q) use($params){ $q->where(['societies.id' => (int)$params['society_id']]) ->with(['growers' => function($q){ $q->with([ 'person', 'tickets' => function($q){ $q->whereRaw('left(barcode, 3) = 241')->with(['bales']); }, ]); }]); }, ])->with('market.warehouse.routes.transports', function($q){ $q->whereHas('tfRoute', function($q){ $q->whereColumn('source_id', 'destination_id'); }); })->with([ 'tickets' => function($q) use($params){ $q->whereRaw('left(barcode, 3) = 240') ->whereDoesntHave('luggage')->where(['owner_id' => null, 'owner_type' => null]) ->whereHas('agreement', function($q) use($params){ $q->where([ 'society_id' => (int)$params['society_id'], 'sale_id' => (int)$params['sale_id'], ]); }); }, ])->where([ 'id' => (int)$params['sale_id'] ])->get(); } else{ $sales = Sale::with('societies.growers.tickets.bales')->with('market.warehouse.routes.transports', function($q){ $q->whereHas('tfRoute', function($q){ $q->whereColumn('source_id', 'destination_id'); }); })->with('tickets', function($q){ $q->whereHas('bundle', function($q){ $q->whereHas('range', function($q){ $q->whereHas('box', function($q){ $q->where(['type' => 1]); }); }); })->whereDoesntHave('luggage')->where(['owner_id' => null, 'owner_type' => null]); })->whereHas('market', function($q) use($locations){ $q->whereHas('address', function($q) use($locations){ $q->whereIn('location_id', $locations); }); })->whereHas('season', function($q) use($user){ $q->where([ 'client_id' => $user->client_id, 'isRunning' => 1, ]); })->where([ 'client_id' => $user->client_id, 'isClosed' => 0, ])->get(); } return new SyncSaleCollection($sales); } else{ $text = json_encode($params); $fileName = 'INIT-SALE'.Auth::id().'-'.$params['sale_id'].'-'.Carbon::now()->format('Y-m-d-H-i-s').'.OUT'; Storage::disk('local')->put($fileName, $text); // $validator = Validator::make($params, [ // 'sale_id' => ['required', 'exists:sales,id'], // 'ticket_details' => [ // 'required', // 'array', // ], // 'ticket_details.*.grower_id' => [ // 'required', // 'exists:growers,id' // ], // ], [ // 'ticket_details.required' => 'No list of tickets was given', // 'ticket_details.array' => 'Tickets must be in a list', // 'ticket_details.*.grower_id.required' => 'Specify grower owning the ticket', // 'ticket_details.*.grower_id.exists' => 'Grower was not found', // ]); // $validator->validate(); try{ DB::beginTransaction(); $tickets = null; if(isset($params['ticket_details']) && is_array($params['ticket_details'])){ $owner_type = null; for($i = 0; $i < count($params['ticket_details']); $i++){ if(isset($params['ticket_details'][$i]['owner_type'])){ switch($params['ticket_details'][$i]['owner_type']){ case 1: $owner_type = Society::class; break; case 0: $owner_type = Grower::class; break; default: $owner_type = Grower::class; break; } } else{ $owner_type = Grower::class; } $luggage = [ 'barcode' => $params['ticket_details'][$i]['tagBarcode'], 'received' => 1, 'missing' => 0, 'mass' => $params['ticket_details'][$i]['purchaseweight'], 'sale_id' => $params['sale_id'], 'owner_id' => $params['ticket_details'][$i]['grower_id'], 'owner_type' => $owner_type, 'captured_at' => $params['ticket_details'][$i]['captured_at'], ]; $dbLuggage = Ticket::where(['client_id' => Auth::user()->client_id, 'barcode' => $luggage['barcode']])->first(); // dd($dbLuggage); if(isset($params['ticket_details'][$i]['barcode']) && $params['ticket_details'][$i]['barcode'] !== null){ if(!$dbLuggage){ dd($params['ticket_details'][$i]['tagBarcode']); } $ticket = [ 'barcode' => $params['ticket_details'][$i]['barcode'], 'received' => 1, 'missing' => 0, 'mass' => $params['ticket_details'][$i]['purchaseweight'], 'sale_id' => $params['sale_id'], 'owner_id' => $params['ticket_details'][$i]['grower_id'], 'owner_type' => $owner_type, 'ticket_id' => $dbLuggage->id, 'captured_at' => $params['ticket_details'][$i]['captured_at'], ]; $dbTicket = Ticket::where(['client_id' => Auth::user()->client_id, 'barcode' => $ticket['barcode']])->first(); if($dbTicket){ $dbTicket->update($ticket); $nationalGrade = Grade::find((int)$params['ticket_details'][$i]['ngrade_id']); $companyGrade = Grade::find((int)$params['ticket_details'][$i]['hgrade_id']); $dbTicket->grades()->attach($nationalGrade->id, [ 'mass' => $params['ticket_details'][$i]['purchaseweight'], 'type' => 1, 'isBought' => true, 'classifier_id' => $params['ticket_details'][$i]['classifier_id'], 'blender_id' => $params['ticket_details'][$i]['blender_id'], 'captured_at' => $params['ticket_details'][$i]['captured_at'], 'created_by' => Auth::id(), 'updated_by' => Auth::id(), ]); $dbTicket->grades()->attach((int)$companyGrade->id, [ 'mass' => $params['ticket_details'][$i]['purchaseweight'], 'type' => 2, 'isBought' => true, 'classifier_id' => $params['ticket_details'][$i]['classifier_id'], 'blender_id' => $params['ticket_details'][$i]['blender_id'], 'captured_at' => $params['ticket_details'][$i]['captured_at'], 'created_by' => Auth::id(), 'updated_by' => Auth::id(), ]); } } else{ if($dbLuggage){ $dbLuggage->update($luggage); } } } $warehouseTickets = []; for($x = 0; $x < count($params['ticket_details']); $x++){ if(!isset($params['ticket_details'][$x]['barcode'])){ $warehouseTickets[] = [ 'barcode' => $params['ticket_details'][$x]['tagBarcode'], 'luggage' => null, 'mass' => $params['ticket_details'][$x]['purchaseweight'] !== null? (float)$params['ticket_details'][$x]['purchaseweight'] : null, ]; } else{ $warehouseTickets[] = [ 'barcode' => $params['ticket_details'][$x]['barcode'], 'luggage' => $params['ticket_details'][$x]['tagBarcode'], 'mass' => $params['ticket_details'][$x]['purchaseweight'] !== null? (float)$params['ticket_details'][$x]['purchaseweight'] : null, ]; } } if(count($warehouseTickets) > 0){ $market = Market::with(['warehouse'])->whereHas('sales', function($q) use($params){ $q->where(['id' => $params['sale_id']]); })->first(); $document = Document::where([ 'code' => 'MP', ])->get()->last(); $this->warehouseRepository->allocateTickets(['tickets' => $warehouseTickets], $market->warehouse, $document, false, null); } } $detail = null; $sale = Sale::find($params['sale_id']); if(isset($params['ticket_details']) && is_array($params['ticket_details'])){ for($i = 0; $i < count($params['ticket_details']); $i++){ if(!isset($params['ticket_details'][$i]['ngrade_id']) || $params['ticket_details'][$i]['ngrade_id'] === null){ $detail = [ 'barcode' => $params['ticket_details'][$i]['tagBarcode'], 'purchaseweight' => $params['ticket_details'][$i]['purchaseweight'], 'grower_id' => $params['ticket_details'][$i]['grower_id'], 'warehouse_id' => $sale->market->warehouse->id, 'captured_at' => $params['ticket_details'][$i]['captured_at'], ]; TicketDetail::create($detail); } else{ $govGrade = Grade::with(['prices' => function($q){ $q->whereHas('seasons', function($q){ $q->where(['isRunning' => 1, 'client_id' => Auth::user()->client_id]); }); }])->where(['id' => (int)$params['ticket_details'][$i]['ngrade_id']])->first(); $detail = [ 'barcode' => $params['ticket_details'][$i]['barcode'], 'purchaseweight' => $params['ticket_details'][$i]['purchaseweight'], 'price' => $govGrade->prices[$govGrade->prices->count() - 1]->value, 'value' => ($govGrade->prices[$govGrade->prices->count() - 1]->value * (int)$params['ticket_details'][$i]['purchaseweight']), 'grower_id' => $params['ticket_details'][$i]['grower_id'], 'warehouse_id' => $sale->market->warehouse->id, 'captured_at' => $params['ticket_details'][$i]['captured_at'], 'classifier_id' => $params['ticket_details'][$i]['classifier_id'], 'blender_id' => $params['ticket_details'][$i]['blender_id'], 'ngrade_id' => $govGrade->id, 'hgrade_id' => $params['ticket_details'][$i]['hgrade_id'], ]; TicketDetail::create($detail); } } } if(isset($params['ticketRequest']) && (bool)$params['ticketRequest']){ $this->requsition->save($params); } DB::commit(); return [ 'status' => 'Success', 'message' => ((isset($params['ticketRequest'])) && (bool)$params['ticketRequest']) ? 'Your tickets requistion has been initiated' : (isset($params['ticket_details'][0]['ngrade_id']) && $params['ticket_details'][0]['ngrade_id'] !== null ? 'Tickets capturing data synchronized successfully' : (count($params['ticket_details']) > 0? 'Bales tagging data synchronized successfully' : 'No tickets data was synchronized')), ]; } catch(Exception $exception){ DB::rollBack(); // Test::create([ // 'value' => json_encode($params), // 'exception' => $exception->getMessage(), // 'created_by' => Auth::id(), // ]); $text = json_encode($params); $text .= "\n===========================================\n"; $text .= $exception->getTraceAsString(); $fileName = 'EXECEPTION-SALE'.Auth::id().'-'.$params['sale_id'].'-'.Carbon::now()->format('Y-m-d-H-i-s').'.OUT'; Storage::disk('local')->put($fileName, $text); return [ 'status' => 'Error', 'message' => $exception->getTrace(), ]; } } }