diff --git a/app/Http/Controllers/V1/Customer/Expense/ExpensesController.php b/app/Http/Controllers/V1/Customer/Expense/ExpensesController.php index c33486bb9..837cf9324 100644 --- a/app/Http/Controllers/V1/Customer/Expense/ExpensesController.php +++ b/app/Http/Controllers/V1/Customer/Expense/ExpensesController.php @@ -46,7 +46,7 @@ public function index(Request $request) public function show(Company $company, $id) { $expense = $company->expenses() - ->whereCustomer(Auth::guard('customer')->id()) + ->whereUser(Auth::guard('customer')->id()) ->where('id', $id) ->first(); diff --git a/routes/web.php b/routes/web.php index 509071e48..2bee4b412 100644 --- a/routes/web.php +++ b/routes/web.php @@ -56,29 +56,37 @@ Auth::guard('customer')->logout(); }); -Route::middleware('pdf-auth')->group(function () { - Route::prefix('reports')->group(function () { - // sales report by customer - //---------------------------------- - Route::get('/sales/customers/{hash}', CustomerSalesReportController::class); +Route::middleware('auth:sanctum')->prefix('reports')->group(function () { + + // sales report by customer + //---------------------------------- + Route::get('/sales/customers/{hash}', CustomerSalesReportController::class); + + // sales report by items + //---------------------------------- + Route::get('/sales/items/{hash}', ItemSalesReportController::class); - // sales report by items - //---------------------------------- - Route::get('/sales/items/{hash}', ItemSalesReportController::class); + // report for expenses + //---------------------------------- + Route::get('/expenses/{hash}', ExpensesReportController::class); - // report for expenses - //---------------------------------- - Route::get('/expenses/{hash}', ExpensesReportController::class); + // report for tax summary + //---------------------------------- + Route::get('/tax-summary/{hash}', TaxSummaryReportController::class); - // report for tax summary - //---------------------------------- - Route::get('/tax-summary/{hash}', TaxSummaryReportController::class); + // report for profit and loss + //---------------------------------- + Route::get('/profit-loss/{hash}', ProfitLossReportController::class); - // report for profit and loss - //---------------------------------- - Route::get('/profit-loss/{hash}', ProfitLossReportController::class); - }); + + // download expense receipt + // ------------------------------------------------- + Route::get('/expenses/{expense}/download-receipt', DownloadReceiptController::class); + Route::get('/expenses/{expense}/receipt', ShowReceiptController::class); +}); + +Route::middleware('pdf-auth')->group(function () { // invoice pdf // ------------------------------------------------- @@ -91,14 +99,10 @@ // payment pdf // ------------------------------------------------- Route::get('/payments/pdf/{payment:unique_hash}', PaymentPdfController::class); - - // download expense receipt - // ------------------------------------------------- - Route::get('/expenses/{expense}/download-receipt', DownloadReceiptController::class); - Route::get('/expenses/{expense}/receipt', ShowReceiptController::class); }); + // customer pdf endpoints for invoice, estimate and Payment // ------------------------------------------------- diff --git a/tests/Feature/Admin/ConfigTest.php b/tests/Feature/Admin/ConfigTest.php index 872b367fd..6e1d3a81b 100644 --- a/tests/Feature/Admin/ConfigTest.php +++ b/tests/Feature/Admin/ConfigTest.php @@ -22,48 +22,48 @@ test('get all languages', function () { $key = 'languages'; - getJson('api/v1/config'.$key) + getJson('api/v1/config?key='.$key) ->assertOk(); }); test('get all fiscal years', function () { $key = 'fiscal_years'; - getJson('api/v1/config'.$key) + getJson('api/v1/config?key='.$key) ->assertOk(); }); test('get all convert estimate options', function () { $key = 'convert_estimate_options'; - getJson('api/v1/config'.$key) + getJson('api/v1/config?key='.$key) ->assertOk(); }); test('get all retrospective edits', function () { $key = 'retrospective_edits'; - getJson('api/v1/config'.$key) + getJson('api/v1/config?key='.$key) ->assertOk(); }); test('get all currency converter servers', function () { $key = 'currency_converter_servers'; - getJson('api/v1/config'.$key) + getJson('api/v1/config?key='.$key) ->assertOk(); }); test('get all exchange rate drivers', function () { $key = 'exchange_rate_drivers'; - getJson('api/v1/config'.$key) + getJson('api/v1/config?key='.$key) ->assertOk(); }); test('get all custom field models', function () { $key = 'custom_field_models'; - getJson('api/v1/config'.$key) + getJson('api/v1/config?key='.$key) ->assertOk(); }); diff --git a/tests/Feature/Admin/PaymentTest.php b/tests/Feature/Admin/PaymentTest.php index 5617c2f4d..e006a263e 100644 --- a/tests/Feature/Admin/PaymentTest.php +++ b/tests/Feature/Admin/PaymentTest.php @@ -4,7 +4,6 @@ use Crater\Http\Requests\PaymentRequest; use Crater\Mail\SendPaymentMail; use Crater\Models\Invoice; -use Crater\Models\InvoiceItem; use Crater\Models\Payment; use Crater\Models\User; use Illuminate\Support\Facades\Artisan; @@ -203,25 +202,22 @@ test('create payment with partially paid', function () { $invoice = Invoice::factory()->create([ - 'discount_type' => 'fixed', - 'discount_val' => 10, 'sub_total' => 100, - 'total' => 95, - 'tax' => 5, - 'due_amount' => 95, - 'exchange_rate' => 86.059663, - 'base_discount_val' => 860.59663, - 'base_sub_total' => 8605.9663, - 'base_total' => 8,175.667985, - 'base_tax' => 430.298315, - 'base_due_amount' => 8,175.667985, + 'total' => 100, + 'due_amount' => 100, + 'exchange_rate' => 1, + 'base_discount_val' => 100, + 'base_sub_total' => 100, + 'base_total' => 100, + 'base_tax' => 100, + 'base_due_amount' => 100, ]); $payment = Payment::factory()->raw([ 'invoice_id' => $invoice->id, 'customer_id' => $invoice->customer_id, 'exchange_rate' => $invoice->exchange_rate, - 'amount' => 90, + 'amount' => 100, 'currency_id' => $invoice->currency_id ]); @@ -229,9 +225,8 @@ $this->assertDatabaseHas('payments', [ 'payment_number' => $payment['payment_number'], - 'customer_id' => $payment['customer_id'], - 'amount' => $payment['amount'], - 'base_amount' => $payment['base_amount'], + 'customer_id' => (string)$payment['customer_id'], + 'amount' => (string)$payment['amount'], ]); $this->assertDatabaseHas('invoices', [ diff --git a/tests/Feature/Customer/EstimateTest.php b/tests/Feature/Customer/EstimateTest.php index def547f2b..02764e43d 100644 --- a/tests/Feature/Customer/EstimateTest.php +++ b/tests/Feature/Customer/EstimateTest.php @@ -32,9 +32,12 @@ test('get customer estimate', function () { $customer = Auth::guard('customer')->user(); - $estimate = Estimate::factory()->create(); + $estimate = Estimate::factory()->create([ + 'customer_id' => $customer->id + ]); - getJson("/api/{$customer->company->slug}/v1/customer/estimates/{$estimate->id}")->assertOk(); + getJson("/api/v1/{$customer->company->slug}/customer/estimates/{$estimate->id}") + ->assertOk(); }); test('customer estimate mark as accepted', function () { @@ -43,15 +46,34 @@ $estimate = Estimate::factory()->create([ 'estimate_date' => '1988-07-18', 'expiry_date' => '1988-08-18', + 'customer_id' => $customer->id ]); $status = [ - 'status' => Estimate::STATUS_ACCEPTED, + 'status' => Estimate::STATUS_ACCEPTED ]; - postJson("api/v1/{$customer->company->slug}/customer/estimate/{$estimate->id}/accept", $status)->assertOk(); + $response = postJson("api/v1/{$customer->company->slug}/customer/estimate/{$estimate->id}/status", $status) + ->assertOk(); + + $this->assertEquals($response->json()['data']['status'], Estimate::STATUS_ACCEPTED); +}); + +test('customer estimate mark as rejected', function () { + $customer = Auth::guard('customer')->user(); + + $estimate = Estimate::factory()->create([ + 'estimate_date' => '1988-07-18', + 'expiry_date' => '1988-08-18', + 'customer_id' => $customer->id + ]); + + $status = [ + 'status' => Estimate::STATUS_REJECTED + ]; - $estimate2 = Estimate::find($estimate->id); + $response = postJson("api/v1/{$customer->company->slug}/customer/estimate/{$estimate->id}/status", $status) + ->assertOk(); - $this->assertEquals($estimate2->status, Estimate::STATUS_ACCEPTED); + $this->assertEquals($response->json()['data']['status'], Estimate::STATUS_REJECTED); }); diff --git a/tests/Feature/Customer/ExpenseTest.php b/tests/Feature/Customer/ExpenseTest.php index aafd95679..4bcb21745 100644 --- a/tests/Feature/Customer/ExpenseTest.php +++ b/tests/Feature/Customer/ExpenseTest.php @@ -7,7 +7,6 @@ use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Auth; use Laravel\Sanctum\Sanctum; - use function Pest\Laravel\getJson; beforeEach(function () { @@ -32,14 +31,11 @@ test('get customer expense', function () { $customer = Auth::guard('customer')->user(); - $expense = Expense::factory()->create(); - - getJson("/api/v1/{$customer->company->slug}/customer/expenses/{$expense->id}")->assertOk(); - - $this->assertDatabaseHas('expenses', [ - 'expense_category_id' => $expense['expense_category_id'], - 'amount' => $expense['amount'], - 'exchange_rate' => $expense['exchange_rate'], - 'notes' => $expense['notes'], + $expense = Expense::factory()->create([ + 'customer_id' => $customer->id, + 'company_id' => $customer->company->id ]); + + getJson("/api/v1/{$customer->company->slug}/customer/expenses/{$expense->id}") + ->assertOk(); }); diff --git a/tests/Feature/Customer/InvoiceTest.php b/tests/Feature/Customer/InvoiceTest.php index 3f1c80eff..e0cfc8484 100644 --- a/tests/Feature/Customer/InvoiceTest.php +++ b/tests/Feature/Customer/InvoiceTest.php @@ -31,7 +31,9 @@ test('get customer invoice', function () { $customer = Auth::guard('customer')->user(); - $invoice = Invoice::factory()->create(); + $invoice = Invoice::factory()->create([ + 'customer_id' => $customer->id + ]); getJson("/api/v1/{$customer->company->slug}/customer/invoices/{$invoice->id}")->assertOk(); diff --git a/tests/Feature/Customer/PaymentTest.php b/tests/Feature/Customer/PaymentTest.php index 6e3c4a945..d671a0871 100644 --- a/tests/Feature/Customer/PaymentTest.php +++ b/tests/Feature/Customer/PaymentTest.php @@ -31,7 +31,9 @@ test('get customer payment', function () { $customer = Auth::guard('customer')->user(); - $payment = Payment::factory()->create(); + $payment = Payment::factory()->create([ + 'customer_id' => $customer->id + ]); getJson("/api/v1/{$customer->company->slug}/customer/payments/{$payment->id}")->assertOk(); }); diff --git a/tests/Unit/Request/CompanyTest.php b/tests/Unit/Request/CompanyTest.php index 3ddfe501f..c2e0e5ed3 100644 --- a/tests/Unit/Request/CompanyTest.php +++ b/tests/Unit/Request/CompanyTest.php @@ -12,6 +12,9 @@ 'required', Rule::unique('companies')->ignore($request->header('company'), 'id'), ], + 'slug' => [ + 'nullable' + ], 'address.country_id' => [ 'required', ], diff --git a/tests/Unit/Request/Customer/CustomerProfileTest.php b/tests/Unit/Request/Customer/CustomerProfileTest.php index d49a406c0..f3bc46e6e 100644 --- a/tests/Unit/Request/Customer/CustomerProfileTest.php +++ b/tests/Unit/Request/Customer/CustomerProfileTest.php @@ -18,7 +18,7 @@ 'email' => [ 'nullable', 'email', - Rule::unique('customers')->ignore(Auth::id(), 'id'), + Rule::unique('customers')->where('company_id', $request->header('company'))->ignore(Auth::id(), 'id') ], 'billing.name' => [ 'nullable', diff --git a/tests/Unit/Request/CustomerTest.php b/tests/Unit/Request/CustomerTest.php index c6d89fa79..440021477 100644 --- a/tests/Unit/Request/CustomerTest.php +++ b/tests/Unit/Request/CustomerTest.php @@ -35,7 +35,7 @@ 'nullable', ], 'enable_portal' => [ - 'nullable', + 'boolean', ], 'currency_id' => [ 'nullable', diff --git a/tests/Unit/Request/TaxTypeTest.php b/tests/Unit/Request/TaxTypeTest.php index 288b9fee2..97d304a75 100644 --- a/tests/Unit/Request/TaxTypeTest.php +++ b/tests/Unit/Request/TaxTypeTest.php @@ -1,6 +1,7 @@ [ 'required', Rule::unique('tax_types') + ->where('type', TaxType::TYPE_GENERAL) ->where('company_id', $request->header('company')) ], 'percent' => [