api.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App;
  3. use App\Constraint\Constraint;
  4. use App\Constraint\HasTextConstraint;
  5. use App\Constraint\StartsWithConstraint;
  6. use Exception;
  7. use RuntimeException;
  8. /**
  9. * @var string $action
  10. */
  11. $request = Request::fromRequest();
  12. header("Content-Type: application/json");
  13. try {
  14. switch($action) {
  15. case "suggest-sku":
  16. $supplier_id = $request->int("supplier_id");
  17. $product_type = $request->int("product_type");
  18. $supplier = Supplier::require($supplier_id);
  19. $product_prefix = Product::getSkuPrefix($product_type);
  20. if(!$supplier->sku_prefix) {
  21. JSON::fail("The supplier has no SKU prefix set.");
  22. }
  23. $products = Product::query([
  24. "sku" => (new StartsWithConstraint($supplier->sku_prefix . $product_prefix))
  25. ]);
  26. $sku_flags = [];
  27. foreach($products as $product) {
  28. if($product->sku) {
  29. $sku_flags[$product->sku] = true;
  30. }
  31. }
  32. $found_sku = null;
  33. foreach(range(1, 999) as $number) {
  34. $sku = $supplier->sku_prefix . $product_prefix . str_pad($number, 3, "0", STR_PAD_LEFT);
  35. if(isset($sku_flags[$sku])) {
  36. continue;
  37. }
  38. $found_sku = $sku;
  39. break;
  40. }
  41. if($found_sku) {
  42. JSON::success(["sku" => $found_sku]);
  43. }
  44. else {
  45. JSON::fail("An available SKU could not be found.");
  46. }
  47. break;
  48. case "get-markers":
  49. $customer_id = $request->int("customer_id");
  50. $customer = Customer::require($customer_id);
  51. $markers = [];
  52. $nvp = [];
  53. $location_id = $request->int("location_id");
  54. if($location_id) {
  55. $nvp["id"] = $location_id;
  56. }
  57. $name = $request->string("name");
  58. if($name) {
  59. $nvp["name"] = new HasTextConstraint($name);
  60. }
  61. $locations = $customer->getLocations($nvp);
  62. Address::load(array_column($locations, "address_id"));
  63. foreach($locations as $location) {
  64. $address = $location->getAddress();
  65. $customer = $location->getCustomer();
  66. if($address->hasGeolocation()) {
  67. $markers[] = [
  68. "id" => (int) $location->id,
  69. "customer" => [
  70. "id" => (int) $customer->id,
  71. "name" => $customer->name,
  72. "url" => $customer->getUrl()
  73. ],
  74. "name" => $location->name,
  75. "addressLinesHtml" => $address->getLinesHtml(),
  76. "latLng" => [
  77. "lat" => (float) $address->latitude,
  78. "lng" => (float) $address->longitude,
  79. ],
  80. "url" => $location->getUrl(),
  81. ];
  82. }
  83. }
  84. echo json_encode([
  85. "success" => true,
  86. "markers" => $markers
  87. ]);
  88. break;
  89. default:
  90. throw new RuntimeException(__("Invalid action."));
  91. }
  92. }
  93. catch(Exception $e) {
  94. Json::fail($e->getMessage());
  95. }