action = $action; $core = Core::getInstance(); $this->user = $core->getUser(); } public function run(): void { switch($this->action) { case "create": $this->create(); break; default: throw new RuntimeException(__("Invalid action.")); } } private function create(): void { $attachments = Request::getFileArray("attachments", true); $request = Request::fromRequest(); $location_id = $request->int("location_id"); $customer_id = $request->string("customer_id"); $location_name = $request->string("location_name"); $postal_code = $request->string("postal_code"); $sale_number = $request->int("sale_number"); $aru_number = $request->int("aru_number"); if($sale_number) { $sale = Sale::getByNumber($sale_number); if(!$sale) { throw new RuntimeException(__("Lead not found.")); } } if($aru_number) { $aru = Document::getByNumber($aru_number, DocumentTypes::ADVANCE_REPLACEMENT_UNIT); if(!$aru) { throw new RuntimeException(__("ARU not found.")); } } $location = null; if($location_id) { $location = Location::require($location_id); } else if($customer_id) { $customer = Customer::require($customer_id); if(!$location_name) { throw new RuntimeException(__("Location name is required.")); } if(!$postal_code) { throw new RuntimeException(__("Postcode is required.")); } $address = new Address(); $address->postal_code = $postal_code; $address->geocode(); $address->commit(); $location = new Location(); $location->customer_id = $customer->id; $location->address_id = $address->id; $location->name = $location_name; $location->dts_created = Database::dts(); $location->commit(); } $ticket = new Ticket(); $ticket->location_id = $location->id; $ticket->sale_id = $sale->id ?? null; $ticket->aru_document_id = $aru->id ?? null; $ticket->creator_user_id = $this->user->id; $ticket->assigned_user_id = $this->user->id; $ticket->subject = $request->string("subject"); $ticket->contact_name = $request->string("contact_name"); $ticket->dts_created = Database::dts(); $ticket->commit(); $comment = new TicketComment(); $comment->ticket_id = $ticket->id; $comment->user_id = $this->user->id; $comment->content = $request->string("content"); $comment->dts_created = Database::dts(); $comment->commit(); foreach($attachments as $attachment) { $comment->addAttachment($attachment); } Session::addAlert("success", __("Ticket created successfully.")); Json::success([ "id" => $ticket->id ]); } }; $controller->run();