api.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace App;
  3. use App\Constraint\Constraint;
  4. use App\Constraint\HasTextConstraint;
  5. use App\Constraint\MoreThanOrEqualsConstraint;
  6. use App\Constraint\StartsWithConstraint;
  7. use App\Enums\DocumentTypes;
  8. use Carbon\CarbonImmutable;
  9. use Exception;
  10. use RuntimeException;
  11. /**
  12. * @var string $action
  13. */
  14. $request = Request::fromRequest();
  15. header("Content-Type: application/json");
  16. try {
  17. switch($action) {
  18. case "get-leads-chart-data":
  19. $dt_min = CarbonImmutable::now("Europe/London")->startOfMonth()->subMonths(12);
  20. $labels = [];
  21. $data = [];
  22. for($i = 0; $i <= 12; $i++) {
  23. $dt = $dt_min->addMonthsNoOverflow($i);
  24. $label = $dt->format("M y");
  25. $labels[] = $label;
  26. $data[$label] = 0;
  27. }
  28. $leads = Sale::query([
  29. "dts_created" => new MoreThanOrEqualsConstraint($dt_min->utc()),
  30. "dts_deleted" => null
  31. ]);
  32. foreach($leads as $lead) {
  33. $dt_created = $lead->getDateTimeCreated()->setTimezone($_ENV["TIMEZONE"]);
  34. $label = $dt_created->format("M y");
  35. $data[$label]++;
  36. }
  37. JSON::success([
  38. "labels" => $labels,
  39. "data" => array_values($data),
  40. ]);
  41. break;
  42. case "get-sales-orders-value-chart-data":
  43. $dt_min = CarbonImmutable::now("Europe/London")->startOfMonth()->subMonths(12);
  44. $labels = [];
  45. $data = [];
  46. for($i = 0; $i <= 12; $i++) {
  47. $dt = $dt_min->addMonthsNoOverflow($i);
  48. $label = $dt->format("M y");
  49. $labels[] = $label;
  50. $data[$label] = "0.00";
  51. }
  52. $sales_orders = Document::query([
  53. "type" => DocumentTypes::SALES_ORDER,
  54. "currency" => "GBP",
  55. "dts_created" => new MoreThanOrEqualsConstraint($dt_min->utc()),
  56. "dts_deleted" => null
  57. ]);
  58. foreach($sales_orders as $sales_order) {
  59. $dt_created = $sales_order->getDateTimeCreated()->setTimezone($_ENV["TIMEZONE"]);
  60. $label = $dt_created->format("M y");
  61. $data[$label] = bcadd($data[$label], $sales_order->getSubtotal(), 2);
  62. }
  63. JSON::success([
  64. "labels" => $labels,
  65. "data" => array_values($data),
  66. ]);
  67. break;
  68. case "suggest-sku":
  69. $supplier_id = $request->int("supplier_id");
  70. $product_type = $request->int("product_type");
  71. $supplier = Supplier::require($supplier_id);
  72. $product_prefix = Product::getSkuPrefix($product_type);
  73. if(!$supplier->sku_prefix) {
  74. JSON::fail("The supplier has no SKU prefix set.");
  75. }
  76. $products = Product::query([
  77. "sku" => (new StartsWithConstraint($supplier->sku_prefix . $product_prefix))
  78. ]);
  79. $sku_flags = [];
  80. foreach($products as $product) {
  81. if($product->sku) {
  82. $sku_flags[$product->sku] = true;
  83. }
  84. }
  85. $found_sku = null;
  86. foreach(range(1, 999) as $number) {
  87. $sku = $supplier->sku_prefix . $product_prefix . str_pad($number, 3, "0", STR_PAD_LEFT);
  88. if(isset($sku_flags[$sku])) {
  89. continue;
  90. }
  91. $found_sku = $sku;
  92. break;
  93. }
  94. if($found_sku) {
  95. JSON::success(["sku" => $found_sku]);
  96. }
  97. else {
  98. JSON::fail("An available SKU could not be found.");
  99. }
  100. break;
  101. case "get-markers":
  102. $customer_id = $request->int("customer_id");
  103. $customer = Customer::require($customer_id);
  104. $markers = [];
  105. $nvp = [];
  106. $location_id = $request->int("location_id");
  107. if($location_id) {
  108. $nvp["id"] = $location_id;
  109. }
  110. $name = $request->string("name");
  111. if($name) {
  112. $nvp["name"] = new HasTextConstraint($name);
  113. }
  114. $locations = $customer->getLocations($nvp);
  115. Address::load(array_column($locations, "address_id"));
  116. foreach($locations as $location) {
  117. $address = $location->getAddress();
  118. $customer = $location->getCustomer();
  119. if($address->hasGeolocation()) {
  120. $markers[] = [
  121. "id" => (int) $location->id,
  122. "customer" => [
  123. "id" => (int) $customer->id,
  124. "name" => $customer->name,
  125. "url" => $customer->getUrl()
  126. ],
  127. "name" => $location->name,
  128. "addressLinesHtml" => $address->getLinesHtml(),
  129. "latLng" => [
  130. "lat" => (float) $address->latitude,
  131. "lng" => (float) $address->longitude,
  132. ],
  133. "url" => $location->getUrl(),
  134. ];
  135. }
  136. }
  137. echo json_encode([
  138. "success" => true,
  139. "markers" => $markers
  140. ]);
  141. break;
  142. default:
  143. throw new RuntimeException(__("Invalid action."));
  144. }
  145. }
  146. catch(Exception $e) {
  147. Json::fail($e->getMessage());
  148. }