|
6 | 6 | use App\Helpers\Helper; |
7 | 7 | use App\Http\Controllers\Controller; |
8 | 8 | use App\Http\Requests\ImageUploadRequest; |
9 | | -use App\Http\Requests\StoreMultipleAssetRequest; |
| 9 | +use App\Http\Requests\CreateMultipleAssetRequest; |
10 | 10 | use App\Http\Requests\UpdateAssetRequest; |
11 | 11 | use App\Models\Actionlog; |
12 | 12 | use App\Http\Requests\UploadFileRequest; |
@@ -99,7 +99,7 @@ public function create(Request $request) : View |
99 | 99 | * @author [A. Gianotto] [<[email protected]>] |
100 | 100 | * @since [v1.0] |
101 | 101 | */ |
102 | | - public function store(StoreMultipleAssetRequest $request): RedirectResponse |
| 102 | + public function store(CreateMultipleAssetRequest $request): RedirectResponse |
103 | 103 | { |
104 | 104 | $this->authorize(Asset::class); |
105 | 105 |
|
@@ -136,123 +136,136 @@ public function store(StoreMultipleAssetRequest $request): RedirectResponse |
136 | 136 | $successes = []; |
137 | 137 | $failures = []; |
138 | 138 |
|
139 | | - for ($a = 1, $aMax = count($asset_tags); $a <= $aMax; $a++) { |
140 | | - $asset = new Asset(); |
| 139 | + try { |
| 140 | + DB::beginTransaction(); |
| 141 | + for ($a = 1, $aMax = count($asset_tags); $a <= $aMax; $a++) { |
| 142 | + $asset = new Asset(); |
141 | 143 |
|
142 | | - $asset->model()->associate($model); |
143 | | - $asset->name = $request->input('name'); |
| 144 | + $asset->model()->associate($model); |
| 145 | + $asset->name = $request->input('name'); |
144 | 146 |
|
145 | | - // Check for a corresponding serial |
146 | | - if (($serials) && (array_key_exists($a, $serials))) { |
147 | | - $asset->serial = $serials[$a]; |
148 | | - } |
149 | | - |
150 | | - if (($asset_tags) && (array_key_exists($a, $asset_tags))) { |
151 | | - $asset->asset_tag = $asset_tags[$a]; |
152 | | - } |
| 147 | + // Check for a corresponding serial |
| 148 | + if (($serials) && (array_key_exists($a, $serials))) { |
| 149 | + $asset->serial = $serials[$a]; |
| 150 | + } |
153 | 151 |
|
154 | | - $asset->company_id = $companyId; |
155 | | - $asset->model_id = $request->input('model_id'); |
156 | | - $asset->order_number = $request->input('order_number'); |
157 | | - $asset->notes = $request->input('notes'); |
158 | | - $asset->created_by = auth()->id(); |
159 | | - $asset->status_id = request('status_id'); |
160 | | - $asset->warranty_months = request('warranty_months', null); |
161 | | - $asset->purchase_cost = request('purchase_cost'); |
162 | | - $asset->purchase_date = request('purchase_date', null); |
163 | | - $asset->asset_eol_date = request('asset_eol_date', null); |
164 | | - $asset->assigned_to = request('assigned_to', null); |
165 | | - $asset->supplier_id = request('supplier_id', null); |
166 | | - $asset->requestable = request('requestable', 0); |
167 | | - $asset->rtd_location_id = request('rtd_location_id', null); |
168 | | - $asset->byod = request('byod', 0); |
169 | | - |
170 | | - if (! empty($settings->audit_interval)) { |
171 | | - $asset->next_audit_date = Carbon::now()->addMonths((int) $settings->audit_interval)->toDateString(); |
172 | | - } |
| 152 | + if (($asset_tags) && (array_key_exists($a, $asset_tags))) { |
| 153 | + $asset->asset_tag = $asset_tags[$a]; |
| 154 | + } |
173 | 155 |
|
174 | | - // Set location_id to rtd_location_id ONLY if the asset isn't being checked out |
175 | | - if (!request('assigned_user') && !request('assigned_asset') && !request('assigned_location')) { |
176 | | - $asset->location_id = $request->input('rtd_location_id', null); |
177 | | - } |
| 156 | + $asset->company_id = $companyId; |
| 157 | + $asset->model_id = $request->input('model_id'); |
| 158 | + $asset->order_number = $request->input('order_number'); |
| 159 | + $asset->notes = $request->input('notes'); |
| 160 | + $asset->created_by = auth()->id(); |
| 161 | + $asset->status_id = request('status_id'); |
| 162 | + $asset->warranty_months = request('warranty_months', null); |
| 163 | + $asset->purchase_cost = request('purchase_cost'); |
| 164 | + $asset->purchase_date = request('purchase_date', null); |
| 165 | + $asset->asset_eol_date = request('asset_eol_date', null); |
| 166 | + $asset->assigned_to = request('assigned_to', null); |
| 167 | + $asset->supplier_id = request('supplier_id', null); |
| 168 | + $asset->requestable = request('requestable', 0); |
| 169 | + $asset->rtd_location_id = request('rtd_location_id', null); |
| 170 | + $asset->byod = request('byod', 0); |
| 171 | + |
| 172 | + if (!empty($settings->audit_interval)) { |
| 173 | + $asset->next_audit_date = Carbon::now()->addMonths((int)$settings->audit_interval)->toDateString(); |
| 174 | + } |
178 | 175 |
|
179 | | - if ($request->has('use_cloned_image')) { |
180 | | - $cloned_model_img = Asset::select('image')->find($request->input('clone_image_from_id')); |
181 | | - if ($cloned_model_img) { |
182 | | - $new_image_name = 'clone-'.date('U').'-'.$cloned_model_img->image; |
183 | | - $new_image = 'assets/'.$new_image_name; |
184 | | - Storage::disk('public')->copy('assets/'.$cloned_model_img->image, $new_image); |
185 | | - $asset->image = $new_image_name; |
| 176 | + // Set location_id to rtd_location_id ONLY if the asset isn't being checked out |
| 177 | + if (!request('assigned_user') && !request('assigned_asset') && !request('assigned_location')) { |
| 178 | + $asset->location_id = $request->input('rtd_location_id', null); |
186 | 179 | } |
187 | 180 |
|
188 | | - } else { |
189 | | - $asset = $request->handleImages($asset); |
190 | | - } |
| 181 | + if ($request->has('use_cloned_image')) { |
| 182 | + $cloned_model_img = Asset::select('image')->find($request->input('clone_image_from_id')); |
| 183 | + if ($cloned_model_img) { |
| 184 | + $new_image_name = 'clone-' . date('U') . '-' . $cloned_model_img->image; |
| 185 | + $new_image = 'assets/' . $new_image_name; |
| 186 | + Storage::disk('public')->copy('assets/' . $cloned_model_img->image, $new_image); |
| 187 | + $asset->image = $new_image_name; |
| 188 | + } |
191 | 189 |
|
192 | | - // Update custom fields in the database. |
193 | | - // Validation for these fields is handled through the AssetRequest form request |
| 190 | + } else { |
| 191 | + $asset = $request->handleImages($asset); |
| 192 | + } |
194 | 193 |
|
195 | | - if (($model) && ($model->fieldset)) { |
196 | | - foreach ($model->fieldset->fields as $field) { |
197 | | - if ($field->field_encrypted == '1') { |
198 | | - if (Gate::allows('assets.view.encrypted_custom_fields')) { |
| 194 | + // Update custom fields in the database. |
| 195 | + // Validation for these fields is handled through the AssetRequest form request |
| 196 | + |
| 197 | + if (($model) && ($model->fieldset)) { |
| 198 | + foreach ($model->fieldset->fields as $field) { |
| 199 | + if ($field->field_encrypted == '1') { |
| 200 | + if (Gate::allows('assets.view.encrypted_custom_fields')) { |
| 201 | + if (is_array($request->input($field->db_column))) { |
| 202 | + $asset->{$field->db_column} = Crypt::encrypt(implode(', ', $request->input($field->db_column))); |
| 203 | + } else { |
| 204 | + $asset->{$field->db_column} = Crypt::encrypt($request->input($field->db_column)); |
| 205 | + } |
| 206 | + } |
| 207 | + } else { |
199 | 208 | if (is_array($request->input($field->db_column))) { |
200 | | - $asset->{$field->db_column} = Crypt::encrypt(implode(', ', $request->input($field->db_column))); |
| 209 | + $asset->{$field->db_column} = implode(', ', $request->input($field->db_column)); |
201 | 210 | } else { |
202 | | - $asset->{$field->db_column} = Crypt::encrypt($request->input($field->db_column)); |
| 211 | + $asset->{$field->db_column} = $request->input($field->db_column); |
203 | 212 | } |
204 | 213 | } |
205 | | - } else { |
206 | | - if (is_array($request->input($field->db_column))) { |
207 | | - $asset->{$field->db_column} = implode(', ', $request->input($field->db_column)); |
208 | | - } else { |
209 | | - $asset->{$field->db_column} = $request->input($field->db_column); |
210 | | - } |
211 | 214 | } |
212 | 215 | } |
213 | | - } |
214 | 216 |
|
215 | | - \Log::error("About to check validity and, possibly, save..."); |
216 | | - // Validate the asset before saving |
217 | | - if ($asset->isValid() && $asset->save()) { |
218 | | - $target = null; |
219 | | - $location = null; |
| 217 | + // Validate the asset before saving |
| 218 | + // Note - it can be tempting to instead want to call saveOrFail(), to automatically throw when an object |
| 219 | + // is invalid (and can't save). But this won't work, because Custom Fields _overrides_ the save() method |
| 220 | + // to inject the Custom Field Rules into the $rules property right before invoking the _real_ save. |
| 221 | + // so, instead, we have to catch failures on the 'else' clause and throw there. |
| 222 | + if ($asset->isValid() && $asset->save()) { |
| 223 | + $target = null; |
| 224 | + $location = null; |
220 | 225 |
|
221 | | - if ($userId = request('assigned_user')) { |
222 | | - $target = User::find($userId); |
| 226 | + if ($userId = request('assigned_user')) { |
| 227 | + $target = User::find($userId); |
223 | 228 |
|
224 | | - if (!$target) { |
225 | | - return redirect()->back()->withInput()->with('error', trans('admin/hardware/message.create.target_not_found.user')); |
226 | | - } |
227 | | - $location = $target->location_id; |
| 229 | + if (!$target) { |
| 230 | + return redirect()->back()->withInput()->with('error', trans('admin/hardware/message.create.target_not_found.user')); |
| 231 | + } |
| 232 | + $location = $target->location_id; |
228 | 233 |
|
229 | | - } elseif ($assetId = request('assigned_asset')) { |
230 | | - $target = Asset::find($assetId); |
| 234 | + } elseif ($assetId = request('assigned_asset')) { |
| 235 | + $target = Asset::find($assetId); |
231 | 236 |
|
232 | | - if (!$target) { |
233 | | - return redirect()->back()->withInput()->with('error', trans('admin/hardware/message.create.target_not_found.asset')); |
234 | | - } |
235 | | - $location = $target->location_id; |
| 237 | + if (!$target) { |
| 238 | + return redirect()->back()->withInput()->with('error', trans('admin/hardware/message.create.target_not_found.asset')); |
| 239 | + } |
| 240 | + $location = $target->location_id; |
236 | 241 |
|
237 | | - } elseif ($locationId = request('assigned_location')) { |
238 | | - $target = Location::find($locationId); |
| 242 | + } elseif ($locationId = request('assigned_location')) { |
| 243 | + $target = Location::find($locationId); |
239 | 244 |
|
240 | | - if (!$target) { |
241 | | - return redirect()->back()->withInput()->with('error', trans('admin/hardware/message.create.target_not_found.location')); |
| 245 | + if (!$target) { |
| 246 | + return redirect()->back()->withInput()->with('error', trans('admin/hardware/message.create.target_not_found.location')); |
| 247 | + } |
| 248 | + $location = $target->id; |
242 | 249 | } |
243 | | - $location = $target->id; |
244 | | - } |
245 | 250 |
|
246 | | - if (isset($target)) { |
247 | | - $asset->checkOut($target, auth()->user(), date('Y-m-d H:i:s'), $request->input('expected_checkin', null), 'Checked out on asset creation', $request->get('name'), $location); |
248 | | - } |
| 251 | + if (isset($target)) { |
| 252 | + $asset->checkOut($target, auth()->user(), date('Y-m-d H:i:s'), $request->input('expected_checkin', null), 'Checked out on asset creation', $request->get('name'), $location); |
| 253 | + } |
249 | 254 |
|
250 | | - $successes[] = "<a href='" . route('hardware.show', $asset) . "' style='color: white;'>" . e($asset->asset_tag) . "</a>"; |
| 255 | + $successes[] = "<a href='" . route('hardware.show', $asset) . "' style='color: white;'>" . e($asset->asset_tag) . "</a>"; |
251 | 256 |
|
252 | | - } else { |
253 | | - $failures[] = join(",", $asset->getErrors()->all()); |
| 257 | + } else { |
| 258 | + $asset->throwValidationException(); // we have to do this for the reason listed above - can't use saveOrFail() |
| 259 | + $failures[] = join(",", $asset->getErrors()->all()); //TODO - this can probably go away soon |
| 260 | + } |
254 | 261 | } |
| 262 | + } catch (\Throwable $e) { |
| 263 | + \Log::debug("Caught exception in multi-create - rolling back: " . $e->getMessage()); |
| 264 | + DB::rollBack(); |
| 265 | + throw $e; |
255 | 266 | } |
| 267 | + DB::commit(); |
| 268 | + |
256 | 269 | if($request->get('redirect_option') === 'back'){ |
257 | 270 | session()->put(['redirect_option' => 'index']); |
258 | 271 | } else { |
|
0 commit comments