diff --git a/app/Console/Commands/CreateTestData.php b/app/Console/Commands/CreateTestData.php index 61b6ab39ed..554a02f0e4 100644 --- a/app/Console/Commands/CreateTestData.php +++ b/app/Console/Commands/CreateTestData.php @@ -284,7 +284,25 @@ class CreateTestData extends Command $invoice->date = $faker->date(); $invoice->line_items = $this->buildLineItems(); - $invoice->uses_inclusive_Taxes = false; + $invoice->uses_inclusive_taxes = false; + + if(rand(0,1)) + { + $invoice->tax_name1 = 'GST'; + $invoice->tax_rate1 = 10.00; + } + + if(rand(0,1)) + { + $invoice->tax_name2 = 'VAT'; + $invoice->tax_rate2 = 17.50; + } + + if(rand(0,1)) + { + $invoice->tax_name3 = 'CA Sales Tax'; + $invoice->tax_rate3 = 5; + } $invoice->save(); @@ -330,6 +348,24 @@ class CreateTestData extends Command $item->quantity = 1; $item->cost =10; + if(rand(0, 1)) + { + $item->tax_name1 = 'GST'; + $item->tax_rate1 = 10.00; + } + + if(rand(0, 1)) + { + $item->tax_name1 = 'VAT'; + $item->tax_rate1 = 17.50; + } + + if(rand(0, 1)) + { + $item->tax_name1 = 'Sales Tax'; + $item->tax_rate1 = 5; + } + $line_items[] = $item; return $line_items; diff --git a/app/Helpers/Invoice/InvoiceSum.php b/app/Helpers/Invoice/InvoiceSum.php index 3941ac2272..828fa3a4bb 100644 --- a/app/Helpers/Invoice/InvoiceSum.php +++ b/app/Helpers/Invoice/InvoiceSum.php @@ -195,6 +195,8 @@ class InvoiceSum /* Set new calculated total */ $this->invoice->amount = $this->formatValue($this->getTotal(), $this->invoice->client->currency()->precision); + $this->invoice->total_taxes = $this->getTotalTaxes(); + return $this; } diff --git a/app/Helpers/Invoice/InvoiceSumInclusive.php b/app/Helpers/Invoice/InvoiceSumInclusive.php index 1f5f84c135..56c31c32d3 100644 --- a/app/Helpers/Invoice/InvoiceSumInclusive.php +++ b/app/Helpers/Invoice/InvoiceSumInclusive.php @@ -207,6 +207,8 @@ class InvoiceSumInclusive /* Set new calculated total */ $this->invoice->amount = $this->formatValue($this->getTotal(), $this->invoice->client->currency()->precision); + $this->invoice->total_taxes = $this->getTotalTaxes(); + return $this; } diff --git a/app/Http/Controllers/ActivityController.php b/app/Http/Controllers/ActivityController.php index 641a98564e..3d07bc4c5c 100644 --- a/app/Http/Controllers/ActivityController.php +++ b/app/Http/Controllers/ActivityController.php @@ -42,9 +42,10 @@ class ActivityController extends BaseController * @OA\Parameter(ref="#/components/parameters/index"), * @OA\Parameter( * name="rows", - * in="path", + * in="query", * description="The number of activities to return", * example="50", + * required=false, * @OA\Schema( * type="number", * format="integer", diff --git a/app/Http/Controllers/OpenAPI/PaymentSchema.php b/app/Http/Controllers/OpenAPI/PaymentSchema.php index ecb9a8be28..ff8e7b04be 100644 --- a/app/Http/Controllers/OpenAPI/PaymentSchema.php +++ b/app/Http/Controllers/OpenAPI/PaymentSchema.php @@ -4,6 +4,6 @@ * schema="Payment", * type="object", * @OA\Property(property="id", type="string", example="Opnel5aKBz", description="______"), - * @OA\Property(property="is_manual", type="bool", example=true, description="______"), + * @OA\Property(property="is_manual", type="boolean", example=true, description="______"), * ) */ \ No newline at end of file diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 6892ed724a..2e3236a5d7 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -408,7 +408,8 @@ class UserController extends BaseController * @OA\Parameter(ref="#/components/parameters/include"), * @OA\Parameter( * name="token_name", - * in="path", + * in="query", + * required=false, * description="Customized name for the Users API Token", * example="iOS Device 11 iPad", * @OA\Schema( @@ -546,7 +547,22 @@ class UserController extends BaseController * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), * @OA\Parameter(ref="#/components/parameters/include"), - * @OA\RequestBody(ref="#/components/schemas/CompanyUser"), + * @OA\Parameter( + * name="user", + * in="path", + * description="The user hashed_id", + * example="FD767dfd7", + * required=true, + * @OA\Schema( + * type="string", + * format="string", + * ), + * ), + * @OA\RequestBody( + * description="The company user object", + * required=true, + * @OA\JsonContent(ref="#/components/schemas/CompanyUser"), + * ), * @OA\Response( * response=200, * description="Returns the saved User object", @@ -595,6 +611,17 @@ class UserController extends BaseController * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), * @OA\Parameter(ref="#/components/parameters/include"), + * @OA\Parameter( + * name="user", + * in="path", + * description="The user hashed_id", + * example="FD767dfd7", + * required=true, + * @OA\Schema( + * type="string", + * format="string", + * ), + * ), * @OA\Response( * response=200, * description="Success response", diff --git a/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php b/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php index df19f208ff..e24b736ca8 100644 --- a/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php +++ b/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php @@ -59,7 +59,6 @@ class UpdateCompanyLedgerWithInvoice $adjustment = $balance + $this->adjustment; - $company_ledger = CompanyLedgerFactory::create($this->invoice->company_id, $this->invoice->user_id); $company_ledger->client_id = $this->invoice->client_id; $company_ledger->adjustment = $this->adjustment; diff --git a/app/Jobs/Invoice/ApplyInvoicePayment.php b/app/Jobs/Invoice/ApplyInvoicePayment.php index 86dcea30ad..ead0c383ba 100644 --- a/app/Jobs/Invoice/ApplyInvoicePayment.php +++ b/app/Jobs/Invoice/ApplyInvoicePayment.php @@ -85,13 +85,13 @@ class ApplyInvoicePayment implements ShouldQueue } elseif($this->invoice->partial > 0 && $this->invoice->partial > $this->amount) //partial amount exists, but the amount is less than the partial amount { - \Log::error("partial > amount"); + $this->invoice->partial -= $this->amount; $this->invoice->updateBalance($this->amount*-1); } elseif($this->invoice->partial > 0 && $this->invoice->partial < $this->amount) //partial exists and the amount paid is GREATER than the partial amount { - \Log::error("partial < amount"); + $this->invoice->clearPartial(); $this->invoice->setDueDate(); $this->invoice->setStatus(Invoice::STATUS_PARTIAL); @@ -101,7 +101,6 @@ class ApplyInvoicePayment implements ShouldQueue } elseif($this->invoice->amount == $this->invoice->balance) //total invoice paid. { - \Log::error("balance == amount"); $this->invoice->clearPartial(); $this->invoice->setDueDate(); diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 350bc6943a..9c491703b7 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -279,7 +279,7 @@ class Invoice extends BaseModel if($this->uses_inclusive_taxes) $invoice_calc = new InvoiceSumInclusive($this); else - $invoice_calc = new InvoiceSum($this, $this); + $invoice_calc = new InvoiceSum($this); return $invoice_calc->build(); diff --git a/app/Repositories/UserRepository.php b/app/Repositories/UserRepository.php index 52bc35336b..c77e14ca42 100644 --- a/app/Repositories/UserRepository.php +++ b/app/Repositories/UserRepository.php @@ -62,14 +62,18 @@ class UserRepository extends BaseRepository $cu = CompanyUser::whereUserId($user->id)->whereCompanyId($company->id)->first(); - + /*No company user exists - attach the user*/ if(!$cu){ - //$cu = CompanyUserFactory::create($user->id, $company->id, $account_id); - $data['company_user']['account_id'] = $account_id; - + + $data['company_user']['account_id'] = $account_id; $user->companies()->attach($company->id, $data['company_user']); } + else + { + $cu->fill($data['company_user']); + $cu->save(); + } } diff --git a/app/Transformers/InvoiceTransformer.php b/app/Transformers/InvoiceTransformer.php index e66ea9ff7f..52011b9cc2 100644 --- a/app/Transformers/InvoiceTransformer.php +++ b/app/Transformers/InvoiceTransformer.php @@ -109,6 +109,7 @@ class InvoiceTransformer extends EntityTransformer 'tax_rate2' => (float) $invoice->tax_rate2, 'tax_name3' => $invoice->tax_name3 ? $invoice->tax_name3 : '', 'tax_rate3' => (float) $invoice->tax_rate3, + 'total_taxes' => (float) $invoice->total_taxes, 'is_amount_discount' => (bool) ($invoice->is_amount_discount ?: false), 'footer' => $invoice->footer ?: '', 'partial' => (float) ($invoice->partial ?: 0.0), diff --git a/database/migrations/2014_10_13_000000_create_users_table.php b/database/migrations/2014_10_13_000000_create_users_table.php index 0beb235efb..db45e13a76 100644 --- a/database/migrations/2014_10_13_000000_create_users_table.php +++ b/database/migrations/2014_10_13_000000_create_users_table.php @@ -438,6 +438,8 @@ class CreateUsersTable extends Migration $t->string('tax_name3')->nullable(); $t->decimal('tax_rate3', 13, 3)->default(0); + $t->decimal('total_taxes', 13, 3)->default(0); + $t->boolean('uses_inclusive_taxes')->default(0); $t->string('custom_value1')->nullable(); diff --git a/tests/Feature/UserTest.php b/tests/Feature/UserTest.php index 3529d7afab..4ec8fb74ba 100644 --- a/tests/Feature/UserTest.php +++ b/tests/Feature/UserTest.php @@ -1,9 +1,10 @@ json(); + $this->assertNotNull($arr['data']['company_user']); } public function testUserAttachAndDetach() @@ -114,4 +116,110 @@ class UserTest extends TestCase $this->assertNotNull($user); } + + public function testAttachUserToMultipleCompanies() + { + + /* Create New Company */ + $company2 = factory(\App\Models\Company::class)->create([ + 'account_id' => $this->account->id, + 'domain' => 'ninja.test:8000', + ]); + + /* Create New Company Token*/ + $user_1_company_token = CompanyToken::create([ + 'user_id' => $this->user->id, + 'company_id' => $company2->id, + 'account_id' => $this->account->id, + 'name' => 'test token', + 'token' => \Illuminate\Support\Str::random(64), + ]); + + /*Manually link this user to the company*/ + $cu = CompanyUserFactory::create($this->user->id, $company2->id, $this->account->id); + $cu->is_owner = true; + $cu->is_admin = true; + $cu->save(); + + /*Create New Blank User and Attach to Company 2*/ + $new_user = UserFactory::create(); + $new_user->first_name = 'Test'; + $new_user->last_name = 'Palloni'; + $new_user->save(); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $user_1_company_token->token, + ])->post('/api/v1/users/'.$this->encodePrimaryKey($new_user->id).'/attach_to_company?include=company_user'); + + $response->assertStatus(200); + + $this->assertNotNull($new_user->company_user); + $this->assertEquals($new_user->company_user->company_id, $company2->id); + + + /*Create brand new user manually with company_user object and attach to a different company*/ + $data = [ + 'first_name' => 'hey', + 'last_name' => 'you', + 'email' => 'bob@good.ole.boys.co2.com', + 'company_user' => [ + 'is_admin' => false, + 'is_owner' => false, + 'permissions' => 'create_client,create_invoice' + ], + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $user_1_company_token->token, + ])->post('/api/v1/users?include=company_user', $data); + + $response->assertStatus(200); + + $arr = $response->json(); + + $this->assertNotNull($arr['data']['company_user']); + $this->assertFalse($arr['data']['company_user']['is_admin']); + $this->assertFalse($arr['data']['company_user']['is_owner']); + $this->assertEquals($arr['data']['company_user']['permissions'], 'create_client,create_invoice'); + + $user = User::whereEmail('bob@good.ole.boys.co2.com')->first(); + + $this->assertNotNull($user); + + $cu = CompanyUser::whereUserId($user->id)->whereCompanyId($company2->id)->first(); + + $this->assertNotNull($cu); + + + + /*Update the user permissions of this user*/ + $data = [ + 'first_name' => 'Captain', + 'last_name' => 'Morgain', + 'email' => 'bob@good.ole.boys.co2.com', + 'company_user' => [ + 'is_admin' => true, + 'is_owner' => false, + 'permissions' => 'create_invoice,create_invoice' + ], + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $user_1_company_token->token, + ])->put('/api/v1/users/'.$this->encodePrimaryKey($user->id).'?include=company_user', $data); + + $response->assertStatus(200); + + $arr = $response->json(); + + $this->assertNotNull($arr['data']['company_user']); + $this->assertTrue($arr['data']['company_user']['is_admin']); + $this->assertFalse($arr['data']['company_user']['is_owner']); + $this->assertEquals($arr['data']['company_user']['permissions'], 'create_invoice,create_invoice'); + + + } } \ No newline at end of file