diff --git a/app/DataMapper/QuickbooksSync.php b/app/DataMapper/QuickbooksSync.php index edebae1e49..82322e75bf 100644 --- a/app/DataMapper/QuickbooksSync.php +++ b/app/DataMapper/QuickbooksSync.php @@ -12,8 +12,14 @@ namespace App\DataMapper; +use App\Models\Product; + /** * QuickbooksSync. + * + * Product type to income account mapping: + * Keys are Product::PRODUCT_TYPE_* constants (int). Values are income account names (string). + * Example: [Product::PRODUCT_TYPE_SERVICE => 'Service Income', Product::PRODUCT_TYPE_PHYSICAL => 'Sales of Product Income'] */ class QuickbooksSync { @@ -39,6 +45,14 @@ class QuickbooksSync public string $default_expense_account = ''; + /** + * Map of product type id (Product::PRODUCT_TYPE_*) to income account name. + * E.g. [2 => 'Service Income', 1 => 'Sales of Product Income'] + * + * @var array + */ + public array $product_type_income_account_map = []; + public function __construct(array $attributes = []) { $this->client = new QuickbooksSyncMap($attributes['client'] ?? []); @@ -52,6 +66,45 @@ class QuickbooksSync $this->expense = new QuickbooksSyncMap($attributes['expense'] ?? []); $this->default_income_account = $attributes['default_income_account'] ?? ''; $this->default_expense_account = $attributes['default_expense_account'] ?? ''; + $map = $attributes['product_type_income_account_map'] ?? []; + $this->product_type_income_account_map = self::normalizeProductTypeIncomeAccountMap($map); + } + + /** + * Normalize product_type_income_account_map so keys are int (product type ids). + */ + private static function normalizeProductTypeIncomeAccountMap(mixed $map): array + { + if (! is_array($map)) { + return []; + } + $out = []; + foreach ($map as $k => $v) { + if (is_string($v) && $v !== '') { + $out[(int) $k] = $v; + } + } + return $out; + } + + /** + * Suggested default mapping of Product::PRODUCT_TYPE_* to common QuickBooks income account names. + * Use when building UI defaults or onboarding; stored config overrides these. + */ + public static function defaultProductTypeIncomeAccountMap(): array + { + return [ + Product::PRODUCT_TYPE_PHYSICAL => 'Sales of Product Income', + Product::PRODUCT_TYPE_SERVICE => 'Service Income', + Product::PRODUCT_TYPE_DIGITAL => 'Sales of Product Income', + Product::PRODUCT_TYPE_SHIPPING => 'Shipping and Delivery Income', + Product::PRODUCT_TYPE_EXEMPT => 'Sales of Product Income', + Product::PRODUCT_TYPE_REDUCED_TAX => 'Sales of Product Income', + Product::PRODUCT_TYPE_OVERRIDE_TAX => 'Sales of Product Income', + Product::PRODUCT_TYPE_ZERO_RATED => 'Sales of Product Income', + Product::PRODUCT_TYPE_REVERSE_TAX => 'Sales of Product Income', + Product::PRODUCT_INTRA_COMMUNITY => 'Sales of Product Income', + ]; } public function toArray(): array @@ -68,6 +121,7 @@ class QuickbooksSync 'expense' => $this->expense->toArray(), 'default_income_account' => $this->default_income_account, 'default_expense_account' => $this->default_expense_account, + 'product_type_income_account_map' => $this->product_type_income_account_map, ]; } }