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); $date_min = $dt_min->toDateString(); $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", "date" => new MoreThanOrEqualsConstraint($date_min), "dts_deleted" => null ]); foreach($sales_orders as $sales_order) { $dt = CarbonImmutable::parse($sales_order->date); $label = $dt->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()); }