public function saveRoyaltyStreamFile($rpf, $deal)
 {
     $periodType = !empty($rpf->quarter) ? 1 : 0;
     $quarter = !empty($rpf->quarter) ? $rpf->quarter : ceil($rpf->month / 3);
     $month = !empty($rpf->month) ? $rpf->month : $quarter * 3 - 2;
     $rsf = RoyaltyStreamFile::create(["stream_file_name" => $rpf->path, "period_year" => $rpf->year, "period_quarter" => $quarter, "period_month" => $month, "period_type" => $periodType, "company_id" => $deal->company_id, "deal_id" => $deal->id, "period_year_quarter" => $rpf->year . "Q" . $quarter]);
     return $rsf;
 }
 protected function processFile($filename, $metadata, $fileList, $deal, $path)
 {
     $monthToQuarter = [1 => 1, 2 => 1, 3 => 1, 4 => 2, 5 => 2, 6 => 2, 7 => 3, 8 => 3, 9 => 3, 10 => 4, 11 => 4, 12 => 4];
     $this->log("Processing file {$filename}");
     $csvFilename = sprintf('%s/%s.csv', $path, $filename);
     if ($this->fileExists($fileList, $csvFilename)) {
         /*
          * MESSAGE LEFT ON 4/14/15
          * For Ascap International and Domestic providers the files come together, meaning in the same processing
          * directory, we have Ascap International and Domestic files. We check the file name to see if contains
          * the word international or domestic and we execute etl with the correct provider.
          */
         $processingProviderId = $metadata['provider'];
         if ($processingProviderId == 3 && strpos($filename, 'Domestic') !== false) {
             $processingProviderId = 50;
         }
         if ($processingProviderId == 50 && strpos($filename, 'International') !== false) {
             $processingProviderId = 3;
         }
         $royaltyProvider = RoyaltyProvider::find($processingProviderId);
         $csvContent = $this->dropboxReadFile($csvFilename);
         $this->log('File size is ' . strlen($csvContent));
         $etlFolder = $royaltyProvider->php_etl_upload_location;
         if (!is_dir($etlFolder)) {
             $this->log("Create upload folder {$etlFolder}");
             mkdir($etlFolder, 0755, true);
             mkdir($etlFolder . '/LogFiles', 0755);
             mkdir($etlFolder . '/Processed', 0755);
         }
         $etlFile = sprintf('%s/%d_%s.csv', $etlFolder, $deal->id, $filename);
         $this->log("Writing {$etlFile}");
         file_put_contents($etlFile, $csvContent);
         $month = $metadata['month'];
         if (empty($month)) {
             $month = $this->quarterToMonth((int) $metadata['quarter']);
             $periodType = RoyaltyStreamFile::PERIOD_TYPE_QUARTER;
         }
         $quarter = $metadata['quarter'];
         if (empty($quarter)) {
             $quarter = $monthToQuarter[(int) $metadata['month']];
             $periodType = RoyaltyStreamFile::PERIOD_TYPE_MONTH;
         }
         $periodYearQuarter = sprintf('%dQ%d', $metadata['year'], $quarter);
         $hasPdf = false;
         if ($metadata['hasPdf']) {
             $pdfFilename = sprintf('%s/%s.pdf', $path, $filename);
             if ($this->fileExists($fileList, $pdfFilename)) {
                 $contents = $this->dropboxReadFile($pdfFilename);
                 $pdfFolder = storage_path('pdf');
                 $pdfFile = sprintf('%s/%d_%s.pdf', $pdfFolder, $deal->id, $filename);
                 $this->log("Saving PDF to {$pdfFile}");
                 file_put_contents($pdfFile, $contents);
                 $hasPdf = true;
             }
         }
         $share = isset($metadata['providerRoyaltyShare']) ? $metadata['providerRoyaltyShare'] : 1;
         $accountName = isset($metadata["providerAccountName"]) ? $metadata["providerAccountName"] : null;
         $rsf = RoyaltyStreamFile::create(['deal_id' => $deal->id, 'stream_file_name' => $etlFile, 'status' => 0, 'period_year' => $metadata['year'], 'period_month' => $month, 'period_quarter' => $quarter, 'company_id' => $deal->company_id, 'royalty_provider_id' => $processingProviderId, 'royalty_type_id' => $metadata['providerRoyaltyType'], 'royalty_share_id' => $share, 'period_type' => $periodType, 'period_year_quarter' => $periodYearQuarter, 'account_name' => $accountName, 'has_pdf' => $hasPdf]);
         $this->log("saved royalty stream file " . $rsf->id);
         $etlCommand = $royaltyProvider->php_etl_command . ' publisher ' . $royaltyProvider->id . ' "' . $etlFile . '"';
         $this->log("Executing {$etlCommand}");
         exec($etlCommand, $output, $status);
         $this->log('Output: ' . print_r($output, true));
         $this->log("Status: {$status}");
         $this->log("Delete etl file: {$etlFile}");
         unlink($etlFile);
         if ($output[0] != "finished") {
             throw new Exception("Processing failed for command: " . $etlCommand . " with output: " . $output[0]);
         }
     }
 }