tickets_ajax.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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::get($location_id);
  51. if(!$location) {
  52. throw new RuntimeException(__("Location not found."));
  53. }
  54. }
  55. else if($customer_id) {
  56. $customer = Customer::get($customer_id);
  57. if(!$customer) {
  58. throw new RuntimeException(__("Customer not found."));
  59. }
  60. if(!$location_name) {
  61. throw new RuntimeException(__("Location name is required."));
  62. }
  63. if(!$postal_code) {
  64. throw new RuntimeException(__("Postcode is required."));
  65. }
  66. $address = new Address();
  67. $address->postal_code = $postal_code;
  68. $address->geocode();
  69. $address->commit();
  70. $location = new Location();
  71. $location->customer_id = $customer->id;
  72. $location->address_id = $address->id;
  73. $location->name = $location_name;
  74. $location->dts_created = Database::dts();
  75. $location->commit();
  76. }
  77. $ticket = new Ticket();
  78. $ticket->location_id = $location->id;
  79. $ticket->sale_id = $sale->id ?? null;
  80. $ticket->aru_document_id = $aru->id ?? null;
  81. $ticket->creator_user_id = $this->user->id;
  82. $ticket->assigned_user_id = $this->user->id;
  83. $ticket->subject = $request->string("subject");
  84. $ticket->contact_name = $request->string("contact_name");
  85. $ticket->dts_created = Database::dts();
  86. $ticket->commit();
  87. $comment = new TicketComment();
  88. $comment->ticket_id = $ticket->id;
  89. $comment->user_id = $this->user->id;
  90. $comment->content = $request->string("content");
  91. $comment->dts_created = Database::dts();
  92. $comment->commit();
  93. foreach($attachments as $attachment) {
  94. $comment->addAttachment($attachment);
  95. }
  96. Session::addAlert("success", __("Ticket created successfully."));
  97. Json::success([
  98. "id" => $ticket->id
  99. ]);
  100. }
  101. };
  102. $controller->run();