Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/Exceptions/BranchNotRightException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Exceptions;

use Exception;

class BranchNotRightException extends Exception
{
public function __construct()
{
parent::__construct("Doesn't belong to the right branch", 422);
}
}
2 changes: 1 addition & 1 deletion app/Exceptions/BranchNullException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class BranchNullException extends Exception
{
public function __construct($action = 'save')
{
parent::__construct('please set default branch to '. $action .' this form', 422);
parent::__construct('please set as default branch', 422);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

namespace App\Http\Controllers\Api\Purchase\PurchaseReceive;

use App\Exceptions\BranchNotRightException;
use App\Exceptions\BranchNullException;
use App\Http\Controllers\Controller;
use App\Http\Requests\Purchase\PurchaseReceive\PurchaseReceive\StorePurchaseReceiveRequest;
use App\Http\Requests\Purchase\PurchaseReceive\PurchaseReceive\UpdatePurchaseReceiveRequest;
use App\Http\Resources\ApiCollection;
use App\Http\Resources\ApiResource;
use App\Model\Inventory\Inventory;
use App\Model\Master\Branch;
use App\Model\Purchase\PurchaseReceive\PurchaseReceive;
use Google\Service\CloudIAP\Brand;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Throwable;
Expand All @@ -23,7 +28,7 @@ class PurchaseReceiveController extends Controller
public function index(Request $request)
{
$purchaseReceives = PurchaseReceive::from(PurchaseReceive::getTableName().' as '.PurchaseReceive::$alias)->eloquentFilter($request);

$purchaseReceives = PurchaseReceive::joins($purchaseReceives, $request->get('join'));

$purchaseReceives = pagination($purchaseReceives, $request->get('limit'));
Expand Down Expand Up @@ -99,8 +104,26 @@ public function store(StorePurchaseReceiveRequest $request)
*/
public function show(Request $request, $id)
{

$purchaseReceive = PurchaseReceive::eloquentFilter($request)->findOrFail($id);

//Check branches
$branches = tenant(auth()->user()->id)->branches;
$userBranch = null;
foreach ($branches as $branch) {
if ($branch->pivot->is_default) {
$userBranch = $branch->id;
break;
}
}

if($userBranch == null) {
throw new BranchNullException();
}
else if ($purchaseReceive->form->branch_id != $userBranch) {
throw new BranchNotRightException();
}

return new ApiResource($purchaseReceive);
}

Expand All @@ -114,6 +137,21 @@ public function show(Request $request, $id)
public function edit(Request $request, $id)
{
$purchaseReceive = PurchaseReceive::eloquentFilter($request)->findOrFail($id)->load('items');
$branches = tenant(auth()->user()->id)->branches;
$userBranch = null;
foreach ($branches as $branch) {
if ($branch->pivot->is_default) {
$userBranch = $branch->id;
break;
}
}

if($userBranch == null) {
throw new BranchNullException();
}
else if ($purchaseReceive->form->branch_id != $userBranch) {
throw new BranchNotRightException();
}

$orderItems = optional($purchaseReceive->purchaseOrder)->items;

Expand Down Expand Up @@ -147,11 +185,27 @@ public function edit(Request $request, $id)
* @return ApiResource
* @throws Throwable
*/
public function update(Request $request, $id)
public function update(UpdatePurchaseReceiveRequest $request, $id)
{
$purchaseReceive = PurchaseReceive::findOrFail($id);
$purchaseReceive->isAllowedToUpdate();

$branches = tenant(auth()->user()->id)->branches;
$userBranch = null;
foreach ($branches as $branch) {
if ($branch->pivot->is_default) {
$userBranch = $branch->id;
break;
}
}

if($userBranch == null) {
throw new BranchNullException();
}
else if ($purchaseReceive->form->branch_id != $userBranch) {
throw new BranchNotRightException();
}

$result = DB::connection('tenant')->transaction(function () use ($request, $purchaseReceive) {
$purchaseReceive->form->archive();

Expand Down Expand Up @@ -186,6 +240,20 @@ public function destroy(Request $request, $id)

$purchaseReceive = PurchaseReceive::findOrFail($id);
$purchaseReceive->isAllowedToDelete();
$branches = tenant(auth()->user()->id)->branches;
$userBranch = null;
foreach ($branches as $branch) {
if ($branch->pivot->is_default) {
$userBranch = $branch->id;
break;
}
}
if($userBranch == null) {
throw new BranchNullException();
}
else if ($purchaseReceive->form->branch_id != $userBranch) {
throw new BranchNotRightException();
}
$purchaseReceive->requestCancel($request);

DB::connection('tenant')->commit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function rules()
'supplier_id' => ValidationRule::foreignKey('suppliers'),
'supplier_name' => 'required|string',
'warehouse_id' => ValidationRule::foreignKey('warehouses'),
'purchase_order_id' => ValidationRule::foreignKeyNullable('purchase_orders'),
'purchase_order_id' => ValidationRule::foreignKey('purchase_orders'),
'items' => 'required_without:services|array',
'services' => 'required_without:items|array',
];
Expand All @@ -55,4 +55,25 @@ public function rules()

return array_merge($ruleForm, $rulePurchaseReceive, $rulePurchaseReceiveItems, $rulePurchaseReceiveServices);
}

public function withValidator($validator)
{
$validator->after(function ($validator) {
$sum = 0;
$items = $this->get('items');
foreach($items as $item) {
if (isset($item['dna'])) {
foreach ($item['dna'] as $dna) {
$sum += $dna['quantity'];
}
} else {
$sum += $item['quantity'];
}
}

if ($sum == 0) {
$validator->errors()->add("total_quantity", 'quantity must be filled in');
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function rules()
'supplier_id' => ValidationRule::foreignKey('suppliers'),
'supplier_name' => 'required|string',
'warehouse_id' => ValidationRule::foreignKey('warehouses'),
'purchase_order_id' => ValidationRule::foreignKeyNullable('purchase_orders'),
'purchase_order_id' => ValidationRule::foreignKey('purchase_orders'),

'items' => 'required_without:services|array',
'services' => 'required_without:items|array',
Expand All @@ -56,4 +56,25 @@ public function rules()

return array_merge($ruleForm, $rulePurchaseReceive, $rulePurchaseReceiveItems, $rulePurchaseReceiveServices);
}

public function withValidator($validator)
{
$validator->after(function ($validator) {
$sum = 0;
$items = $this->get('items');
foreach($items as $item) {
if ($item['dna']) {
foreach ($item['dna'] as $dna) {
$sum += $dna['quantity'];
}
} else {
$sum += $item['quantity'];
}
}

if ($sum == 0) {
$validator->errors()->add("total_quantity", 'quantity must be filled in');
}
});
}
}
9 changes: 6 additions & 3 deletions resources/lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
'numeric' => 'The :attribute must be a number.',
'present' => 'The :attribute field must be present.',
'regex' => 'The :attribute format is invalid.',
'required' => 'The :attribute field is required.',
'required' => ':attribute field can\'t be null.',
'required_if' => 'The :attribute field is required when :other is :value.',
'required_unless' => 'The :attribute field is required unless :other is in :values.',
'required_with' => 'The :attribute field is required when :values is present.',
Expand Down Expand Up @@ -100,8 +100,11 @@
*/

'custom' => [
'attribute-name' => [
'rule-name' => 'custom-message',
'purchase_order_id' => [
'required' => "Purchase Order can't be null",
],
'warehouse_id' => [
'required' => "Warehouse can't be null",
],
],

Expand Down
Loading