api.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace App;
  3. use App\Constraint\HasTextConstraint;
  4. use App\Constraint\MoreThanOrEqualsConstraint;
  5. use App\Constraint\StartsWithConstraint;
  6. use App\Enums\DocumentTypes;
  7. use Carbon\CarbonImmutable;
  8. use Exception;
  9. use RuntimeException;
  10. /**
  11. * @var string $action
  12. */
  13. $request = Request::fromRequest();
  14. header("Content-Type: application/json");
  15. try {
  16. switch($action) {
  17. case "get-leads-chart-data":
  18. $dt_min = CarbonImmutable::now("Europe/London")->startOfMonth()->subMonths(12);
  19. $labels = [];
  20. $data = [];
  21. for($i = 0; $i <= 12; $i++) {
  22. $dt = $dt_min->addMonthsNoOverflow($i);
  23. $label = $dt->format("M y");
  24. $labels[] = $label;
  25. $data[$label] = 0;
  26. }
  27. $leads = Sale::query([
  28. "dts_created" => new MoreThanOrEqualsConstraint($dt_min->utc()),
  29. "dts_deleted" => null
  30. ]);
  31. foreach($leads as $lead) {
  32. $dt_created = $lead->getDateTimeCreated()->setTimezone($_ENV["TIMEZONE"]);
  33. $label = $dt_created->format("M y");
  34. $data[$label]++;
  35. }
  36. Json::success([
  37. "labels" => $labels,
  38. "data" => array_values($data),
  39. ]);
  40. break;
  41. case "get-sales-orders-value-chart-data":
  42. $dt_min = CarbonImmutable::now("Europe/London")->startOfMonth()->subMonths(12);
  43. $date_min = $dt_min->toDateString();
  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. "date" => new MoreThanOrEqualsConstraint($date_min),
  56. "dts_deleted" => null
  57. ]);
  58. foreach($sales_orders as $sales_order) {
  59. $dt = CarbonImmutable::parse($sales_order->date);
  60. $label = $dt->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. }