Last active
July 8, 2019 05:37
-
-
Save duwaljyoti/2db22fe6bb42e5a42aa18ad4b05bdc46 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Modules\Core\Http\Controllers; | |
use App\Models\Item; | |
use App\Models\Permission; | |
use App\Models\projDesign; | |
use App\Models\Rma; | |
use App\Models\Task; | |
use App\Modules\Core\Http\Requests\CommentRequest; | |
use App\Modules\Core\Services\CommentService; | |
use App\Modules\Core\Traits\EmailTrait; | |
use App\Modules\CustomerRelations\Models\MasterShipment; | |
use App\Modules\CustomerRelations\Models\NqCredit; | |
use App\Modules\CustomerRelations\Models\Project; | |
use App\Modules\CustomerRelations\Models\PurchaseOrder; | |
use App\Modules\CustomerRelations\Models\QcAlert; | |
use App\Modules\CustomerRelations\Models\Shipment; | |
use App\Modules\Users\Services\UserService; | |
use Carbon\Carbon; | |
use Illuminate\Http\JsonResponse; | |
use Illuminate\Support\Str; | |
use App\Modules\Core\Models\Comment; | |
class SaveCommentActionController extends AuthController | |
{ | |
use EmailTrait; | |
private $commentableId; | |
private $commentedBy; | |
private $commentableType; | |
private $comment; | |
private $commentService; | |
private $currentDateTime; | |
private $displayId = null; | |
private $emailFf = false; | |
private $emailRecipients = []; | |
private $emailVendor = false; | |
private $filledInt; | |
private $msg = ''; | |
private $portalVendor; | |
private $pref = ''; | |
private $subject = ''; | |
private $typ = ''; | |
private $userInfo; | |
private $userService; | |
private $sanitizedComment = ''; | |
public function __construct( | |
UserService $userService, | |
CommentService $commentService | |
) { | |
$this->userService = $userService; | |
$this->commentService = $commentService; | |
$this->currentDateTime = Carbon::now(); | |
} | |
private function formatPayload(CommentRequest $commentRequest, string $commentableType, int $commentableId): void | |
{ | |
$this->commentedBy = $commentRequest->get('commented_by'); | |
$this->userInfo = $this->userService->find($this->commentedBy); | |
$this->commentableId = $commentableId; | |
$this->commentableType = $commentableType; | |
$this->sanitizedComment = str_replace("\r", "", str_replace("\n", "<br/>", $commentRequest->get('comment'))); | |
$this->filledInt = sprintf("%04d", $this->commentableId); | |
} | |
/** | |
* @SWG\Post( | |
* path="/api/core/{commentable_type}/{commentable_id}/projects", | |
* summary="Create a comment.", | |
* tags={"CRM"}, | |
* @SWG\Parameter( | |
* name="comment", | |
* type="string", | |
* required=true, | |
* in="path", | |
* ), | |
* @SWG\Parameter( | |
* name="commentable_id", | |
* type="number", | |
* required=true, | |
* in="path", | |
* ), | |
* @SWG\Parameter( | |
* name="commentable_type", | |
* type="string", | |
* required=true, | |
* in="path", | |
* ), | |
* @SWG\Parameter( | |
* name="commented_by", | |
* type="number", | |
* required=true, | |
* in="path", | |
* ), | |
* @SWG\Response( | |
* response=200, | |
* description="Create a customer." | |
* ) | |
* ) | |
*/ | |
/** | |
* @param string $commentableType | |
* @param int $commentableId | |
* @param CommentRequest $commentRequest | |
* @param Str $str | |
* @return JsonResponse | |
*/ | |
public function store( | |
string $commentableType, | |
int $commentableId, | |
CommentRequest $commentRequest, | |
Str $str | |
): JsonResponse { | |
$this->formatPayload($commentRequest, $commentableType, $commentableId); | |
$dynamicMetaDataSetter = $str->camel($commentableType); | |
$comment = $this->commentService->save(new Comment(), [ | |
'commentable_id' => $this->commentableId, | |
'commentable_type' => $this->commentableType, | |
'commented_by' => $this->commentedBy, | |
'comment' => $this->sanitizedComment | |
]); | |
// This dynamically determines how the other related details for email and log should be set. | |
$this->$dynamicMetaDataSetter(); | |
$this->email_and_notify( | |
$this->emailRecipients, | |
$this->commentedBy, | |
$this->commentableId, | |
$this->displayId, | |
$this->portalVendor, | |
$this->subject, | |
$this->msg, | |
$this->pref, | |
$this->typ, | |
$this->emailVendor, | |
$this->emailFf | |
); | |
// Add to log | |
$this->proj_log( | |
$this->commentableId, | |
$this->commentableType, | |
$this->commentedBy, | |
$this->userInfo->full_name . " has made a new comment: " . $this->sanitizedComment | |
); | |
return new JsonResponse( | |
$comment->newQuery()->with( | |
'user' | |
)->find($comment->id) | |
); | |
} | |
private function getConcernedUserId($code): int | |
{ | |
return $this->userService->getUsersByPermissionCode( | |
$code, | |
['id'] | |
)->first()->id; | |
} | |
private function internalQuote(): void | |
{ | |
$quoteInfo = Item::find($this->commentableId); | |
$quoteInfo->update(['last_updated_am' => $this->currentDateTime]); | |
array_push($this->emailRecipients, $quoteInfo->account_manager_id); | |
if ($quoteInfo->type === 'Manufacturing') { | |
$designAdminId = $this->getConcernedUserId(Permission::DESIGN_ADMIN); | |
$engineeringAdminId = $this->getConcernedUserId(Permission::ENGINEERING_ADMIN); | |
array_push($this->emailRecipients, $designAdminId, $engineeringAdminId); | |
} else { | |
$asiaAdminId = $this->getConcernedUserId(Permission::ASIA_ADMIN_CODE); | |
array_push($this->emailRecipients, $quoteInfo->assignee_id, $quoteInfo->asia_assignee_id, $asiaAdminId); | |
} | |
$this->emailFf = false; | |
$this->emailVendor = false; | |
$this->msg = $this->userInfo->full_name . ' posted the following comment:<br />' . $this->sanitizedComment; | |
$this->pref = 'proj_quote_internal_comments'; | |
$this->subject = 'Quote ' . $this->filledInt . ' has a new comment'; | |
$this->type = 'Quote'; | |
} | |
private function vendorQuote(): void | |
{ | |
$quote = Item::find($this->commentableId); | |
if ($this->commentedBy === $quote->account_manager_id) { | |
$quote->last_updated_am = $this->currentDateTime; | |
$quote->save(); | |
} | |
array_push($this->emailRecipients, $quote->account_manager_id); | |
if ($quote->portal_vendor === "OTHER") { | |
$asiaAdminId = $this->getConcernedUserId('asia-admin'); | |
array_push($this->emailRecipients, $quote->assignee_id, $quote->asia_assignee_id, $asiaAdminId); | |
} | |
$this->displayId = $this->filledInt; | |
$this->emailFf = false; | |
$this->emailVendor = true; | |
$this->msg = $this->userInfo->full_name . ' posted the following comment: <br />' . $this->sanitizedComment; | |
$this->portalVendor = $quote->portal_vendor; | |
$this->pref = 'proj_quote_comments'; | |
$this->typ = 'Quote'; | |
$this->subject = 'Quote ' . $this->filledInt. ' has a new comment'; | |
} | |
private function internalOrder(): void | |
{ | |
$order = PurchaseOrder::where('po', '=', $this->commentableId)->first(); | |
if ($this->commentedBy === $order->account_manager_id) { | |
PurchaseOrder::where('po', $this->commentedBy)->update(['last_updated_am' => $this->currentDateTime]); | |
} | |
$asiaAdminId = $this->getConcernedUserId('asia-admin'); | |
array_push( | |
$this->emailRecipients, | |
$order->account_manager_id, | |
$order->assignee_id, | |
$order->asia_assignee_id, | |
$asiaAdminId | |
); | |
$this->displayId = $this->commentableId; | |
$this->emailFf = false; | |
$this->emailVendor = true; | |
$this->msg = $this->userInfo->full_name . ' posted the following comment: <br /><br />' . $this->sanitizedComment; | |
$this->pref = 'proj_order_internal_comments'; | |
$this->typ = 'Order'; | |
$this->subject = 'PO' . $this->commentableId . ' has a new comment.'; | |
} | |
private function vendorOrder(): void | |
{ | |
$order = PurchaseOrder::where('po', '=', $this->commentableId)->first(); | |
if ($this->commentedBy == $order->account_manager_id) { | |
PurchaseOrder::where('po', $this->commentableId)->update(['last_updated_am' => $this->currentDateTime]); | |
} | |
array_push($this->emailRecipients, $order->account_manager_id); | |
if ($order->portal_vendor === "OTHER") { | |
$asiaAdminId = $this->getConcernedUserId('asia-admin'); | |
array_push($this->emailRecipients, $order->assignee_id, $order->asia_assignee_id, $asiaAdminId); | |
} | |
$this->displayId = $this->commentableId; | |
$this->emailFf = false; | |
$this->emailVendor = true; | |
$this->msg = $this->userInfo->full_name . ' posted the following comment: <br /><br />' . $this->sanitizedComment; | |
$this->pref = 'proj_order_comments'; | |
$this->typ = 'Order'; | |
$this->subject = 'PO' . $this->commentableId . ' has a new comment.'; | |
$this->portalVendor = $order->portal_vendor; | |
} | |
private function shipment(): void | |
{ | |
$shipment = Shipment::find($this->commentableId); | |
array_push($this->emailRecipients, $shipment->account_manager_id); | |
$this->displayId = $shipment->shipment_id; | |
$this->subject = 'Shipment ' . $this->displayId . ' has a new comment'; | |
$this->msg = $this->userInfo->full_name . ' posted the following comment: <br /><br />' . $this->sanitizedComment; | |
$this->pref = 'proj_shipment_comments'; | |
$this->typ = 'Shipment'; | |
$this->emailVendor = false; | |
$this->emailFf = false; | |
} | |
private function masterShipment(): void | |
{ | |
$logisticsUserId = $this->getConcernedUserId('logistics'); | |
array_push($this->emailRecipients, $logisticsUserId); | |
$ms = MasterShipment::find($this->commentableId); | |
$this->portalVendor = $ms->ff; | |
$this->displayId = $this->filledInt; | |
$this->subject = 'Master Shipment ' . $this->filledInt . ' has a new comment'; | |
$this->msg = $this->userInfo->full_name . ' posted the following comment: <br /><br />' . $this->sanitizedComment; | |
$this->pref = 'proj_master_shipment'; | |
$this->typ = 'Master Shipment'; | |
$this->emailVendor = false; | |
$this->emailFf = true; | |
} | |
private function design(): void | |
{ | |
$design = projDesign::find($this->commentableId); | |
array_push($this->emailRecipients, $design->account_manager_id); | |
$this->displayId = $this->filledInt; | |
$this->subject = 'Design ' . $this->displayId . ' has a new comment'; | |
$this->msg = $this->userInfo->full_name . ' posted the following comment: <br /><br />' . $this->sanitizedComment; | |
$this->pref = 'proj_designs'; | |
$this->typ = 'Design'; | |
$this->emailVendor = false; | |
$this->emailFf = false; | |
} | |
private function task(): void | |
{ | |
$task = Task::find($this->commentableId); | |
$task->update(['waiting_for_response' => (int) ($this->commentedBy === $task->assignee)]); | |
array_push($this->emailRecipients, $task->creator, $task->assignee); | |
// Define Main Email Info | |
$this->displayId = $task->name; | |
$this->subject = 'Task "' . $this->displayId . '" has a new comment'; | |
$this->msg = $this->userInfo->full_name . ' posted the following comment: <br /><br />' . $this->sanitizedComment; | |
$this->pref = 'tasks'; | |
$this->typ = 'Task'; | |
$this->emailVendor= false; | |
$this->emailFf = false; | |
} | |
private function project(): void | |
{ | |
$project = Project::find($this->commentableId); | |
array_push($this->emailRecipients, $project->account_manager_id); | |
$this->displayId = $this->filledInt; | |
$this->subject = 'Project ' . $this->displayId . ' has a new comment'; | |
$this->msg = $this->userInfo->full_name . ' posted the following comment: <br /><br />' . $this->sanitizedComment; | |
$this->pref = 'project'; | |
$this->typ = 'Project'; | |
$this->emailVendor = false; | |
$this->emailFf = false; | |
} | |
private function qcAlert(): void | |
{ | |
$qcAlert = QcAlert::find($this->commentableId); | |
array_push($this->emailRecipients, $qcAlert->account_manager_id, $qcAlert->assignee_id); | |
$this->displayId = $this->filledInt; | |
$this->subject = 'QC Alert #' . $this->displayId . ' has a new comment'; | |
$this->msg = $this->userInfo->full_name . ' posted the following comment: <br /><br />' . $this->sanitizedComment; | |
$this->pref = 'qc-alerts'; | |
$this->typ = 'QC Alert'; | |
$this->emailVendor = false; | |
$this->emailFf = false; | |
} | |
private function rma(): void | |
{ | |
$rma = Rma::find($this->commentableId); | |
array_push($this->emailRecipients, $rma->account_manager_id, $rma->assignee_id); | |
$this->displayId = $this->filledInt; | |
$this->subject = 'RMA #' . $this->displayId . ' has a new comment'; | |
$this->msg = $this->userInfo->full_name . ' posted the following comment: <br /><br />' . $this->sanitizedComment; | |
$this->pref = 'rmas'; | |
$this->typ = 'RMA'; | |
$this->emailVendor = false; | |
$this->emailFf = false; | |
} | |
private function nqCredit(): void | |
{ | |
$credit = NqCredit::find($this->commentableId); | |
array_push($this->emailRecipients, $credit->account_manager_id, $credit->assignee_id); | |
$this->displayId = $this->filledInt; | |
$this->subject = 'NQ Credit/Debit #' . $this->displayId . ' has a new comment'; | |
$this->msg = $this->userInfo->full_name . ' posted the following comment: <br /><br />' . $this->sanitizedComment; | |
$this->pref = 'nq_credit'; | |
$this->typ = 'NQ-Credit'; | |
$this->emailVendor = false; | |
$this->emailFf = false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function upload() | |
{ | |
if (Request::file('file')) { | |
$file = Request::file('file'); | |
$size = Request::file('file')->getSize(); | |
$mime = Request::file('file')->getMimeType(); | |
// Get file information | |
$file_name = Request::get('file_name'); | |
$uploadable_id = Request::get('uploadable_id'); | |
$uploadable_type = Request::get('uploadable_type'); | |
$upload_type = Request::get('upload_type'); | |
// email and notify settings | |
$email_recipients = []; | |
$object_id = $uploadable_id; | |
$display_id = $uploadable_id; | |
$portal_vendor = 0; | |
$subject = ''; | |
$msg = ''; | |
$pref = ''; | |
$typ = ''; | |
$email_vendor = false; | |
// Set comment to None if null or undefined | |
$comment = Request::get('comment'); | |
if ($comment == null or $comment == 'undefined') { | |
$comment = "None"; | |
} | |
$user_id = Request::get('uploaded_by'); | |
$user_info = User::find($user_id); | |
$filled_int = sprintf("%04d", $uploadable_id); | |
if ($uploadable_type === 'quote') { | |
$quote = projQuote::find(Request::get('uploadable_id')); | |
// Show that quote has been updated | |
$quote->last_updated_am = Carbon::now(); | |
$quote->save(); | |
// Add account manager, assignee and asia_assignee to email recipients | |
array_push($email_recipients, $quote->account_manager_id); | |
array_push($email_recipients, $quote->assignee_id); | |
array_push($email_recipients, $quote->asia_assignee_id); | |
// Define Main Email Info | |
$email_recipients = $this->add_quote_followers($email_recipients, $uploadable_id); | |
$display_id = $filled_int; | |
$portal_vendor = $quote->portal_vendor; | |
$subject = 'Quote ' . $display_id . ' has a new upload'; | |
$msg = $user_info->full_name . " added an upload to Quote " . $display_id; | |
$pref = 'proj_quote_uploads'; | |
$typ = 'Quote'; | |
$email_vendor = true; | |
} | |
elseif ($uploadable_type === 'purchase-order') { | |
$poArray = projPurchaseOrder::where('po', '=', $uploadable_id)->get(); | |
// Show that PO has been updated | |
foreach($poArray as $po){ | |
$po->last_updated_am = Carbon::now(); | |
$po->save(); | |
} | |
$order = $poArray[0]; | |
// Add account manager, assignee and asia_assignee to email recipients | |
array_push($email_recipients, $order->account_manager_id); | |
array_push($email_recipients, $order->assignee_id); | |
array_push($email_recipients, $order->asia_assignee_id); | |
// Define Main Email Info | |
$email_recipients = $this->add_order_followers($email_recipients, $uploadable_id); | |
$subject = 'PO' . $display_id . ' has a new upload'; | |
$msg = $user_info->full_name . " added an upload to PO" . $display_id; | |
$portal_vendor = $order->portal_vendor; | |
$pref = 'proj_order_uploads'; | |
$typ = 'Order'; | |
$email_vendor = true; | |
} | |
elseif ($uploadable_type === 'shipment') { | |
$shipment_uploadable_id = $uploadable_id; | |
$shipment = projShipment::find($uploadable_id); | |
$uploadable_id = $shipment->shipment_id; | |
// Add account manager to email recipients | |
array_push($email_recipients, $shipment->account_manager_id); | |
// Define Main Email Info | |
$object_id = $shipment->id; | |
$display_id = $shipment->shipment_id; | |
$subject = 'Shipment ' . $display_id . ' has a new upload'; | |
$msg = $user_info->full_name . " added an upload to Shipment " . $display_id; | |
$pref = 'proj_shipment_uploads'; | |
$typ = 'Shipment'; | |
$email_vendor = false; | |
} | |
elseif ($uploadable_type == "master-shipment") | |
{ | |
// Get logistics users | |
$permission = Permission::with('users')->where('code_name', '=', 'logistics')->first(); | |
foreach ($permission->users as $user) { | |
array_push($email_recipients, $user->id); | |
} | |
// Define Main Email Info | |
$display_id = sprintf("%05d", $uploadable_id); | |
$subject = 'Master Shipment ' . $display_id . ' has a new upload'; | |
$msg = $user_info->full_name . " added an upload to Master Shipment " . $display_id; | |
$pref = 'proj_master_shipment'; | |
$typ = 'Master Shipment'; | |
$email_vendor = false; | |
} | |
elseif ($uploadable_type == "design") { | |
$design = projDesign::find($uploadable_id); | |
// Add account manager to email recipients | |
array_push($email_recipients, $design->account_manager_id); | |
// Define Main Email Info | |
$display_id = $filled_int; | |
$subject = 'Design ' . $display_id . ' has a new upload'; | |
$msg = $user_info->full_name . " added an upload to Design " . $display_id; | |
$pref = 'proj_designs'; | |
$typ = 'Design'; | |
$email_vendor = false; | |
} | |
elseif ($uploadable_type == "qc-alert") { | |
$QcAlert = QcAlert::find($uploadable_id); | |
// Add account manager and assignee to email recipients | |
array_push($email_recipients, $QcAlert->account_manager_id); | |
array_push($email_recipients, $QcAlert->assignee_id); | |
// Define Main Email Info | |
$display_id = $filled_int; | |
$subject = 'QC Alert #' . $display_id . ' has a new upload'; | |
$msg = $user_info->full_name . " added an upload to QC Alert #" . $display_id; | |
$pref = 'qc-alerts'; | |
$typ = 'QC Alert'; | |
$email_vendor = false; | |
if ($upload_type == "Certificate of Destruction") { | |
$QcAlert->certificate_of_destruction_uploaded = 1; | |
$QcAlert->save(); | |
} | |
} | |
elseif ($uploadable_type == "rma") { | |
$Rma = Rma::find($uploadable_id); | |
// Add account manager and assignee to email recipients | |
array_push($email_recipients, $Rma->account_manager_id); | |
array_push($email_recipients, $Rma->assignee_id); | |
// Define Main Email Info | |
$display_id = $filled_int; | |
$subject = 'RMA ' . $display_id . ' has a new upload'; | |
$msg = $user_info->full_name . " added an upload to RMA " . $display_id; | |
$pref = 'rmas'; | |
$typ = 'RMA'; | |
$email_vendor = false; | |
} | |
$file_path = $uploadable_type . "/" . $display_id . "/" . $file_name; | |
$file_contents = file_get_contents($file->getRealPath()); | |
// Get HR Doc Arrays | |
$it_policy_docs = Upload::where('upload_type', 'it_policy')->get(); | |
$utah_telecomm_policy_docs = Upload::where('upload_type', 'telecomm_policy_utah')->get(); | |
$idaho_telecomm_policy_docs = Upload::where('upload_type', 'telecomm_policy_idaho')->get(); | |
$cal_telecomm_policy_docs = Upload::where('upload_type', 'telecomm_policy_cal')->get(); | |
$comm_policy_docs = Upload::where('upload_type', 'comm_policy')->get(); | |
$utah_handbook_policy_docs = Upload::where('upload_type', 'ut_office')->get(); | |
$utah_plant_handbook_policy_docs = Upload::where('upload_type', 'ut_plant')->get(); | |
$cal_handbook_policy_docs = Upload::where('upload_type', 'california')->get(); | |
$idaho_handbook_policy_docs = Upload::where('upload_type', 'idaho')->get(); | |
// If gravatar make public - Also if HR documents then do this | |
if ($uploadable_type == 'gravatar') { | |
Storage::disk('s3')->write($file_path, $file_contents, ['visibility' => 'public']); | |
} | |
else if(count($it_policy_docs) != 0 && $upload_type == 'it_policy'){ | |
$old_policy = Upload::where('upload_type', 'it_policy')->first(); | |
$old_file_name = $old_policy->file_name; | |
$old_file_path = 'human-resources/0/' . $old_file_name; | |
Storage::disk('s3')->delete($old_file_path); | |
$old_file = Upload::where('upload_type', 'it_policy')->first(); | |
$old_file->delete(); | |
Storage::disk('s3')->write($file_path, $file_contents); | |
} | |
else if(count($utah_handbook_policy_docs) != 0 && $upload_type == 'ut_office'){ | |
$old_policy = Upload::where('upload_type', 'ut_office')->first(); | |
$old_file_name = $old_policy->file_name; | |
$old_file_path = 'human-resources/0/' . $old_file_name; | |
Storage::disk('s3')->delete($old_file_path); | |
$old_file = Upload::where('upload_type', 'ut_office')->first(); | |
$old_file->delete(); | |
Storage::disk('s3')->write($file_path, $file_contents); | |
} | |
else if(count($utah_plant_handbook_policy_docs) != 0 && $upload_type == 'ut_plant'){ | |
$old_policy = Upload::where('upload_type', 'ut_plant')->first(); | |
$old_file_name = $old_policy->file_name; | |
$old_file_path = 'human-resources/0/' . $old_file_name; | |
Storage::disk('s3')->delete($old_file_path); | |
$old_file = Upload::where('upload_type', 'ut_plant')->first(); | |
$old_file->delete(); | |
Storage::disk('s3')->write($file_path, $file_contents); | |
} | |
else if(count($cal_handbook_policy_docs) != 0 && $upload_type == 'california'){ | |
$old_policy = Upload::where('upload_type', 'california')->first(); | |
$old_file_name = $old_policy->file_name; | |
$old_file_path = 'human-resources/0/' . $old_file_name; | |
Storage::disk('s3')->delete($old_file_path); | |
$old_file = Upload::where('upload_type', 'california')->first(); | |
$old_file->delete(); | |
Storage::disk('s3')->write($file_path, $file_contents); | |
} | |
else if(count($idaho_handbook_policy_docs) != 0 && $upload_type == 'idaho'){ | |
$old_policy = Upload::where('upload_type', 'idaho')->first(); | |
$old_file_name = $old_policy->file_name; | |
$old_file_path = 'human-resources/0/' . $old_file_name; | |
Storage::disk('s3')->delete($old_file_path); | |
$old_file = Upload::where('upload_type', 'idaho')->first(); | |
$old_file->delete(); | |
Storage::disk('s3')->write($file_path, $file_contents); | |
} | |
else if(count($utah_telecomm_policy_docs) != 0 && $upload_type == 'telecomm_policy_utah'){ | |
$old_policy = Upload::where('upload_type', 'telecomm_policy_utah')->first(); | |
$old_file_name = $old_policy->file_name; | |
$old_file_path = 'human-resources/0/' . $old_file_name; | |
Storage::disk('s3')->delete($old_file_path); | |
$old_file = Upload::where('upload_type', 'telecomm_policy_utah')->first(); | |
$old_file->delete(); | |
Storage::disk('s3')->write($file_path, $file_contents); | |
} | |
else if(count($idaho_telecomm_policy_docs) != 0 && $upload_type == 'telecomm_policy_idaho'){ | |
$old_policy = Upload::where('upload_type', 'telecomm_policy_idaho')->first(); | |
$old_file_name = $old_policy->file_name; | |
$old_file_path = 'human-resources/0/' . $old_file_name; | |
Storage::disk('s3')->delete($old_file_path); | |
$old_file = Upload::where('upload_type', 'telecomm_policy_idaho')->first(); | |
$old_file->delete(); | |
Storage::disk('s3')->write($file_path, $file_contents); | |
} | |
else if(count($cal_telecomm_policy_docs) != 0 && $upload_type == 'telecomm_policy_cal'){ | |
$old_policy = Upload::where('upload_type', 'telecomm_policy_cal')->first(); | |
$old_file_name = $old_policy->file_name; | |
$old_file_path = 'human-resources/0/' . $old_file_name; | |
Storage::disk('s3')->delete($old_file_path); | |
$old_file = Upload::where('upload_type', 'telecomm_policy_cal')->first(); | |
$old_file->delete(); | |
Storage::disk('s3')->write($file_path, $file_contents); | |
} | |
else if(count($comm_policy_docs) != 0 && $upload_type == 'comm_policy'){ | |
$old_policy = Upload::where('upload_type', 'comm_policy')->first(); | |
$old_file_name = $old_policy->file_name; | |
$old_file_path = 'human-resources/0/' . $old_file_name; | |
Storage::disk('s3')->delete($old_file_path); | |
$old_file = Upload::where('upload_type', 'comm_policy')->first(); | |
$old_file->delete(); | |
Storage::disk('s3')->write($file_path, $file_contents); | |
} | |
else if($uploadable_type == 'company-directory') { | |
// Delete the existing directory | |
$directory = Upload::where('uploadable_type', 'company-directory')->first(); | |
$old_file_path = 'company-directory/0/' . $directory->file_name; | |
Storage::disk('s3')->delete($old_file_path); | |
Storage::disk('s3')->write($file_path, $file_contents); | |
$directory->delete(); | |
} | |
else { | |
Storage::disk('s3')->write($file_path, $file_contents); | |
} | |
if ($uploadable_type === 'shipment') { | |
$uploadable_id = $shipment_uploadable_id; | |
} | |
$upload = new Upload; | |
$upload->file_name = $file_name; | |
$upload->file_size = $size; | |
$upload->file_type = $mime; | |
$upload->uploaded_by = Request::get('uploaded_by'); | |
$upload->uploadable_id = $uploadable_id; | |
$upload->uploadable_type = $uploadable_type; | |
$upload->upload_type = $upload_type; | |
$upload->comment = $comment; | |
$upload->save(); | |
// Special scripts based on uploadable type | |
if ($uploadable_type == 'gravatar') { | |
$user_array = User::where('username', '=', $uploadable_id)->get(); | |
$user_info = $user_array[0]; | |
$user_info->gravatar = $file_name; | |
$user_info->save(); | |
} | |
elseif ($uploadable_type == 'expense-statement') { | |
$exp_item_id = Request::get('upload_type'); | |
if ($exp_item_id == 'orig-amex') { | |
// Do nothing | |
} elseif ($exp_item_id == 'approved-excel') { | |
// Do nothing | |
} elseif ($exp_item_id == 'amex-csv') { | |
// Do nothing | |
} elseif ($exp_item_id == 'other-csv') { | |
// Do nothing | |
} else { | |
// Receipt Uploads | |
// Update original expense item | |
$exp_item = ExpenseItem::find($exp_item_id); | |
$exp_item->receipt_uploaded = 1; | |
$exp_item->save(); | |
// Loop through and create addtional lines | |
$expense_lines = Request::get('expense_lines'); | |
if ($expense_lines == null) { | |
// Do nothing | |
} else { | |
$expense_line_array = explode(',', $expense_lines); | |
foreach ($expense_line_array as $expense_line) { | |
$upload = new Upload; | |
$upload->file_name = $file_name; | |
$upload->file_size = $size; | |
$upload->file_type = $mime; | |
$upload->uploaded_by = Request::get('uploaded_by'); | |
$upload->uploadable_id = $uploadable_id; | |
$upload->uploadable_type = $uploadable_type; | |
$upload->upload_type = $expense_line; | |
$upload->comment = $comment; | |
$upload->save(); | |
// Update this expense item | |
$exp_item = ExpenseItem::find($expense_line); | |
$exp_item->receipt_uploaded = 1; | |
$exp_item->save(); | |
} | |
} | |
} | |
} | |
// Write to log | |
$this->proj_log( | |
$uploadable_id, | |
$uploadable_type, | |
$user_id, | |
$msg | |
); | |
$this->email_and_notify($email_recipients, $user_id, $object_id, $display_id, $portal_vendor, $subject, $msg, $pref, $typ, $email_vendor, false); | |
return Response::json(Upload::with('vendorUser.assignable', 'user', 'logisticsUser.assignable')->find($upload->id)); | |
} else { | |
return Response::json("Fail!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment