Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thedocrow0124's avatar

Livewire wire:model not updating for some fields

Hey, I've got an issue where wire:model inputs don't work for some. Customer PO and Order Date do not work, but others do. When I change a value in them it just changes it back to what it was straight away. Any ideas? Sorry the page is so long... orders-edit.blade.php

<div>
	<x-slot name="header">
		<h2 class="font-semibold text-xl text-gray-800 leading-tight">
			{{ __('Order ' . $order->customer_po) }}
		</h2>
	</x-slot>
	<main>
		<div class="container-fluid p-0 bg-light">
			<div class="container mt-3">
				<div class="row">
					<div class="col-12">
						@if (Session::get('message'))
							<div class="alert {{ Session::get('alert-class') }}" role="alert">
								{{ Session::get('message') }}
							</div>
						@endif
					</div>
				</div>
				<form class="row">
					<div class="col-12 d-flex align-items-stretch">
						<div class="card mb-3 w-100">
							<div class="card-header">Order Details</div>
							<div class="card-body">
								<div class="row">
									<div class="col-lg-6">
										<div class="row align-items-center">
											<div class="col-sm-5">
												<h6 class="mb-0 fw-bold">Department</h6>
											</div>
											<div class="col-sm-7 text-secondary">{{ $order->departments->description }}</div>
										</div>
										<hr>
										<div class="row align-items-center">
											<div class="col-sm-5">
												<h6 class="mb-0 fw-bold">Season</h6>
											</div>
											<div class="col-sm-7 text-secondary">{{ $order->seasons->description }}</div>
										</div>
										<hr>
										<div class="row align-items-center">
											<div class="col-sm-5">
												<h6 class="mb-0 fw-bold">Customer</h6>
											</div>
											<div class="col-sm-7 text-secondary">{{ $order->customers->name }}</div>
										</div>
										{{-- <hr>
										<div class="row align-items-center">
											<div class="col-sm-3">
												<h6 class="mb-0 fw-bold">Incoterms</h6>
											</div>
											@if($editCheck && Gate::check('order:update'))
												<div class="col-sm-7">
													<div></div>
													<select class="form-select @error('order.*') is-valid @enderror @error('order.incoterms') is-invalid @enderror" wire:model.lazy="order.incoterms">
														<option disabled selected value="">Choose...</option>
														<option value="EXW">EXW</option>
														<option value="FCA">FCA</option>
														<option value="CPT">CPT</option>
														<option value="CIP">CIP</option>
														<option value="DAP">DAP</option>
														<option value="DPU">DPU</option>
														<option value="DDP">DDP</option>
														<option value="FAS">FAS</option>
														<option value="FOB">FOB</option>
														<option value="CFR">CFR</option>
														<option value="CIF">CIF</option>
													</select>
													<div class="invalid-feedback">@error("order.incoterms") {{ $message }} @enderror</div>
												</div>
											@else
												<div class="col-sm-7 text-secondary">{{ $order->incoterms }}</div>
											@endif
										</div> --}}
									</div>
									<div class="col-lg-6">
										<div class="row align-items-center">
											<div class="col-sm-5">
												<h6 class="mb-0 fw-bold">Customer PO</h6>
											</div>
											@if($editCheck && Gate::check('order:update'))
												<div class="col-sm-7">
													<input type="text" class="form-control @error('order.*') is-valid @enderror @error('order.customer_po') is-invalid @enderror" wire:model.lazy="order.customer_po">
													<div class="invalid-feedback">@error('order.customer_po') {{ $message }} @enderror</div>
												</div>
											@else
												<div class="col-sm-7 text-secondary">{{ $order->customer_po }}</div>
											@endif
										</div>
										<hr>
										<div class="row align-items-center">
											<div class="col-sm-5">
												<h6 class="mb-0 fw-bold">Order Date</h6>
											</div>
											@if($editCheck && Gate::check('order:update'))
												<div class="col-sm-7">
													<input type="date" placeholder="Reference" class="form-control @error('order_date') is-valid @enderror @error('order.order_date') is-invalid @enderror" wire:model.lazy="order.order_date">
													<div class="invalid-feedback">@error('order.order_date') {{ $message }} @enderror</div>
												</div>
											@else
												<div class="col-sm-7 text-secondary">{{ $order->order_date }}</div>
											@endif
										</div>
										<hr>
										<div class="row align-items-center">
											<div class="col-sm-5">
												<h6 class="mb-0 fw-bold">Files</h6>
											</div>
											@if($editCheck && Gate::check('order:update'))
												<div class="col-sm-7">
													<input multiple type="file"  class="form-control @error('colourwayLines.*') is-valid @enderror @error('uploadFiles') is-invalid @enderror" wire:model.lazy="uploadFiles">
													<div class="invalid-feedback">@error('uploadFiles') {{ $message }} @enderror</div>
												</div>
											@else
												<div class="col-sm-7">
													<button class="btn btn-link m-0 p-0 dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">View Files</button>
													<ul class="dropdown-menu dropdown-menu-end">
														@foreach($order->customer_order_files as $file)
															<li><a download class="dropdown-item" href="{{url(asset('storage/' . $file->file))}}">{{ $file->filename }}</a></li>
														@endforeach
													</ul>
												</div>
											@endif
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
				</form>
			</div>
		</div>
		
		<div class="container">
			<div class="row">
				<div class="col-12 d-flex align-items-stretch">
					<div class="card mb-3 w-100">
						<div class="card-header">Colourways</div>
						<div class="card-body">
							<div class="table-responsive">
								<table class="table align-middle">
									<thead>
										<tr>
											<th>Style Code</th>
											<th>Colourway</th>
											<th>Style Description</th>
											<th>Factory</th>
											<th>Style Price</th>
											<th>Customer Date</th>
											<th></th>
										</tr>
									</thead>
									<tbody>
										@foreach ($order->customer_order_lines->sortBy('created_at') as $orderLine)
											<tr @can('design:update') style="cursor:pointer;" wire:click="editLine({{$orderLine->id}})" data-bs-toggle="modal" data-bs-target="#editModal"  @endcan >
												<td>{{ $orderLine->colourways->style_versions->styles->customer_ref }} <br> RTD{{ $orderLine->colourways->style_versions->styles->designs_id }}</td>
												<td>{{ $orderLine->colourways->name }}</td>
												<td>{{ $orderLine->colourways->style_versions->styles->description }}</td>
												<td>{{ $orderLine->colourways->style_versions->factories->name }}</td>
												<td>{{ $this->getPrice($orderLine) }}</td>
												<td>{{ date('d-M', strtotime($orderLine->customer_date)) }}</td>
												<td style="width: 1%">
													<div class="btn-group-vertical">
														@can('shipment:update')
															<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#shipmentModal" wire:click.stop="shipmentSizes({{$orderLine->id}})">Drops</button>
														@endcan
													</div>
												</td>
											</tr>
										@endforeach
									</tbody>
								</table>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<div class="container">
			<div class="row">
				<div class="col-12 d-flex align-items-stretch">
					<div class="card mb-3 w-100">
						<div class="card-header">Supplier POs</div>
						<div class="card-body">
							<div class="row">
								@if($editCheck && Gate::check('order:update'))
										@foreach($raw_material_orders as $i=>$materialOrder)
											<div class="row gx-2">
												<div class="col-lg-1 col-2">
													<button class="btn btn-danger" style="width:100%; height: 58px;" wire:click.prevent="removeOrderRawMatOrder({{ $i }})">X</button>
												</div>
												<div class="col-lg-2 col-4">
													<div class="form-floating mb-3">
														<select class="form-select" wire:model.lazy="raw_material_orders.{{ $i }}.raw_materials_id">
															<option selected disabled value="">Choose...</option>
																@foreach ($materials as $material)
																	<option value="{{$material->id}}">{{$material->material}}</option>
																@endforeach
														</select>
														<label>Type:</label>
													</div>
												</div>
												<div class="col-lg-2 col-4">
													<div class="form-floating mb-3">
														<select class="form-select" wire:model.lazy="raw_material_orders.{{ $i }}.style_versions_id">
															<option selected disabled value="">Choose...</option>
																@foreach ($order->customer_order_lines as $style)
																	<option value="{{$style->colourways->style_versions_id}}">RTD{{$style->colourways->style_versions->styles->designs->id}} - {{$style->colourways->style_versions->styles->designs->description}}</option>
																@endforeach
														</select>
														<label>Style:</label>
													</div>
												</div>
												<div class="col-lg-3 col-6">
													<div class="form-floating mb-3">
														<input type="text" placeholder="Brand/Size Labels Ref" class="form-control @error('orderLineSelected.*') is-valid @enderror @error("raw_material_orders.$i.ref") is-invalid @enderror" wire:model.lazy="raw_material_orders.{{ $i }}.ref">
														<label>Purchase Order:</label>
													</div>
												</div>
												<div class="col-lg-2 col-4">
													<div class="form-floating mb-3">
														<input type="date" placeholder="Brand/Size Labels Ref" class="form-control @error('orderLineSelected.*') is-valid @enderror @error("raw_material_orders.$i.order_date") is-invalid @enderror" wire:model.lazy="raw_material_orders.{{ $i }}.order_date">
														<label>Order Date:</label>
													</div>
												</div>
												<div class="col-lg-2 col-4">
													<div class="form-floating mb-3">
														<input type="date" placeholder="Brand/Size Labels Ref" class="form-control @error('orderLineSelected.*') is-valid @enderror @error("raw_material_orders.$i.ship_date") is-invalid @enderror" wire:model.lazy="raw_material_orders.{{ $i }}.ship_date">
														<label>Ship Date:</label>
													</div>
												</div>
												<div class="col-lg-5 col-6">
													<div class="form-floating mb-3">
														<input type="text" placeholder="Brand/Size Labels Ref" class="form-control @error('orderLineSelected.*') is-valid @enderror @error("raw_material_orders.$i.tracking") is-invalid @enderror" wire:model.lazy="raw_material_orders.{{ $i }}.tracking">
														<label>Tracking:</label>
													</div>
												</div>
												<div class="col-lg-1 col-6">
													<div class="form-floating mb-3">
														<input type="text" placeholder="Brand/Size Labels Ref" class="form-control @error('orderLineSelected.*') is-valid @enderror @error("raw_material_orders.$i.percent_of_total") is-invalid @enderror" wire:model.lazy="raw_material_orders.{{ $i }}.percent_of_total">
														<label>%</label>
													</div>
												</div>
												<div class="col-lg-3 col-6">
													<div class="form-floating mb-3">
														<input type="date" placeholder="Brand/Size Labels Ref" class="form-control @error('orderLineSelected.*') is-valid @enderror @error("raw_material_orders.$i.act_ship_date") is-invalid @enderror" wire:model.lazy="raw_material_orders.{{ $i }}.act_ship_date">
														<label>Actual Ship Date:</label>
													</div>
												</div>
												<div class="col-lg-3 col-6">
													<div class="form-floating mb-3">
														<input type="date" placeholder="Brand/Size Labels Ref" class="form-control @error('orderLineSelected.*') is-valid @enderror @error("raw_material_orders.$i.delivery_date") is-invalid @enderror" wire:model.lazy="raw_material_orders.{{ $i }}.delivery_date">
														<label>Delivery Date:</label>
													</div>
												</div>
												<div class="col-lg-12 col-12">
													<div class="form-floating mb-3">
														<input type="text" placeholder="Brand/Size Labels Ref" class="form-control @error('orderLineSelected.*') is-valid @enderror @error("raw_material_orders.$i.notes") is-invalid @enderror" wire:model.lazy="raw_material_orders.{{ $i }}.notes">
														<label>Notes:</label>
													</div>
												</div>
											</div>
										@endforeach
									<div class="row">
										<div class="col-12 text-end mt-3 mb-1 gx-2">
											@if($editCheck && Gate::check('order:update'))
												<button class="btn btn-primary" wire:click.prevent="addOrderRawMaterialOrder()">Add</button>
											@endif
										</div>
									</div>
								@else
									<div class="responsive-table">
										<table class="table">
											<thead>
												<tr>
													<th>Type</th>
													<th>Purchase Order</th>
													<th>Style</th>
													<th>Order Date</th>
													<th>Ship Date</th>
													<th>Tracking</th>
													<th>%</th>
													<th>Actual Ship Date</th>
													<th>Delivery Date</th>
													<th>Notes</th>
												</tr>
											</thead>
											<tbody>
												@foreach($raw_material_orders as $i=>$materialOrder)
													<tr>
														<td>{{ $materials->find($materialOrder['raw_materials_id'])->material }}</td>
														<td>{{ $materialOrder['ref'] }}</td>
														<td>RTD{{ $styleVersions->find($materialOrder['style_versions_id'])->styles->designs_id }}</td>
														<td>{{ $materialOrder['order_date'] }}</td>
														<td>{{ $materialOrder['ship_date'] }}</td>
														<td>{{ $materialOrder['tracking'] }}</td>
														<td>{{ $materialOrder['percent_of_total'] }}</td>
														<td>{{ $materialOrder['act_ship_date'] }}</td>
														<td>{{ $materialOrder['delivery_date'] }}</td>
														<td>{{ $materialOrder['notes'] }}</td>
													</tr>
												@endforeach
											</tbody>
										</table>
									</div>
								@endif
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>


		<footer class="mt-auto fixed-bottom bg-white border-top navbar navbar-light py-3">
			<div class="container">
				<div class="row w-100 align-items-center">
					<div class="col-lg-6">
						<livewire:footer />
					</div>
					<div class="col-lg-6 text-end">
						@if(!$editCheck && Gate::check('order:update'))
							<button class="btn btn-primary" wire:click.prevent="$toggle('editCheck')">Edit</button>
						@elseif($editCheck && Gate::check('order:update'))
							<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#newModal" wire:click.prevent="newLine()">New Line</button>
							<button class="btn btn-success" wire:click.prevent="saveOrder()">Save</button>
						@endif
					</div>
				</div>
			</div>
		</footer>
	</main>



	{{-- NEW --}}
	<div wire:ignore.self class="modal fade" id="newModal" data-bs-backdrop="static"  tabindex="-1" aria-labelledby="newModalLabel" aria-hidden="false">
		<div class="modal-dialog modal-xs">
			<div class="modal-content">
				<div class="modal-header">
					<h5 class="modal-title" id="newModalLabel">New Line</h5>
					<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" wire:click.prevent="closeEditLine"></button>
				</div>
				<div class="modal-body">
					<div class="container">
						<div class="row">
							<div class="col-12">
								@if (Session::get('message'))
									<div class="alert {{ Session::get('alert-class') }}" role="alert">
										{{ Session::get('message') }}
									</div>
								@endif
							</div>
						</div>
						@if($newLine && (Gate::check('order:create')))
						<form>
							<div class="row">
								<div class="col-lg-12">
									<div class="form-floating mb-3">
										<select class="form-select @error('orderLineSelected.*') is-valid @enderror @error('orderLineSelected.colourways_id') is-invalid @enderror" wire:model.lazy="orderLineSelected.colourways_id">
											<option selected disabled value="">Choose...</option>
											@foreach ($styles->where('customers_id', $order->customers_id)->where('departments_id', $order->departments_id) as $style)
													@foreach ($style->style_versions as $version)
														@foreach ($version->colourways as $colourway)
															<option value="{{$colourway->id}}">{{$style->designs_id}} | {{$style->customer_ref}} | {{$version->name}} | {{ $colourway->name }}</option>
														@endforeach
													@endforeach
											@endforeach
										</select>
										<label class="form-label">Colourway:</label>
										<div class="invalid-feedback">@error('orderLineSelected.colourways_id') {{ $message }} @enderror</div>
									</div>
								</div>
							</div>
						</form>
						@endif
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-danger" wire:click.prevent="closeNewLine">Close</button>
					@if(Gate::check('order:update'))
						<button type="button" class="btn btn-success" wire:click.prevent="saveNewLine">Save</button>
					@endif
				</div>
			</div>
		</div>
	</div>

	{{-- SHIPMENT --}}
	<div wire:ignore.self class="modal fade" id="shipmentModal" data-bs-backdrop="static"  tabindex="-1" aria-labelledby="shipmentModalLabel" aria-hidden="false">
		<div class="modal-dialog modal-fullscreen">
			<div class="modal-content">
				<div class="modal-header">
					<h5 class="modal-title" id="shipmentModalLabel">Drops</h5>
					<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" wire:click.prevent="closeShipments"></button>
				</div>
				<div class="modal-body">
					<div class="container">
						<div class="row">
							<div class="col-12">
								@if (Session::get('message'))
									<div class="alert {{ Session::get('alert-class') }}" role="alert">
										{{ Session::get('message') }}
									</div>
								@endif
							</div>
						</div>
						@if($shipments && (Gate::check('shipment:update') || Gate::check('shipment:create')))
						<form>
							<div class="row">
								<div class="col-lg-12">
									<div class="form-floating mb-3">
										@foreach($this->shipmentLines as $i=>$shipmentLine)
											<div class="card mb-3 w-100">
												<div class="card-header">Drop {{$i}}</div>
												<div class="card-body">
													<div class="row">
														<div class="col 4">
															<div class="row align-items-center">
																<div class="col-sm-5">
																	<button class="btn btn-link m-0 p-0 dropdown-toggle rounded-end fw-bold" type="button" data-bs-toggle="dropdown" aria-expanded="false">Confirmed ExFty</button>
																	<ul class="dropdown-menu dropdown-menu-end">
																		@if($orderLineSelected->shipment_lines->find($shipmentLine['id']) != NULL)
																			@foreach($orderLineSelected->shipment_lines->find($shipmentLine['id'])->shipment_line_exfty_dates as $date)
																				<li class="px-3"><b>{{ $date->created_at->format('d-M h:m') }}:</b> {{ $date->date->format('d-M') }}</li>
																			@endforeach
																		@endif
																	</ul>
																</div>
																@if($editShipmentCheck && Gate::check('order:update'))
																	<div class="col-sm-7">
																		<input type="date" placeholder="Location" class="form-control @error('orderLineSelected.*') is-valid @enderror @error("shipmentLines.$i.exfty") is-invalid @enderror" wire:model.lazy="shipmentLines.{{ $i }}.exfty">
																		<div class="invalid-feedback">@error("shipmentLines.$i.exfty") {{ $message }} @enderror</div>
																	</div>
																@else
																	<div class="col-sm-7 text-secondary">{{ date('d-M', strtotime($shipmentLines[$i]['exfty'])) }}</div>
																@endif
															</div>
														</div>
														<div class="col-4">
															<div class="row align-items-center">
																<div class="col-sm-5">
																	<h6 class="mb-0 fw-bold">Delivery Date</h6>
																</div>
																@if($editShipmentCheck && Gate::check('order:update'))
																	<div class="col-sm-7">
																		<input type="date" placeholder="Location" class="form-control @error('orderLineSelected.*') is-valid @enderror @error("shipmentLines.$i.delivery") is-invalid @enderror" wire:model.lazy="shipmentLines.{{ $i }}.delivery">
																		<div class="invalid-feedback">@error("shipmentLines.$i.delivery") {{ $message }} @enderror</div>
																	</div>
																@else
																	<div class="col-sm-7 text-secondary">{{ $shipmentLines[$i]['delivery'] }}</div>
																@endif
															</div>
														</div>
														<div class="col-4">
															<div class="row align-items-center">
																<div class="col-sm-5">
																	<h6 class="mb-0 fw-bold">Shipment</h6>
																</div>
																@if($editShipmentCheck && Gate::check('order:update'))
																	<div class="col-sm-7">
																		<select class="form-select @error('orderLineSelected.*') is-valid @enderror @error('shipmentLines.{{ $i }}.cost') is-invalid @enderror" wire:model.lazy="shipmentLines.{{ $i }}.shipment">
																			<option selected value="">Not Assigned</option>
																			@foreach ($shipmentsList as $shipment)
																				<option value="{{ $shipment->id }}">{{ $shipment->id }} | {{ $shipment->cost }}</option>
																			@endforeach
																		</select>
																		<div class="invalid-feedback">@error("shipmentLines.$i.shipment") {{ $message }} @enderror</div>
																	</div>
																@else
																	<div class="col-sm-7 text-secondary">{{ $shipmentLines[$i]['shipment'] }}</div>
																@endif
															</div>
														</div>
													</div>
													<div class="row">
															<hr class="mt-3">
															@if(!empty($shipmentLine['sizes']))
																@foreach($shipmentLine['sizes'] as $x=>$size)
																	<div class="col">
																		<div class="form-control border-bottom-0 rounded-0 rounded-top">{{ $this->sizes->find($shipmentLines[$i]['sizes'][$x]['size'])->name }}</div>
																		@if($editShipmentCheck && Gate::check('order:update'))
																			<div></div> {{-- NOT SURE WHY, BUT WIRE:MODEL DOESN'T WORK WITHOUT THIS IN --}}
																			<input type="text" class="form-control rounded-0 rounded-bottom" wire:model="shipmentLines.{{ $i }}.sizes.{{$x}}.qty">
																			<div class="invalid-feedback">@error("shipmentLines.$i.sizes.$x") {{ $message }} @enderror</div>
																		@else
																			<input disabled type="text" class="form-control rounded-0 rounded-bottom" value="{{ $shipmentLines[$i]['sizes'][$x]['qty'] }}">
																		@endif
																	</div>
																@endforeach
															@else
																<div class="alert alert-warning">Please add sizes to order to add sizes and quantities to drop.</div>
															@endif
													</div>
													<div class="row mt-3">
														<div class="col d-flex flex-row-reverse">
															@if($editShipmentCheck && Gate::check('order:update'))
																@if($shipmentLine['id'] != NULL || $shipmentLine['id'] != "")
																	@if($checkRemoveShipment == $i)
																		<button type="button" class="btn btn-danger" wire:click="deleteShipment({{$i}}, {{$shipmentLine['id']}})">Sure?</button>
																	@else
																		<button type="button" class="btn btn-primary" wire:click="$set('checkRemoveShipment', {{$i}})">Remove</button>
																	@endif
																@else
																	@if($checkRemoveShipment == $i)
																		<button type="button" class="btn btn-danger" wire:click="deleteShipment({{$i}})">Sure?</button>
																	@else
																		<button type="button" class="btn btn-primary" wire:click="$set('checkRemoveShipment', {{$i}})">Remove</button>
																	@endif
																@endif
															@endif
														</div>
													</div>
												</div>
											</div>
										@endforeach
									</div>
								</div>
							</div>
						</form>
						@endif
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-danger" wire:click.prevent="closeShipments">Close</button>
					@if(!$editShipmentCheck && Gate::check('order:update'))
						<button type="button" class="btn btn-primary" wire:click.prevent="$toggle('editShipmentCheck')">Edit</button>
					@elseif($editShipmentCheck && Gate::check('order:update'))
						<button type="button" class="btn btn-success" wire:click.prevent="saveShipments">Save</button>
						<button type="button" class="btn btn-success" wire:click.prevent="addShipment">Add Drop</button>
					@endif
				</div>
			</div>
		</div>
	</div>

	{{-- EDIT --}}
	<div wire:ignore.self class="modal fade" id="editModal" data-bs-backdrop="static"  tabindex="-1" aria-labelledby="editModalLabel" aria-hidden="false">
		<div class="modal-dialog modal-fullscreen">
			<div class="modal-content">
				<div class="modal-header">
					<h5 class="modal-title" id="editModalLabel">Order Line</h5>
					<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" wire:click.prevent="closeEditLine"></button>
				</div>
				<div class="modal-body">
					<div class="container">
						<div class="row">
							<div class="col-12">
								@if (Session::get('message'))
									<div class="alert {{ Session::get('alert-class') }}" role="alert">
										{{ Session::get('message') }}
									</div>
								@endif
							</div>
						</div>
						<div class="row">
							<div class="col-12">
								@if($editLine && (Gate::check('order:update')))
									<form>
										<div class="col-12">
											<div class="card mb-3 w-100">
												<div class="card-header">Order Details</div>
												<div class="card-body">
													<div class="row">
														<div class="col-lg-4">
															<div class="row align-items-center">
																<div class="col-sm-5">
																	<h6 class="mb-0 fw-bold">Colourway</h6>
																</div>
																@if($editLineCheck && Gate::check('order:update'))
																	<div class="col-sm-7">
																		<select class="form-select @error('orderLineSelected.*') is-valid @enderror @error('orderLineSelected.colourways_id') is-invalid @enderror" wire:model.lazy="orderLineSelected.colourways_id">
																			<option selected disabled value="">Choose...</option>
																			@foreach ($styles->where('customers_id', $order->customers->id) as $style)
																				@if($style->use_version != NULL)
																					@foreach ($style->style_versions->find($style->use_version)->colourways as $colourway)
																						<option value="{{$colourway->id}}">{{$style->designs_id}} | {{$style->customer_ref}} | {{$colourway->name}}</option>
																					@endforeach
																				@endif
																			@endforeach
																		</select>
																		<div class="invalid-feedback">@error('orderLineSelected.colourways_id') {{ $message }} @enderror</div>
																	</div>
																@else
																	<div class="col-sm-7 text-secondary">{{ $orderLineSelected->colourways->name }}</div>
																@endif
															</div>
															<hr>
															<div class="row align-items-center">
																<div class="col-sm-5">
																	<h6 class="mb-0 fw-bold">Destination</h6>
																</div>
																@if($editLineCheck && Gate::check('order:update'))
																	<div class="col-sm-7">
																		<input required type="text" class="form-control @error('orderLineSelected.*') is-valid @enderror @error('orderLineSelected.location') is-invalid @enderror" wire:model.lazy="orderLineSelected.location">
																		<div class="invalid-feedback">@error('orderLineSelected.location') {{ $message }} @enderror</div>
																	</div>
																@else
																	<div class="col-sm-7 text-secondary">{{ $orderLineSelected->location }}</div>
																@endif
															</div>
														</div>
														<div class="col-lg-4">
															<div class="row align-items-center">
																<div class="col-sm-5">
																	<button style="width: 40px" class="btn btn-link dropdown-toggle m-0 p-0 fw-bold" data-bs-toggle="dropdown">Customer Date</button>
																	<ul class="dropdown-menu">
																		@foreach($orderLineSelected->customer_order_line_cust_dates->sortByDesc('created_at') as $historicDate)
																			<li><span class="dropdown-item"><b>{{ $historicDate->created_at->format('d-M h:m') }}: </b> {{ $historicDate->date->format('d-M') }}</span></li>
																		@endforeach
																	</ul>
																</div>
																@if($editLineCheck && Gate::check('order:update'))
																	<div class="col-sm-7">
																		<div class="btn-group">
																			<input type="date" placeholder="Customer Date" class="w-100 form-control rounded-0 rounded-start @error('lineDates.*') is-valid @enderror @error('lineDates.customerDate') is-invalid @enderror" wire:model.lazy="lineDates.customerDate">
																		</div>
																		<div class="invalid-feedback">@error('lineDates.customerDate') {{ $message }} @enderror</div>
																	</div>
																@else
																	@if(array_key_exists('customerDate', $lineDates))
																		<div class="col-sm-7 -text-secondary">
																			{{ $lineDates['customerDate'] }}
																		</div>
																	@endif
																@endif
															</div>
															<hr>
															<div class="row align-items-center">
																<div class="col-sm-5">
																	<button style="width: 40px" class="btn btn-link dropdown-toggle m-0 p-0 fw-bold" data-bs-toggle="dropdown">Start Knit</button>
																	<ul class="dropdown-menu">
																		@foreach($orderLineSelected->customer_order_line_knit_dates->sortByDesc('created_at') as $historicDate)
																		<li><span class="dropdown-item"><b>{{ $historicDate->created_at->format('d-M h:m') }}: </b> {{ $historicDate->date->format('d-M') }}</span></li>
																		@endforeach
																	</ul>
																</div>
																@if($editLineCheck && Gate::check('order:update'))
																	<div class="col-sm-7">
																		<div class="btn-group">
																			<input type="date" placeholder="Start Knit Date" class="w-100 form-control rounded-0 rounded-start @error('lineDates.*') is-valid @enderror @error('lineDates.startKnitDate') is-invalid @enderror" wire:model.lazy="lineDates.startKnitDate">
																		</div>
																		<div class="invalid-feedback">@error('lineDates.startKnitDate') {{ $message }} @enderror</div>
																	</div>
																@elseif(array_key_exists('startKnitDate', $lineDates))
																	<div class="col-sm-7 -text-secondary">
																		{{ $lineDates['startKnitDate'] }}
																	</div>
																@endif
															</div>
														</div>
														<div class="col-lg-4">
															<div class="row">
																<div class="col-sm-5">
																	<h6 class="mb-0 fw-bold">Requested exFty</h6>
																</div>
																@if($editLineCheck && Gate::check('order:update'))
																	<div class="col-sm-7">
																		<input required type="date" class="form-control @error('orderLineSelected.*') is-valid @enderror @error('orderLineSelected.factory_cust_date') is-invalid @enderror" wire:model.lazy="orderLineSelected.factory_cust_date">
																		<div class="invalid-feedback">@error('orderLineSelected.factory_cust_date') {{ $message }} @enderror</div>
																	</div>
																@else
																	<div class="col-sm-7 text-secondary">{{ $orderLineSelected->factory_cust_date }}</div>
																@endif
															</div>
															<hr>
															<div class="row">
																<div class="col-sm-5">
																	<h6 class="mb-0 fw-bold">Total Qty</h6>
																</div>
																<div class="col-sm-7 text-secondary">@if($this->orderLineSelected->id != NULL){{ $this->totalQty() }}@endif</div>
															</div>
														</div>
													</div>
													<div class="row">
														<div class="col-12">
															<hr>
															<h6 class="mb-0 fw-bold">Notes</h6>
														</div>
														@if($editLineCheck && Gate::check('order:update'))
														<div class="col-12">
															<textarea class="form-control h-100 @error('orderLineSelected.*') is-valid @enderror @error("orderLineSelected.notes") is-invalid @enderror" placeholder="Comments" rows="5" wire:model.lazy="orderLineSelected.notes"></textarea>
															<div class="invalid-feedback">@error('orderLineSelected.notes') {{ $message }} @enderror</div>
														</div>
														@else
															<div class="col-sm-2 text-secondary">{{ $orderLineSelected->notes }}</div>
														@endif
													</div>
												</div>
											</div>
										</div>
										<div class="col-12">
											<div class="card mb-3 w-100">
												<div class="card-header">Quantity Breakdown</div>
												<div class="card-body">
													<div class="row">
														<div class="row mt-3">
															@if($editLineCheck && Gate::check('order:update'))
																@if(!empty($line['sizes']))
																	@foreach($line['sizes'] as $x=>$size)
																		<div class="input-group mb-3">
																			<select class="form-control rounded-0 rounded-top bg-primary text-white border border-primary" style="height: calc(3.5rem + 2px);" wire:model.lazy="line.sizes.{{ $x }}.size">
																				<option selected value="">⯆</option>
																				@foreach ($sizes as $size)
																				<option value="{{ $size->id }}">{{ $size->name }}</option>
																				@endforeach
																			</select>											
																			<input disabled type="text" placeholder="Price" class="form-control" value="@if(array_key_exists('sizes', $line) && array_key_exists($x, $line['sizes']) && array_key_exists('size', $line['sizes'][$x])){{ $this->getSizePrice($line['sizes'][$x]['size']) }}@endif">
																			<input type="text" placeholder="Qty" class="form-control" wire:model.lazy="line.sizes.{{ $x }}.qty">
																			<input type="text" placeholder="SKU" class="form-control" wire:model.lazy="line.sizes.{{ $x }}.sku">
																			<input type="text" placeholder="Barcode" class="form-control" wire:model.lazy="line.sizes.{{ $x }}.barcode">
																			<button class="btn btn-danger" wire:click.prevent="removeQty({{ $x }})">X</button>
																		</div>
																	@endforeach
																@endif
															@else
																<div class="responsive-table">
																	<table class="table">
																		<thead>
																			<tr>
																				<th>Size</th>
																				<th>Quantity</th>
																				<th>SKU</th>
																				<th>Barcode</th>
																			</tr>
																		</thead>
																		<tbody>
																			@if(!empty($line['sizes']))
																				@foreach($line['sizes'] as $x=>$size)
																					<tr>
																						<td>{{ $sizes->find($size['size'])->name }}</td>
																						<td>{{ $size['qty'] }}</td>
																						<td>{{ $size['sku'] }}</td>
																						<td>{{ $size['barcode'] }}</td>
																					</tr>
																				@endforeach
																			@endif
																		</tbody>
																	</table>
																</div>
															@endif
														</div>
														<div class="row">
															<div class="col-12 text-end mt-3 mb-1 gx-2">
																@if($editLineCheck && Gate::check('order:update'))
																	<button type="button" class="btn btn-primary" wire:click.prevent="addSize">Add</button>
																@endif
															</div>
														</div>
													</div>
												</div>
											</div>
										</div>
										<div class="col-12">
											<div class="card mb-3 w-100">
												<div class="card-header">Supplier POs</div>
												<div class="card-body">
													<div class="row">
														@if($editLineCheck && Gate::check('order:update'))
															@if(array_key_exists('raw_material_orders', $line))
																@foreach($line['raw_material_orders'] as $i=>$materialOrder)
																	<div class="row gx-2">
																		<div class="col-lg-1 col-2">
																			<button class="btn btn-danger" style="width:100%; height: 58px;" wire:click.prevent="removeRawMatOrder({{ $i }})">X</button>
																		</div>
																		<div class="col-lg-2 col-4">
																			<div class="form-floating mb-3">
																				<select class="form-select" wire:model.lazy="line.raw_material_orders.{{ $i }}.raw_materials_id">
																					<option selected disabled value="">Choose...</option>
																						@foreach ($materials as $material)
																							<option value="{{$material->id}}">{{$material->material}}</option>
																						@endforeach
																				</select>
																				<label>Type:</label>

																			</div>
																		</div>
																		<div class="col-lg-3 col-6">
																			<div class="form-floating mb-3">
																				<input type="text" placeholder="Brand/Size Labels Ref" class="form-control @error('orderLineSelected.*') is-valid @enderror @error("line.raw_material_orders.$i.ref") is-invalid @enderror" wire:model.lazy="line.raw_material_orders.{{ $i }}.ref">
																				<label>Purchase Order:</label>
																			</div>
																		</div>
																		<div class="col-lg-3 col-6">
																			<div class="form-floating mb-3">
																				<input type="date" placeholder="Brand/Size Labels Ref" class="form-control @error('orderLineSelected.*') is-valid @enderror @error("line.raw_material_orders.$i.order_date") is-invalid @enderror" wire:model.lazy="line.raw_material_orders.{{ $i }}.order_date">
																				<label>Order Date:</label>
																			</div>
																		</div>
																		<div class="col-lg-3 col-6">
																			<div class="form-floating mb-3">
																				<input type="date" placeholder="Brand/Size Labels Ref" class="form-control @error('orderLineSelected.*') is-valid @enderror @error("line.raw_material_orders.$i.ship_date") is-invalid @enderror" wire:model.lazy="line.raw_material_orders.{{ $i }}.ship_date">
																				<label>Ship Date:</label>
																			</div>
																		</div>
																		<div class="col-lg-5 col-6">
																			<div class="form-floating mb-3">
																				<input type="text" placeholder="Brand/Size Labels Ref" class="form-control @error('orderLineSelected.*') is-valid @enderror @error("line.raw_material_orders.$i.tracking") is-invalid @enderror" wire:model.lazy="line.raw_material_orders.{{ $i }}.tracking">
																				<label>Tracking:</label>
																			</div>
																		</div>
																		<div class="col-lg-1 col-6">
																			<div class="form-floating mb-3">
																				<input type="text" placeholder="Brand/Size Labels Ref" class="form-control @error('orderLineSelected.*') is-valid @enderror @error("line.raw_material_orders.$i.percent_of_total") is-invalid @enderror" wire:model.lazy="line.raw_material_orders.{{ $i }}.percent_of_total">
																				<label>%</label>
																			</div>
																		</div>
																		<div class="col-lg-3 col-6">
																			<div class="form-floating mb-3">
																				<input type="date" placeholder="Brand/Size Labels Ref" class="form-control @error('orderLineSelected.*') is-valid @enderror @error("line.raw_material_orders.$i.act_ship_date") is-invalid @enderror" wire:model.lazy="line.raw_material_orders.{{ $i }}.act_ship_date">
																				<label>Actual Ship Date:</label>
																			</div>
																		</div>
																		<div class="col-lg-3 col-6">
																			<div class="form-floating mb-3">
																				<input type="date" placeholder="Brand/Size Labels Ref" class="form-control @error('orderLineSelected.*') is-valid @enderror @error("line.raw_material_orders.$i.delivery_date") is-invalid @enderror" wire:model.lazy="line.raw_material_orders.{{ $i }}.delivery_date">
																				<label>Delivery Date:</label>
																			</div>
																		</div>
																		<div class="col-lg-12 col-12">
																			<div class="form-floating mb-3">
																				<input type="text" placeholder="Brand/Size Labels Ref" class="form-control @error('orderLineSelected.*') is-valid @enderror @error("line.raw_material_orders.$i.notes") is-invalid @enderror" wire:model.lazy="line.raw_material_orders.{{ $i }}.notes">
																				<label>Notes:</label>
																			</div>
																		</div>	
																	</div>
																@endforeach
															@endif
															<div class="row">
																<div class="col-12 text-end mt-3 mb-1 gx-2">
																	@if($editLineCheck && Gate::check('order:update'))
																		<button class="btn btn-primary" wire:click.prevent="addRawMaterialOrder()">Add</button>
																	@endif
																</div>
															</div>
														@else
															<div class="responsive-table">
																<table class="table">
																	<thead>
																		<tr>
																			<th>Type</th>
																			<th>Purchase Order</th>
																			<th>Order Date</th>
																			<th>Ship Date</th>
																			<th>Tracking</th>
																			<th>%</th>
																			<th>Actual Ship Date</th>
																			<th>Delivery Date</th>
																			<th>Notes</th>
																		</tr>
																	</thead>
																	<tbody>
																		@if(array_key_exists('raw_material_orders', $line))
																			@foreach($line['raw_material_orders'] as $i=>$materialOrder)
																				<tr>
																					<td>{{ $materials->find($materialOrder['raw_materials_id'])->material }}</td>
																					<td>{{ $materialOrder['ref'] }}</td>
																					<td>{{ $materialOrder['order_date'] }}</td>
																					<td>{{ $materialOrder['ship_date'] }}</td>
																					<td>{{ $materialOrder['tracking'] }}</td>
																					<td>{{ $materialOrder['percent_of_total'] }}</td>
																					<td>{{ $materialOrder['act_ship_date'] }}</td>
																					<td>{{ $materialOrder['delivery_date'] }}</td>
																					<td>{{ $materialOrder['notes'] }}</td>
																				</tr>
																			@endforeach
																		@endif
																	</tbody>
																</table>
															</div>
														@endif
													</div>
												</div>
											</div>
										</div>
									</form>
								@endif
							</div>
						</div>
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-danger" wire:click.prevent="closeEditLine">Close</button>
					@if($editLineCheck && Gate::check('order:update'))
						<button type="button" class="btn btn-success" wire:click.prevent="updateLine()">Save</button>
					@elseif(!$editLineCheck && Gate::check('order:update'))
						<button type="button" class="btn btn-primary" wire:click.prevent="$toggle('editLineCheck')">Edit</button>
					@endif
				</div>
			</div>
		</div>
	</div>
	<x-slot name="buttons">
		@if(!$editCheck && Gate::check('order:update'))
			<button class="btn btn-primary" wire:click.prevent="$toggle('editCheck')">Edit</button>
			<button class="btn btn-primary" wire:click.prevent="edit">Edit</button>
		@elseif($editCheck && Gate::check('order:update'))
			<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#newModal" wire:click.prevent="newLine()">New Line</button>
			<button class="btn btn-success" wire:click.prevent="saveOrder()">Save</button>
		@endif
	</x-slot>
	<x-slot name="modalScripts">
		document.addEventListener('DOMContentLoaded', () => {
			var newModal = new bootstrap.Modal('#newModal');
			var editModal = new bootstrap.Modal('#editModal');
			var shipmentModal = new bootstrap.Modal('#shipmentModal');
			document.addEventListener('closeNewModal', () => {
				newModal.hide();
			});
			document.addEventListener('closeEditModal', () => {
				editModal.hide();
			});
			document.addEventListener('closeShipmentModal', () => {
				shipmentModal.hide();
			});
		});
	</x-slot>
</div>

OrdersEdit.php

<?php

namespace App\Http\Livewire\Orders;

use Throwable;
use App\Models\Yarn;
use App\Models\Sizes;
use App\Models\Styles;
use Livewire\Component;
use App\Models\Shipment;
use App\Models\Spinners;
use App\Models\YarnColours;
use App\Models\RawMaterials;
use App\Models\ShipmentLine;
use Bkwld\Cloner\Stubs\Image;
use Livewire\WithFileUploads;
use App\Models\CustomerOrders;
use App\Models\RawMaterialOrders;
use App\Models\ShipmentLineSizes;
use App\Models\CustomerOrderFiles;
use App\Models\CustomerOrderLines;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Gate;
use App\Models\CustomerOrderLineYarns;
use App\Models\ShipmentLineExftyDates;
use Illuminate\Support\Facades\Storage;
use App\Models\CustomerOrderRawMatOrder;
use App\Models\CustomerOrderLineCustDates;
use App\Models\CustomerOrderLineKnitDates;
use App\Models\CustomerOrderLineQuantities;
use App\Http\Controllers\CustomerOrderLineStartKnitDatesController;
use App\Models\StyleVersions;

class OrdersEdit extends Component
{
	use WithFileUploads;

	
	public $newLine = FALSE, $editLine = FALSE, $editCheck = FALSE, $editLineCheck, $editShipmentCheck, $checkRemoveShipment = NULL;
	public $order;
	public $orderID;

	public $sizes, $yarns, $yarnColours, $spinners, $styles, $materials, $shipmentsList, $styleVersions;

	

	public $orderLineSelected, $raw_material_orders = [];
	public $lineDates = [], $uploadFiles = [];

	public $line = [];

	public $exFtyDateHistory= [], $StartKnitDateHistory = [];

	public $inputs = [];
	public $shipmentLines = [], $shipments, $shipmenti = 0;
	public $i = 0;

	protected $rules = [
		'order.customer_po' => 'required|string|max:35',
		'order.order_date' => 'required|date|after:2022-01-01',
		// 'order.incoterms' => 'required|in:EXW,FCA,CPT,CIP,DAP,DPU,DDP,FAS,FOB,CFR,CIF',
		'uploadFiles.*' => 'nullable|file|max:10000',
		'lineDates.customerDate' => 'required|date|after_or_equal:2022-01-01',
		'lineDates.startKnitDate' => 'nullable|date|after_or_equal:2022-01-01',
		'orderLineSelected.actual_ship_date' => 'nullable|date|after_or_equal:lineDates.startKnitDate',
		'orderLineSelected.factory_cust_date' => 'nullable|date|after_or_equal:lineDates.startKnitDate',
		'orderLineSelected.location' => 'nullable|string|max:30',
		'orderLineSelected.colourways_id' => 'required|numeric|min:0',
		'orderLineSelected.notes' => 'nullable|string|max:5000',
		'line.sizes.*.size' => 'required_with:line.sizes.*.qty|required_with:line.sizes.*.sku|required_with:line.sizes.*.barcode|numeric|min:0',
		'line.sizes.*.qty' => 'required_with:line.sizes.*.size|numeric|min:0',
		'line.sizes.*.sku' => 'nullable|string|max:20',
		'line.sizes.*.barcode' => 'nullable|string|max:20',
		'line.raw_material_orders.*.raw_materials_id' => 'required|numeric|min:0',
		'line.raw_material_orders.*.ref' => 'nullable|string|max:35',
		'line.raw_material_orders.*.order_date' => 'nullable|date|after_or_equal:order.2022-01-01',
		'line.raw_material_orders.*.ship_date' => 'nullable|date|after_or_equal:line.raw_material_orders.*.order_date',
		'line.raw_material_orders.*.tracking' => 'nullable|string|max:15',
		'line.raw_material_orders.*.act_ship_date' => 'nullable|date|after_or_equal:line.raw_material_orders.*.order_date',
		'line.raw_material_orders.*.delivery_date' => 'nullable|date|after_or_equal:line.raw_material_orders.*.act_ship_date',
		'line.raw_material_orders.*.percent_of_total' => 'nullable|numeric|min:0|max:100',
		'line.raw_material_orders.*.notes' => 'nullable|string|max:100',

		'raw_material_orders.*.raw_materials_id' => 'required|numeric|min:0',
		'raw_material_orders.*.ref' => 'nullable|string|max:35',
		'raw_material_orders.*.order_date' => 'nullable|date|after_or_equal:order.2022-01-01',
		'raw_material_orders.*.ship_date' => 'nullable|date|after_or_equal:line.raw_material_orders.*.order_date',
		'raw_material_orders.*.tracking' => 'nullable|string|max:15',
		'raw_material_orders.*.act_ship_date' => 'nullable|date|after_or_equal:line.raw_material_orders.*.order_date',
		'raw_material_orders.*.delivery_date' => 'nullable|date|after_or_equal:line.raw_material_orders.*.act_ship_date',
		'raw_material_orders.*.percent_of_total' => 'nullable|numeric|min:0|max:100',
		'raw_material_orders.*.notes' => 'nullable|string|max:100',
	];
	protected $orderRules = [
		'order.customer_po' => 'required|string|max:35',
		'order.order_date' => 'required|date|after:2022-01-01',
		// 'order.incoterms' => 'required|in:EXW,FCA,CPT,CIP,DAP,DPU,DDP,FAS,FOB,CFR,CIF',
		'uploadFiles.*' => 'nullable|file|max:10000',

		'raw_material_orders.*.raw_materials_id' => 'required|numeric|min:0',
		'raw_material_orders.*.ref' => 'nullable|string|max:35',
		'raw_material_orders.*.order_date' => 'nullable|date|after_or_equal:order.2022-01-01',
		'raw_material_orders.*.ship_date' => 'nullable|date|after_or_equal:line.raw_material_orders.*.order_date',
		'raw_material_orders.*.tracking' => 'nullable|string|max:15',
		'raw_material_orders.*.act_ship_date' => 'nullable|date|after_or_equal:line.raw_material_orders.*.order_date',
		'raw_material_orders.*.delivery_date' => 'nullable|date|after_or_equal:line.raw_material_orders.*.act_ship_date',
		'raw_material_orders.*.percent_of_total' => 'nullable|numeric|min:0|max:100',
		'raw_material_orders.*.notes' => 'nullable|string|max:100',
	];
	protected $lineRules = [ 
		'lineDates.customerDate' => 'required|date|after_or_equal:2022-01-01',
		'lineDates.startKnitDate' => 'nullable|date|after_or_equal:2022-01-01',
		'orderLineSelected.actual_ship_date' => 'nullable|date|after_or_equal:lineDates.startKnitDate',
		'orderLineSelected.factory_cust_date' => 'nullable|date|after_or_equal:lineDates.startKnitDate',
		'orderLineSelected.location' => 'nullable|string|max:30',
		'orderLineSelected.colourways_id' => 'required|numeric|min:0',
		'orderLineSelected.notes' => 'nullable|string|max:5000',
		'line.sizes.*.size' => 'required_with:line.sizes.*.qty|required_with:line.sizes.*.sku|required_with:line.sizes.*.barcode|numeric|min:0',
		'line.sizes.*.qty' => 'required_with:line.sizes.*.size|numeric|min:0',
		'line.sizes.*.sku' => 'nullable|string|max:20',
		'line.sizes.*.barcode' => 'nullable|string|max:20',
		'line.raw_material_orders.*.raw_materials_id' => 'required|numeric|min:0',
		'line.raw_material_orders.*.ref' => 'nullable|string|max:35',
		'line.raw_material_orders.*.order_date' => 'nullable|date|after_or_equal:order.2022-01-01',
		'line.raw_material_orders.*.ship_date' => 'nullable|date|after_or_equal:line.raw_material_orders.*.order_date',
		'line.raw_material_orders.*.tracking' => 'nullable|string|max:15',
		'line.raw_material_orders.*.act_ship_date' => 'nullable|date|after_or_equal:line.raw_material_orders.*.order_date',
		'line.raw_material_orders.*.delivery_date' => 'nullable|date|after_or_equal:line.raw_material_orders.*.act_ship_date',
		'line.raw_material_orders.*.percent_of_total' => 'nullable|numeric|min:0|max:100',
		'line.raw_material_orders.*.notes' => 'nullable|string|max:100',
    ];
	protected $shipmentRules = [ 
		'shipmentLines.*.exfty' => 'required|date|after_or_equal:2022-01-01',
		'shipmentLines.*.delivery' => 'nullable|date|after_or_equal:shipmentLines.*.exfty',
		'shipmentLines.*.shipment' => 'nullable|numeric|min:0',
		'shipmentLines.*.sizes.*.qty' => 'required|numeric|min:0|max:99999',
    ];


	public function render()
	{ 
		Gate::authorize('order:read');
		if(request()->orderID != NULL){
			$this->orderID = request()->orderID;
		}
		$this->refreshOrder();
		return view('livewire.orders.orders-edit');
	}
	private function refreshOrder(){
		$this->order = CustomerOrders::with([
			'seasons',
			'customers',
			'customer_order_lines.shipment_lines.shipment_line_sizes',
			'customer_order_lines.shipment_lines.shipment_line_exfty_dates',
			'customer_order_files',
			'customer_order_lines.raw_material_orders',
			'customer_order_lines.customer_order_line_quantities',
			'customer_order_lines.colourways.style_versions.styles.designs',
			'customer_order_lines.colourways.style_versions.style_version_prices',
			'customer_order_lines.colourways.style_versions.style_version_price_sizes',
			'customer_order_lines.colourways.style_versions.factories',
			'customer_order_lines.customer_order_line_cust_dates',
			'customer_order_lines.customer_order_line_knit_dates',
			'customer_order_raw_mat_orders',
		])->find($this->orderID);
		$this->sizes = Sizes::all();
		$this->yarns = Yarn::all();
		$this->styles = Styles::with('style_versions.colourways')->get();
		$this->styleVersions = StyleVersions::get();
		$this->season = $this->order->seasons_id;
		$this->factory = $this->order->factories_id;
		$this->customer = $this->order->customers_id;
		$this->customerPO = $this->order->customer_po;
		$this->orderDate = $this->order->order_date;
		$this->materials = RawMaterials::all();
		$this->shipmentsList = Shipment::all();

		if($this->raw_material_orders == [] || !$this->editCheck){
			foreach($this->order->customer_order_raw_mat_orders as $i=>$matOrder){
				$this->raw_material_orders[$i]['id'] = $matOrder->id;
				$this->raw_material_orders[$i]['raw_materials_id'] = $matOrder->raw_materials_id;
				$this->raw_material_orders[$i]['customer_order_lines_id'] = $matOrder->customer_order_lines_id;
				$this->raw_material_orders[$i]['style_versions_id'] = $matOrder->style_versions_id;
				$this->raw_material_orders[$i]['ref'] = $matOrder->ref;
				$this->raw_material_orders[$i]['order_date'] = $matOrder->order_date;
				$this->raw_material_orders[$i]['ship_date'] = $matOrder->ship_date;
				$this->raw_material_orders[$i]['tracking'] = $matOrder->tracking;
				$this->raw_material_orders[$i]['act_ship_date'] = $matOrder->act_ship_date;
				$this->raw_material_orders[$i]['delivery_date'] = $matOrder->delivery_date;
				$this->raw_material_orders[$i]['percent_of_total'] = $matOrder->percent_of_total;
				$this->raw_material_orders[$i]['notes'] = $matOrder->notes;
			}
		}
	}

	public function saveOrder(){
		Gate::authorize('order:update');
		$validatedData = $this->validate($this->orderRules);
		session()->flash('message', 'Error Saving');
		session()->flash('alert-class', 'alert-danger');
		$this->order->customer_po = $this->customerPO;
		$this->order->order_date = $this->orderDate;
		$this->order->save();
		session()->flash('message', 'Order Saved. Files could not be uploaded.');
		session()->flash('alert-class', 'alert-warning');
		if($this->uploadFiles != []){
			foreach($this->uploadFiles as $file){
				$path = NULL;
				$entry = new CustomerOrderFiles();
				$path = Storage::disk('public')->put('customerOrderFiles', $file);
				if($path != NULL){
					$entry->customer_orders_id = $this->order->id;
					$entry->filename = $file->getClientOriginalName();
					$entry->file = $path;
					$entry->save();
				}
			}
		}

		//SAVE RAW MATERIAL ORDERS
		$rawMaterials = [];
		foreach ($this->raw_material_orders as $i) { 
			$rawMaterial = new CustomerOrderRawMatOrder();
			if(array_key_exists('raw_materials_id', $i) && !$i['raw_materials_id'] == "")
				$rawMaterial->raw_materials_id = $i['raw_materials_id'];
			if(array_key_exists('customer_orders_id', $i) && !$i['customer_orders_id'] == "")
				$rawMaterial->customer_orders_id = $i['customer_orders_id'];
			if(array_key_exists('style_versions_id', $i) && !$i['style_versions_id'] == "")
				$rawMaterial->style_versions_id = $i['style_versions_id'];
			if(array_key_exists('ref', $i) && !$i['ref'] == "")
				$rawMaterial->ref = $i['ref'];
			if(array_key_exists('order_date', $i) && !$i['order_date'] == "")
				$rawMaterial->order_date = $i['order_date'];
			if(array_key_exists('ship_date', $i) && !$i['ship_date'] == "")
				$rawMaterial->ship_date = $i['ship_date'];
			if(array_key_exists('tracking', $i) && !$i['tracking'] == "")
				$rawMaterial->tracking = $i['tracking'];
			if(array_key_exists('act_ship_date', $i) && !$i['act_ship_date'] == "")
				$rawMaterial->act_ship_date = $i['act_ship_date'];
			if(array_key_exists('delivery_date', $i) && !$i['delivery_date'] == "")
				$rawMaterial->delivery_date = $i['delivery_date'];
			if(array_key_exists('percent_of_total', $i) && !$i['percent_of_total'] == "")
				$rawMaterial->percent_of_total = $i['percent_of_total'];
			if(array_key_exists('notes', $i) && !$i['notes'] == "")
				$rawMaterial->notes = $i['notes'];
			$rawMaterials[] = $rawMaterial;
		}
		$this->order->customer_order_raw_mat_orders()->delete();
		$this->order->customer_order_raw_mat_orders()->saveMany($rawMaterials);

		session()->flash('message', 'Order Saved');
		session()->flash('alert-class', 'alert-success');
		$this->editCheck = FALSE;
	}

	public function editLine($lineID){
		$this->editLine = TRUE;
		$this->orderLineSelected = $this->order->customer_order_lines->find($lineID);
		if(!$this->orderLineSelected->customer_order_line_cust_dates->isEmpty()){
			$this->lineDates['customerDate'] = $this->orderLineSelected->customer_order_line_cust_dates->sortByDesc('created_at')->first()->date->format('Y-m-d');
		}
		if(!$this->orderLineSelected->customer_order_line_knit_dates->isEmpty()){
			$this->lineDates['startKnitDate'] = $this->orderLineSelected->customer_order_line_knit_dates->sortByDesc('created_at')->first()->date->format('Y-m-d');
		}
		foreach($this->orderLineSelected->customer_order_line_quantities as $i=>$size){
			$this->line['sizes'][$i]['size'] = $size->sizes_id;
			$this->line['sizes'][$i]['qty'] = $size->qty;
			$this->line['sizes'][$i]['sku'] = $size->SKU;
			$this->line['sizes'][$i]['barcode'] = $size->barcode;
		}
		foreach($this->orderLineSelected->raw_material_orders as $i=>$matOrder){
			$this->addRawMaterialOrder();
			$this->line['raw_material_orders'][$i]['id'] = $matOrder->id;
			$this->line['raw_material_orders'][$i]['raw_materials_id'] = $matOrder->raw_materials_id;
			$this->line['raw_material_orders'][$i]['customer_order_lines_id'] = $matOrder->customer_order_lines_id;
			$this->line['raw_material_orders'][$i]['ref'] = $matOrder->ref;
			$this->line['raw_material_orders'][$i]['order_date'] = $matOrder->order_date;
			$this->line['raw_material_orders'][$i]['ship_date'] = $matOrder->ship_date;
			$this->line['raw_material_orders'][$i]['tracking'] = $matOrder->tracking;
			$this->line['raw_material_orders'][$i]['act_ship_date'] = $matOrder->act_ship_date;
			$this->line['raw_material_orders'][$i]['delivery_date'] = $matOrder->delivery_date;
			$this->line['raw_material_orders'][$i]['percent_of_total'] = $matOrder->percent_of_total;
			$this->line['raw_material_orders'][$i]['notes'] = $matOrder->notes;
		}
	}

	public function updateLine(){
		Gate::authorize('order:update');
		Gate::authorize('order:update');
		session()->flash('message', 'Validation Error');
		session()->flash('alert-class', 'alert-danger');
		$validatedData = $this->validate($this->lineRules);
		$this->orderLineSelected->save();
		if($this->orderLineSelected->customer_order_line_cust_dates->isEmpty() || $this->lineDates['customerDate'] != $this->orderLineSelected->customer_order_line_cust_dates->sortByDesc('created_at')->first()->date->format('Y-m-d')){
			if(array_key_exists('customerDate', $this->lineDates)){
				$date = new CustomerOrderLineCustDates();
				$date->date = $this->lineDates['customerDate'];
				$this->orderLineSelected->customer_order_line_cust_dates()->save($date);
			}
		}
		if($this->orderLineSelected->customer_order_line_knit_dates->isEmpty() || $this->lineDates['startKnitDate'] != $this->orderLineSelected->customer_order_line_knit_dates->sortByDesc('created_at')->first()->date->format('Y-m-d')){
			if(array_key_exists('startKnitDate', $this->lineDates)){
				$date = new CustomerOrderLineKnitDates();
				$date->date = $this->lineDates['startKnitDate'];
				$this->orderLineSelected->customer_order_line_knit_dates()->save($date);
			}
		}

		//SAVE SIZES
		if(array_key_exists('sizes', $this->line)){
			$sizes = [];
			foreach ($this->line['sizes'] as $i) { 
				if(!empty($i)){
					$size = new CustomerOrderLineQuantities();
					if(array_key_exists('size', $i))
						$size->sizes_id = $i['size'];
					if(array_key_exists('qty', $i))
						$size->qty = $i['qty'];
					if(array_key_exists('sku', $i))
						$size->sku = $i['sku'];
					if(array_key_exists('barcode', $i))
						$size->barcode = $i['barcode'];
					$sizes[] = $size;
				}
			}
			$this->orderLineSelected->customer_order_line_quantities()->delete();
			$this->orderLineSelected->customer_order_line_quantities()->saveMany($sizes);
		}

		//SAVE RAW MATERIAL ORDERS
		if(array_key_exists('raw_material_orders', $this->line)){
			$rawMaterials = [];
			foreach ($this->line['raw_material_orders'] as $i) { 
				$rawMaterial = new RawMaterialOrders();
				if(array_key_exists('raw_materials_id', $i) && !$i['raw_materials_id'] == "")
					$rawMaterial->raw_materials_id = $i['raw_materials_id'];
				if(array_key_exists('customer_order_lines_id', $i) && !$i['customer_order_lines_id'] == "")
					$rawMaterial->customer_order_lines_id = $i['customer_order_lines_id'];
				if(array_key_exists('ref', $i) && !$i['ref'] == "")
					$rawMaterial->ref = $i['ref'];
				if(array_key_exists('order_date', $i) && !$i['order_date'] == "")
					$rawMaterial->order_date = $i['order_date'];
				if(array_key_exists('ship_date', $i) && !$i['ship_date'] == "")
					$rawMaterial->ship_date = $i['ship_date'];
				if(array_key_exists('tracking', $i) && !$i['tracking'] == "")
					$rawMaterial->tracking = $i['tracking'];
				if(array_key_exists('act_ship_date', $i) && !$i['act_ship_date'] == "")
					$rawMaterial->act_ship_date = $i['act_ship_date'];
				if(array_key_exists('delivery_date', $i) && !$i['delivery_date'] == "")
					$rawMaterial->delivery_date = $i['delivery_date'];
				if(array_key_exists('percent_of_total', $i) && !$i['percent_of_total'] == "")
					$rawMaterial->percent_of_total = $i['percent_of_total'];
				if(array_key_exists('notes', $i) && !$i['notes'] == "")
					$rawMaterial->notes = $i['notes'];
				$rawMaterials[] = $rawMaterial;
			}
			$this->orderLineSelected->raw_material_orders()->delete();
			$this->orderLineSelected->raw_material_orders()->saveMany($rawMaterials);
		}

				//SAVE SIZES
				if(array_key_exists('shipmentSizes', $this->line)){
					$sizes = [];
					foreach ($this->line['sizes'] as $i) { 
						if(!empty($i)){
							$size = new CustomerOrderLineQuantities();
							if(array_key_exists('size', $i))
								$size->sizes_id = $i['size'];
							if(array_key_exists('qty', $i))
								$size->qty = $i['qty'];
							$sizes[] = $size;
						}
					}
					$this->orderLineSelected->customer_order_line_quantities()->delete();
					$this->orderLineSelected->customer_order_line_quantities()->saveMany($sizes);
				}

		session()->flash('message', 'Order Line Updated!');
		session()->flash('alert-class', 'alert-success');

		$this->closeEditLine();
	}
	public function shipmentSizes($i){
		$this->shipments = TRUE;
		$this->orderLineSelected = $this->order->customer_order_lines->find($i);

		foreach($this->orderLineSelected->shipment_lines as $line){
			if(!$line->shipment_line_sizes->isEmpty()){
				$this->addShipment();
				$this->shipmentLines[$this->shipmenti]['id'] = $line->id;
				$this->shipmentLines[$this->shipmenti]['shipment'] = $line->shipment_id;
				if(!$line->delivery_date == NULL)
					$this->shipmentLines[$this->shipmenti]['delivery'] = $line->delivery_date->format('Y-m-d');
				if(!$line->shipment_line_exfty_dates->sortByDesc('created_at')->isEmpty())
					$this->shipmentLines[$this->shipmenti]['exfty'] = $line->shipment_line_exfty_dates->sortByDesc('created_at')->first()->date->format('Y-m-d');
				foreach($this->shipmentLines[$this->shipmenti]['sizes'] as $x=>$size)
					$this->shipmentLines[$this->shipmenti]['sizes'][$x]['qty'] = $line->shipment_line_sizes->where('sizes_id', $this->shipmentLines[$this->shipmenti]['sizes'][$x]['size'])->first()->qty;
			}
		}
	}
	public function addShipment(){
		Gate::authorize('shipment:update');
		if(empty($this->shipmentLines)){
			$this->shipmentLines = [];
		}
		$this->shipmenti++;
		$this->shipmentLines[$this->shipmenti]['shipment'] = NULL;
		$this->shipmentLines[$this->shipmenti]['id'] = NULL;
		$this->shipmentLines[$this->shipmenti]['exfty'] = NULL;
		$this->shipmentLines[$this->shipmenti]['delivery'] = NULL;
		foreach($this->orderLineSelected->customer_order_line_quantities as $x=>$size){
			$this->shipmentLines[$this->shipmenti]['sizes'][$x] = ['size' => $size->sizes_id, 'qty' => $size->qty];
		}
	}
	public function deleteShipment($line, $id = NULL){
		Gate::authorize('shipment:update');
		if($id != NULL)
			$this->orderLineSelected->shipment_lines->find($id)->delete();
		unset($this->shipmentLines[$line]);
		$this->shipmentSizes($this->orderLineSelected->id);
		$this->checkRemoveShipment = NULL;
	}
	public function closeShipments(){
		$this->shipments = FALSE;
		$this->dispatchBrowserEvent('closeShipmentModal');
		$this->shipmentLines = [];
		$this->shipmenti = 0;
		$this->editShipmentCheck = FALSE;
	}
	public function saveShipments(){
		Gate::authorize('shipment:update');
		session()->flash('message', 'Validation Error');
		session()->flash('alert-class', 'alert-danger');
		$validatedData = $this->validate($this->shipmentRules);

		session()->flash('message', 'Error Saving');
		session()->flash('alert-class', 'alert-danger');
		//dd($this->shipmentLines);
		foreach($this->shipmentLines as $i=>$shipment){
			//dd($shipment);
			if($this->shipmentLines[$i]['shipment'] == "")
				$this->shipmentLines[$i]['shipment'] = NULL;
			$shipmentLine = ShipmentLine::with('shipment_line_exfty_dates')->updateOrCreate(['id' => $this->shipmentLines[$i]['id']],[
				'customer_order_lines_id' => $this->orderLineSelected->id,
				'shipment_id' => $this->shipmentLines[$i]['shipment'],
				'delivery_date' => $this->shipmentLines[$i]['delivery'],
			]);

			//SAVE SHIPMENT SIZES
			$shipmentLines = [];
			foreach ($shipment['sizes'] as $i) { 
				$shipmentSize = new ShipmentLineSizes();
				if(array_key_exists('size', $i))
					$shipmentSize->sizes_id = $i['size'];
				if(array_key_exists('qty', $i))
					$shipmentSize->qty = $i['qty'];
				$shipmentSize->shipment_line_id = $shipmentLine->id;
				$shipmentLines[] = $shipmentSize;
			}
			//dd($shipment['sizes']);
			$shipmentLine->shipment_line_sizes()->delete();
			$shipmentLine->shipment_line_sizes()->saveMany($shipmentLines);

			if(!empty($shipment['exfty']) && $shipment['exfty'] != NULL &&
			($shipmentLine->shipment_line_exfty_dates->isEmpty() ||
			$shipment['exfty'] != $shipmentLine->shipment_line_exfty_dates->sortByDesc('created_at')->first()->date->format('Y-m-d'))
			){
				$date = new ShipmentLineExftyDates();
				$date->date = $shipment['exfty'];
				$shipmentLine->shipment_line_exfty_dates()->save($date);
			}
			$this->shipmentSizes($this->orderLineSelected);
		}
		$this->closeShipments();
		session()->flash('message', 'Shipments Saved');
		session()->flash('alert-class', 'alert-success');
	}

	public function addSize(){
		if(empty($this->line['sizes']))
			$this->line['sizes'] = [];
		array_push($this->line['sizes'], []);
	}

	public function saveNewLine(){
		Gate::authorize('order:update');
		$order = $this->order->customer_order_lines()->save($this->orderLineSelected);
		session()->flash('message', 'Line Added to Order!');
		session()->flash('alert-class', 'alert-success');
		$order = $this->orderLineSelected->id;
		$this->closeNewLine();
		$this->refreshOrder();
		}

	public function newLine(){
		$this->newLine = TRUE;
		//$this->add();
		$this->orderLineSelected = new CustomerOrderLines();
		$this->orderLineSelected->colourways_id = "";
	}

	public function closeEditLine(){
		$this->editLine = FALSE;
		$this->resetLine();
		$this->dispatchBrowserEvent('closeEditModal');
		$this->editLineCheck = FALSE;
	}

	public function closeNewLine(){
		$this->newLine = FALSE;
		$this->resetLine();
		$this->dispatchBrowserEvent('closeNewModal');
	}

	public function addOrderRawMaterialOrder(){
		if(empty($this->raw_material_orders))
			$this->raw_material_orders = [];
		array_push($this->raw_material_orders , ['raw_materials_id' => "", 'customer_order_lines_id' => "", 'style_versions_id' => ""]);
	}
	public function removeOrderRawMatOrder($i){
		unset($this->raw_material_orders[$i]);
	}

	public function addRawMaterialOrder(){
		if(empty($this->line['raw_material_orders']))
			$this->line['raw_material_orders'] = [];
		array_push($this->line['raw_material_orders'] , ['raw_materials_id' => ""]);
	}
	public function removeRawMatOrder($i){
		unset($this->line['raw_material_orders'][$i]);
	}

	public function remove($i)
	{
		unset($this->inputs[$i]);
		unset($this->line['customer_order_line_yarns'][$i]);
	}
	
	public function removeQty($line){
		unset($this->line['sizes'][$line]);
	}

	public function resetLine(){
		$this->line = [];
		$this->orderLineSelected = NULL;
		$this->lineDates = [];
		$this->inputs = [];
		$this->i = -1;
	}

	public function totalQty(){
		$qty = 0;
		if(array_key_exists('sizes', $this->line)){
			foreach ($this->line['sizes'] as $i) { 
				if(array_key_exists('qty', $i))
					$qty = $qty + $i['qty'];
			}
		}
		return $qty;
	}
	public function getPrice($orderLine){
		if($orderLine->colourways->style_versions->style_version_prices->sortByDesc('created_at')->first() != NULL)
			return number_format((float)$orderLine->colourways->style_versions->style_version_prices->sortByDesc('created_at')->first()->price, 2, '.', '');
	}
	public function getSizePrice($size){
		if($this->orderLineSelected->colourways->style_versions->style_version_price_sizes->where('sizes_id', $size)->first() != NULL){
			return number_format((float)$this->orderLineSelected->colourways->style_versions->style_version_price_sizes->where('sizes_id', $size)->first()->price, 2, '.', '');
		}
		elseif($this->orderLineSelected->colourways->style_versions->style_version_prices->sortByDesc('created_at')->first() != NULL){
			return number_format((float)$this->orderLineSelected->colourways->style_versions->style_version_prices->sortByDesc('created_at')->first()->price, 2, '.', '');
		}
		else
			return "Not Set";
	}
}
0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

You refresh the Order instance every time the Component re-renders; so the persisted data is re-rendered.

Aside; there is so much noise in the Component, it is very difficult to reason about.

Please or to participate in this conversation.