public function testSaveDelete()
 {
     RoyaltyStreamFile::truncate();
     $rsf = new RoyaltyStreamFile();
     $rsf->deal_id = 2;
     $rsf->stream_file_name = "123123123";
     $rsf->save();
     $this->assertTrue($rsf->id != 0);
     $this->assertTrue($rsf->created != null);
     $this->assertTrue($rsf->updated != null);
     //        $this->assertTrue($rp["deleted"] == 0);
     $rsf->delete();
     $this->assertTrue($rsf->deleted == 1);
     RoyaltyStreamFile::truncate();
 }
 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;
 }
 public static function boot()
 {
     parent::boot();
     RoyaltyStreamFile::deleting(function ($royaltyFile) {
         $royaltyFile->deleted = 1;
         $royaltyFile->update();
     });
 }
示例#4
0
 public static function apiStats($id)
 {
     $query = self::query();
     $statsStartDate = new DateTime();
     //get average
     if (sizeof(RoyaltyStreamFile::lastRoyaltyFile($id)->get()) > 0) {
         $lastRoyaltyStreamFile = RoyaltyStreamFile::lastRoyaltyFile($id)->get()[0];
         $statsStartDate->setDate($lastRoyaltyStreamFile->period_year, $lastRoyaltyStreamFile->period_month, 1);
     }
     $last12monthDate = $statsStartDate->modify('-12 month');
     $last12monthAmount = $query->select(DB::raw("SUM(royalty_stream.royalty_amount) as amount"))->join(RoyaltyStreamFile::table() . " as royalty_stream_file", 'royalty_stream_file.deal_id', '=', 'deal.id')->join(RoyaltyStream::table() . " as royalty_stream", 'royalty_stream_file.id', '=', 'stream_file_id')->where('deal_id', $id)->where(DB::raw("CONCAT(royalty_stream_file.period_year, '-',royalty_stream_file.period_month)"), '>=', $last12monthDate)->first()["amount"];
     $query = self::query();
     $last12monthAmountTop5Songs = $query->select('royalty_stream.song_title', DB::raw("SUM(royalty_stream.royalty_amount) as amount"))->join(RoyaltyStreamFile::table() . " as royalty_stream_file", 'royalty_stream_file.deal_id', '=', 'deal.id')->join(RoyaltyStream::table() . " as royalty_stream", 'royalty_stream_file.id', '=', 'stream_file_id')->groupBy('royalty_stream.song_title')->where('deal_id', $id)->orderBy('amount', 'desc')->limit(5)->where(DB::raw("CONCAT(royalty_stream_file.period_year, '-',royalty_stream_file.period_month)"), '>=', $last12monthDate)->get();
     $sum = 0;
     foreach ($last12monthAmountTop5Songs as $song) {
         $sum += $song->amount;
     }
     return ['last_12_month_avg' => $last12monthAmount, 'top_5_song_avg' => $sum];
 }
 public function testEmptyLinesInFile()
 {
     $this->resetEvents();
     $fileName = "with_empty_lines.csv";
     $path = $this->copyFileToTemp($fileName);
     $s = $this->service();
     $companyId = rand(1000, 1000000);
     $fileDetails = ["year" => 2015, "month" => 7];
     $rpf = $s->process($path, $companyId, $fileDetails);
     $this->assertTrue($rpf->status == RoyaltyPaymentFile::STATUS_PAYMENTS_PROCESSED);
     $rpf = $s->processRoyaltyStreams($rpf);
     $this->assertTrue($rpf->status == RoyaltyPaymentFile::STATUS_PROCESSED);
     $payments = RoyaltyPayment::all();
     $this->assertTrue(count($payments) == 4);
     $this->assertTrue($payments[0]->payee_code == 1128);
     $this->assertTrue($payments[1]->payee_code == 1111);
     $this->assertTrue($payments[2]->payee_code == 1545);
     $this->assertTrue($payments[3]->payee_code == 1332);
     $clients = Client::all();
     $this->assertTrue(count($clients) == 4);
     $payeePayments = PayeePayment::all();
     $this->assertTrue(count($payments) == 4);
     $deals = Deal::all();
     $this->assertTrue(count($deals) == 4);
     $streams = RoyaltyStream::all();
     $this->assertTrue(count($streams) == 4);
     $this->assertTrue($streams[0]->song_number == 33959);
     $this->assertTrue($streams[1]->song_number == 14402);
     $this->assertTrue($streams[2]->song_number == 12043);
     $this->assertTrue($streams[3]->song_number == 51311);
     $streamFiles = RoyaltyStreamFile::all();
     $this->assertTrue(count($streamFiles) == 4);
     $this->resetEvents();
 }
 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]);
         }
     }
 }
 public function royalty_stream_file_pdf($id)
 {
     $royaltyStreamFile = RoyaltyStreamFile::find($id);
     $user = Auth::getUser();
     if (is_null($royaltyStreamFile) || !$user->isAdmin() && $royaltyStreamFile->company_id !== $user->company_id) {
         return Response::make('Not Found', 404);
     }
     $pdfFilename = $royaltyStreamFile->stream_file_name;
     $pdfFilename = substr($pdfFilename, strrpos($pdfFilename, '/') + 1);
     $pdfFilename = str_replace('csv', 'pdf', $pdfFilename);
     $pdfFile = storage_path("pdf/{$pdfFilename}");
     return Response::make(file_get_contents($pdfFile), 200, ['Content-Type' => 'application/pdf']);
 }