| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- namespace App;
- use App\Constraint\Constraint;
- use App\Constraint\HasTextConstraint;
- use App\Constraint\MoreThanOrEqualsConstraint;
- use App\Constraint\StartsWithConstraint;
- use App\Enums\DocumentTypes;
- use Carbon\CarbonImmutable;
- use Exception;
- use RuntimeException;
- /**
- * @var string $action
- */
- $request = Request::fromRequest();
- header("Content-Type: application/json");
- try {
- switch($action) {
- case "get-leads-chart-data":
- $dt_min = CarbonImmutable::now("Europe/London")->startOfMonth()->subMonths(12);
- $labels = [];
- $data = [];
- for($i = 0; $i <= 12; $i++) {
- $dt = $dt_min->addMonthsNoOverflow($i);
- $label = $dt->format("M y");
- $labels[] = $label;
- $data[$label] = 0;
- }
- $leads = Sale::query([
- "dts_created" => new MoreThanOrEqualsConstraint($dt_min->utc()),
- "dts_deleted" => null
- ]);
- foreach($leads as $lead) {
- $dt_created = $lead->getDateTimeCreated()->setTimezone($_ENV["TIMEZONE"]);
- $label = $dt_created->format("M y");
- $data[$label]++;
- }
- Json::success([
- "labels" => $labels,
- "data" => array_values($data),
- ]);
- break;
- case "get-sales-orders-value-chart-data":
- $dt_min = CarbonImmutable::now("Europe/London")->startOfMonth()->subMonths(12);
- $labels = [];
- $data = [];
- for($i = 0; $i <= 12; $i++) {
- $dt = $dt_min->addMonthsNoOverflow($i);
- $label = $dt->format("M y");
- $labels[] = $label;
- $data[$label] = "0.00";
- }
- $sales_orders = Document::query([
- "type" => DocumentTypes::SALES_ORDER,
- "currency" => "GBP",
- "dts_created" => new MoreThanOrEqualsConstraint($dt_min->utc()),
- "dts_deleted" => null
- ]);
- foreach($sales_orders as $sales_order) {
- $dt_created = $sales_order->getDateTimeCreated()->setTimezone($_ENV["TIMEZONE"]);
- $label = $dt_created->format("M y");
- $data[$label] = bcadd($data[$label], $sales_order->getSubtotal(), 2);
- }
- Json::success([
- "labels" => $labels,
- "data" => array_values($data),
- ]);
- break;
- case "suggest-sku":
- $supplier_id = $request->int("supplier_id");
- $product_type = $request->int("product_type");
- $supplier = Supplier::require($supplier_id);
- $product_prefix = Product::getSkuPrefix($product_type);
- if(!$supplier->sku_prefix) {
- Json::fail("The supplier has no SKU prefix set.");
- }
- $products = Product::query([
- "sku" => (new StartsWithConstraint($supplier->sku_prefix . $product_prefix))
- ]);
- $sku_flags = [];
- foreach($products as $product) {
- if($product->sku) {
- $sku_flags[$product->sku] = true;
- }
- }
- $found_sku = null;
- foreach(range(1, 999) as $number) {
- $sku = $supplier->sku_prefix . $product_prefix . str_pad($number, 3, "0", STR_PAD_LEFT);
- if(isset($sku_flags[$sku])) {
- continue;
- }
- $found_sku = $sku;
- break;
- }
- if($found_sku) {
- Json::success(["sku" => $found_sku]);
- }
- else {
- Json::fail("An available SKU could not be found.");
- }
- break;
- case "get-markers":
- $customer_id = $request->int("customer_id");
- $customer = Customer::require($customer_id);
- $markers = [];
- $nvp = [];
- $location_id = $request->int("location_id");
- if($location_id) {
- $nvp["id"] = $location_id;
- }
- $name = $request->string("name");
- if($name) {
- $nvp["name"] = new HasTextConstraint($name);
- }
- $locations = $customer->getLocations($nvp);
- Address::load(array_column($locations, "address_id"));
- foreach($locations as $location) {
- $address = $location->getAddress();
- $customer = $location->getCustomer();
- if($address->hasGeolocation()) {
- $markers[] = [
- "id" => (int) $location->id,
- "customer" => [
- "id" => (int) $customer->id,
- "name" => $customer->name,
- "url" => $customer->getUrl()
- ],
- "name" => $location->name,
- "addressLinesHtml" => $address->getLinesHtml(),
- "latLng" => [
- "lat" => (float) $address->latitude,
- "lng" => (float) $address->longitude,
- ],
- "url" => $location->getUrl(),
- ];
- }
- }
- echo json_encode([
- "success" => true,
- "markers" => $markers
- ]);
- break;
- default:
- throw new RuntimeException(__("Invalid action."));
- }
- }
- catch(Exception $e) {
- Json::fail($e->getMessage());
- }
|