public function unsubscribe($user_id)
 {
     $validator = Validator::make(array('id' => $user_id), array('id' => 'numeric'));
     if ($validator->fails()) {
         return $this->respondInsufficientPrivileges($validator->messages()->all());
     }
     $category_id = Input::get('category_id');
     if ($category_id) {
         $subscription = Subscription::whereRaw('user_id = ' . $user_id . ' and category_id = ' . $category_id)->first();
         if ($subscription) {
             $subscription->delete();
             return $this->respondNoContent();
         }
         return $this->respondNotFound('Subscription not found');
     }
     return $this->respondNotFound('Category not found');
 }
 public function postCSVUpload()
 {
     $input = Input::all();
     error_log(json_encode($input));
     // validation
     $rules = array('file' => 'mimes:csv,txt');
     $validation = Validator::make($input, $rules);
     if ($validation->fails()) {
         return Response::make($validation->errors->first(), 400);
     }
     // check for duplicates and move to uploads dir
     $file = Input::file('file');
     $destinationPath = 'uploads';
     $filename = $file->getClientOriginalName();
     $file_path = public_path() . '/' . $destinationPath . '/' . $filename;
     // get rid of old entries
     Emailusage::where('filename', $filename)->delete();
     // delete old file
     if (File::exists($file_path)) {
         File::delete($file_path);
     }
     // move new file
     $upload_success = Input::file('file')->move($destinationPath, $filename);
     if ($upload_success) {
         $handle = fopen($file_path, 'r');
         while (($line = fgetcsv($handle)) !== FALSE) {
             // aws account
             $curr_aws_account = $line[1];
             // 1st line of col labels
             if (!is_numeric($curr_aws_account)) {
                 //error_log($curr_aws_account." not numeric ):");
                 continue;
             }
             $cat_cmp = strcmp($line[4], 'Amazon Simple Email Service');
             if ($cat_cmp !== 0) {
                 //error_log($cat_cmp." Cat not relevant ".$line[4]);
                 continue;
             }
             $mail_service_cmp = strpos(strtolower($line[5]), strtolower('Cost per recipient of SendEmail'));
             $attachments_service_cmp = strpos(strtolower($line[5]), strtolower('Cost per GB of attachments'));
             if ($mail_service_cmp === false && $attachments_service_cmp === false) {
                 error_log($service_cmp . " Service not relevant " . $line[5]);
                 continue;
             }
             //$line is an array of the csv elements
             $num_columns = count($line);
             error_log(">>> LINE " . json_encode($line));
             // get the corresponding company_id
             $company_meta = CompanyMeta::where('type', 'aws')->where('subtype', 'auth')->where('key', 'account')->where('value', $curr_aws_account)->first();
             $company_id = -1;
             if ($company_meta) {
                 $company_id = $company_meta->company_id;
                 error_log($company_id . " Company FOUND :)");
             }
             // Error no company found
             if ($company_id === -1) {
                 error_log("Company NOT found ):");
                 continue;
             }
             // use start date for period
             $curr_period = explode(" ", $line[2])[0];
             error_log("Curr period " . $curr_period);
             // make sure day is 1st of the month
             // so that date can de used as a month tag ?
             //
             // get the subscription
             $email_subscription = Subscription::where("service_id", Config::get('eenvoudcrm.nieuwsbrieven_service_id'))->where('company_id', $company_id)->first();
             if (!$email_subscription) {
                 error_log("Consistency check falure - no nieuwsbrieven subscription (28) found");
                 continue;
             }
             $mail_bill_type = $mail_service_cmp === false ? 'attach' : 'mail';
             // search emailusage table for already existing entries
             $curr_emailusage = Emailusage::where('type', $mail_bill_type)->where('period', '=', $curr_period)->where('subscription_id', '=', $email_subscription->id)->first();
             // if they exist update
             if ($curr_emailusage) {
                 //update
                 error_log('update ' . json_encode($curr_emailusage));
                 $curr_emailusage->type = $mail_bill_type;
                 $curr_emailusage->cnt = $line[6];
                 $curr_emailusage->filename = $filename;
                 $curr_emailusage->save();
             } else {
                 // get the subscription
                 $email_subscription = Subscription::whereRaw("service_id=" . Config::get('eenvoudcrm.nieuwsbrieven_service_id') . " AND company_id={$company_id}")->first();
                 error_log('create ' . json_encode($email_subscription));
                 if ($email_subscription) {
                     $curr_emailusage = new Emailusage();
                     $curr_emailusage->type = $mail_bill_type;
                     $curr_emailusage->subscription_id = $email_subscription->id;
                     $curr_emailusage->period = $curr_period;
                     $curr_emailusage->cnt = $line[6];
                     $curr_emailusage->filename = $filename;
                     $curr_emailusage->save();
                     error_log(json_encode($curr_emailusage));
                 } else {
                     error_log("No niewsbrieven subscriptions for " . $company_id);
                 }
             }
         }
         return Response::json(['success' => 'success', 'reload' => true]);
     }
 }