working on company gateway API endpoint

This commit is contained in:
David Bomba 2019-10-03 11:13:25 +10:00
parent 4e51256b51
commit b41966d41e
7 changed files with 297 additions and 7 deletions

View File

@ -0,0 +1,195 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Controllers;
use App\Models\CompanyGateway;
use App\Repositories\CompanyRepository;
use App\Utils\Traits\MakesHash;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
/**
* Class CompanyGatewayController
* @package App\Http\Controllers
*/
class CompanyGatewayController extends BaseController
{
use DispatchesJobs;
use MakesHash;
protected $entity_type = CompanyGateway::class;
protected $entity_transformer = CompanyGatewayTransformer::class;
protected $company_repo;
public $forced_includes = [];
/**
* CompanyGatewayController constructor.
*/
public function __construct(CompanyRepository $company_repo)
{
parent::__construct();
$this->company_repo = $company_repo;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$company_gateways = CompanyGateway::whereCompanyId(auth()->user()->company()->id);
return $this->listResponse($company_gateways);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create(CreateCompanyRequest $request)
{
$company = CompanyFactory::create(auth()->user()->company()->account->id);
return $this->itemResponse($company);
}
/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\SignupRequest $request
* @return \Illuminate\Http\Response
*/
public function store(StoreCompanyRequest $request)
{
$this->forced_includes = ['company_user'];
$company = CreateCompany::dispatchNow($request->all(), auth()->user()->company()->account);
if($request->file('logo'))
{
\Log::error('logo exists');
$path = UploadAvatar::dispatchNow($request->file('logo'), $company->company_key);
if($path){
$company->logo = $path;
$company->save();
}
}
auth()->user()->companies()->attach($company->id, [
'account_id' => $company->account->id,
'is_owner' => 1,
'is_admin' => 1,
'is_locked' => 0,
'permissions' => json_encode([]),
'settings' => json_encode(DefaultSettings::userSettings()),
]);
/*
* Required dependencies
*/
auth()->user()->setCompany($company);
/*
* Create token
*/
$company_token = CreateCompanyToken::dispatchNow($company, auth()->user());
//todo Need to discuss this with Hillel which is the best representation to return
//when a company is created. Do we send the entire account? Do we only send back the created CompanyUser?
$this->entity_transformer = CompanyUserTransformer::class;
$this->entity_type = CompanyUser::class;
//return $this->itemResponse($company);
$ct = CompanyUser::whereUserId(auth()->user()->id);
return $this->listResponse($ct);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show(ShowCompanyRequest $request, Company $company)
{
return $this->itemResponse($company);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(EditCompanyRequest $request, Company $company)
{
return $this->itemResponse($company);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(UpdateCompanyRequest $request, Company $company)
{
$company = $this->company_repo->save($request->all(), $company);
if($request->file('logo'))
{
\Log::error('logo exists');
$path = UploadAvatar::dispatchNow($request->file('logo'), $company->company_key);
if($path){
$company->logo = $path;
$company->save();
}
}
return $this->itemResponse($company);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(DestroyCompanyRequest $request, Company $company)
{
$company->delete();
return response()->json([], 200);
}
}

View File

@ -63,7 +63,7 @@ class UpdateInvoicePayment implements ShouldQueue
foreach($invoice as $invoice)
{
if($invoice->isPartial())
if($invoice->hasPartial())
$total += $invoice->partial;
else
$total += $invoice->balance;
@ -77,7 +77,7 @@ class UpdateInvoicePayment implements ShouldQueue
$invoices->each(function ($invoice) use($payment){
if($invoice->isPartial()) {
if($invoice->hasPartial()) {
UpdateCompanyLedgerWithPayment::dispatchNow($payment, ($invoice->partial*-1));
$invoice->updateBalance($invoice->partial*-1);

View File

@ -22,8 +22,26 @@ class CompanyGateway extends BaseModel
{
protected $fillable = [
'gateway_key',
'accepted_credit_cards',
'require_cvv',
'show_address',
'show_shipping_address',
'update_details',
'config',
'priority_id',
'min_limit',
'max_limit',
'fee_amount',
'fee_percent',
'fee_tax_name1',
'fee_tax_name2',
'fee_tax_rate1',
'fee_tax_rate2',
'fee_cap',
'adjust_fee_percent',
];
public static $credit_cards = [
1 => ['card' => 'images/credit_cards/Test-Visa-Icon.png', 'text' => 'Visa'],
2 => ['card' => 'images/credit_cards/Test-MasterCard-Icon.png', 'text' => 'Master Card'],

View File

@ -282,7 +282,7 @@ class Invoice extends BaseModel
$status_id = self::STATUS_PAID;
} elseif ($paid && $this->balance > 0 && $this->balance < $this->amount) {
$status_id = self::STATUS_PARTIAL;
} elseif ($this->isPartial() && $this->balance > 0) {
} elseif ($this->hasPartial() && $this->balance > 0) {
$status_id = ($this->balance == $this->amount ? self::STATUS_SENT : self::STATUS_PARTIAL);
}
@ -297,10 +297,17 @@ class Invoice extends BaseModel
/**
* @return bool
*/
public function isPartial() : bool
public function hasPartial() : bool
{
return ($this->partial && $this->partial > 0) === true;
//return $this->status_id >= self::STATUS_PARTIAL;
}
/**
* @return bool
*/
public function isPartial() : bool
{
return $this->status_id >= self::STATUS_PARTIAL;
}
/**

View File

@ -58,7 +58,7 @@ class AccountTransformer extends EntityTransformer
{
return [
'id' => $this->encodePrimaryKey($account->id),
'id' => (string)$this->encodePrimaryKey($account->id),
'default_url' => config('ninja.site_url'),
'plan' => $account->getPlan(),
];

View File

@ -0,0 +1,69 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Transformers;
use App\Models\CompanyGateway;
use App\Utils\Traits\MakesHash;
/**
* Class CompanyGatewayTransformer.
*/
class CompanyGatewayTransformer extends EntityTransformer
{
use MakesHash;
/**
* @var array
*/
protected $defaultIncludes = [
];
/**
* @var array
*/
protected $availableIncludes = [
];
/**
* @param CompanyGateway $company_gateway
*
* @return array
*/
public function transform(CompanyGateway $company_gateway)
{
return [
'id' => (string)$this->encodePrimaryKey($company_gateway->id),
'gateway_key' => (string)$company_gateway->gateway_key ?: '',
'accepted_credit_cards' => (int)$company_gateway->accepted_credit_cards,
'require_cvv' => (bool)$company_gateway->require_cvv,
'show_address' => (bool)$company_gateway->show_address,
'show_shipping_address' => (bool)$company_gateway->show_shipping_address,
'update_details' => (bool)$company_gateway->update_details,
'config' => (string)$company_gateway->config ?: '',
'priority_id' => (int)$company_gateway->priority_id,
'min_limit' => (float)$company_gateway->min_limit,
'max_limit' => (float)$company_gateway->max_limit,
'fee_amount' => (float) $company_gateway->fee_amount,
'fee_percent' => (float)$company_gateway->fee_percent ?: '',
'fee_tax_name1' => (string)$company_gateway->fee_tax_name1 ?: '',
'fee_tax_name2' => (string) $company_gateway->fee_tax_name2 ?: '',
'fee_tax_rate1' => (float) $company_gateway->fee_tax_rate1,
'fee_tax_rate2' => (float)$company_gateway->fee_tax_rate2,
'fee_cap' => (float)$company_gateway->fee_cap,
'adjust_fee_percent' => (bool)$company_gateway->adjust_fee_percent,
'updated_at' => $company_gateway->updated_at,
'deleted_at' => $company_gateway->deleted_at,
];
}
}

View File

@ -56,6 +56,7 @@ class UploadLogoTest extends TestCase
])->put('/api/v1/companies/'.$this->encodePrimaryKey($this->company->id), $data);
$response->assertStatus(200);
$acc = $response->json();
$logo = $acc['data']['logo'];