|
|
|
|
@ -126,9 +126,25 @@ class NewTvRequest extends Model {
|
|
|
|
|
public static function approveFromRequest(Request $request) {
|
|
|
|
|
$request->validate([
|
|
|
|
|
'id' => 'required|integer|exists:App\Models\NewTvRequest',
|
|
|
|
|
'tv' => 'required|array',
|
|
|
|
|
'tv' => 'nullable|array',
|
|
|
|
|
'existingTv' => 'nullable|array',
|
|
|
|
|
'action' => 'required|string',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$newTvReq = NewTvRequest::addColumnCanApprove()
|
|
|
|
|
->findOrFail($request->id);
|
|
|
|
|
if(!$newTvReq->can_approve) throw new \Exception('Cannot approve current request');
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$tv = null;
|
|
|
|
|
if($request->action == 'existing') {
|
|
|
|
|
$existingTvRequest = new Request($request->existingTv);
|
|
|
|
|
$existingTvRequest->validate([
|
|
|
|
|
'id' => 'required|integer|exists:App\Models\Tv',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$tv = TV::findOrFail($existingTvRequest->id);
|
|
|
|
|
} else if ($request->action == 'new') {
|
|
|
|
|
$tvRequest = new Request($request->tv);
|
|
|
|
|
if($tvRequest->code) $tvRequest->merge(['code' => strtoupper($tvRequest->code)]);
|
|
|
|
|
|
|
|
|
|
@ -149,11 +165,6 @@ class NewTvRequest extends Model {
|
|
|
|
|
'notes' => 'nullable|string',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$newTvReq = NewTvRequest::addColumnCanApprove()
|
|
|
|
|
->findOrFail($request->id);
|
|
|
|
|
if(!$newTvReq->can_approve) throw new \Exception('Cannot approve current request');
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
DB::beginTransaction();
|
|
|
|
|
$tv = new Tv();
|
|
|
|
|
$tv->code = TV::generateUniqueCode();
|
|
|
|
|
@ -176,18 +187,22 @@ class NewTvRequest extends Model {
|
|
|
|
|
$tv->save();
|
|
|
|
|
TVLog::historyCreate($request->user(), $tv->id, $tv);
|
|
|
|
|
|
|
|
|
|
$newTvReq->approved_at = now();
|
|
|
|
|
$newTvReq->tv_fk = $tv->id;
|
|
|
|
|
$newTvReq->save();
|
|
|
|
|
|
|
|
|
|
DB::commit();
|
|
|
|
|
return JSONResponse::Success(['message'=>'Success to update tv data']);
|
|
|
|
|
|
|
|
|
|
// TODO: waiting execution until update from ops
|
|
|
|
|
// NEED TO REFACTOR (cause by add existing tv code)
|
|
|
|
|
// // try to sys_to_sys with indokargo
|
|
|
|
|
// DB::commit();
|
|
|
|
|
// $jsonResponse = Indokargo::createTVAddress($tvRequest, $tv->id);
|
|
|
|
|
// return $jsonResponse;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \Exception('INVALID FORM ACTION');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$newTvReq->approved_at = now();
|
|
|
|
|
$newTvReq->tv_fk = $tv->id;
|
|
|
|
|
$newTvReq->save();
|
|
|
|
|
|
|
|
|
|
DB::commit();
|
|
|
|
|
return JSONResponse::Success(['message'=>'Success to approve new tv request']);
|
|
|
|
|
} catch(\Throwable $th) {
|
|
|
|
|
DB::rollback();
|
|
|
|
|
throw $th;
|
|
|
|
|
|