Merge pull request #6150 from turbo124/v5-develop

Minor fixes for subscription service
This commit is contained in:
David Bomba 2021-06-29 09:47:32 +10:00 committed by GitHub
commit 80141b9ae9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 44 deletions

View File

@ -21,6 +21,7 @@ use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use ZipArchive;
use Illuminate\Support\Facades\Storage;
class ImportJsonController extends BaseController
{
@ -62,38 +63,19 @@ class ImportJsonController extends BaseController
public function import(ImportJsonRequest $request)
{
$import_file = $request->file('files');
$file_location = $request->file('files')
->storeAs(
'migrations',
$request->file('files')->getClientOriginalName()
);
$file_location = $this->unzipFile($import_file->getPathname());
// if(Ninja::isHosted())
// CompanyImport::dispatch(auth()->user()->getCompany(), auth()->user(), $file_location, $request->except('files'))->onQueue('himem');
// else
CompanyImport::dispatch(auth()->user()->getCompany(), auth()->user(), $file_location, $request->except('files'));
if(Ninja::isHosted())
CompanyImport::dispatch(auth()->user()->getCompany(), auth()->user(), $file_location, $request->except('files'))->onQueue('migration');
else
CompanyImport::dispatch(auth()->user()->getCompany(), auth()->user(), $file_location, $request->except('files'));
return response()->json(['message' => 'Processing'], 200);
}
private function unzipFile($file_contents)
{
$zip = new ZipArchive();
$archive = $zip->open($file_contents);
$filename = pathinfo($file_contents, PATHINFO_FILENAME);
$zip->extractTo(public_path("storage/backups/{$filename}"));
$zip->close();
$file_location = public_path("storage/backups/$filename/backup.json");
if (! file_exists($file_location))
throw new NonExistingMigrationFile('Backup file does not exist, or is corrupted.');
//$data = file_get_contents($file_location);
return $file_location;
//unlink($file_contents);
//unlink($file_location);
//return $data;
}
}

View File

@ -387,7 +387,6 @@ class MigrationController extends BaseController
else
StartMigration::dispatch($migration_file, $user, $fresh_company);
}
}

View File

@ -56,6 +56,7 @@ use App\Models\Vendor;
use App\Models\VendorContact;
use App\Models\Webhook;
use App\Utils\Ninja;
use App\Utils\TempFile;
use App\Utils\Traits\MakesHash;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
@ -171,7 +172,10 @@ class CompanyImport implements ShouldQueue
if ( empty( $this->file_location ) )
throw new \Exception('No import data found, has the cache expired?');
$this->backup_file = json_decode(file_get_contents($this->file_location));
// $this->backup_file = json_decode(file_get_contents($this->file_location));
$tmp_file = $this->unzipFile();
$this->backup_file = json_decode(file_get_contents($tmp_file));
// nlog($this->backup_file);
$this->checkUserCount();
@ -198,10 +202,31 @@ class CompanyImport implements ShouldQueue
}
unlink($this->file_location);
unlink($tmp_file);
}
private function unzipFile()
{
$path = TempFile::filePath(Storage::get($this->file_location), basename($this->file_location));
$zip = new ZipArchive();
$archive = $zip->open($path);
$file_path = sys_get_temp_dir().'/'.sha1(microtime());
$zip->extractTo($file_path);
$zip->close();
$file_location = "{$file_path}/backup.json";
if (! file_exists($file_location))
throw new NonExistingMigrationFile('Backup file does not exist, or is corrupted.');
return $file_location;
}
/**
* On the hosted platform we cannot allow the
* import to start if there are users > plan number
@ -1122,6 +1147,10 @@ class CompanyImport implements ShouldQueue
foreach($this->backup_file->{$object_property} as $obj)
{
if(is_null($obj))
continue;
/* Remove unwanted keys*/
$obj_array = (array)$obj;
foreach($unset as $un){
@ -1248,6 +1277,12 @@ class CompanyImport implements ShouldQueue
if (! array_key_exists("{$old}", $this->ids[$resource])) {
// nlog($this->ids[$resource]);
nlog("searching for {$old} in {$resource}");
nlog("If we are missing a user - default to the company owner");
if($resource == 'users')
return $this->company_owner->id;
throw new \Exception("Missing {$resource} key: {$old}");
}

View File

@ -92,16 +92,6 @@ class StartMigration implements ShouldQueue
$archive = $zip->open(public_path("storage/{$this->filepath}"));
$filename = pathinfo($this->filepath, PATHINFO_FILENAME);
// if($this->company->id == $this->company->account->default_company_id)
// {
// $new_default_company = $this->company->account->companies->first();
// if ($new_default_company) {
// $this->company->account->default_company_id = $new_default_company->id;
// $this->company->account->save();
// }
// }
$update_product_flag = $this->company->update_products;
$this->company->update_products = false;
@ -129,9 +119,6 @@ class StartMigration implements ShouldQueue
Storage::deleteDirectory(public_path("storage/migrations/{$filename}"));
// $this->company->account->default_company_id = $this->company->id;
// $this->company->account->save();
$this->company->update_products = $update_product_flag;
$this->company->save();

View File

@ -20,6 +20,7 @@ use App\Jobs\Util\SubscriptionWebhookHandler;
use App\Jobs\Util\SystemLogger;
use App\Models\Client;
use App\Models\ClientContact;
use App\Models\Credit;
use App\Models\Invoice;
use App\Models\PaymentHash;
use App\Models\Product;
@ -212,6 +213,16 @@ class SubscriptionService
->orderBy('id', 'desc')
->first();
//sometimes the last document could be a credit if the user is paying for their service with credits.
if(!$outstanding_invoice){
$outstanding_invoice = Credit::where('subscription_id', $this->subscription->id)
->where('client_id', $recurring_invoice->client_id)
->where('is_deleted', 0)
->orderBy('id', 'desc')
->first();
}
if ($outstanding->count() == 0){
//nothing outstanding
return $target->price - $this->calculateProRataRefundForSubscription($outstanding_invoice);