<?php
namespace ApplicationBundle\Controller;
use ApplicationBundle\Constants\AccountsConstant;
use ApplicationBundle\Constants\GeneralConstant;
use ApplicationBundle\Constants\UserConstants;
use ApplicationBundle\Entity\AccCheck;
use ApplicationBundle\Entity\AccCostCentre;
use ApplicationBundle\Entity\AccSettings;
use ApplicationBundle\Entity\CheckFormat;
use ApplicationBundle\Entity\EncryptedSignature;
use ApplicationBundle\Helper\Generic;
use ApplicationBundle\Interfaces\SessionCheckInterface;
use ApplicationBundle\Modules\Accounts\Accounts;
use ApplicationBundle\Modules\Inventory\Inventory;
use ApplicationBundle\Modules\Purchase\Purchase;
use ApplicationBundle\Modules\System\DocValidation;
use ApplicationBundle\Modules\User\Company;
use ApplicationBundle\Modules\System\System;
use ApplicationBundle\Modules\System\MiscActions;
use ApplicationBundle\Modules\User\Users;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use ApplicationBundle\Entity\AccTransactions;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Validator\Constraints\DateTime;
class ApprovalController extends GenericController implements SessionCheckInterface
{
public function ApproveDocumentAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
if ($request->isMethod('POST')) {
if(isset(GeneralConstant::$Entity_list_details[ $request->request->get('approvalEntity')]['entity_view_route_path_name']))
$url = $this->generateUrl(
GeneralConstant::$Entity_list_details[ $request->request->get('approvalEntity')]['entity_view_route_path_name']
);
else
{
$url='';
}
$options=array(
'notification_enabled'=>$this->container->getParameter('notification_enabled'),
'notification_server'=>$this->container->getParameter('notification_server'),
// 'appId'=>$request->getSession()->get(UserConstants::USER_APP_ID),
// 'url'=>$this->generateUrl(
// GeneralConstant::$Entity_list_details[$request->request->get('approvalEntity')]['entity_view_route_path_name']
// )
);
if($request->request->has('approvalAction')) {
$message = System::approveDocument(
$em,
$options,
$request->request->get('approvalEntity'),
$request->request->get('approvalEntityId'),
$request->request->get('approvalId'),
$this->getLoggedUserLoginId($request),
$request->request->get('approvalAction'),
$request->request->get('approvalNote'),
$request->request->get('approvalHash'),
$url,
$request->request->get('forwardTo'),
$this->get('mail_module')
);
// $url = $this->generateUrl(
// GeneralConstant::$Entity_list_details[$request->request->get('approvalEntity')]['entity_view_route_path_name']
// );
// if(!empty($currentApprovalData['UserIdList']))
// System::AddNewNotification(
// $this->container->getParameter('notification_enabled'),
// $this->container->getParameter('notification_server'),$request->getSession()->get(UserConstants::USER_APP_ID),
// $request->getSession()->get(UserConstants::USER_COMPANY_ID),
// "Approval: ".GeneralConstant::$Entity_list_details[$request->request->get('approvalEntity')]['entity_alias'].": " . $message['docHash'] . " is pending Your approval",
// 'user',
// $message['UserIdList'],
// 'information',
//// GeneralConstant::$Entity_list_details[$entity_id]."/".$projectId,
// $url . "/" . $request->request->get('approvalEntityId'),
// "Approval Pending"
//
// );
// $this->addFlash(
// $message[0],
// $message[1]
// );
if ($message['success'] == true)
return new JsonResponse(array("success" => true,
'message' => $message,
'approvalEntity' => $request->request->get('approvalEntity'),
'approvalEntityId' => $request->request->get('approvalEntityId'),
'approvalId' => $request->request->get('approvalId'),
'approvalAction' => $request->request->get('approvalAction'),
)
);
else
return new JsonResponse(array("success" => false, 'message' => $message,
'approvalEntity' => $request->request->get('approvalEntity'),
'approvalEntityId' => $request->request->get('approvalEntityId'),
'approvalId' => $request->request->get('approvalId'),
'approvalAction' => $request->request->get('approvalAction'),
)
);
}
return new JsonResponse(array("success"=>false));
}
// $userList=$this->get('user_module')->showUserList();
return new JsonResponse(array("success"=>false));
}
public function getPendingApprovalListForUserAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$absoluteUrlList=[];
foreach(GeneralConstant::$Entity_list_details as $e=>$d)
{
if(isset($d['entity_view_route_path_name']))
$absoluteUrlList[$e]=$this->generateUrl($d['entity_view_route_path_name'], [], UrlGenerator::ABSOLUTE_URL);
}
return new JsonResponse(System::getPendingApprovalListByUserLoginId($em, $this->getLoggedUserLoginId($request),1,$absoluteUrlList));
}
public function UpdateSignatureAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$g_path='';
$user_data=Users::getUserInfoByLoginId($em,$this->getLoggedUserLoginId($request));
if ($request->isMethod('POST')) {
$post = $request->request;
$path="";
$extension_here='';
foreach($request->files as $uploadedFile) {
if($uploadedFile!=null) {
$extension_here=$uploadedFile->guessExtension();
$fileName = md5(uniqid()) . '.' . $uploadedFile->guessExtension();
$path = $fileName;
$upl_dir = $this->container->getParameter('kernel.root_dir') . '/../web/uploads/FileUploads/';
if (!file_exists($upl_dir)) {
mkdir($upl_dir, 0777, true);
}
$file = $uploadedFile->move($upl_dir, $path);
}
}
if($path!="")
$file_path='uploads/FileUploads/'.$path;
$g_path = $this->container->getParameter('kernel.root_dir') . '/../web/uploads/FileUploads/'. $path;
$img_file = file_get_contents($g_path);
//new one
list($width, $height) = getimagesize($g_path);
$percent=(200/$width);
$newwidth = $width * $percent;
$newheight = $height * $percent;
$thumb = imagecreatetruecolor($newwidth, $newheight);
if($extension_here=='jpg'||$extension_here=='jpeg')
$source = imagecreatefromjpeg($g_path);
if($extension_here=='png')
$source = imagecreatefrompng($g_path);
// Resize
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// ob_start();
imagepng($thumb,$this->container->getParameter('kernel.root_dir') . '/../web/uploads/FileUploads/th.png');
$th_file=file_get_contents($this->container->getParameter('kernel.root_dir') . '/../web/uploads/FileUploads/th.png');
//new one ends
// $image_data=base64_encode($img_file);
$image_data=base64_encode($th_file);
$encoded_data=System::encryptSignature($image_data,$request->request->get('approvalHash'));
if($encoded_data!=false)
{
$query_here=$this->getDoctrine()
->getRepository('ApplicationBundle:EncryptedSignature')
->findOneBy(
array(
'userId'=>$request->request->get('userId')
)
);
if($query_here)
{
$new=$query_here;
// $new->setRefreshFlag(1);
$new->setSigExists(0);
$new->setLastDecryptedSigId(0);
$new->setData($encoded_data);
}
else
{
$new=new EncryptedSignature();
$new->setData($encoded_data);
$new->setUserId($request->request->get('userId'));
// $new->setRefreshFlag(1);
$new->setSigExists(0);
$new->setLastDecryptedSigId(0);
$em->persist($new);
}
$em->flush();
}
// now deleting the file
if (!unlink($g_path))
{
// echo ("Error deleting $file");
}
else
{
// echo ("Deleted $file");
}
if (!unlink($this->container->getParameter('kernel.root_dir') . '/../web/uploads/FileUploads/th.png'))
{
// echo ("Error deleting $file");
}
else
{
// echo ("Deleted $file");
}
}
// $userList=$this->get('user_module')->showUserList();
return $this->render('ApplicationBundle:pages/system/settings:update_signature.html.twig',
array(
'page_title'=>'Update Signature',
'user_data'=>$user_data,
'path'=>$g_path
// 'incomeLedgerHeads'=>Accounts::getChildLedgerHeads($this->getDoctrine()->getManager(),AccountsConstant::INCOME)
)
);
}
public function CheckSignatureHashAction(Request $request)
{
$details_ids=[];
$em=$this->getDoctrine()->getManager();
if($request->isMethod('POST')) {
$em=$this->getDoctrine()->getManager();
$loginId = $request->getSession()->get(UserConstants::USER_LOGIN_ID);
$approveHash=$request->request->get('approvalHash');
//
$retData=DocValidation::isSignatureOk($em,$loginId,$approveHash,1);
// $this->addFlash(
// 'success',
// 'New Transaction Added.'
// );
return new JsonResponse(array(
"success"=>$retData['success'],
"data"=>$retData['data'],
));
}
return $this->render('ApplicationBundle:pages/accounts/input_forms:payment_voucher.html.twig',
array(
'test'=>$details_ids,
)
);
}
public function PendingApprovalListAction(Request $request)
{
$em=$this->getDoctrine()->getManager();
$login_id=$this->getLoggedUserLoginId($request);
$pending_data=System::getPendingApprovalListByUserLoginId($em,$login_id);
// $user_data=Users::getUserInfoByLoginId($em,$login_id);
//1st get only currently pendings
if($request->request->has('getSummaryData'))
$pending_data=MiscActions::getSummaryData($em,$pending_data);
$pending_approval_list=$pending_data['pending_approval_list'];
$override_approval_list=$pending_data['override_approval_list'];
if ($request->request->has('returnJson')) {
return new JsonResponse(
array(
'success' => true,
'page_title'=>'View',
'override_approval_list'=>$override_approval_list,
'pending_approval_list'=>$pending_approval_list
// 'productByCodeData' => $productByCodeData,
// 'productData' => $productData,
// 'currInvList' => $currInvList,
// 'productList' => Inventory::ProductList($em, $companyId),
// 'subCategoryList' => Inventory::ProductSubCategoryList($em, $companyId),
// 'categoryList' => Inventory::ProductCategoryList($em, $companyId),
// 'igList' => Inventory::ItemGroupList($em, $companyId),
// 'unitList' => Inventory::UnitTypeList($em),
// 'brandList' => Inventory::GetBrandList($em, $companyId),
// 'warehouse_action_list' => Inventory::warehouse_action_list($em,$this->getLoggedUserCompanyId($request),'object'),
// 'warehouseList' => Inventory::WarehouseList($em),
)
);
}
return $this->render('ApplicationBundle:pages/system/settings:my_pending_list.html.twig',
array(
'page_title'=>'View',
'override_approval_list'=>$override_approval_list,
'pending_approval_list'=>$pending_approval_list
)
);
}
}