settings = $settings; $this->pricing = $pricing; } /** * Options available to a calculator (labels for the form, no prices). * * @param array|null $calculator Calculator or null for all options. * @return array> */ public function options( ?array $calculator = null ): array { $pricing = $this->settings->get( 'pricing' ); $config = $calculator['config'] ?? array(); $materials = array(); foreach ( $this->pricing->materials( true ) as $row ) { $materials[] = array( 'key' => $row['key'], 'label' => $row['label'], ); } $project_types = array(); foreach ( (array) $pricing['project_types'] as $row ) { $project_types[] = array( 'key' => $row['key'], 'label' => $row['label'], 'mode' => $row['mode'], 'allowTearOff' => (bool) $row['allow_tear_off'], ); } $out = array( 'property_types' => self::pick( (array) $pricing['property_types'], $config['property_types'] ?? array() ), 'project_types' => self::filter_enabled( $project_types, $config['project_types'] ?? array() ), 'materials' => self::filter_enabled( $materials, $config['materials'] ?? array() ), 'pitches' => self::pick( (array) $pricing['pitches'], array() ), 'stories' => self::pick( (array) $pricing['stories'], array() ), ); return $out; } /** * Compute an estimate. * * @param array $input Normalised input: property_type, project_type, material, zip, area, pitch, stories, tear_off. * @return array|WP_Error */ public function estimate( array $input ) { $pricing = $this->settings->get( 'pricing' ); $property = self::find( (array) $pricing['property_types'], (string) ( $input['property_type'] ?? '' ) ); $project = self::find( (array) $pricing['project_types'], (string) ( $input['project_type'] ?? '' ) ); if ( ! $property || ! $project ) { return new WP_Error( 'thinroof_invalid_option', __( 'Please choose a property and project type.', 'thinroof' ) ); } $pitch = self::find( (array) $pricing['pitches'], (string) ( $input['pitch'] ?? '' ) ) ?? self::find( (array) $pricing['pitches'], 'medium' ) ?? array( 'key' => '', 'label' => '', 'area_factor' => 1, 'labor_factor' => 1, ); $story = self::find( (array) $pricing['stories'], (string) ( $input['stories'] ?? '1' ) ) ?? array( 'key' => '1', 'label' => '', 'factor' => 1, ); $materials = $this->pricing->materials( true ); $material = $materials[ (string) ( $input['material'] ?? '' ) ] ?? null; if ( 'area' === $project['mode'] && ! $material ) { return new WP_Error( 'thinroof_invalid_option', __( 'Please choose a roofing material.', 'thinroof' ) ); } $context = array( 'input' => $input, 'property' => $property, 'project' => $project, 'pitch' => $pitch, 'story' => $story, 'material' => $material, 'area_basis' => (string) $pricing['area_basis'], 'waste_pct' => ( $material && null !== $material['waste_pct'] ) ? (float) $material['waste_pct'] : (float) $pricing['waste_pct'], 'tearoff_per_square' => (float) $pricing['tearoff_per_square'], 'disposal_per_square' => (float) $pricing['disposal_per_square'], 'min_project_price' => (float) $pricing['min_project_price'], 'range_pct' => (float) $pricing['range_pct'], 'material_multiplier' => 1.0, 'labor_multiplier' => 1.0, 'extra_charges' => array(), 'available' => true, 'unavailable_message' => '', 'pricing_source' => 'default', ); /** * Filter the pricing context before calculation. Premium ZIP pricing * adjusts multipliers, minimums, extra charges and availability here. * * @param array $context Context. * @param array $input Normalised input. */ $context = (array) apply_filters( 'thinroof_pricing_context', $context, $input ); $result = 'flat' === $project['mode'] ? $this->flat( $context ) : $this->area( $context ); /** * Filter the computed estimate. * * @param array $result Estimate. * @param array $context Context used. */ return (array) apply_filters( 'thinroof_estimate_result', $result, $context ); } /** * Area-priced estimate. * * @param array $c Context. */ private function area( array $c ): array { $input_area = max( 0, (int) ( $c['input']['area'] ?? 0 ) ); $roof_area = 'footprint' === $c['area_basis'] ? $input_area * (float) $c['pitch']['area_factor'] : $input_area; $roof_area = (int) round( $roof_area ); $squares = $roof_area / self::SQ_FT_PER_SQUARE; $order_squares = $squares * ( 1 + (float) $c['waste_pct'] / 100 ); $labor_factor = (float) $c['pitch']['labor_factor'] * (float) $c['story']['factor']; $tear_off = ! empty( $c['input']['tear_off'] ) && ! empty( $c['project']['allow_tear_off'] ); $material_cost = round( $order_squares * (float) $c['material']['material_cost'] * (float) $c['material_multiplier'], 2 ); $labor_cost = round( $squares * (float) $c['material']['labor_cost'] * $labor_factor * (float) $c['labor_multiplier'], 2 ); $tear_off_cost = $tear_off ? round( $squares * (float) $c['tearoff_per_square'] * $labor_factor * (float) $c['labor_multiplier'], 2 ) : 0.0; $disposal_cost = $tear_off ? round( $squares * (float) $c['disposal_per_square'], 2 ) : 0.0; $base = $material_cost + $labor_cost + $tear_off_cost + $disposal_cost; $multiplier = (float) $c['property']['multiplier'] * (float) $c['project']['multiplier']; $subtotal = round( $base * $multiplier, 2 ); $extras = $this->extras( $c['extra_charges'] ); $total = $subtotal + $extras['sum']; $minimum_applied = false; if ( $total < (float) $c['min_project_price'] ) { $total = (float) $c['min_project_price']; $minimum_applied = true; } $total = round( $total, 2 ); $range = (float) $c['range_pct'] / 100; return $this->result( $c, array( 'mode' => 'area', 'input_area' => $input_area, 'roof_area' => $roof_area, 'roof_squares' => round( $squares, 2 ), 'order_squares' => round( $order_squares, 2 ), 'waste_pct' => (float) $c['waste_pct'], 'tear_off' => $tear_off, 'material_cost' => $material_cost, 'labor_cost' => $labor_cost, 'tear_off_cost' => $tear_off_cost, 'disposal_cost' => $disposal_cost, 'multiplier' => round( $multiplier, 4 ), 'subtotal' => $subtotal, 'extra_charges' => $extras['items'], 'minimum_applied' => $minimum_applied, 'total' => $total, 'low' => self::round_to( $total * ( 1 - $range ), 10 ), 'high' => self::round_to( $total * ( 1 + $range ), 10 ), ) ); } /** * Flat-priced estimate (repairs, inspections). * * @param array $c Context. */ private function flat( array $c ): array { $input_area = max( 0, (int) ( $c['input']['area'] ?? 0 ) ); $roof_area = 'footprint' === $c['area_basis'] ? (int) round( $input_area * (float) $c['pitch']['area_factor'] ) : $input_area; $extras = $this->extras( $c['extra_charges'] ); $total = round( (float) $c['project']['flat_amount'] * (float) $c['project']['multiplier'] + $extras['sum'], 2 ); return $this->result( $c, array( 'mode' => 'flat', 'input_area' => $input_area, 'roof_area' => $roof_area, 'roof_squares' => round( $roof_area / self::SQ_FT_PER_SQUARE, 2 ), 'order_squares' => 0.0, 'waste_pct' => 0.0, 'tear_off' => false, 'material_cost' => 0.0, 'labor_cost' => 0.0, 'tear_off_cost' => 0.0, 'disposal_cost' => 0.0, 'multiplier' => (float) $c['project']['multiplier'], 'subtotal' => $total, 'extra_charges' => $extras['items'], 'minimum_applied' => false, 'total' => $total, 'low' => $total, 'high' => $total, ) ); } /** * Add common fields. * * @param array $c Context. * @param array $data Computed values. */ private function result( array $c, array $data ): array { return array_merge( $data, array( 'currency' => 'USD', 'available' => (bool) $c['available'], 'unavailable_message' => (string) $c['unavailable_message'], 'pricing_source' => (string) $c['pricing_source'], 'labels' => array( 'property_type' => (string) $c['property']['label'], 'project_type' => (string) $c['project']['label'], 'material' => $c['material'] ? (string) $c['material']['label'] : '', 'pitch' => (string) $c['pitch']['label'], 'stories' => (string) $c['story']['label'], ), 'factors' => array( 'pitch_area' => (float) $c['pitch']['area_factor'], 'pitch_labor' => (float) $c['pitch']['labor_factor'], 'stories' => (float) $c['story']['factor'], 'property' => (float) $c['property']['multiplier'], 'project' => (float) $c['project']['multiplier'], 'material_mul' => (float) $c['material_multiplier'], 'labor_mul' => (float) $c['labor_multiplier'], ), ) ); } /** * Normalise extra charges added by filters. * * @param mixed $charges List of [label, amount]. * @return array{sum: float, items: array} */ private function extras( $charges ): array { $items = array(); $sum = 0.0; foreach ( is_array( $charges ) ? $charges : array() as $charge ) { if ( ! is_array( $charge ) || ! isset( $charge['amount'] ) ) { continue; } $amount = round( (float) $charge['amount'], 2 ); $items[] = array( 'label' => sanitize_text_field( (string) ( $charge['label'] ?? '' ) ), 'amount' => $amount, ); $sum += $amount; } return array( 'sum' => $sum, 'items' => $items, ); } /** * Data safe to send to the browser (no unit prices or factors). * * @param array $estimate Estimate. */ public static function public_view( array $estimate ): array { return array( 'mode' => $estimate['mode'], 'available' => $estimate['available'], 'unavailableMessage' => $estimate['unavailable_message'], 'roofArea' => $estimate['roof_area'], 'roofSquares' => $estimate['roof_squares'], 'materialCost' => $estimate['material_cost'], 'laborCost' => $estimate['labor_cost'], 'tearOffCost' => $estimate['tear_off_cost'], 'disposalCost' => $estimate['disposal_cost'], 'extraCharges' => $estimate['extra_charges'], 'minimumApplied' => $estimate['minimum_applied'], 'total' => $estimate['total'], 'low' => $estimate['low'], 'high' => $estimate['high'], 'labels' => $estimate['labels'], ); } /** * Format USD without cents. * * @param float $amount Amount. */ public static function money( float $amount ): string { return '$' . number_format( round( $amount ), 0, '.', ',' ); } /** * Human-readable estimate range. * * @param array $estimate Estimate (or lead row with estimate_* columns). */ public static function range_text( array $estimate ): string { $low = (float) ( $estimate['low'] ?? $estimate['estimate_low'] ?? 0 ); $high = (float) ( $estimate['high'] ?? $estimate['estimate_high'] ?? 0 ); $total = (float) ( $estimate['total'] ?? $estimate['estimate_total'] ?? 0 ); if ( $total <= 0 && $high <= 0 ) { return __( 'No charge', 'thinroof' ); } if ( abs( $high - $low ) < 0.01 ) { return self::money( $total ); } return self::money( $low ) . ' – ' . self::money( $high ); } /** * Find a row by key. * * @param array $rows Rows. * @param string $key Key. */ public static function find( array $rows, string $key ): ?array { foreach ( $rows as $row ) { if ( isset( $row['key'] ) && (string) $row['key'] === $key ) { return $row; } } return null; } /** * Key/label pairs, optionally restricted to enabled keys. * * @param array $rows Rows. * @param array $enabled Enabled keys (empty = all). */ private static function pick( array $rows, array $enabled ): array { $out = array(); foreach ( $rows as $row ) { $out[] = array( 'key' => (string) $row['key'], 'label' => (string) $row['label'], ); } return self::filter_enabled( $out, $enabled ); } /** * Restrict to enabled keys; empty list means everything is enabled. * * @param array $rows Rows with key. * @param array $enabled Keys. */ private static function filter_enabled( array $rows, array $enabled ): array { if ( empty( $enabled ) ) { return array_values( $rows ); } return array_values( array_filter( $rows, static fn( array $r ): bool => in_array( $r['key'], $enabled, true ) ) ); } /** * Round to nearest step. * * @param float $value Value. * @param int $step Step. */ private static function round_to( float $value, int $step ): float { return (float) ( round( max( 0, $value ) / $step ) * $step ); } }