1
0

tickets_ajax.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App;
  3. use App\Enums\DocumentTypes;
  4. use RuntimeException;
  5. /**
  6. * @var string $action
  7. */
  8. $controller = new class($action) {
  9. /** @var string */
  10. private $action;
  11. /** @var User */
  12. private $user;
  13. public function __construct(string $action) {
  14. $this->action = $action;
  15. $core = Core::getInstance();
  16. $this->user = $core->getUser();
  17. }
  18. public function run(): void {
  19. switch($this->action) {
  20. case "create":
  21. $this->create();
  22. break;
  23. default:
  24. throw new RuntimeException(__("Invalid action."));
  25. }
  26. }
  27. private function create(): void {
  28. $attachments = Request::getFileArray("attachments", true);
  29. $request = Request::fromRequest();
  30. $location_id = $request->int("location_id");
  31. $customer_id = $request->string("customer_id");
  32. $location_name = $request->string("location_name");
  33. $postal_code = $request->string("postal_code");
  34. $sale_number = $request->int("sale_number");
  35. $aru_number = $request->int("aru_number");
  36. if($sale_number) {
  37. $sale = Sale::getByNumber($sale_number);
  38. if(!$sale) {
  39. throw new RuntimeException(__("Lead not found."));
  40. }
  41. }
  42. if($aru_number) {
  43. $aru = Document::getByNumber($aru_number, DocumentTypes::ADVANCE_REPLACEMENT_UNIT);
  44. if(!$aru) {
  45. throw new RuntimeException(__("ARU not found."));
  46. }
  47. }
  48. $location = null;
  49. if($location_id) {
  50. $location = Location::require($location_id);
  51. }
  52. else if($customer_id) {
  53. $customer = Customer::require($customer_id);
  54. if(!$location_name) {
  55. throw new RuntimeException(__("Location name is required."));
  56. }
  57. if(!$postal_code) {
  58. throw new RuntimeException(__("Postcode is required."));
  59. }
  60. $address = new Address();
  61. $address->postal_code = $postal_code;
  62. $address->geocode();
  63. $address->commit();
  64. $location = new Location();
  65. $location->customer_id = $customer->id;
  66. $location->address_id = $address->id;
  67. $location->name = $location_name;
  68. $location->dts_created = Database::dts();
  69. $location->commit();
  70. }
  71. $ticket = new Ticket();
  72. $ticket->location_id = $location->id;
  73. $ticket->sale_id = $sale->id ?? null;
  74. $ticket->aru_document_id = $aru->id ?? null;
  75. $ticket->creator_user_id = $this->user->id;
  76. $ticket->assigned_user_id = $this->user->id;
  77. $ticket->subject = $request->string("subject");
  78. $ticket->contact_name = $request->string("contact_name");
  79. $ticket->dts_created = Database::dts();
  80. $ticket->commit();
  81. $comment = new TicketComment();
  82. $comment->ticket_id = $ticket->id;
  83. $comment->user_id = $this->user->id;
  84. $comment->content = $request->string("content");
  85. $comment->dts_created = Database::dts();
  86. $comment->commit();
  87. foreach($attachments as $attachment) {
  88. $comment->addAttachment($attachment);
  89. }
  90. Session::addAlert("success", __("Ticket created successfully."));
  91. Json::success([
  92. "id" => $ticket->id
  93. ]);
  94. }
  95. };
  96. $controller->run();