public function syncLocations($locations)
 {
     $currentLocations = $this->locationStep()->get();
     if (!is_array($locations)) {
         $locations = [];
     }
     $index = 0;
     foreach ($locations as $id => $location) {
         $index++;
         $locations[$id]['step_order'] = $index;
     }
     foreach ($currentLocations as $currentLocation) {
         if (!array_key_exists($currentLocation->id, $locations)) {
             $currentLocation->delete();
             continue;
         }
         $currentLocation->fill($locations[$currentLocation->id]);
         $this->locationStep()->save($currentLocation);
         if (!array_key_exists('flow_steps', $locations[$currentLocation->id])) {
             $locations[$currentLocation->id]['flow_steps'] = [];
         }
         $currentLocation->syncSteps($locations[$currentLocation->id]['flow_steps']);
         unset($locations[$currentLocation->id]);
     }
     foreach ($locations as $locationData) {
         $newLocation = new LocationStep();
         $newLocation->fill($locationData);
         if (!isset($locationData['location_id']) || $locationData['location_id'] == "") {
             $newLocation['location_id'] = 1;
         }
         $this->locationStep()->save($newLocation);
         if (!array_key_exists('flow_steps', $locationData)) {
             $locationData['flow_steps'] = [];
         }
         $newLocation->syncSteps($locationData['flow_steps']);
     }
     return true;
 }
 /** @test */
 public function createFlowTemplate_WithMultipleLocationStepAttached_DeleteAllLocationStep()
 {
     $flowTemplate = FlowTemplate::create($this->flowTemplateData());
     $locationStep1 = LocationStep::create($this->locationStepData());
     $locationStep2 = LocationStep::create($this->locationStepData());
     $locationStep3 = LocationStep::create($this->locationStepData());
     $locationStep4 = LocationStep::create($this->locationStepData());
     $flowTemplate->locationStep()->save($locationStep1);
     $flowTemplate->locationStep()->save($locationStep2);
     $flowTemplate->locationStep()->save($locationStep3);
     $flowTemplate->locationStep()->save($locationStep4);
     $locationStep1->delete();
     $locationStep2->delete();
     $locationStep3->delete();
     $locationStep4->delete();
     $flowTemplateCreated = FlowTemplate::find($flowTemplate->id);
     $this->assertCount(0, $flowTemplateCreated->locationStep);
     $flowTemplate->delete();
 }
 protected function importLocationStepsForContinuedIssues()
 {
     $issues = DB::connection('oldissue')->select('select * from initlaws where currentphase <> 1 and originpropid > 0');
     foreach ($issues as $issue) {
         $count = 0;
         $importSteps = DB::connection('oldissue')->select("select * from customsteps where marckfordelete = 0 and propid = {$issue->propid} order by id asc");
         if (empty($importSteps)) {
             continue;
         }
         $newLocationStep = new LocationStep();
         if (isset($importSteps[0]->optionlocation) && $importSteps[0]->optionlocation == 1) {
             $newLocationStep->location_id = 3;
         } elseif (isset($importSteps[0]->optionlocation) && $importSteps[0]->optionlocation == 2) {
             $newLocationStep->location_id = 4;
         } elseif ($importSteps[0]->lextypeid == 3 || $importSteps[0]->lextypeid == 4 || $importSteps[0]->lextypeid == 5) {
             $newLocationStep->location_id = 5;
         } else {
             $newLocationStep->location_id = 2;
         }
         $newLocationStep->issue_id = $issue->originpropid;
         $newLocationStep->step_order = 1;
         $newLocationStep->save();
         foreach ($importSteps as $importStep) {
             $newFlowStep = new FlowStep();
             $newFlowStep->id = $importStep->id;
             $newFlowStep->flow_name = StepAutocomplete::find($importStep->basestepid)->name;
             $newFlowStep->estimated_duration = $importStep->durata ? $importStep->durata : '';
             $newFlowStep->location_step_id = $newLocationStep->id;
             $newFlowStep->flowstep_order = $count++;
             $newFlowStep->start_date = $importStep->dataStart;
             $newFlowStep->end_date = $importStep->dataEnd;
             $newFlowStep->finalizat = $importStep->dataEnd ? 1 : 0;
             $translatableData = ['ro' => ['observatii' => $importStep->obsv ? $importStep->obsv : ''], 'en' => ['observatii' => $importStep->enobsv ? $importStep->enobsv : '']];
             $newFlowStep->fill($translatableData);
             $newFlowStep->save();
         }
     }
     print_r("Au fost importati: " . LocationStep::count() . " locationSteps \n cu " . FlowStep::count() . " flowSteps.");
     return true;
 }