public function upload($file, $type)
 {
     $uploader = new FileUploader();
     $location = 'uploads/services/' . date('m') . '/';
     $uploadedFile = $uploader->upload($file, $location, $type);
     return $uploadedFile;
 }
 public function addNew($job, $data, $file)
 {
     if (!($contractor = \Contractor::getContractor())) {
         throw new \Exception("Current user is not a contractor.", 1);
         return;
     }
     if (!is_null($file)) {
         $uploader = new FileUploader();
         $location = 'uploads/contractors/' . $contractor->id . '/expense/' . date('m') . '/';
         try {
             $uploadedFile = $uploader->upload($file, $location, 'expense');
         } catch (\Exception $e) {
             throw new \Exception($e->getMessage(), 1);
             return;
         }
     }
     $model = $this->getModel();
     $model->fill(['contractor_id' => $contractor->id, 'job_id' => $job->id, 'auth_company' => false, 'auth_agency' => is_null($job->agency_id) ? true : false, 'title' => $data['title'], 'amount' => $data['amount'], 'type' => $data['type'], 'file' => isset($uploadedFile) ? $uploadedFile : null, 'description' => $data['description'] !== '' ? $data['description'] : null]);
     if ($model->save()) {
         return $model;
     } else {
         throw new \Exception("Something wrong when saving data.", 1);
         return;
     }
 }
 public function addNew($job, $data, $file)
 {
     if (!($contractor = \Contractor::getContractor())) {
         throw new \Exception("Current user is not a contractor.", 1);
         return;
     }
     if (!is_null($file) && $data['timesheet_type'] === 'file') {
         $uploader = new FileUploader();
         $location = 'uploads/contractors/' . $contractor->id . '/timesheet/' . date('m') . '/';
         try {
             $uploadedFile = $uploader->upload($file, $location, 'resume');
         } catch (\Exception $e) {
             throw new \Exception($e->getMessage(), 1);
             return;
         }
     }
     $model = $this->getModel();
     if ($data['timesheet_type'] === 'data') {
         $startDate = Carbon::createFromFormat('Y-m-d', $data['timesheet_date_from']);
         $endDate = Carbon::createFromFormat('Y-m-d', $data['timesheet_date_to']);
     }
     $model->fill(['contractor_id' => $contractor->id, 'job_id' => $job->id, 'auth_company' => false, 'auth_agency' => is_null($job->agency_id) ? true : false, 'name' => $data['timesheet_name'], 'type' => $data['timesheet_type'], 'file' => $data['timesheet_type'] === 'file' ? $uploadedFile : null, 'report' => $data['timesheet_type'] === 'data' ? $data['report_time'] : null, 'hours' => $data['timesheet_type'] === 'data' ? $data['num_hours'] : null, 'start_date' => isset($startDate) ? $startDate : null, 'end_date' => isset($endDate) ? $endDate : null]);
     if ($model->save()) {
         return $model;
     } else {
         throw new \Exception("Something wrong when saving data.", 1);
         return;
     }
 }
 public function makeResume($contractor, $file)
 {
     try {
         $uploader = new FileUploader();
         $location = 'uploads/contractors/' . $contractor->id . '/resume/' . date('m') . '/';
         $uploadedFile = $uploader->upload($file, $location, 'document');
         $model = $this->createModel();
         $model->fill(['contractor_id' => $contractor->id, 'file' => $uploadedFile]);
         $model->save();
         \Session::forget('_sess_contractor');
         return $model;
     } catch (\Exception $e) {
         throw new \Exception($e->getMessage(), 1);
         return;
     }
 }
 public function updateAvatar($contractor, $file)
 {
     if (!is_null($contractor->image)) {
         if (\File::exists(public_path() . '/' . $contractor->image)) {
             \File::delete(public_path() . '/' . $contractor->image);
         }
     }
     $uploader = new FileUploader();
     $location = 'uploads/contractors/' . $contractor->id . '/images/' . date('m') . '/';
     try {
         $uploadedFile = $uploader->upload($file, $location);
         $contractor->image = $uploadedFile;
         $contractor->updated_at = Carbon::now();
         $contractor->save();
         session(['_sess_contractor' => ['model' => $contractor]]);
         return $contractor;
     } catch (\Exception $e) {
         throw new \Exception($e->getMessage(), 1);
         return;
     }
 }
 public function updateAvatar($file)
 {
     if (!($agency = \Agency::getAgency())) {
         throw new \Exception("You are not in agency account.", 1);
         return;
     }
     if (!is_null($agency->image)) {
         if (\File::exists(public_path() . '/' . $agency->image)) {
             \File::delete(public_path() . '/' . $agency->image);
         }
     }
     $uploader = new FileUploader();
     $location = 'uploads/agency/' . $agency->id . '/images/' . date('m') . '/';
     try {
         $uploadedFile = $uploader->upload($file, $location);
         $agency->image = $uploadedFile;
         $agency->updated_at = Carbon::now();
         $agency->save();
         session(['_sess_agency' => ['model' => $agency]]);
         return $agency;
     } catch (\Exception $e) {
         throw new \Exception($e->getMessage(), 1);
         return;
     }
 }