@@ -178,6 +178,87 @@ public IActionResult ConvertPlanRecordTemplateToPlanRecord(int planRecordTemplat
178178 var result = _planRecordDataAccess . SavePlanRecordToVehicle ( existingRecord . ToPlanRecord ( ) ) ;
179179 return Json ( OperationResponse . Conditional ( result , "Plan Record Added" , StaticHelper . GenericErrorMessage ) ) ;
180180 }
181+ [ HttpPost ]
182+ public IActionResult ConvertPlanRecordToPlanRecordTemplate ( int planRecordId )
183+ {
184+ var existingRecord = _planRecordDataAccess . GetPlanRecordById ( planRecordId ) ;
185+ if ( existingRecord . Id == default )
186+ {
187+ return Json ( OperationResponse . Failed ( "Unable to find plan record" ) ) ;
188+ }
189+ //security check.
190+ if ( ! _userLogic . UserCanEditVehicle ( GetUserID ( ) , existingRecord . VehicleId , HouseholdPermission . Edit ) )
191+ {
192+ return Json ( OperationResponse . Failed ( "Access Denied" ) ) ;
193+ }
194+ //check if template name already taken.
195+ var existingTemplateRecord = _planRecordTemplateDataAccess . GetPlanRecordTemplatesByVehicleId ( existingRecord . VehicleId ) . Any ( x => x . Description == existingRecord . Description ) ;
196+ if ( existingTemplateRecord )
197+ {
198+ return Json ( OperationResponse . Failed ( "A template with that description already exists for this vehicle" ) ) ;
199+ }
200+ var planRecordTemplate = new PlanRecordInput ( )
201+ {
202+ VehicleId = existingRecord . VehicleId ,
203+ Description = existingRecord . Description ,
204+ Notes = existingRecord . Notes ,
205+ Files = existingRecord . Files ,
206+ ImportMode = existingRecord . ImportMode ,
207+ Priority = existingRecord . Priority ,
208+ Progress = existingRecord . Progress ,
209+ Cost = existingRecord . Cost ,
210+ ExtraFields = existingRecord . ExtraFields
211+ } ;
212+ if ( existingRecord . ReminderRecordId != default )
213+ {
214+ //check if reminder still exists and is still recurring.
215+ var existingReminder = _reminderRecordDataAccess . GetReminderRecordById ( existingRecord . ReminderRecordId ) ;
216+ if ( existingReminder is null || existingReminder . Id == default || ! existingReminder . IsRecurring )
217+ {
218+ return Json ( OperationResponse . Failed ( "Missing or Non-recurring Reminder, Unable to Create Template." ) ) ;
219+ }
220+ planRecordTemplate . ReminderRecordId = existingRecord . ReminderRecordId ;
221+ }
222+ else if ( existingRecord . ReminderRecordIds . Any ( ) )
223+ {
224+ foreach ( int reminderRecordId in existingRecord . ReminderRecordIds )
225+ {
226+ //check if reminder still exists and is still recurring.
227+ var existingReminder = _reminderRecordDataAccess . GetReminderRecordById ( reminderRecordId ) ;
228+ if ( existingReminder is null || existingReminder . Id == default || ! existingReminder . IsRecurring )
229+ {
230+ return Json ( OperationResponse . Failed ( "Missing or Non-recurring Reminder, Unable to Create Template." ) ) ;
231+ }
232+ planRecordTemplate . ReminderRecordIds = existingRecord . ReminderRecordIds ;
233+ }
234+ }
235+ if ( existingRecord . RequisitionHistory . Any ( ) )
236+ {
237+ foreach ( SupplyUsageHistory supplyUsageHistory in existingRecord . RequisitionHistory )
238+ {
239+ //check if supply still exists.
240+ if ( supplyUsageHistory . Id != default )
241+ {
242+ planRecordTemplate . Supplies . Add ( new SupplyUsage { SupplyId = supplyUsageHistory . Id , Quantity = supplyUsageHistory . Quantity } ) ;
243+ }
244+ }
245+ }
246+ if ( planRecordTemplate . Supplies . Any ( ) )
247+ {
248+ //check if all supplies are available
249+ var supplyAvailability = CheckSupplyRecordsAvailability ( planRecordTemplate . Supplies ) ;
250+ if ( supplyAvailability . Any ( x => x . Missing ) )
251+ {
252+ return Json ( OperationResponse . Failed ( "Missing Supplies, Unable to Create Template." ) ) ;
253+ }
254+ else if ( supplyAvailability . Any ( x => x . Insufficient ) )
255+ {
256+ return Json ( OperationResponse . Failed ( "Insufficient Supplies" ) ) ;
257+ }
258+ }
259+ var result = _planRecordTemplateDataAccess . SavePlanRecordTemplateToVehicle ( planRecordTemplate ) ;
260+ return Json ( OperationResponse . Conditional ( result , "Plan Record Template Created" , StaticHelper . GenericErrorMessage ) ) ;
261+ }
181262 [ HttpGet ]
182263 public IActionResult GetAddPlanRecordPartialView ( )
183264 {
0 commit comments