From abd05cdc5f5b2417184393aad2ab9f96f0e9a3e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Kj=C3=A6r=20Rasmussen?= Date: Fri, 22 Dec 2023 14:20:54 +0100 Subject: [PATCH 01/41] =?UTF-8?q?=E2=9C=A8Run=20phpconfig=20script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/FUNDING.yml | 2 +- .github/ISSUE_TEMPLATE/config.yml | 6 +- .../fix-php-code-style-issues-cs-fixer.yml | 30 -- ...pint.yml => fix-php-code-style-issues.yml} | 0 .github/workflows/run-tests-phpunit.yml | 45 --- .../{run-tests-pest.yml => run-tests.yml} | 0 .php-cs-fixer.dist.php | 39 --- CHANGELOG.md | 2 +- LICENSE.md | 2 +- README.md | 31 +- composer.json | 29 +- configure.php | 288 ------------------ src/SkeletonClass.php | 7 - src/WoocommerceEconomicClass.php | 7 + .../{ExampleTestPest.php => ExampleTest.php} | 0 tests/ExampleTestPhpunit.php | 9 - 16 files changed, 39 insertions(+), 458 deletions(-) delete mode 100644 .github/workflows/fix-php-code-style-issues-cs-fixer.yml rename .github/workflows/{fix-php-code-style-issues-pint.yml => fix-php-code-style-issues.yml} (100%) delete mode 100644 .github/workflows/run-tests-phpunit.yml rename .github/workflows/{run-tests-pest.yml => run-tests.yml} (100%) delete mode 100644 .php-cs-fixer.dist.php delete mode 100644 configure.php delete mode 100755 src/SkeletonClass.php create mode 100755 src/WoocommerceEconomicClass.php rename tests/{ExampleTestPest.php => ExampleTest.php} (100%) delete mode 100644 tests/ExampleTestPhpunit.php diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index c68765b..6991be2 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1 +1 @@ -github: :vendor_name +github: Morning-Train diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml index 96701be..3def537 100644 --- a/.github/ISSUE_TEMPLATE/config.yml +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -1,11 +1,11 @@ blank_issues_enabled: false contact_links: - name: Ask a question - url: https://github.com/:vendor_name/:package_name/discussions/new?category=q-a + url: https://github.com/Morning-Train/woocommerce-economic/discussions/new?category=q-a about: Ask the community for help - name: Request a feature - url: https://github.com/:vendor_name/:package_name/discussions/new?category=ideas + url: https://github.com/Morning-Train/woocommerce-economic/discussions/new?category=ideas about: Share ideas for new features - name: Report a security issue - url: https://github.com/:vendor_name/:package_name/security/policy + url: https://github.com/Morning-Train/woocommerce-economic/security/policy about: Learn how to notify us for sensitive bugs diff --git a/.github/workflows/fix-php-code-style-issues-cs-fixer.yml b/.github/workflows/fix-php-code-style-issues-cs-fixer.yml deleted file mode 100644 index 8e61c64..0000000 --- a/.github/workflows/fix-php-code-style-issues-cs-fixer.yml +++ /dev/null @@ -1,30 +0,0 @@ -name: Check & fix styling - -on: - push: - paths: - - '**.php' - -permissions: - contents: write - -jobs: - php-cs-fixer: - runs-on: ubuntu-latest - timeout-minutes: 5 - - steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - ref: ${{ github.head_ref }} - - - name: Run PHP CS Fixer - uses: docker://oskarstark/php-cs-fixer-ga - with: - args: --config=.php-cs-fixer.dist.php --allow-risky=yes - - - name: Commit changes - uses: stefanzweifel/git-auto-commit-action@v5 - with: - commit_message: Fix styling diff --git a/.github/workflows/fix-php-code-style-issues-pint.yml b/.github/workflows/fix-php-code-style-issues.yml similarity index 100% rename from .github/workflows/fix-php-code-style-issues-pint.yml rename to .github/workflows/fix-php-code-style-issues.yml diff --git a/.github/workflows/run-tests-phpunit.yml b/.github/workflows/run-tests-phpunit.yml deleted file mode 100644 index b093bc8..0000000 --- a/.github/workflows/run-tests-phpunit.yml +++ /dev/null @@ -1,45 +0,0 @@ -name: Tests - -on: - push: - paths: - - '**.php' - - '.github/workflows/run-tests-phpunit.yml' - - 'phpunit.xml.dist' - - 'composer.json' - - 'composer.lock' - -jobs: - test: - runs-on: ${{ matrix.os }} - timeout-minutes: 5 - strategy: - fail-fast: true - matrix: - os: [ubuntu-latest, windows-latest] - php: [8.2, 8.1] - stability: [prefer-lowest, prefer-stable] - - name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }} - - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php }} - extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo - coverage: none - - - name: Setup problem matchers - run: | - echo "::add-matcher::${{ runner.tool_cache }}/php.json" - echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" - - - name: Install dependencies - run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction - - - name: Execute tests - run: vendor/bin/phpunit diff --git a/.github/workflows/run-tests-pest.yml b/.github/workflows/run-tests.yml similarity index 100% rename from .github/workflows/run-tests-pest.yml rename to .github/workflows/run-tests.yml diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php deleted file mode 100644 index 7ee4ffa..0000000 --- a/.php-cs-fixer.dist.php +++ /dev/null @@ -1,39 +0,0 @@ -in([ - __DIR__ . '/src', - __DIR__ . '/tests', - ]) - ->name('*.php') - ->ignoreDotFiles(true) - ->ignoreVCS(true); - -return (new PhpCsFixer\Config()) - ->setRules([ - '@PSR12' => true, - 'array_syntax' => ['syntax' => 'short'], - 'ordered_imports' => ['sort_algorithm' => 'alpha'], - 'no_unused_imports' => true, - 'not_operator_with_successor_space' => true, - 'trailing_comma_in_multiline' => true, - 'phpdoc_scalar' => true, - 'unary_operator_spaces' => true, - 'binary_operator_spaces' => true, - 'blank_line_before_statement' => [ - 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], - ], - 'phpdoc_single_line_var_spacing' => true, - 'phpdoc_var_without_name' => true, - 'class_attributes_separation' => [ - 'elements' => [ - 'method' => 'one', - ], - ], - 'method_argument_space' => [ - 'on_multiline' => 'ensure_fully_multiline', - 'keep_multiple_spaces_after_comma' => true, - ], - 'single_trait_insert_per_statement' => true, - ]) - ->setFinder($finder); diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d01146..51f97b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ # Changelog -All notable changes to `:package_name` will be documented in this file. +All notable changes to `woocommerce-economic` will be documented in this file. diff --git a/LICENSE.md b/LICENSE.md index 58c9ad4..66837e1 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) :vendor_name +Copyright (c) Morning-Train Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index da3e831..6b64ee7 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,14 @@ -# :package_description - -[![Latest Version on Packagist](https://img.shields.io/packagist/v/:vendor_slug/:package_slug.svg?style=flat-square)](https://packagist.org/packages/:vendor_slug/:package_slug) -[![Tests](https://img.shields.io/github/actions/workflow/status/:vendor_slug/:package_slug/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/:vendor_slug/:package_slug/actions/workflows/run-tests.yml) -[![Total Downloads](https://img.shields.io/packagist/dt/:vendor_slug/:package_slug.svg?style=flat-square)](https://packagist.org/packages/:vendor_slug/:package_slug) - ---- -This package can be used as to scaffold a framework agnostic package. Follow these steps to get started: - -1. Press the "Use template" button at the top of this repo to create a new repo with the contents of this skeleton -2. Run "php ./configure.php" to run a script that will replace all placeholders throughout all the files -3. Have fun creating your package. -4. If you need help creating a package, consider picking up our Laravel Package Training video course. ---- - +# This is my package woocommerce-economic + +[![Latest Version on Packagist](https://img.shields.io/packagist/v/morning-train/woocommerce-economic.svg?style=flat-square)](https://packagist.org/packages/morning-train/woocommerce-economic) +[![Tests](https://img.shields.io/github/actions/workflow/status/morning-train/woocommerce-economic/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/morning-train/woocommerce-economic/actions/workflows/run-tests.yml) +[![Total Downloads](https://img.shields.io/packagist/dt/morning-train/woocommerce-economic.svg?style=flat-square)](https://packagist.org/packages/morning-train/woocommerce-economic) + This is where your description should go. Try and limit it to a paragraph or two. Consider adding a small example. ## Support us -[](https://spatie.be/github-ad-click/:package_name) +[](https://spatie.be/github-ad-click/woocommerce-economic) We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). @@ -28,14 +19,14 @@ We highly appreciate you sending us a postcard from your hometown, mentioning wh You can install the package via composer: ```bash -composer require :vendor_slug/:package_slug +composer require morning-train/woocommerce-economic ``` ## Usage ```php -$skeleton = new VendorName\Skeleton(); -echo $skeleton->echoPhrase('Hello, VendorName!'); +$skeleton = new Morning-Train\WoocommerceEconomic(); +echo $skeleton->echoPhrase('Hello, Morning-Train!'); ``` ## Testing @@ -58,7 +49,7 @@ Please review [our security policy](../../security/policy) on how to report secu ## Credits -- [:author_name](https://github.com/:author_username) +- [Lars Rasmussen](https://github.com/Morning-Train) - [All Contributors](../../contributors) ## License diff --git a/composer.json b/composer.json index 98297a1..e0ddde5 100644 --- a/composer.json +++ b/composer.json @@ -1,16 +1,16 @@ { - "name": ":vendor_slug/:package_slug", - "description": ":package_description", + "name": "morning-train/woocommerce-economic", + "description": "This is my package woocommerce-economic", "keywords": [ - ":vendor_name", - ":package_slug" + "Morning-Train", + "woocommerce-economic" ], - "homepage": "https://github.com/:vendor_slug/:package_slug", + "homepage": "https://github.com/morning-train/woocommerce-economic", "license": "MIT", "authors": [ { - "name": ":author_name", - "email": "author@domain.com", + "name": "Lars Rasmussen", + "email": "laras@morningtrain.dk", "role": "Developer" } ], @@ -18,28 +18,29 @@ "php": "^8.1" }, "require-dev": { - :require_dev_testing, - :require_dev_codestyle, + "pestphp/pest": "^2.15", + "laravel/pint": "^1.0", "spatie/ray": "^1.28" }, "autoload": { "psr-4": { - "VendorName\\Skeleton\\": "src" + "Morning-Train\\WoocommerceEconomic\\": "src" } }, "autoload-dev": { "psr-4": { - "VendorName\\Skeleton\\Tests\\": "tests" + "Morning-Train\\WoocommerceEconomic\\Tests\\": "tests" } }, "scripts": { - :scripts_testing, - :scripts_codestyle + "test": "vendor/bin/pest", + "test-coverage": "vendor/bin/pest --coverage", + "format": "vendor/bin/pint" }, "config": { "sort-packages": true, "allow-plugins": { - :plugins_testing, + "pestphp/pest-plugin": true, "phpstan/extension-installer": true } }, diff --git a/configure.php b/configure.php deleted file mode 100644 index dcff87e..0000000 --- a/configure.php +++ /dev/null @@ -1,288 +0,0 @@ -#!/usr/bin/env php - $option === $default ? strtoupper($option) : $option, - $options, - )); - - $answer = ask("{$question} ({$suggestions})"); - - $validOptions = implode(', ', $options); - - while (! in_array($answer, $options)) { - if ($default && $answer === '') { - $answer = $default; - - break; - } - - writeln(PHP_EOL."Please pick one of the following options: {$validOptions}"); - - $answer = ask("{$question} ({$suggestions})"); - } - - if (! $answer) { - $answer = $default; - } - - return $answer; -} - -function confirm(string $question, bool $default = false): bool -{ - $answer = ask($question.' ('.($default ? 'Y/n' : 'y/N').')'); - - if (! $answer) { - return $default; - } - - return strtolower($answer) === 'y'; -} - -function writeln(string $line): void -{ - echo $line.PHP_EOL; -} - -function run(string $command): string -{ - return trim(shell_exec($command)); -} - -function str_after(string $subject, string $search): string -{ - $pos = strrpos($subject, $search); - - if ($pos === false) { - return $subject; - } - - return substr($subject, $pos + strlen($search)); -} - -function slugify(string $subject): string -{ - return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $subject), '-')); -} - -function title_case(string $subject): string -{ - return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $subject))); -} - -function replace_in_file(string $file, array $replacements): void -{ - $contents = file_get_contents($file); - - file_put_contents( - $file, - str_replace( - array_keys($replacements), - array_values($replacements), - $contents - ) - ); -} - -function removeReadmeParagraphs(string $file): void -{ - $contents = file_get_contents($file); - - file_put_contents( - $file, - preg_replace('/.*/s', '', $contents) ?: $contents - ); -} - -function determineSeparator(string $path): string -{ - return str_replace('/', DIRECTORY_SEPARATOR, $path); -} - -function replaceForWindows(): array -{ - return preg_split('/\\r\\n|\\r|\\n/', run('dir /S /B * | findstr /v /i .git\ | findstr /v /i vendor | findstr /v /i '.basename(__FILE__).' | findstr /r /i /M /F:/ ":author :vendor :package VendorName skeleton vendor_name vendor_slug author@domain.com"')); -} - -function replaceForAllOtherOSes(): array -{ - return explode(PHP_EOL, run('grep -E -r -l -i ":author|:vendor|:package|VendorName|skeleton|vendor_name|vendor_slug|author@domain.com" --exclude-dir=vendor ./* ./.github/* | grep -v '.basename(__FILE__))); -} - -function setupTestingLibrary(string $testingLibrary): void -{ - if ($testingLibrary === 'pest') { - unlink(__DIR__.'/tests/ExampleTestPhpunit.php'); - unlink(__DIR__.'/.github/workflows/run-tests-phpunit.yml'); - - rename( - from: __DIR__.'/tests/ExampleTestPest.php', - to: __DIR__.'/tests/ExampleTest.php' - ); - - rename( - from: __DIR__.'/.github/workflows/run-tests-pest.yml', - to: __DIR__.'/.github/workflows/run-tests.yml' - ); - - replace_in_file(__DIR__.'/composer.json', [ - ':require_dev_testing' => '"pestphp/pest": "^2.15"', - ':scripts_testing' => '"test": "vendor/bin/pest", - "test-coverage": "vendor/bin/pest --coverage"', - ':plugins_testing' => '"pestphp/pest-plugin": true', - ]); - } elseif ($testingLibrary === 'phpunit') { - unlink(__DIR__.'/tests/ExampleTestPest.php'); - unlink(__DIR__.'/tests/ArchTest.php'); - unlink(__DIR__.'/tests/Pest.php'); - unlink(__DIR__.'/.github/workflows/run-tests-pest.yml'); - - rename( - from: __DIR__.'/tests/ExampleTestPhpunit.php', - to: __DIR__.'/tests/ExampleTest.php' - ); - - rename( - from: __DIR__.'/.github/workflows/run-tests-phpunit.yml', - to: __DIR__.'/.github/workflows/run-tests.yml' - ); - - replace_in_file(__DIR__.'/composer.json', [ - ':require_dev_testing' => '"phpunit/phpunit": "^10.3.2"', - ':scripts_testing' => '"test": "vendor/bin/phpunit", - "test-coverage": "vendor/bin/phpunit --coverage"', - ':plugins_testing,' => '', // We need to remove the comma here as well, since there's nothing to add - ]); - } -} - -function setupCodeStyleLibrary(string $codeStyleLibrary): void -{ - if ($codeStyleLibrary === 'pint') { - unlink(__DIR__.'/.github/workflows/fix-php-code-style-issues-cs-fixer.yml'); - - rename( - from: __DIR__.'/.github/workflows/fix-php-code-style-issues-pint.yml', - to: __DIR__.'/.github/workflows/fix-php-code-style-issues.yml' - ); - - replace_in_file(__DIR__.'/composer.json', [ - ':require_dev_codestyle' => '"laravel/pint": "^1.0"', - ':scripts_codestyle' => '"format": "vendor/bin/pint"', - ':plugins_testing' => '', - ]); - - unlink(__DIR__.'/.php-cs-fixer.dist.php'); - } elseif ($codeStyleLibrary === 'cs fixer') { - unlink(__DIR__.'/.github/workflows/fix-php-code-style-issues-pint.yml'); - - rename( - from: __DIR__.'/.github/workflows/fix-php-code-style-issues-cs-fixer.yml', - to: __DIR__.'/.github/workflows/fix-php-code-style-issues.yml' - ); - - replace_in_file(__DIR__.'/composer.json', [ - ':require_dev_codestyle' => '"friendsofphp/php-cs-fixer": "^3.21.1"', - ':scripts_codestyle' => '"format": "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php --allow-risky=yes"', - ':plugins_testing' => '', - ]); - } -} - -$gitName = run('git config user.name'); -$authorName = ask('Author name', $gitName); - -$gitEmail = run('git config user.email'); -$authorEmail = ask('Author email', $gitEmail); - -$usernameGuess = explode(':', run('git config remote.origin.url'))[1]; -$usernameGuess = dirname($usernameGuess); -$usernameGuess = basename($usernameGuess); -$authorUsername = ask('Author username', $usernameGuess); - -$vendorName = ask('Vendor name', $authorUsername); -$vendorSlug = slugify($vendorName); -$vendorNamespace = ucwords($vendorName); -$vendorNamespace = ask('Vendor namespace', $vendorNamespace); - -$currentDirectory = getcwd(); -$folderName = basename($currentDirectory); - -$packageName = ask('Package name', $folderName); -$packageSlug = slugify($packageName); - -$className = title_case($packageName); -$className = ask('Class name', $className); -$description = ask('Package description', "This is my package {$packageSlug}"); - -$testingLibrary = askWithOptions( - 'Which testing library do you want to use?', - ['pest', 'phpunit'], - 'pest', -); - -$codeStyleLibrary = askWithOptions( - 'Which code style library do you want to use?', - ['pint', 'cs fixer'], - 'pint', -); - -writeln('------'); -writeln("Author : {$authorName} ({$authorUsername}, {$authorEmail})"); -writeln("Vendor : {$vendorName} ({$vendorSlug})"); -writeln("Package : {$packageSlug} <{$description}>"); -writeln("Namespace : {$vendorNamespace}\\{$className}"); -writeln("Class name : {$className}"); -writeln("Testing library : {$testingLibrary}"); -writeln("Code style library : {$codeStyleLibrary}"); -writeln('------'); - -writeln('This script will replace the above values in all relevant files in the project directory.'); - -if (! confirm('Modify files?', true)) { - exit(1); -} - -$files = (str_starts_with(strtoupper(PHP_OS), 'WIN') ? replaceForWindows() : replaceForAllOtherOSes()); - -foreach ($files as $file) { - replace_in_file($file, [ - ':author_name' => $authorName, - ':author_username' => $authorUsername, - 'author@domain.com' => $authorEmail, - ':vendor_name' => $vendorName, - ':vendor_slug' => $vendorSlug, - 'VendorName' => $vendorNamespace, - ':package_name' => $packageName, - ':package_slug' => $packageSlug, - 'Skeleton' => $className, - ':package_description' => $description, - ]); - - match (true) { - str_contains($file, determineSeparator('src/SkeletonClass.php')) => rename($file, determineSeparator('./src/'.$className.'Class.php')), - str_contains($file, 'README.md') => removeReadmeParagraphs($file), - default => [], - }; -} - -setupTestingLibrary($testingLibrary); -setupCodeStyleLibrary($codeStyleLibrary); - -confirm('Execute `composer install` and run tests?') && run('composer install && composer test'); - -confirm('Let this script delete itself?', true) && unlink(__FILE__); diff --git a/src/SkeletonClass.php b/src/SkeletonClass.php deleted file mode 100755 index dcf362c..0000000 --- a/src/SkeletonClass.php +++ /dev/null @@ -1,7 +0,0 @@ - Date: Tue, 2 Jan 2024 08:34:11 +0100 Subject: [PATCH 02/41] =?UTF-8?q?=E2=9C=A8=20setup=20package?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 14 ++- src/Services/EconomicService.php | 47 +++++++++ src/WoocommerceEconomic.php | 26 +++++ src/WoocommerceEconomicClass.php | 7 -- .../WC_Gateway_Economic_Invoice.php | 95 +++++++++++++++++++ 5 files changed, 180 insertions(+), 9 deletions(-) create mode 100644 src/Services/EconomicService.php create mode 100755 src/WoocommerceEconomic.php delete mode 100755 src/WoocommerceEconomicClass.php create mode 100644 src/woocommerce/WC_Gateway_Economic_Invoice.php diff --git a/composer.json b/composer.json index e0ddde5..c5f8d9d 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ } ], "require": { - "php": "^8.1" + "php": "^8.1", + "morningtrain/morning-train-wp-e-conomic": "*" }, "require-dev": { "pestphp/pest": "^2.15", @@ -24,7 +25,7 @@ }, "autoload": { "psr-4": { - "Morning-Train\\WoocommerceEconomic\\": "src" + "MorningTrain\\WoocommerceEconomic\\": "src" } }, "autoload-dev": { @@ -37,6 +38,15 @@ "test-coverage": "vendor/bin/pest --coverage", "format": "vendor/bin/pint" }, + "repositories": { + "local": { + "type": "path", + "url": "repos/wp-e-conomic", + "options": { + "symlink": true + } + } + }, "config": { "sort-packages": true, "allow-plugins": { diff --git a/src/Services/EconomicService.php b/src/Services/EconomicService.php new file mode 100644 index 0000000..beb38c1 --- /dev/null +++ b/src/Services/EconomicService.php @@ -0,0 +1,47 @@ +first(); + + if (! $customer) { + $customer = Customer::create( + name: 'Mikkel Sciegienny', + customerGroup: 1, + currency: $order->get_currency(), + vatZone: 1, + paymentTerms: 3, + email: 'ms@morningtrain.dk', + ); + } + + $vatZone = VatZone::all()->first(); + + $recipient = Recipient::new( + name: 'Mikkel Sciegienny', + vatZone: $vatZone, + ); + + DraftInvoice::new(customer: $customer, + layout: 1, + currency: $order->get_currency(), + paymentTerms: 3, + date: current_datetime(), + recipient: $recipient) + ->addLine(ProductLine::new( + product: new Product(), + quantity: 1, + unitNetPrice: 500 + )) + ->create() + ?->book(); + } + +} diff --git a/src/WoocommerceEconomic.php b/src/WoocommerceEconomic.php new file mode 100755 index 0000000..2cb262d --- /dev/null +++ b/src/WoocommerceEconomic.php @@ -0,0 +1,26 @@ +id = 'economic_invoice'; + $this->has_fields = false; + $this->title = __('Betal med faktura', 'mt-wc-economic'); + $this->method_title = __('Betal med faktura', 'mt-wc-economic'); + $this->method_description = __('Economic Faktura Betaling', 'mt-wc-economic'); + $this->init_form_fields(); + $this->init_settings(); + } + + public function init_form_fields(): void + { + $this->form_fields = array( + + 'enabled' => array( + 'title' => __('Aktiver/Deaktiver','mt-wc-economic'), + 'type' => 'checkbox', + 'label' => __('Aktiver Faktura Betaling','mt-wc-economic'), + 'default' => 'yes' + ), + + 'title' => array( + 'title' => __('Titel','mt-wc-economic'), + 'type' => 'text', + 'description' => __('Dette styrer title for betalingsmetoden som brugere ser under tjek-ud'), + 'default' => __('Fakturabetaling','mt-wc-economic'), + 'desc_tip' => true, + ), + + 'description' => array( + 'title' => __('Beskrivelse'),'mt-wc-economic', + 'type' => 'textarea', + 'description' => __('Dette styrer beskrivelsen for betalingsmetoden som brugere ser under tjek-ud'),'mt-wc-economic', + 'default' => '', + 'desc_tip' => true, + ), + + 'instructions' => array( + 'title' => __( 'Instruktioner'), + 'type' => 'textarea', + 'description' => __('Disse instruktioner gives til brugeren på Tak-siden og i emails','mt-wc-economic'), + 'default' => '', + 'desc_tip' => true, + ), + 'economic_layout_number' => array( + 'title' => __( 'Economic layout ID'), + 'type' => 'number', + 'description' => __('indtast id til det ønskede layout i economic','mt-wc-economic'), + 'default' => '', + 'desc_tip' => true, + ), + 'economic_invoice_term' => array( + 'title' => __( 'Economic betalingsfrist'), + 'type' => 'number', + 'description' => __('indtast id til den ønskede betalingsfrist i economic','mt-wc-economic'), + 'default' => '', + 'desc_tip' => true, + ), + 'economic_invoice_draft' => array( + 'title' => __( 'Economic udkast'), + 'type' => 'select', + 'options' => array( + 'draft' => 'Draft', + 'book' => 'Rigtig', //TODO: rename this + ), + 'description' => __('Vælg hvordan fakturaen skal oprettes','mt-wc-economic'), + 'default' => '', + 'desc_tip' => true, + ), + + ); + } + + public function process_payment( $order_id ): array + { + $order = \wc_get_order( $order_id ); + + $order->update_status( 'processing', __('Afventer færdiggørelse','mt-wc-economic') ); + + WC()->cart->empty_cart(); + + return array( + 'result' => 'success', + 'redirect' => $this->get_return_url( $order ) + ); + } + +} From 5c7195c7217898d3b74c0f48ebcb18d2cd315119 Mon Sep 17 00:00:00 2001 From: larasmorningtrain Date: Tue, 2 Jan 2024 07:34:32 +0000 Subject: [PATCH 03/41] Fix styling --- src/Services/EconomicService.php | 2 - src/WoocommerceEconomic.php | 6 +- .../WC_Gateway_Economic_Invoice.php | 117 +++++++++--------- 3 files changed, 61 insertions(+), 64 deletions(-) diff --git a/src/Services/EconomicService.php b/src/Services/EconomicService.php index beb38c1..dcf8725 100644 --- a/src/Services/EconomicService.php +++ b/src/Services/EconomicService.php @@ -4,7 +4,6 @@ class EconomicService { - public static function createInvoice($orderId) { $order = \wc_get_order(4351); @@ -43,5 +42,4 @@ public static function createInvoice($orderId) ->create() ?->book(); } - } diff --git a/src/WoocommerceEconomic.php b/src/WoocommerceEconomic.php index 2cb262d..a75393a 100755 --- a/src/WoocommerceEconomic.php +++ b/src/WoocommerceEconomic.php @@ -8,17 +8,17 @@ class WoocommerceEconomic { public static function init() { - add_filter('woocommerce_payment_gateways', [self::class, 'registerGateway']); - add_action('woocommerce_new_order', [self::class, 'sendEconomicInvoice'], 10, 1); + add_filter('woocommerce_payment_gateways', [self::class, 'registerGateway']); + add_action('woocommerce_new_order', [self::class, 'sendEconomicInvoice'], 10, 1); } public static function registerGateway($gateways): array { $gateways['economic_invoice'] = WC_Gateway_Economic_Invoice::class; + return $gateways; } - public static function SendEconomicInvoice($orderId): void { EconomicService::createInvoice($orderId); diff --git a/src/woocommerce/WC_Gateway_Economic_Invoice.php b/src/woocommerce/WC_Gateway_Economic_Invoice.php index 612ee72..61887de 100644 --- a/src/woocommerce/WC_Gateway_Economic_Invoice.php +++ b/src/woocommerce/WC_Gateway_Economic_Invoice.php @@ -17,79 +17,78 @@ public function __construct() public function init_form_fields(): void { - $this->form_fields = array( + $this->form_fields = [ - 'enabled' => array( - 'title' => __('Aktiver/Deaktiver','mt-wc-economic'), - 'type' => 'checkbox', - 'label' => __('Aktiver Faktura Betaling','mt-wc-economic'), - 'default' => 'yes' - ), + 'enabled' => [ + 'title' => __('Aktiver/Deaktiver', 'mt-wc-economic'), + 'type' => 'checkbox', + 'label' => __('Aktiver Faktura Betaling', 'mt-wc-economic'), + 'default' => 'yes', + ], - 'title' => array( - 'title' => __('Titel','mt-wc-economic'), - 'type' => 'text', + 'title' => [ + 'title' => __('Titel', 'mt-wc-economic'), + 'type' => 'text', 'description' => __('Dette styrer title for betalingsmetoden som brugere ser under tjek-ud'), - 'default' => __('Fakturabetaling','mt-wc-economic'), - 'desc_tip' => true, - ), + 'default' => __('Fakturabetaling', 'mt-wc-economic'), + 'desc_tip' => true, + ], - 'description' => array( - 'title' => __('Beskrivelse'),'mt-wc-economic', - 'type' => 'textarea', - 'description' => __('Dette styrer beskrivelsen for betalingsmetoden som brugere ser under tjek-ud'),'mt-wc-economic', - 'default' => '', - 'desc_tip' => true, - ), + 'description' => [ + 'title' => __('Beskrivelse'), 'mt-wc-economic', + 'type' => 'textarea', + 'description' => __('Dette styrer beskrivelsen for betalingsmetoden som brugere ser under tjek-ud'), 'mt-wc-economic', + 'default' => '', + 'desc_tip' => true, + ], - 'instructions' => array( - 'title' => __( 'Instruktioner'), - 'type' => 'textarea', - 'description' => __('Disse instruktioner gives til brugeren på Tak-siden og i emails','mt-wc-economic'), - 'default' => '', - 'desc_tip' => true, - ), - 'economic_layout_number' => array( - 'title' => __( 'Economic layout ID'), - 'type' => 'number', - 'description' => __('indtast id til det ønskede layout i economic','mt-wc-economic'), - 'default' => '', - 'desc_tip' => true, - ), - 'economic_invoice_term' => array( - 'title' => __( 'Economic betalingsfrist'), - 'type' => 'number', - 'description' => __('indtast id til den ønskede betalingsfrist i economic','mt-wc-economic'), - 'default' => '', - 'desc_tip' => true, - ), - 'economic_invoice_draft' => array( - 'title' => __( 'Economic udkast'), - 'type' => 'select', - 'options' => array( + 'instructions' => [ + 'title' => __('Instruktioner'), + 'type' => 'textarea', + 'description' => __('Disse instruktioner gives til brugeren på Tak-siden og i emails', 'mt-wc-economic'), + 'default' => '', + 'desc_tip' => true, + ], + 'economic_layout_number' => [ + 'title' => __('Economic layout ID'), + 'type' => 'number', + 'description' => __('indtast id til det ønskede layout i economic', 'mt-wc-economic'), + 'default' => '', + 'desc_tip' => true, + ], + 'economic_invoice_term' => [ + 'title' => __('Economic betalingsfrist'), + 'type' => 'number', + 'description' => __('indtast id til den ønskede betalingsfrist i economic', 'mt-wc-economic'), + 'default' => '', + 'desc_tip' => true, + ], + 'economic_invoice_draft' => [ + 'title' => __('Economic udkast'), + 'type' => 'select', + 'options' => [ 'draft' => 'Draft', 'book' => 'Rigtig', //TODO: rename this - ), - 'description' => __('Vælg hvordan fakturaen skal oprettes','mt-wc-economic'), - 'default' => '', - 'desc_tip' => true, - ), + ], + 'description' => __('Vælg hvordan fakturaen skal oprettes', 'mt-wc-economic'), + 'default' => '', + 'desc_tip' => true, + ], - ); + ]; } - public function process_payment( $order_id ): array + public function process_payment($order_id): array { - $order = \wc_get_order( $order_id ); + $order = \wc_get_order($order_id); - $order->update_status( 'processing', __('Afventer færdiggørelse','mt-wc-economic') ); + $order->update_status('processing', __('Afventer færdiggørelse', 'mt-wc-economic')); WC()->cart->empty_cart(); - return array( - 'result' => 'success', - 'redirect' => $this->get_return_url( $order ) - ); + return [ + 'result' => 'success', + 'redirect' => $this->get_return_url($order), + ]; } - } From adf3a0c4f2d66f47cd2ec08d2754a8c6fb373389 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Kj=C3=A6r=20Rasmussen?= Date: Thu, 4 Jan 2024 14:25:30 +0100 Subject: [PATCH 04/41] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Update=20ReadMe?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 6b64ee7..27102e3 100644 --- a/README.md +++ b/README.md @@ -1,34 +1,34 @@ -# This is my package woocommerce-economic +# This is a wrapper for integrating with e-conomic for woocommerce [![Latest Version on Packagist](https://img.shields.io/packagist/v/morning-train/woocommerce-economic.svg?style=flat-square)](https://packagist.org/packages/morning-train/woocommerce-economic) [![Tests](https://img.shields.io/github/actions/workflow/status/morning-train/woocommerce-economic/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/morning-train/woocommerce-economic/actions/workflows/run-tests.yml) [![Total Downloads](https://img.shields.io/packagist/dt/morning-train/woocommerce-economic.svg?style=flat-square)](https://packagist.org/packages/morning-train/woocommerce-economic) -This is where your description should go. Try and limit it to a paragraph or two. Consider adding a small example. - -## Support us - -[](https://spatie.be/github-ad-click/woocommerce-economic) - -We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). - -We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). +This is a wrapper for integrating with e-conomic for woocommerce using the [WP-Economic Package](https://github.com/Morning-Train/wp-e-conomic) ## Installation You can install the package via composer: ```bash -composer require morning-train/woocommerce-economic +composer require morningtrain/woocommerce-economic ``` ## Usage ```php -$skeleton = new Morning-Train\WoocommerceEconomic(); -echo $skeleton->echoPhrase('Hello, Morning-Train!'); +WoocommerceEconomic::init(); ``` +### Filters + +```php +TODO: Add documentation for filters +``` + +### Woocommerce +TODO: add documentation for woocommerce usage + ## Testing ```bash @@ -49,7 +49,7 @@ Please review [our security policy](../../security/policy) on how to report secu ## Credits -- [Lars Rasmussen](https://github.com/Morning-Train) +- [Lars Rasmussen](https://github.com/larasmorningtrain) - [All Contributors](../../contributors) ## License From 0b27223a4e14d9859069e1406b2bda6d6830c008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Kj=C3=A6r=20Rasmussen?= Date: Thu, 4 Jan 2024 14:25:48 +0100 Subject: [PATCH 05/41] =?UTF-8?q?=E2=9C=A8=20Add=20actionSchedule=20servic?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Services/ActionScheduleService.php | 39 ++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/Services/ActionScheduleService.php diff --git a/src/Services/ActionScheduleService.php b/src/Services/ActionScheduleService.php new file mode 100644 index 0000000..1071cb7 --- /dev/null +++ b/src/Services/ActionScheduleService.php @@ -0,0 +1,39 @@ + $order, 'paymentMethod' => $paymentMethod]); + } + + public static function handleCreateInvoiceJob(\WC_Order $order, \WC_Payment_Gateway $paymentMethod): void + { + try { + OrderService::createInvoice($order, $paymentMethod); + }catch (\Exception $e) { + EconomicLoggerService::critical('Could not create invoice', [ + 'exception' => $e, + ]); + + $order->update_status('failed'); + } + + } + + + +} From 70cfc06b395736d40ecf1e5e002a94418e6536c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Kj=C3=A6r=20Rasmussen?= Date: Thu, 4 Jan 2024 14:26:04 +0100 Subject: [PATCH 06/41] =?UTF-8?q?=F0=9F=94=A5=20Remove=20Economic=20Servic?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Services/EconomicService.php | 45 -------------------------------- 1 file changed, 45 deletions(-) delete mode 100644 src/Services/EconomicService.php diff --git a/src/Services/EconomicService.php b/src/Services/EconomicService.php deleted file mode 100644 index dcf8725..0000000 --- a/src/Services/EconomicService.php +++ /dev/null @@ -1,45 +0,0 @@ -first(); - - if (! $customer) { - $customer = Customer::create( - name: 'Mikkel Sciegienny', - customerGroup: 1, - currency: $order->get_currency(), - vatZone: 1, - paymentTerms: 3, - email: 'ms@morningtrain.dk', - ); - } - - $vatZone = VatZone::all()->first(); - - $recipient = Recipient::new( - name: 'Mikkel Sciegienny', - vatZone: $vatZone, - ); - - DraftInvoice::new(customer: $customer, - layout: 1, - currency: $order->get_currency(), - paymentTerms: 3, - date: current_datetime(), - recipient: $recipient) - ->addLine(ProductLine::new( - product: new Product(), - quantity: 1, - unitNetPrice: 500 - )) - ->create() - ?->book(); - } -} From d3594617228f7648f549519a9541ef7094b4f64e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Kj=C3=A6r=20Rasmussen?= Date: Thu, 4 Jan 2024 14:26:37 +0100 Subject: [PATCH 07/41] =?UTF-8?q?=E2=9C=A8=20Add=20Invoice=20gateway?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WC_Gateway_Economic_Invoice.php | 210 ++++++++++++++++-- 1 file changed, 191 insertions(+), 19 deletions(-) diff --git a/src/woocommerce/WC_Gateway_Economic_Invoice.php b/src/woocommerce/WC_Gateway_Economic_Invoice.php index 61887de..8f157a1 100644 --- a/src/woocommerce/WC_Gateway_Economic_Invoice.php +++ b/src/woocommerce/WC_Gateway_Economic_Invoice.php @@ -1,29 +1,50 @@ id = 'economic_invoice'; + $this->icon = ''; $this->has_fields = false; $this->title = __('Betal med faktura', 'mt-wc-economic'); $this->method_title = __('Betal med faktura', 'mt-wc-economic'); $this->method_description = __('Economic Faktura Betaling', 'mt-wc-economic'); + $this->init_form_fields(); $this->init_settings(); + + $this->title = $this->get_option('title'); + $this->description = $this->get_option('description'); + + // This action hook saves the settings + add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']); + + add_action('woocommerce_new_order', [$this, 'onNewOrder'], 10, 1); + add_action('woocommerce_order_status_completed', [$this, 'onOrderCompleted'], 10, 1); + add_filter('woocommerce_checkout_fields', [$this,'addEanField']); + add_action('woocommerce_checkout_process', [$this,'validateEanField']); + add_action('woocommerce_after_checkout_validation', [$this,'validateEanField']); + add_action('woocommerce_checkout_update_order_meta', [$this,'eanFieldUpdateOrderMeta']); + add_action('woocommerce_admin_order_data_after_shipping_address', [$this, 'addEanFieldFisplayAdminOrderMeta'], 10, 1 ); } public function init_form_fields(): void { $this->form_fields = [ - 'enabled' => [ 'title' => __('Aktiver/Deaktiver', 'mt-wc-economic'), 'type' => 'checkbox', 'label' => __('Aktiver Faktura Betaling', 'mt-wc-economic'), - 'default' => 'yes', + 'default' => 'no', ], 'title' => [ @@ -35,47 +56,99 @@ public function init_form_fields(): void ], 'description' => [ - 'title' => __('Beskrivelse'), 'mt-wc-economic', + 'title' => __('Beskrivelse', 'mt-wc-economic'), 'type' => 'textarea', - 'description' => __('Dette styrer beskrivelsen for betalingsmetoden som brugere ser under tjek-ud'), 'mt-wc-economic', + 'description' => __('Dette styrer beskrivelsen for betalingsmetoden som brugere ser under tjek-ud', 'mt-wc-economic'), 'default' => '', 'desc_tip' => true, ], 'instructions' => [ - 'title' => __('Instruktioner'), + 'title' => __('Instruktioner', 'mt-wc-economic'), 'type' => 'textarea', 'description' => __('Disse instruktioner gives til brugeren på Tak-siden og i emails', 'mt-wc-economic'), 'default' => '', 'desc_tip' => true, ], + 'economic_layout_number' => [ - 'title' => __('Economic layout ID'), - 'type' => 'number', - 'description' => __('indtast id til det ønskede layout i economic', 'mt-wc-economic'), - 'default' => '', + 'title' => __('Economic layout', 'mt-wc-economic'), + 'type' => 'select', + 'options' => $this->getLayouts(), + 'description' => __('Vælg det ønskede layout i economic', 'mt-wc-economic'), + 'default' => null, 'desc_tip' => true, ], + 'economic_invoice_term' => [ - 'title' => __('Economic betalingsfrist'), - 'type' => 'number', - 'description' => __('indtast id til den ønskede betalingsfrist i economic', 'mt-wc-economic'), - 'default' => '', + 'title' => __('Economic betalingsfrist', 'mt-wc-economic'), + 'type' => 'select', + 'options' => $this->getPaymentTerms(), + 'description' => __('indtast id til den ønskede betalingsfrist i economic', 'mt-wc-economic'), + 'default' => null, 'desc_tip' => true, ], + 'economic_invoice_draft' => [ - 'title' => __('Economic udkast'), + 'title' => __('Economic udkast', 'mt-wc-economic'), 'type' => 'select', 'options' => [ - 'draft' => 'Draft', - 'book' => 'Rigtig', //TODO: rename this + 'draft' => 'Kladde', + 'book' => 'Bogført', ], 'description' => __('Vælg hvordan fakturaen skal oprettes', 'mt-wc-economic'), - 'default' => '', + 'default' => 'draft', + 'desc_tip' => true, + ], + + 'economic_invoice_event' => [ + 'title' => __('Faktura oprettelse', 'mt-wc-economic'), + 'type' => 'select', + 'options' => [ + 'creation' => 'Ordrer oprettelse', + 'completed' => 'Ordrer færdiggørelse', + ], + 'description' => __('Vælg hvornår fakturaen skal oprettes', 'mt-wc-economic'), + 'default' => 'creation', 'desc_tip' => true, ], + 'economic_vat_zone' => [ + 'title' => __('Economic momszone', 'mt-wc-economic'), + 'type' => 'select', + 'options' => $this->getVatZones(), + 'description' => __('Vælg den ønskede momszone i economic', 'mt-wc-economic'), + 'default' => null, + 'desc_tip' => true, + ], + + 'economic_customer_group' => [ + 'title' => __('Economic Kundegruppe', 'mt-wc-economic'), + 'type' => 'select', + 'options' => $this->getCustomerGroup(), + 'description' => __('Vælg den ønskede kundegruppe i economic', 'mt-wc-economic'), + 'default' => null, + 'desc_tip' => true, + ], + + 'economic_ean' => [ + 'title' => __('Tilføj EAN', 'mt-wc-economic'), + 'label' => __('Tilføj EAN', 'mt-wc-economic'), + 'type' => 'checkbox', + 'description' => __('Skal der være mulighed for at tilføje EAN?', 'mt-wc-economic'), + 'default' => 'no', + 'desc_tip' => true, + ], + + 'economic_shipping_product' => [ + 'title' => __('Economic forsendelses produkt ID', 'mt-wc-economic'), + 'type' => 'number', + 'description' => __('Indtast produkt ID tilknyttet fragt i economic', 'mt-wc-economic'), + 'default' => '', + 'desc_tip' => true, + ], ]; + } public function process_payment($order_id): array @@ -88,7 +161,106 @@ public function process_payment($order_id): array return [ 'result' => 'success', - 'redirect' => $this->get_return_url($order), + 'redirect' => $this->get_return_url($order) ]; } + + private function createEconomicInvoice(int $orderId): void + { + + $order = wc_get_order($orderId); + + $paymentMethod = $order->get_payment_method(); + + if ($paymentMethod !== 'economic_invoice') { + return; + } + + ActionScheduleService::addCreateInvoiceJob($order, $this); + } + + public function onNewOrder(int $orderId): void + { + if ($this->get_option('economic_invoice_event') === 'creation') { + $this->createEconomicInvoice($orderId); + } + } + + public function onOrderCompleted(int $orderId): void + { + if ($this->get_option('economic_invoice_event') === 'completed') { + + $this->createEconomicInvoice($orderId); + } + } + + private function getLayouts(): array + { + return Layout::all()->mapWithKeys(function ($layout) { + return [$layout->layoutNumber => $layout->name]; + })->toArray(); + } + + private function getPaymentTerms() + { + return PaymentTerm::all()->mapWithKeys(function ($paymentTerm) { + return [$paymentTerm->paymentTermsNumber => $paymentTerm->name]; + })->toArray(); + } + + private function getVatZones() + { + return VatZone::all()->mapWithKeys(function ($vatZone) { + return [$vatZone->vatZoneNumber => $vatZone->name]; + })->toArray(); + } + + private function getCustomerGroup() + { + return CustomerGroup::all()->mapWithKeys(function ($customerGroup) { + return [$customerGroup->customerGroupNumber => $customerGroup->name]; + })->toArray(); + } + + public function addEanField( $fields ): array + { + if($this->get_option('economic_ean') === 'no'){ + return $fields; + } + + $fields['billing']['economic_billing_ean'] = [ + 'label' => __('EAN Nummer', 'woocommerce'), + 'placeholder' => __('EAN', 'EAN Nummer', 'woocommerce'), + 'required' => false, + 'class' => ['form-row-wide'], + 'clear' => true, + 'priority' => 31, //after billing_company + ]; + return $fields; + } + + public function validateEanField(): void + { + if($this->get_option('economic_ean') === 'no'){ + return; + } + + if (! empty($_POST['economic_billing_ean']) && strlen($_POST['economic_billing_ean']) !== 13){ + wc_add_notice(__('EAN nummer ikke gyldigt!', 'woocommerce') , 'error'); + } + } + + public function eanFieldUpdateOrderMeta($order_id): void + { + if (!empty($_POST['economic_billing_ean'])) { + update_post_meta($order_id, 'economic_billing_ean', sanitize_text_field($_POST['economic_billing_ean'])); + } + } + + public function addEanFieldFisplayAdminOrderMeta($order): void + { + echo '

'.__('EAN nummer', 'woocommerce').': ' . get_post_meta( $order->get_id(), 'economic_billing_ean', true ) . '

'; + } + + } From 28cffe0b049d424eebd36f0235416fcc0ce5eb3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Kj=C3=A6r=20Rasmussen?= Date: Thu, 4 Jan 2024 14:26:50 +0100 Subject: [PATCH 08/41] =?UTF-8?q?=E2=9C=A8=20Add=20OrderService?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/woocommerce/OrderService.php | 209 +++++++++++++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 src/woocommerce/OrderService.php diff --git a/src/woocommerce/OrderService.php b/src/woocommerce/OrderService.php new file mode 100644 index 0000000..5fd9739 --- /dev/null +++ b/src/woocommerce/OrderService.php @@ -0,0 +1,209 @@ +get_option('economic_vat_zone')); + } + + private static function getLayout($paymentMethod): ?Layout + { + $layout = apply_filters('woocommerce_economic_invoice_get_layout', null); + + if ($layout) { + return $layout; + } + return Layout::find($paymentMethod->get_option('economic_layout_number')); + } + + private static function getPaymentTerm($paymentMethod): PaymentTerm + { + $paymentTerm = apply_filters('woocommerce_economic_invoice_get_payment_term', null); + + if ($paymentTerm) { + return $paymentTerm; + } + + return PaymentTerm::new( + paymentTermsNumber: $paymentMethod->get_option('economic_invoice_term'), + ); + } + + private static function getCustomer(\WC_Order $order, ?VatZone $vatZone, PaymentTerm $paymentTerm, \WC_Gateway_Economic_Invoice $paymentMethod): mixed + { + $customer = apply_filters('woocommerce_economic_invoice_get_customer', null); + + if (!$customer) { + $customer = Customer::where('email', $order->get_billing_email())->first(); + + if (!$customer) { + $customerGroup = apply_filters('woocommerce_economic_invoice_customer_group', null); + + if(! $customerGroup) { + $customerGroup = $paymentMethod->get_option('economic_customer_group'); + } + + $customer = Customer::create( + name: $order->get_billing_email(), + customerGroup:$customerGroup, + currency: $order->get_currency(), + vatZone: $vatZone, + paymentTerms: $paymentTerm, + email: $order->get_billing_email(), + ean: get_metadata('post', $order->get_id(), 'economic_billing_ean', true) + ); + } + } + + return $customer; + } + + private static function getRecipient(\WC_Order $order, ?VatZone $vatZone): Recipient + { + $recipient = apply_filters('woocommerce_economic_invoice_get_recipient', null); + + if ($recipient) { + return $recipient; + } + + return Recipient::new( + name: $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(), + vatZone: $vatZone, + address: $order->get_shipping_address_1() . ' ' . $order->get_shipping_address_2(), + zip: $order->get_shipping_postcode(), + city: $order->get_shipping_city(), + country: $order->get_shipping_country(), + ); + } + + private static function getDraftInvoice(Customer $customer, ?Layout $layout, \WC_Order $order, PaymentTerm $paymentTerm, Recipient $recipient): DraftInvoice + { + $invoice = apply_filters('woocommerce_economic_invoice_draft_get_invoice', null); + + if ($invoice) { + return $invoice; + } + + return DraftInvoice::new(customer: $customer->customerNumber, + layout: $layout, + currency: $order->get_currency(), + paymentTerms: $paymentTerm, + date: new \DateTime(current_time('mysql')), + recipient: $recipient, + ); + } + + private static function addLineItems(\WC_Order $order, DraftInvoice $invoice, \WC_Payment_Gateway $paymentMethod): DraftInvoice + { + collect($order->get_items())->each(function ($item) use ($invoice) { + $customProductLine = apply_filters('woocommerce_economic_invoice_add_product_line', null, $item, $invoice); + + if (is_a($customProductLine, ProductLine::class)) { + $invoice->addLine($customProductLine); + return; + } + + $product = self::getEconomicProduct($item, $invoice); + + + if($product){ + EconomicLoggerService::critical('Product not found', [ + 'product_id' => $item->get_product_id(), + 'economic_product_id' => $item->get_meta('economic_product_id'), + ]); + + throw new \Exception('Product not found'); + } + + $invoice->addLine(ProductLine::new( + product: $product->productNumber, + quantity: $item->get_quantity(), + unitNetPrice: $item->get_total(), + )); + }); + + $customShippingLine = apply_filters('woocommerce_economic_invoice_add_shipping_line', null, $order, $invoice); + + if($customShippingLine){ + $invoice->addLine($customShippingLine); + return $invoice; + } + + $invoice->addLine(ProductLine::new( + product: $paymentMethod->get_option('economic_shipping_product'), + quantity: 1, + unitNetPrice: $order->get_shipping_total(), + )); + + return $invoice; + } + + private static function createAndBookInvoice($paymentMethod, DraftInvoice $invoice): void + { + $customInvoice = apply_filters('woocommerce_economic_invoice_create_and_book_invoice', null, $paymentMethod, $invoice); + + if ($customInvoice !== null) { + return; + } + + if ($paymentMethod->get_option('economic_invoice_draft') === 'book') { + $invoice->create()?->book(); + return; + } + + $invoice->create(); + } + + private static function getEconomicProduct($item, DraftInvoice $invoice): ?Product + { + $product = apply_filters('woocommerce_economic_invoice_get_economic_product_line', null, $item, $invoice); + + if (!$product) { + $productEconomicId = get_post_meta($item->get_product_id(), 'economic_product_id', true); + + $product = Product::find($productEconomicId); + } + + return $product; + } + +} From ae025e03c83a504caf2d39267d5f74705c2eb4f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Kj=C3=A6r=20Rasmussen?= Date: Thu, 4 Jan 2024 14:27:08 +0100 Subject: [PATCH 09/41] =?UTF-8?q?=E2=9C=A8=20Add=20Product=20Service?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/woocommerce/ProductService.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/woocommerce/ProductService.php diff --git a/src/woocommerce/ProductService.php b/src/woocommerce/ProductService.php new file mode 100644 index 0000000..705f206 --- /dev/null +++ b/src/woocommerce/ProductService.php @@ -0,0 +1,25 @@ + 'economic_product_id', + 'label' => __('E-conomic produkt id', 'mt-wc-economic'), + 'placeholder' => __('E-conomic produkt id', 'mt-wc-economic'), + 'desc_tip' => 'true', + 'description' => __('Udfyld varenummerer fra e-conomic', 'mt-wc-economic'), + 'type' => 'number', + ]); + } + + public static function saveEconomicProductField($post_id): void + { + $economic_product_id = $_POST['economic_product_id'] ?? ''; + update_post_meta($post_id, 'economic_product_id', sanitize_text_field($economic_product_id)); + } + +} From 4c639bee1b709c147db55d761b7d2fd6fc7979e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Kj=C3=A6r=20Rasmussen?= Date: Thu, 4 Jan 2024 14:27:31 +0100 Subject: [PATCH 10/41] =?UTF-8?q?=E2=9C=A8=20Setup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/WoocommerceEconomic.php | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/WoocommerceEconomic.php b/src/WoocommerceEconomic.php index a75393a..d8f946d 100755 --- a/src/WoocommerceEconomic.php +++ b/src/WoocommerceEconomic.php @@ -2,25 +2,32 @@ namespace MorningTrain\WoocommerceEconomic; -use MorningTrain\WoocommerceEconomic\Services\EconomicService; +use MorningTrain\WoocommerceEconomic\Services\ActionScheduleService; +use MorningTrain\WoocommerceEconomic\Woocommerce\ProductService; + class WoocommerceEconomic { - public static function init() + public static function init(): void { - add_filter('woocommerce_payment_gateways', [self::class, 'registerGateway']); - add_action('woocommerce_new_order', [self::class, 'sendEconomicInvoice'], 10, 1); + add_filter('woocommerce_payment_gateways', [self::class, 'registerGateway'], 10,1); + add_action('init', [self::class, 'requireGateway'],1); + add_action('woocommerce_product_options_general_product_data', [ProductService::class, 'addEconomicProductField']); + add_action('woocommerce_variation_options', [ProductService::class, 'addEconomicProductField']); + add_action('woocommerce_save_product_variation', [ProductService::class, 'saveEconomicProductField'], 10, 2); + add_action('woocommerce_process_product_meta', [ProductService::class, 'saveEconomicProductField'], 10, 2); + + add_action(ActionScheduleService::CREATE_INVOICE, [ActionScheduleService::class, 'handleCreateInvoiceJob'], 10, 2 ); } public static function registerGateway($gateways): array { - $gateways['economic_invoice'] = WC_Gateway_Economic_Invoice::class; - + $gateways[] = \WC_Gateway_Economic_Invoice::class; return $gateways; } - public static function SendEconomicInvoice($orderId): void + public static function requireGateway(): void { - EconomicService::createInvoice($orderId); + require_once __DIR__ . '/woocommerce/WC_Gateway_Economic_Invoice.php'; } } From 0c089f533a32b04475c9afb553038d964b893d1a Mon Sep 17 00:00:00 2001 From: larasmorningtrain Date: Thu, 4 Jan 2024 13:27:53 +0000 Subject: [PATCH 11/41] Fix styling --- src/Services/ActionScheduleService.php | 11 +--- src/WoocommerceEconomic.php | 18 +++---- src/woocommerce/OrderService.php | 50 ++++++++++--------- src/woocommerce/ProductService.php | 3 +- .../WC_Gateway_Economic_Invoice.php | 43 ++++++++-------- 5 files changed, 57 insertions(+), 68 deletions(-) diff --git a/src/Services/ActionScheduleService.php b/src/Services/ActionScheduleService.php index 1071cb7..edc5aec 100644 --- a/src/Services/ActionScheduleService.php +++ b/src/Services/ActionScheduleService.php @@ -2,12 +2,6 @@ namespace MorningTrain\WoocommerceEconomic\Services; -use MorningTrain\Economic\DTOs\Recipient; -use MorningTrain\Economic\Resources\Customer; -use MorningTrain\Economic\Resources\Invoice\DraftInvoice; -use MorningTrain\Economic\Resources\Invoice\ProductLine; -use MorningTrain\Economic\Resources\Product; -use MorningTrain\Economic\Resources\VatZone; use MorningTrain\Economic\Services\EconomicLoggerService; use MorningTrain\WoocommerceEconomic\Woocommerce\OrderService; @@ -24,7 +18,7 @@ public static function handleCreateInvoiceJob(\WC_Order $order, \WC_Payment_Gate { try { OrderService::createInvoice($order, $paymentMethod); - }catch (\Exception $e) { + } catch (\Exception $e) { EconomicLoggerService::critical('Could not create invoice', [ 'exception' => $e, ]); @@ -33,7 +27,4 @@ public static function handleCreateInvoiceJob(\WC_Order $order, \WC_Payment_Gate } } - - - } diff --git a/src/WoocommerceEconomic.php b/src/WoocommerceEconomic.php index d8f946d..15c3bf5 100755 --- a/src/WoocommerceEconomic.php +++ b/src/WoocommerceEconomic.php @@ -5,29 +5,29 @@ use MorningTrain\WoocommerceEconomic\Services\ActionScheduleService; use MorningTrain\WoocommerceEconomic\Woocommerce\ProductService; - class WoocommerceEconomic { public static function init(): void { - add_filter('woocommerce_payment_gateways', [self::class, 'registerGateway'], 10,1); - add_action('init', [self::class, 'requireGateway'],1); - add_action('woocommerce_product_options_general_product_data', [ProductService::class, 'addEconomicProductField']); - add_action('woocommerce_variation_options', [ProductService::class, 'addEconomicProductField']); - add_action('woocommerce_save_product_variation', [ProductService::class, 'saveEconomicProductField'], 10, 2); - add_action('woocommerce_process_product_meta', [ProductService::class, 'saveEconomicProductField'], 10, 2); + add_filter('woocommerce_payment_gateways', [self::class, 'registerGateway'], 10, 1); + add_action('init', [self::class, 'requireGateway'], 1); + add_action('woocommerce_product_options_general_product_data', [ProductService::class, 'addEconomicProductField']); + add_action('woocommerce_variation_options', [ProductService::class, 'addEconomicProductField']); + add_action('woocommerce_save_product_variation', [ProductService::class, 'saveEconomicProductField'], 10, 2); + add_action('woocommerce_process_product_meta', [ProductService::class, 'saveEconomicProductField'], 10, 2); - add_action(ActionScheduleService::CREATE_INVOICE, [ActionScheduleService::class, 'handleCreateInvoiceJob'], 10, 2 ); + add_action(ActionScheduleService::CREATE_INVOICE, [ActionScheduleService::class, 'handleCreateInvoiceJob'], 10, 2); } public static function registerGateway($gateways): array { $gateways[] = \WC_Gateway_Economic_Invoice::class; + return $gateways; } public static function requireGateway(): void { - require_once __DIR__ . '/woocommerce/WC_Gateway_Economic_Invoice.php'; + require_once __DIR__.'/woocommerce/WC_Gateway_Economic_Invoice.php'; } } diff --git a/src/woocommerce/OrderService.php b/src/woocommerce/OrderService.php index 5fd9739..fc9d141 100644 --- a/src/woocommerce/OrderService.php +++ b/src/woocommerce/OrderService.php @@ -17,21 +17,21 @@ class OrderService public static function createInvoice(\WC_Order $order, $paymentMethod): void { - $vatZone = self::getVatZone($paymentMethod); + $vatZone = self::getVatZone($paymentMethod); - $layout = self::getLayout($paymentMethod); + $layout = self::getLayout($paymentMethod); - $paymentTerm = self::getPaymentTerm($paymentMethod); + $paymentTerm = self::getPaymentTerm($paymentMethod); - $customer = self::getCustomer($order, $vatZone, $paymentTerm, $paymentMethod); + $customer = self::getCustomer($order, $vatZone, $paymentTerm, $paymentMethod); - $recipient = self::getRecipient($order, $vatZone); + $recipient = self::getRecipient($order, $vatZone); - $invoice = self::getDraftInvoice($customer, $layout, $order, $paymentTerm, $recipient); + $invoice = self::getDraftInvoice($customer, $layout, $order, $paymentTerm, $recipient); - $invoice = self::addLineItems($order, $invoice, $paymentMethod); + $invoice = self::addLineItems($order, $invoice, $paymentMethod); - self::createAndBookInvoice($paymentMethod, $invoice); + self::createAndBookInvoice($paymentMethod, $invoice); } private static function getVatZone($paymentMethod): ?VatZone @@ -52,6 +52,7 @@ private static function getLayout($paymentMethod): ?Layout if ($layout) { return $layout; } + return Layout::find($paymentMethod->get_option('economic_layout_number')); } @@ -72,19 +73,19 @@ private static function getCustomer(\WC_Order $order, ?VatZone $vatZone, Payment { $customer = apply_filters('woocommerce_economic_invoice_get_customer', null); - if (!$customer) { + if (! $customer) { $customer = Customer::where('email', $order->get_billing_email())->first(); - if (!$customer) { + if (! $customer) { $customerGroup = apply_filters('woocommerce_economic_invoice_customer_group', null); - if(! $customerGroup) { + if (! $customerGroup) { $customerGroup = $paymentMethod->get_option('economic_customer_group'); } $customer = Customer::create( name: $order->get_billing_email(), - customerGroup:$customerGroup, + customerGroup: $customerGroup, currency: $order->get_currency(), vatZone: $vatZone, paymentTerms: $paymentTerm, @@ -106,9 +107,9 @@ private static function getRecipient(\WC_Order $order, ?VatZone $vatZone): Recip } return Recipient::new( - name: $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(), + name: $order->get_shipping_first_name().' '.$order->get_shipping_last_name(), vatZone: $vatZone, - address: $order->get_shipping_address_1() . ' ' . $order->get_shipping_address_2(), + address: $order->get_shipping_address_1().' '.$order->get_shipping_address_2(), zip: $order->get_shipping_postcode(), city: $order->get_shipping_city(), country: $order->get_shipping_country(), @@ -139,13 +140,13 @@ private static function addLineItems(\WC_Order $order, DraftInvoice $invoice, \W if (is_a($customProductLine, ProductLine::class)) { $invoice->addLine($customProductLine); + return; } $product = self::getEconomicProduct($item, $invoice); - - if($product){ + if ($product) { EconomicLoggerService::critical('Product not found', [ 'product_id' => $item->get_product_id(), 'economic_product_id' => $item->get_meta('economic_product_id'), @@ -161,12 +162,13 @@ private static function addLineItems(\WC_Order $order, DraftInvoice $invoice, \W )); }); - $customShippingLine = apply_filters('woocommerce_economic_invoice_add_shipping_line', null, $order, $invoice); + $customShippingLine = apply_filters('woocommerce_economic_invoice_add_shipping_line', null, $order, $invoice); + + if ($customShippingLine) { + $invoice->addLine($customShippingLine); - if($customShippingLine){ - $invoice->addLine($customShippingLine); - return $invoice; - } + return $invoice; + } $invoice->addLine(ProductLine::new( product: $paymentMethod->get_option('economic_shipping_product'), @@ -187,17 +189,18 @@ private static function createAndBookInvoice($paymentMethod, DraftInvoice $invoi if ($paymentMethod->get_option('economic_invoice_draft') === 'book') { $invoice->create()?->book(); + return; } $invoice->create(); } - private static function getEconomicProduct($item, DraftInvoice $invoice): ?Product + private static function getEconomicProduct($item, DraftInvoice $invoice): ?Product { $product = apply_filters('woocommerce_economic_invoice_get_economic_product_line', null, $item, $invoice); - if (!$product) { + if (! $product) { $productEconomicId = get_post_meta($item->get_product_id(), 'economic_product_id', true); $product = Product::find($productEconomicId); @@ -205,5 +208,4 @@ private static function getEconomicProduct($item, DraftInvoice $invoice): ?Produ return $product; } - } diff --git a/src/woocommerce/ProductService.php b/src/woocommerce/ProductService.php index 705f206..6e69e93 100644 --- a/src/woocommerce/ProductService.php +++ b/src/woocommerce/ProductService.php @@ -1,6 +1,6 @@ description = $this->get_option('description'); // This action hook saves the settings - add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']); + add_action('woocommerce_update_options_payment_gateways_'.$this->id, [$this, 'process_admin_options']); add_action('woocommerce_new_order', [$this, 'onNewOrder'], 10, 1); add_action('woocommerce_order_status_completed', [$this, 'onOrderCompleted'], 10, 1); - add_filter('woocommerce_checkout_fields', [$this,'addEanField']); - add_action('woocommerce_checkout_process', [$this,'validateEanField']); - add_action('woocommerce_after_checkout_validation', [$this,'validateEanField']); - add_action('woocommerce_checkout_update_order_meta', [$this,'eanFieldUpdateOrderMeta']); - add_action('woocommerce_admin_order_data_after_shipping_address', [$this, 'addEanFieldFisplayAdminOrderMeta'], 10, 1 ); + add_filter('woocommerce_checkout_fields', [$this, 'addEanField']); + add_action('woocommerce_checkout_process', [$this, 'validateEanField']); + add_action('woocommerce_after_checkout_validation', [$this, 'validateEanField']); + add_action('woocommerce_checkout_update_order_meta', [$this, 'eanFieldUpdateOrderMeta']); + add_action('woocommerce_admin_order_data_after_shipping_address', [$this, 'addEanFieldFisplayAdminOrderMeta'], 10, 1); } public function init_form_fields(): void @@ -161,7 +159,7 @@ public function process_payment($order_id): array return [ 'result' => 'success', - 'redirect' => $this->get_return_url($order) + 'redirect' => $this->get_return_url($order), ]; } @@ -222,45 +220,44 @@ private function getCustomerGroup() })->toArray(); } - public function addEanField( $fields ): array + public function addEanField($fields): array { - if($this->get_option('economic_ean') === 'no'){ + if ($this->get_option('economic_ean') === 'no') { return $fields; } $fields['billing']['economic_billing_ean'] = [ - 'label' => __('EAN Nummer', 'woocommerce'), - 'placeholder' => __('EAN', 'EAN Nummer', 'woocommerce'), - 'required' => false, - 'class' => ['form-row-wide'], - 'clear' => true, + 'label' => __('EAN Nummer', 'woocommerce'), + 'placeholder' => __('EAN', 'EAN Nummer', 'woocommerce'), + 'required' => false, + 'class' => ['form-row-wide'], + 'clear' => true, 'priority' => 31, //after billing_company ]; + return $fields; } public function validateEanField(): void { - if($this->get_option('economic_ean') === 'no'){ + if ($this->get_option('economic_ean') === 'no') { return; } - if (! empty($_POST['economic_billing_ean']) && strlen($_POST['economic_billing_ean']) !== 13){ - wc_add_notice(__('EAN nummer ikke gyldigt!', 'woocommerce') , 'error'); + if (! empty($_POST['economic_billing_ean']) && strlen($_POST['economic_billing_ean']) !== 13) { + wc_add_notice(__('EAN nummer ikke gyldigt!', 'woocommerce'), 'error'); } } public function eanFieldUpdateOrderMeta($order_id): void { - if (!empty($_POST['economic_billing_ean'])) { + if (! empty($_POST['economic_billing_ean'])) { update_post_meta($order_id, 'economic_billing_ean', sanitize_text_field($_POST['economic_billing_ean'])); } } public function addEanFieldFisplayAdminOrderMeta($order): void { - echo '

'.__('EAN nummer', 'woocommerce').': ' . get_post_meta( $order->get_id(), 'economic_billing_ean', true ) . '

'; + echo '

'.__('EAN nummer', 'woocommerce').': '.get_post_meta($order->get_id(), 'economic_billing_ean', true).'

'; } - - } From 9a0195c27bc650b4fb470ea954be86c896b3095d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Kj=C3=A6r=20Rasmussen?= Date: Mon, 8 Jan 2024 13:38:58 +0100 Subject: [PATCH 12/41] =?UTF-8?q?=F0=9F=90=9B=20Send=20correct=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Services/ActionScheduleService.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Services/ActionScheduleService.php b/src/Services/ActionScheduleService.php index edc5aec..af8b979 100644 --- a/src/Services/ActionScheduleService.php +++ b/src/Services/ActionScheduleService.php @@ -1,21 +1,25 @@ $order, 'paymentMethod' => $paymentMethod]); + \as_schedule_single_action(time(), static::CREATE_INVOICE, [ $order->get_id()]); } - public static function handleCreateInvoiceJob(\WC_Order $order, \WC_Payment_Gateway $paymentMethod): void + public static function handleCreateInvoiceJob(int $orderId): void { + $order = \wc_get_order($orderId); + + $paymentMethod = wc_get_payment_gateway_by_order($orderId); + try { OrderService::createInvoice($order, $paymentMethod); } catch (\Exception $e) { From 94e5681c0f478c2de48c8a7963a1a965832604db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Kj=C3=A6r=20Rasmussen?= Date: Mon, 8 Jan 2024 13:39:46 +0100 Subject: [PATCH 13/41] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Rename=20morningtrai?= =?UTF-8?q?n=20namespace?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 19 +++++-------------- src/WoocommerceEconomic.php | 8 ++++---- src/woocommerce/OrderService.php | 30 ++++++++++++++++-------------- src/woocommerce/ProductService.php | 2 +- 4 files changed, 26 insertions(+), 33 deletions(-) diff --git a/composer.json b/composer.json index c5f8d9d..f7c2c57 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "morning-train/woocommerce-economic", + "name": "morningtrain/woocommerce-economic", "description": "This is my package woocommerce-economic", "keywords": [ "Morning-Train", @@ -16,7 +16,7 @@ ], "require": { "php": "^8.1", - "morningtrain/morning-train-wp-e-conomic": "*" + "morningtrain/wp-economic": "^0.1" }, "require-dev": { "pestphp/pest": "^2.15", @@ -25,28 +25,19 @@ }, "autoload": { "psr-4": { - "MorningTrain\\WoocommerceEconomic\\": "src" + "Morningtrain\\WoocommerceEconomic\\": "src" } }, "autoload-dev": { "psr-4": { - "Morning-Train\\WoocommerceEconomic\\Tests\\": "tests" + "Morningtrain\\WoocommerceEconomic\\Tests\\": "tests" } }, "scripts": { "test": "vendor/bin/pest", - "test-coverage": "vendor/bin/pest --coverage", + "test-coverage": "vendor/bin/pest --coverage", "format": "vendor/bin/pint" }, - "repositories": { - "local": { - "type": "path", - "url": "repos/wp-e-conomic", - "options": { - "symlink": true - } - } - }, "config": { "sort-packages": true, "allow-plugins": { diff --git a/src/WoocommerceEconomic.php b/src/WoocommerceEconomic.php index 15c3bf5..74083c0 100755 --- a/src/WoocommerceEconomic.php +++ b/src/WoocommerceEconomic.php @@ -1,9 +1,9 @@ get_shipping_first_name().' '.$order->get_shipping_last_name(), + name: $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(), vatZone: $vatZone, - address: $order->get_shipping_address_1().' '.$order->get_shipping_address_2(), + address: $order->get_shipping_address_1() . ' ' . $order->get_shipping_address_2(), zip: $order->get_shipping_postcode(), city: $order->get_shipping_city(), country: $order->get_shipping_country(), @@ -124,7 +124,8 @@ private static function getDraftInvoice(Customer $customer, ?Layout $layout, \WC return $invoice; } - return DraftInvoice::new(customer: $customer->customerNumber, + return DraftInvoice::new( + customer: $customer->customerNumber, layout: $layout, currency: $order->get_currency(), paymentTerms: $paymentTerm, @@ -204,6 +205,7 @@ private static function getEconomicProduct($item, DraftInvoice $invoice): ?Produ $productEconomicId = get_post_meta($item->get_product_id(), 'economic_product_id', true); $product = Product::find($productEconomicId); + ray($product); } return $product; diff --git a/src/woocommerce/ProductService.php b/src/woocommerce/ProductService.php index 6e69e93..6c49724 100644 --- a/src/woocommerce/ProductService.php +++ b/src/woocommerce/ProductService.php @@ -1,6 +1,6 @@ Date: Mon, 8 Jan 2024 13:40:06 +0100 Subject: [PATCH 14/41] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Send=20correct=20par?= =?UTF-8?q?ams?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/woocommerce/WC_Gateway_Economic_Invoice.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/woocommerce/WC_Gateway_Economic_Invoice.php b/src/woocommerce/WC_Gateway_Economic_Invoice.php index c6a6d84..602665d 100644 --- a/src/woocommerce/WC_Gateway_Economic_Invoice.php +++ b/src/woocommerce/WC_Gateway_Economic_Invoice.php @@ -1,10 +1,10 @@ description = $this->get_option('description'); // This action hook saves the settings - add_action('woocommerce_update_options_payment_gateways_'.$this->id, [$this, 'process_admin_options']); + add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']); add_action('woocommerce_new_order', [$this, 'onNewOrder'], 10, 1); add_action('woocommerce_order_status_completed', [$this, 'onOrderCompleted'], 10, 1); @@ -174,7 +174,7 @@ private function createEconomicInvoice(int $orderId): void return; } - ActionScheduleService::addCreateInvoiceJob($order, $this); + ActionScheduleService::addCreateInvoiceJob($order); } public function onNewOrder(int $orderId): void @@ -258,6 +258,6 @@ public function eanFieldUpdateOrderMeta($order_id): void public function addEanFieldFisplayAdminOrderMeta($order): void { - echo '

'.__('EAN nummer', 'woocommerce').': '.get_post_meta($order->get_id(), 'economic_billing_ean', true).'

'; + echo '

' . __('EAN nummer', 'woocommerce') . ': ' . get_post_meta($order->get_id(), 'economic_billing_ean', true) . '

'; } } From e6964c47e384ee391afe7b2b8f72eb23b842b8bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Kj=C3=A6r=20Rasmussen?= Date: Mon, 8 Jan 2024 13:40:40 +0100 Subject: [PATCH 15/41] =?UTF-8?q?=E2=9C=A8=20Rewrite=20destription?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 27102e3..903a4c5 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# This is a wrapper for integrating with e-conomic for woocommerce +# PHP SDK for integrating with e-conomic for woocommerce [![Latest Version on Packagist](https://img.shields.io/packagist/v/morning-train/woocommerce-economic.svg?style=flat-square)](https://packagist.org/packages/morning-train/woocommerce-economic) [![Tests](https://img.shields.io/github/actions/workflow/status/morning-train/woocommerce-economic/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/morning-train/woocommerce-economic/actions/workflows/run-tests.yml) From 72a2496c5fa7befb15ae546990cb628c0e8053f3 Mon Sep 17 00:00:00 2001 From: larasmorningtrain Date: Mon, 8 Jan 2024 12:41:07 +0000 Subject: [PATCH 16/41] Fix styling --- src/Services/ActionScheduleService.php | 2 +- src/WoocommerceEconomic.php | 2 +- src/woocommerce/OrderService.php | 4 ++-- src/woocommerce/WC_Gateway_Economic_Invoice.php | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Services/ActionScheduleService.php b/src/Services/ActionScheduleService.php index af8b979..e82ad16 100644 --- a/src/Services/ActionScheduleService.php +++ b/src/Services/ActionScheduleService.php @@ -11,7 +11,7 @@ class ActionScheduleService public static function addCreateInvoiceJob(\WC_Order $order): void { - \as_schedule_single_action(time(), static::CREATE_INVOICE, [ $order->get_id()]); + \as_schedule_single_action(time(), static::CREATE_INVOICE, [$order->get_id()]); } public static function handleCreateInvoiceJob(int $orderId): void diff --git a/src/WoocommerceEconomic.php b/src/WoocommerceEconomic.php index 74083c0..cb28590 100755 --- a/src/WoocommerceEconomic.php +++ b/src/WoocommerceEconomic.php @@ -28,6 +28,6 @@ public static function registerGateway($gateways): array public static function requireGateway(): void { - require_once __DIR__ . '/woocommerce/WC_Gateway_Economic_Invoice.php'; + require_once __DIR__.'/woocommerce/WC_Gateway_Economic_Invoice.php'; } } diff --git a/src/woocommerce/OrderService.php b/src/woocommerce/OrderService.php index 06eb4fd..b9ba06e 100644 --- a/src/woocommerce/OrderService.php +++ b/src/woocommerce/OrderService.php @@ -107,9 +107,9 @@ private static function getRecipient(\WC_Order $order, ?VatZone $vatZone): Recip } return Recipient::new( - name: $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(), + name: $order->get_shipping_first_name().' '.$order->get_shipping_last_name(), vatZone: $vatZone, - address: $order->get_shipping_address_1() . ' ' . $order->get_shipping_address_2(), + address: $order->get_shipping_address_1().' '.$order->get_shipping_address_2(), zip: $order->get_shipping_postcode(), city: $order->get_shipping_city(), country: $order->get_shipping_country(), diff --git a/src/woocommerce/WC_Gateway_Economic_Invoice.php b/src/woocommerce/WC_Gateway_Economic_Invoice.php index 602665d..7cad56f 100644 --- a/src/woocommerce/WC_Gateway_Economic_Invoice.php +++ b/src/woocommerce/WC_Gateway_Economic_Invoice.php @@ -24,7 +24,7 @@ public function __construct() $this->description = $this->get_option('description'); // This action hook saves the settings - add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']); + add_action('woocommerce_update_options_payment_gateways_'.$this->id, [$this, 'process_admin_options']); add_action('woocommerce_new_order', [$this, 'onNewOrder'], 10, 1); add_action('woocommerce_order_status_completed', [$this, 'onOrderCompleted'], 10, 1); @@ -258,6 +258,6 @@ public function eanFieldUpdateOrderMeta($order_id): void public function addEanFieldFisplayAdminOrderMeta($order): void { - echo '

' . __('EAN nummer', 'woocommerce') . ': ' . get_post_meta($order->get_id(), 'economic_billing_ean', true) . '

'; + echo '

'.__('EAN nummer', 'woocommerce').': '.get_post_meta($order->get_id(), 'economic_billing_ean', true).'

'; } } From b5efa3bd7521161458ea0342c7e5a143aeff6ab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Kj=C3=A6r=20Rasmussen?= Date: Tue, 9 Jan 2024 10:27:33 +0100 Subject: [PATCH 17/41] =?UTF-8?q?=E2=9C=A8=20Add=20description=20for=20pro?= =?UTF-8?q?duct=20line?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/WoocommerceEconomic.php | 5 ++--- src/woocommerce/OrderService.php | 8 +++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/WoocommerceEconomic.php b/src/WoocommerceEconomic.php index cb28590..64efbe0 100755 --- a/src/WoocommerceEconomic.php +++ b/src/WoocommerceEconomic.php @@ -15,8 +15,7 @@ public static function init(): void add_action('woocommerce_variation_options', [ProductService::class, 'addEconomicProductField']); add_action('woocommerce_save_product_variation', [ProductService::class, 'saveEconomicProductField'], 10, 2); add_action('woocommerce_process_product_meta', [ProductService::class, 'saveEconomicProductField'], 10, 2); - - add_action(ActionScheduleService::CREATE_INVOICE, [ActionScheduleService::class, 'handleCreateInvoiceJob'], 10, 2); + add_action(ActionScheduleService::CREATE_INVOICE, [ActionScheduleService::class, 'handleCreateInvoiceJob'], 10, 1); } public static function registerGateway($gateways): array @@ -28,6 +27,6 @@ public static function registerGateway($gateways): array public static function requireGateway(): void { - require_once __DIR__.'/woocommerce/WC_Gateway_Economic_Invoice.php'; + require_once __DIR__ . '/woocommerce/WC_Gateway_Economic_Invoice.php'; } } diff --git a/src/woocommerce/OrderService.php b/src/woocommerce/OrderService.php index b9ba06e..afbfe4c 100644 --- a/src/woocommerce/OrderService.php +++ b/src/woocommerce/OrderService.php @@ -107,9 +107,9 @@ private static function getRecipient(\WC_Order $order, ?VatZone $vatZone): Recip } return Recipient::new( - name: $order->get_shipping_first_name().' '.$order->get_shipping_last_name(), + name: $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(), vatZone: $vatZone, - address: $order->get_shipping_address_1().' '.$order->get_shipping_address_2(), + address: $order->get_shipping_address_1() . ' ' . $order->get_shipping_address_2(), zip: $order->get_shipping_postcode(), city: $order->get_shipping_city(), country: $order->get_shipping_country(), @@ -147,7 +147,7 @@ private static function addLineItems(\WC_Order $order, DraftInvoice $invoice, \W $product = self::getEconomicProduct($item, $invoice); - if ($product) { + if (! $product) { EconomicLoggerService::critical('Product not found', [ 'product_id' => $item->get_product_id(), 'economic_product_id' => $item->get_meta('economic_product_id'), @@ -160,6 +160,7 @@ private static function addLineItems(\WC_Order $order, DraftInvoice $invoice, \W product: $product->productNumber, quantity: $item->get_quantity(), unitNetPrice: $item->get_total(), + description: $item->get_name(), )); }); @@ -175,6 +176,7 @@ private static function addLineItems(\WC_Order $order, DraftInvoice $invoice, \W product: $paymentMethod->get_option('economic_shipping_product'), quantity: 1, unitNetPrice: $order->get_shipping_total(), + description: __('Fragt', 'mt-wc-economic'), )); return $invoice; From 6dbc7cffa6b4e30c4dbcb64b4c772ec85854d8db Mon Sep 17 00:00:00 2001 From: larasmorningtrain Date: Tue, 9 Jan 2024 09:27:55 +0000 Subject: [PATCH 18/41] Fix styling --- src/WoocommerceEconomic.php | 2 +- src/woocommerce/OrderService.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/WoocommerceEconomic.php b/src/WoocommerceEconomic.php index 64efbe0..284c6ab 100755 --- a/src/WoocommerceEconomic.php +++ b/src/WoocommerceEconomic.php @@ -27,6 +27,6 @@ public static function registerGateway($gateways): array public static function requireGateway(): void { - require_once __DIR__ . '/woocommerce/WC_Gateway_Economic_Invoice.php'; + require_once __DIR__.'/woocommerce/WC_Gateway_Economic_Invoice.php'; } } diff --git a/src/woocommerce/OrderService.php b/src/woocommerce/OrderService.php index afbfe4c..4d31f57 100644 --- a/src/woocommerce/OrderService.php +++ b/src/woocommerce/OrderService.php @@ -107,9 +107,9 @@ private static function getRecipient(\WC_Order $order, ?VatZone $vatZone): Recip } return Recipient::new( - name: $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(), + name: $order->get_shipping_first_name().' '.$order->get_shipping_last_name(), vatZone: $vatZone, - address: $order->get_shipping_address_1() . ' ' . $order->get_shipping_address_2(), + address: $order->get_shipping_address_1().' '.$order->get_shipping_address_2(), zip: $order->get_shipping_postcode(), city: $order->get_shipping_city(), country: $order->get_shipping_country(), From 35adebabd73f343c70ce99b7a61bb5451f31c442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Kj=C3=A6r=20Rasmussen?= Date: Wed, 17 Jan 2024 10:42:28 +0100 Subject: [PATCH 19/41] =?UTF-8?q?=F0=9F=9A=A7=20Only=20make=20requests=20o?= =?UTF-8?q?n=20settings=20tab?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WC_Gateway_Economic_Invoice.php | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/woocommerce/WC_Gateway_Economic_Invoice.php b/src/woocommerce/WC_Gateway_Economic_Invoice.php index 7cad56f..008a2b9 100644 --- a/src/woocommerce/WC_Gateway_Economic_Invoice.php +++ b/src/woocommerce/WC_Gateway_Economic_Invoice.php @@ -24,7 +24,7 @@ public function __construct() $this->description = $this->get_option('description'); // This action hook saves the settings - add_action('woocommerce_update_options_payment_gateways_'.$this->id, [$this, 'process_admin_options']); + add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']); add_action('woocommerce_new_order', [$this, 'onNewOrder'], 10, 1); add_action('woocommerce_order_status_completed', [$this, 'onOrderCompleted'], 10, 1); @@ -194,6 +194,10 @@ public function onOrderCompleted(int $orderId): void private function getLayouts(): array { + if(! $this->isSettingsPage()){ + return []; + } + return Layout::all()->mapWithKeys(function ($layout) { return [$layout->layoutNumber => $layout->name]; })->toArray(); @@ -201,6 +205,10 @@ private function getLayouts(): array private function getPaymentTerms() { + if(! $this->isSettingsPage()){ + return []; + } + return PaymentTerm::all()->mapWithKeys(function ($paymentTerm) { return [$paymentTerm->paymentTermsNumber => $paymentTerm->name]; })->toArray(); @@ -208,6 +216,10 @@ private function getPaymentTerms() private function getVatZones() { + if(! $this->isSettingsPage()){ + return []; + } + return VatZone::all()->mapWithKeys(function ($vatZone) { return [$vatZone->vatZoneNumber => $vatZone->name]; })->toArray(); @@ -215,6 +227,10 @@ private function getVatZones() private function getCustomerGroup() { + if(! $this->isSettingsPage()){ + return []; + } + return CustomerGroup::all()->mapWithKeys(function ($customerGroup) { return [$customerGroup->customerGroupNumber => $customerGroup->name]; })->toArray(); @@ -258,6 +274,16 @@ public function eanFieldUpdateOrderMeta($order_id): void public function addEanFieldFisplayAdminOrderMeta($order): void { - echo '

'.__('EAN nummer', 'woocommerce').': '.get_post_meta($order->get_id(), 'economic_billing_ean', true).'

'; + echo '

' . __('EAN nummer', 'woocommerce') . ': ' . get_post_meta($order->get_id(), 'economic_billing_ean', true) . '

'; + } + + function isSettingsPage() { + $currentScreen = get_current_screen(); + + return $currentScreen->base === 'woocommerce_page_wc-settings' && + isset($_GET['tab']) && $_GET['tab'] === 'checkout' && + isset($_GET['section']) && $_GET['section'] === 'economic_invoice'; } + + } From ee7f3aace43688b5887e93e3d09ef7f08b1eef2e Mon Sep 17 00:00:00 2001 From: larasmorningtrain Date: Wed, 17 Jan 2024 09:42:58 +0000 Subject: [PATCH 20/41] Fix styling --- src/woocommerce/WC_Gateway_Economic_Invoice.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/woocommerce/WC_Gateway_Economic_Invoice.php b/src/woocommerce/WC_Gateway_Economic_Invoice.php index 008a2b9..a0a2bb8 100644 --- a/src/woocommerce/WC_Gateway_Economic_Invoice.php +++ b/src/woocommerce/WC_Gateway_Economic_Invoice.php @@ -24,7 +24,7 @@ public function __construct() $this->description = $this->get_option('description'); // This action hook saves the settings - add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']); + add_action('woocommerce_update_options_payment_gateways_'.$this->id, [$this, 'process_admin_options']); add_action('woocommerce_new_order', [$this, 'onNewOrder'], 10, 1); add_action('woocommerce_order_status_completed', [$this, 'onOrderCompleted'], 10, 1); @@ -194,7 +194,7 @@ public function onOrderCompleted(int $orderId): void private function getLayouts(): array { - if(! $this->isSettingsPage()){ + if (! $this->isSettingsPage()) { return []; } @@ -205,7 +205,7 @@ private function getLayouts(): array private function getPaymentTerms() { - if(! $this->isSettingsPage()){ + if (! $this->isSettingsPage()) { return []; } @@ -216,7 +216,7 @@ private function getPaymentTerms() private function getVatZones() { - if(! $this->isSettingsPage()){ + if (! $this->isSettingsPage()) { return []; } @@ -227,7 +227,7 @@ private function getVatZones() private function getCustomerGroup() { - if(! $this->isSettingsPage()){ + if (! $this->isSettingsPage()) { return []; } @@ -274,16 +274,15 @@ public function eanFieldUpdateOrderMeta($order_id): void public function addEanFieldFisplayAdminOrderMeta($order): void { - echo '

' . __('EAN nummer', 'woocommerce') . ': ' . get_post_meta($order->get_id(), 'economic_billing_ean', true) . '

'; + echo '

'.__('EAN nummer', 'woocommerce').': '.get_post_meta($order->get_id(), 'economic_billing_ean', true).'

'; } - function isSettingsPage() { + public function isSettingsPage() + { $currentScreen = get_current_screen(); return $currentScreen->base === 'woocommerce_page_wc-settings' && isset($_GET['tab']) && $_GET['tab'] === 'checkout' && isset($_GET['section']) && $_GET['section'] === 'economic_invoice'; } - - } From dbc94903522b1e836b40c901f7783c32d39cf0ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Kj=C3=A6r=20Rasmussen?= Date: Wed, 17 Jan 2024 12:00:06 +0100 Subject: [PATCH 21/41] =?UTF-8?q?=F0=9F=90=9B=20Check=20if=20admin=20and?= =?UTF-8?q?=20if=20function=20exists?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/woocommerce/WC_Gateway_Economic_Invoice.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/woocommerce/WC_Gateway_Economic_Invoice.php b/src/woocommerce/WC_Gateway_Economic_Invoice.php index a0a2bb8..d7459ce 100644 --- a/src/woocommerce/WC_Gateway_Economic_Invoice.php +++ b/src/woocommerce/WC_Gateway_Economic_Invoice.php @@ -279,6 +279,10 @@ public function addEanFieldFisplayAdminOrderMeta($order): void public function isSettingsPage() { + if(! is_admin() || ! function_exists('get_current_screen')){ + return false; + } + $currentScreen = get_current_screen(); return $currentScreen->base === 'woocommerce_page_wc-settings' && From 3017a09028d3988ad166b5d20da7f6aff0f1629d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Kj=C3=A6r=20Rasmussen?= Date: Wed, 17 Jan 2024 12:00:06 +0100 Subject: [PATCH 22/41] =?UTF-8?q?=F0=9F=90=9B=20Check=20if=20admin=20and?= =?UTF-8?q?=20if=20function=20exists?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/woocommerce/WC_Gateway_Economic_Invoice.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/woocommerce/WC_Gateway_Economic_Invoice.php b/src/woocommerce/WC_Gateway_Economic_Invoice.php index a0a2bb8..d7459ce 100644 --- a/src/woocommerce/WC_Gateway_Economic_Invoice.php +++ b/src/woocommerce/WC_Gateway_Economic_Invoice.php @@ -279,6 +279,10 @@ public function addEanFieldFisplayAdminOrderMeta($order): void public function isSettingsPage() { + if(! is_admin() || ! function_exists('get_current_screen')){ + return false; + } + $currentScreen = get_current_screen(); return $currentScreen->base === 'woocommerce_page_wc-settings' && From b6e3dc2571134c63b9f14b17965b2242a9258b17 Mon Sep 17 00:00:00 2001 From: larasmorningtrain Date: Wed, 17 Jan 2024 11:00:29 +0000 Subject: [PATCH 23/41] Fix styling --- src/woocommerce/WC_Gateway_Economic_Invoice.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/woocommerce/WC_Gateway_Economic_Invoice.php b/src/woocommerce/WC_Gateway_Economic_Invoice.php index d7459ce..6fea383 100644 --- a/src/woocommerce/WC_Gateway_Economic_Invoice.php +++ b/src/woocommerce/WC_Gateway_Economic_Invoice.php @@ -279,7 +279,7 @@ public function addEanFieldFisplayAdminOrderMeta($order): void public function isSettingsPage() { - if(! is_admin() || ! function_exists('get_current_screen')){ + if (! is_admin() || ! function_exists('get_current_screen')) { return false; } From d385a0d031eb005bd7cf89e8c795b7de052b351f Mon Sep 17 00:00:00 2001 From: larasmorningtrain Date: Wed, 17 Jan 2024 11:00:46 +0000 Subject: [PATCH 24/41] Fix styling --- src/woocommerce/WC_Gateway_Economic_Invoice.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/woocommerce/WC_Gateway_Economic_Invoice.php b/src/woocommerce/WC_Gateway_Economic_Invoice.php index d7459ce..6fea383 100644 --- a/src/woocommerce/WC_Gateway_Economic_Invoice.php +++ b/src/woocommerce/WC_Gateway_Economic_Invoice.php @@ -279,7 +279,7 @@ public function addEanFieldFisplayAdminOrderMeta($order): void public function isSettingsPage() { - if(! is_admin() || ! function_exists('get_current_screen')){ + if (! is_admin() || ! function_exists('get_current_screen')) { return false; } From 3d8ed0a207d1e1b251072cfa100fae2ba4c15121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Kj=C3=A6r=20Rasmussen?= Date: Mon, 29 Jan 2024 11:54:58 +0100 Subject: [PATCH 25/41] =?UTF-8?q?=F0=9F=90=9B=20Fix=20printing=20on=20wron?= =?UTF-8?q?g=20pages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/WoocommerceEconomic.php | 4 ++-- src/woocommerce/OrderService.php | 4 ++-- src/woocommerce/ProductService.php | 7 +++++++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/WoocommerceEconomic.php b/src/WoocommerceEconomic.php index 284c6ab..196d053 100755 --- a/src/WoocommerceEconomic.php +++ b/src/WoocommerceEconomic.php @@ -11,7 +11,7 @@ public static function init(): void { add_filter('woocommerce_payment_gateways', [self::class, 'registerGateway'], 10, 1); add_action('init', [self::class, 'requireGateway'], 1); - add_action('woocommerce_product_options_general_product_data', [ProductService::class, 'addEconomicProductField']); + add_action('woocommerce_product_options_general_product_data', [ProductService::class, 'addEconomicProductFieldWithWrapper']); add_action('woocommerce_variation_options', [ProductService::class, 'addEconomicProductField']); add_action('woocommerce_save_product_variation', [ProductService::class, 'saveEconomicProductField'], 10, 2); add_action('woocommerce_process_product_meta', [ProductService::class, 'saveEconomicProductField'], 10, 2); @@ -27,6 +27,6 @@ public static function registerGateway($gateways): array public static function requireGateway(): void { - require_once __DIR__.'/woocommerce/WC_Gateway_Economic_Invoice.php'; + require_once __DIR__ . '/Woocommerce/WC_Gateway_Economic_Invoice.php'; } } diff --git a/src/woocommerce/OrderService.php b/src/woocommerce/OrderService.php index 4d31f57..afbfe4c 100644 --- a/src/woocommerce/OrderService.php +++ b/src/woocommerce/OrderService.php @@ -107,9 +107,9 @@ private static function getRecipient(\WC_Order $order, ?VatZone $vatZone): Recip } return Recipient::new( - name: $order->get_shipping_first_name().' '.$order->get_shipping_last_name(), + name: $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(), vatZone: $vatZone, - address: $order->get_shipping_address_1().' '.$order->get_shipping_address_2(), + address: $order->get_shipping_address_1() . ' ' . $order->get_shipping_address_2(), zip: $order->get_shipping_postcode(), city: $order->get_shipping_city(), country: $order->get_shipping_country(), diff --git a/src/woocommerce/ProductService.php b/src/woocommerce/ProductService.php index 6c49724..76bc526 100644 --- a/src/woocommerce/ProductService.php +++ b/src/woocommerce/ProductService.php @@ -4,6 +4,13 @@ class ProductService { + public static function addEconomicProductFieldWithWrapper(): void + { + echo ''; + } + public static function addEconomicProductField(): void { woocommerce_wp_text_input([ From 7df06de3d41c52e52cba7bd8bffe167f7aa94eb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Kj=C3=A6r=20Rasmussen?= Date: Mon, 29 Jan 2024 11:55:43 +0100 Subject: [PATCH 26/41] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Do=20not=20check=20c?= =?UTF-8?q?urrentscreen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/woocommerce/WC_Gateway_Economic_Invoice.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/woocommerce/WC_Gateway_Economic_Invoice.php b/src/woocommerce/WC_Gateway_Economic_Invoice.php index 6fea383..19f4a0e 100644 --- a/src/woocommerce/WC_Gateway_Economic_Invoice.php +++ b/src/woocommerce/WC_Gateway_Economic_Invoice.php @@ -24,7 +24,7 @@ public function __construct() $this->description = $this->get_option('description'); // This action hook saves the settings - add_action('woocommerce_update_options_payment_gateways_'.$this->id, [$this, 'process_admin_options']); + add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']); add_action('woocommerce_new_order', [$this, 'onNewOrder'], 10, 1); add_action('woocommerce_order_status_completed', [$this, 'onOrderCompleted'], 10, 1); @@ -274,19 +274,17 @@ public function eanFieldUpdateOrderMeta($order_id): void public function addEanFieldFisplayAdminOrderMeta($order): void { - echo '

'.__('EAN nummer', 'woocommerce').': '.get_post_meta($order->get_id(), 'economic_billing_ean', true).'

'; + echo '

' . __('EAN nummer', 'woocommerce') . ': ' . get_post_meta($order->get_id(), 'economic_billing_ean', true) . '

'; } public function isSettingsPage() { - if (! is_admin() || ! function_exists('get_current_screen')) { + if (! is_admin()) { return false; } - $currentScreen = get_current_screen(); - - return $currentScreen->base === 'woocommerce_page_wc-settings' && - isset($_GET['tab']) && $_GET['tab'] === 'checkout' && - isset($_GET['section']) && $_GET['section'] === 'economic_invoice'; + return ! empty($_GET['page']) && $_GET['page'] === 'wc-settings' && + ! empty($_GET['tab']) && $_GET['tab'] === 'checkout' && + ! empty($_GET['section']) && $_GET['section'] === 'economic_invoice'; } } From a6728fa630117463808ba5242aa3c2e577a95448 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Kj=C3=A6r=20Rasmussen?= Date: Mon, 29 Jan 2024 11:57:30 +0100 Subject: [PATCH 27/41] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Make=20Woocoomerce?= =?UTF-8?q?=20uppercase?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/{woocommerce => Woocommerce}/OrderService.php | 0 src/{woocommerce => Woocommerce}/ProductService.php | 0 src/{woocommerce => Woocommerce}/WC_Gateway_Economic_Invoice.php | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename src/{woocommerce => Woocommerce}/OrderService.php (100%) rename src/{woocommerce => Woocommerce}/ProductService.php (100%) rename src/{woocommerce => Woocommerce}/WC_Gateway_Economic_Invoice.php (100%) diff --git a/src/woocommerce/OrderService.php b/src/Woocommerce/OrderService.php similarity index 100% rename from src/woocommerce/OrderService.php rename to src/Woocommerce/OrderService.php diff --git a/src/woocommerce/ProductService.php b/src/Woocommerce/ProductService.php similarity index 100% rename from src/woocommerce/ProductService.php rename to src/Woocommerce/ProductService.php diff --git a/src/woocommerce/WC_Gateway_Economic_Invoice.php b/src/Woocommerce/WC_Gateway_Economic_Invoice.php similarity index 100% rename from src/woocommerce/WC_Gateway_Economic_Invoice.php rename to src/Woocommerce/WC_Gateway_Economic_Invoice.php From 24bb1a0dce578755459ee38159751d4a7386fb2f Mon Sep 17 00:00:00 2001 From: larasmorningtrain Date: Mon, 29 Jan 2024 10:58:06 +0000 Subject: [PATCH 28/41] Fix styling --- src/Woocommerce/OrderService.php | 4 ++-- src/Woocommerce/WC_Gateway_Economic_Invoice.php | 4 ++-- src/WoocommerceEconomic.php | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Woocommerce/OrderService.php b/src/Woocommerce/OrderService.php index afbfe4c..4d31f57 100644 --- a/src/Woocommerce/OrderService.php +++ b/src/Woocommerce/OrderService.php @@ -107,9 +107,9 @@ private static function getRecipient(\WC_Order $order, ?VatZone $vatZone): Recip } return Recipient::new( - name: $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(), + name: $order->get_shipping_first_name().' '.$order->get_shipping_last_name(), vatZone: $vatZone, - address: $order->get_shipping_address_1() . ' ' . $order->get_shipping_address_2(), + address: $order->get_shipping_address_1().' '.$order->get_shipping_address_2(), zip: $order->get_shipping_postcode(), city: $order->get_shipping_city(), country: $order->get_shipping_country(), diff --git a/src/Woocommerce/WC_Gateway_Economic_Invoice.php b/src/Woocommerce/WC_Gateway_Economic_Invoice.php index 19f4a0e..6eebba1 100644 --- a/src/Woocommerce/WC_Gateway_Economic_Invoice.php +++ b/src/Woocommerce/WC_Gateway_Economic_Invoice.php @@ -24,7 +24,7 @@ public function __construct() $this->description = $this->get_option('description'); // This action hook saves the settings - add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']); + add_action('woocommerce_update_options_payment_gateways_'.$this->id, [$this, 'process_admin_options']); add_action('woocommerce_new_order', [$this, 'onNewOrder'], 10, 1); add_action('woocommerce_order_status_completed', [$this, 'onOrderCompleted'], 10, 1); @@ -274,7 +274,7 @@ public function eanFieldUpdateOrderMeta($order_id): void public function addEanFieldFisplayAdminOrderMeta($order): void { - echo '

' . __('EAN nummer', 'woocommerce') . ': ' . get_post_meta($order->get_id(), 'economic_billing_ean', true) . '

'; + echo '

'.__('EAN nummer', 'woocommerce').': '.get_post_meta($order->get_id(), 'economic_billing_ean', true).'

'; } public function isSettingsPage() diff --git a/src/WoocommerceEconomic.php b/src/WoocommerceEconomic.php index 196d053..8e2d736 100755 --- a/src/WoocommerceEconomic.php +++ b/src/WoocommerceEconomic.php @@ -27,6 +27,6 @@ public static function registerGateway($gateways): array public static function requireGateway(): void { - require_once __DIR__ . '/Woocommerce/WC_Gateway_Economic_Invoice.php'; + require_once __DIR__.'/Woocommerce/WC_Gateway_Economic_Invoice.php'; } } From 5e357f0e8199739f036bf20260bc48a6b6908402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Kj=C3=A6r=20Rasmussen?= Date: Thu, 1 Feb 2024 13:11:13 +0100 Subject: [PATCH 29/41] =?UTF-8?q?=E2=9C=A8=20Handle=20variations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Woocommerce/OrderService.php | 5 ++--- src/Woocommerce/ProductService.php | 25 +++++++++++++++++++++---- src/WoocommerceEconomic.php | 6 +++--- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/Woocommerce/OrderService.php b/src/Woocommerce/OrderService.php index 4d31f57..492f25b 100644 --- a/src/Woocommerce/OrderService.php +++ b/src/Woocommerce/OrderService.php @@ -199,15 +199,14 @@ private static function createAndBookInvoice($paymentMethod, DraftInvoice $invoi $invoice->create(); } - private static function getEconomicProduct($item, DraftInvoice $invoice): ?Product + private static function getEconomicProduct(\WC_Order_Item_Product $item, DraftInvoice $invoice): ?Product { $product = apply_filters('woocommerce_economic_invoice_get_economic_product_line', null, $item, $invoice); if (! $product) { - $productEconomicId = get_post_meta($item->get_product_id(), 'economic_product_id', true); + $productEconomicId = get_post_meta($item->get_variation_id() ?? $item->get_product_id(), 'economic_product_id', true); $product = Product::find($productEconomicId); - ray($product); } return $product; diff --git a/src/Woocommerce/ProductService.php b/src/Woocommerce/ProductService.php index 76bc526..908e9a1 100644 --- a/src/Woocommerce/ProductService.php +++ b/src/Woocommerce/ProductService.php @@ -7,25 +7,42 @@ class ProductService public static function addEconomicProductFieldWithWrapper(): void { echo ''; } - public static function addEconomicProductField(): void + public static function addEconomicProductField( $loop, $variation_data, $variation): void { woocommerce_wp_text_input([ - 'id' => 'economic_product_id', + 'id' => 'economic_product_id_'."[{$loop}]", 'label' => __('E-conomic produkt id', 'mt-wc-economic'), 'placeholder' => __('E-conomic produkt id', 'mt-wc-economic'), 'desc_tip' => 'true', 'description' => __('Udfyld varenummerer fra e-conomic', 'mt-wc-economic'), 'type' => 'number', + 'value' => get_post_meta($variation->ID, 'economic_product_id', true), ]); } public static function saveEconomicProductField($post_id): void { $economic_product_id = $_POST['economic_product_id'] ?? ''; - update_post_meta($post_id, 'economic_product_id', sanitize_text_field($economic_product_id)); + + update_post_meta($post_id, 'economic_product_id', $economic_product_id); + } + + public static function saveVariationEconomicProductField($post_id, $id): void + { + $economic_product_id = $_POST['economic_product_id_'][$id] ?? ''; + + update_post_meta($post_id, 'economic_product_id', $economic_product_id); } } diff --git a/src/WoocommerceEconomic.php b/src/WoocommerceEconomic.php index 8e2d736..53c9ad8 100755 --- a/src/WoocommerceEconomic.php +++ b/src/WoocommerceEconomic.php @@ -12,9 +12,9 @@ public static function init(): void add_filter('woocommerce_payment_gateways', [self::class, 'registerGateway'], 10, 1); add_action('init', [self::class, 'requireGateway'], 1); add_action('woocommerce_product_options_general_product_data', [ProductService::class, 'addEconomicProductFieldWithWrapper']); - add_action('woocommerce_variation_options', [ProductService::class, 'addEconomicProductField']); - add_action('woocommerce_save_product_variation', [ProductService::class, 'saveEconomicProductField'], 10, 2); - add_action('woocommerce_process_product_meta', [ProductService::class, 'saveEconomicProductField'], 10, 2); + add_action('woocommerce_variation_options', [ProductService::class, 'addEconomicProductField'],10,3); + add_action('woocommerce_save_product_variation', [ProductService::class, 'saveVariationEconomicProductField'], 10, 2); + add_action('woocommerce_process_product_meta', [ProductService::class, 'saveEconomicProductField'], 10, 1); add_action(ActionScheduleService::CREATE_INVOICE, [ActionScheduleService::class, 'handleCreateInvoiceJob'], 10, 1); } From af68d832a82c2e7b2b76e3273359235e7e48e158 Mon Sep 17 00:00:00 2001 From: larasmorningtrain Date: Thu, 1 Feb 2024 12:11:57 +0000 Subject: [PATCH 30/41] Fix styling --- src/Woocommerce/ProductService.php | 2 +- src/WoocommerceEconomic.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Woocommerce/ProductService.php b/src/Woocommerce/ProductService.php index 908e9a1..9531bb3 100644 --- a/src/Woocommerce/ProductService.php +++ b/src/Woocommerce/ProductService.php @@ -19,7 +19,7 @@ public static function addEconomicProductFieldWithWrapper(): void echo ''; } - public static function addEconomicProductField( $loop, $variation_data, $variation): void + public static function addEconomicProductField($loop, $variation_data, $variation): void { woocommerce_wp_text_input([ 'id' => 'economic_product_id_'."[{$loop}]", diff --git a/src/WoocommerceEconomic.php b/src/WoocommerceEconomic.php index 53c9ad8..7f59321 100755 --- a/src/WoocommerceEconomic.php +++ b/src/WoocommerceEconomic.php @@ -12,7 +12,7 @@ public static function init(): void add_filter('woocommerce_payment_gateways', [self::class, 'registerGateway'], 10, 1); add_action('init', [self::class, 'requireGateway'], 1); add_action('woocommerce_product_options_general_product_data', [ProductService::class, 'addEconomicProductFieldWithWrapper']); - add_action('woocommerce_variation_options', [ProductService::class, 'addEconomicProductField'],10,3); + add_action('woocommerce_variation_options', [ProductService::class, 'addEconomicProductField'], 10, 3); add_action('woocommerce_save_product_variation', [ProductService::class, 'saveVariationEconomicProductField'], 10, 2); add_action('woocommerce_process_product_meta', [ProductService::class, 'saveEconomicProductField'], 10, 1); add_action(ActionScheduleService::CREATE_INVOICE, [ActionScheduleService::class, 'handleCreateInvoiceJob'], 10, 1); From 32e9498f9dbb59890c52149f88b00c28dd89232e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Kj=C3=A6r=20Rasmussen?= Date: Thu, 14 Mar 2024 16:53:32 +0100 Subject: [PATCH 31/41] =?UTF-8?q?=E2=9C=A8=20Send=20pdf=20email=20on=20boo?= =?UTF-8?q?ked=20invoince?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Woocommerce/OrderService.php | 56 +++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/src/Woocommerce/OrderService.php b/src/Woocommerce/OrderService.php index 492f25b..d7d6c99 100644 --- a/src/Woocommerce/OrderService.php +++ b/src/Woocommerce/OrderService.php @@ -2,21 +2,23 @@ namespace Morningtrain\WoocommerceEconomic\Woocommerce; -use Morningtrain\Economic\DTOs\Recipient; + +use Morningtrain\Economic\DTOs\Invoice\ProductLine; +use Morningtrain\Economic\DTOs\Invoice\Recipient; use Morningtrain\Economic\Resources\Customer; +use Morningtrain\Economic\Resources\Invoice\BookedInvoice; use Morningtrain\Economic\Resources\Invoice\DraftInvoice; -use Morningtrain\Economic\Resources\Invoice\ProductLine; use Morningtrain\Economic\Resources\Layout; use Morningtrain\Economic\Resources\PaymentTerm; use Morningtrain\Economic\Resources\Product; use Morningtrain\Economic\Resources\VatZone; use Morningtrain\Economic\Services\EconomicLoggerService; + class OrderService { public static function createInvoice(\WC_Order $order, $paymentMethod): void { - $vatZone = self::getVatZone($paymentMethod); $layout = self::getLayout($paymentMethod); @@ -31,7 +33,7 @@ public static function createInvoice(\WC_Order $order, $paymentMethod): void $invoice = self::addLineItems($order, $invoice, $paymentMethod); - self::createAndBookInvoice($paymentMethod, $invoice); + self::createAndBookInvoice($paymentMethod, $invoice, $customer); } private static function getVatZone($paymentMethod): ?VatZone @@ -107,9 +109,9 @@ private static function getRecipient(\WC_Order $order, ?VatZone $vatZone): Recip } return Recipient::new( - name: $order->get_shipping_first_name().' '.$order->get_shipping_last_name(), + name: $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(), vatZone: $vatZone, - address: $order->get_shipping_address_1().' '.$order->get_shipping_address_2(), + address: $order->get_shipping_address_1() . ' ' . $order->get_shipping_address_2(), zip: $order->get_shipping_postcode(), city: $order->get_shipping_city(), country: $order->get_shipping_country(), @@ -157,7 +159,7 @@ private static function addLineItems(\WC_Order $order, DraftInvoice $invoice, \W } $invoice->addLine(ProductLine::new( - product: $product->productNumber, + product: $product, quantity: $item->get_quantity(), unitNetPrice: $item->get_total(), description: $item->get_name(), @@ -182,7 +184,7 @@ private static function addLineItems(\WC_Order $order, DraftInvoice $invoice, \W return $invoice; } - private static function createAndBookInvoice($paymentMethod, DraftInvoice $invoice): void + private static function createAndBookInvoice($paymentMethod, DraftInvoice $invoice, Customer $customer): void { $customInvoice = apply_filters('woocommerce_economic_invoice_create_and_book_invoice', null, $paymentMethod, $invoice); @@ -190,13 +192,22 @@ private static function createAndBookInvoice($paymentMethod, DraftInvoice $invoi return; } - if ($paymentMethod->get_option('economic_invoice_draft') === 'book') { - $invoice->create()?->book(); + $draftInvoice = $invoice->save(); + + if($draftInvoice === null) { return; } - $invoice->create(); + + if ($paymentMethod->get_option('economic_invoice_draft') === 'book') { + + $bookedInvoice = BookedInvoice::createFromDraft($draftInvoice->draftInvoiceNumber); + + self::sendInvoicePdf($bookedInvoice, $customer); + + return; + } } private static function getEconomicProduct(\WC_Order_Item_Product $item, DraftInvoice $invoice): ?Product @@ -204,11 +215,32 @@ private static function getEconomicProduct(\WC_Order_Item_Product $item, DraftIn $product = apply_filters('woocommerce_economic_invoice_get_economic_product_line', null, $item, $invoice); if (! $product) { - $productEconomicId = get_post_meta($item->get_variation_id() ?? $item->get_product_id(), 'economic_product_id', true); + $productEconomicId = get_post_meta(! empty($item->get_variation_id()) ? $item->get_variation_id() : $item->get_product_id(), 'economic_product_id', true); $product = Product::find($productEconomicId); } return $product; } + + public static function sendInvoicePdf(BookedInvoice $bookedInvoice, Customer $customer) + { + $mailRecipient = $customer->email; + + $fileName = $bookedInvoice->bookedInvoiceNumber .'-'.$customer->name . '-' . time() . '.pdf'; + + $file = $bookedInvoice->pdf->getPdfContentResponse()->getBody(); //example.pdf + + $filepath = WP_CONTENT_DIR . '/private/'. $fileName; + + $fp = fopen($filepath, "w"); + fwrite($fp, $file); + + if(fclose($fp)){ + $mail = \wp_mail($mailRecipient, 'Faktura', 'Se vedhæftet faktura', '', $filepath); //TODO: Style mail + if($mail){ + unlink($filepath); + } + } + } } From 1b9e690b7db4df115209c1070e4784fa34c9a136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Kj=C3=A6r=20Rasmussen?= Date: Thu, 14 Mar 2024 16:54:02 +0100 Subject: [PATCH 32/41] =?UTF-8?q?=F0=9F=9A=A8=20Linting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Woocommerce/ProductService.php | 2 +- src/Woocommerce/WC_Gateway_Economic_Invoice.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Woocommerce/ProductService.php b/src/Woocommerce/ProductService.php index 9531bb3..697abcb 100644 --- a/src/Woocommerce/ProductService.php +++ b/src/Woocommerce/ProductService.php @@ -22,7 +22,7 @@ public static function addEconomicProductFieldWithWrapper(): void public static function addEconomicProductField($loop, $variation_data, $variation): void { woocommerce_wp_text_input([ - 'id' => 'economic_product_id_'."[{$loop}]", + 'id' => 'economic_product_id_' . "[{$loop}]", 'label' => __('E-conomic produkt id', 'mt-wc-economic'), 'placeholder' => __('E-conomic produkt id', 'mt-wc-economic'), 'desc_tip' => 'true', diff --git a/src/Woocommerce/WC_Gateway_Economic_Invoice.php b/src/Woocommerce/WC_Gateway_Economic_Invoice.php index 6eebba1..19f4a0e 100644 --- a/src/Woocommerce/WC_Gateway_Economic_Invoice.php +++ b/src/Woocommerce/WC_Gateway_Economic_Invoice.php @@ -24,7 +24,7 @@ public function __construct() $this->description = $this->get_option('description'); // This action hook saves the settings - add_action('woocommerce_update_options_payment_gateways_'.$this->id, [$this, 'process_admin_options']); + add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']); add_action('woocommerce_new_order', [$this, 'onNewOrder'], 10, 1); add_action('woocommerce_order_status_completed', [$this, 'onOrderCompleted'], 10, 1); @@ -274,7 +274,7 @@ public function eanFieldUpdateOrderMeta($order_id): void public function addEanFieldFisplayAdminOrderMeta($order): void { - echo '

'.__('EAN nummer', 'woocommerce').': '.get_post_meta($order->get_id(), 'economic_billing_ean', true).'

'; + echo '

' . __('EAN nummer', 'woocommerce') . ': ' . get_post_meta($order->get_id(), 'economic_billing_ean', true) . '

'; } public function isSettingsPage() From cdd5c41f0644741c080a3465aadb864f4d288df2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Kj=C3=A6r=20Rasmussen?= Date: Thu, 14 Mar 2024 16:54:23 +0100 Subject: [PATCH 33/41] =?UTF-8?q?=F0=9F=90=9B=20Handle=20if=20gateway=20is?= =?UTF-8?q?=20not=20activated?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/WoocommerceEconomic.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/WoocommerceEconomic.php b/src/WoocommerceEconomic.php index 7f59321..caf875c 100755 --- a/src/WoocommerceEconomic.php +++ b/src/WoocommerceEconomic.php @@ -20,6 +20,9 @@ public static function init(): void public static function registerGateway($gateways): array { + if (! class_exists('WC_Payment_Gateway')) { + return $gateways; + } $gateways[] = \WC_Gateway_Economic_Invoice::class; return $gateways; @@ -27,6 +30,9 @@ public static function registerGateway($gateways): array public static function requireGateway(): void { - require_once __DIR__.'/Woocommerce/WC_Gateway_Economic_Invoice.php'; + if (! class_exists('WC_Payment_Gateway')) { + return; + } + require_once __DIR__ . '/Woocommerce/WC_Gateway_Economic_Invoice.php'; } } From 52f497ef4eb612e8e79af4d39e92d507c29eb60e Mon Sep 17 00:00:00 2001 From: larasmorningtrain Date: Thu, 14 Mar 2024 15:54:58 +0000 Subject: [PATCH 34/41] Fix styling --- src/Woocommerce/OrderService.php | 24 ++++++++----------- src/Woocommerce/ProductService.php | 2 +- .../WC_Gateway_Economic_Invoice.php | 4 ++-- src/WoocommerceEconomic.php | 2 +- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/Woocommerce/OrderService.php b/src/Woocommerce/OrderService.php index d7d6c99..ec4202b 100644 --- a/src/Woocommerce/OrderService.php +++ b/src/Woocommerce/OrderService.php @@ -2,7 +2,6 @@ namespace Morningtrain\WoocommerceEconomic\Woocommerce; - use Morningtrain\Economic\DTOs\Invoice\ProductLine; use Morningtrain\Economic\DTOs\Invoice\Recipient; use Morningtrain\Economic\Resources\Customer; @@ -14,7 +13,6 @@ use Morningtrain\Economic\Resources\VatZone; use Morningtrain\Economic\Services\EconomicLoggerService; - class OrderService { public static function createInvoice(\WC_Order $order, $paymentMethod): void @@ -109,9 +107,9 @@ private static function getRecipient(\WC_Order $order, ?VatZone $vatZone): Recip } return Recipient::new( - name: $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(), + name: $order->get_shipping_first_name().' '.$order->get_shipping_last_name(), vatZone: $vatZone, - address: $order->get_shipping_address_1() . ' ' . $order->get_shipping_address_2(), + address: $order->get_shipping_address_1().' '.$order->get_shipping_address_2(), zip: $order->get_shipping_postcode(), city: $order->get_shipping_city(), country: $order->get_shipping_country(), @@ -192,17 +190,15 @@ private static function createAndBookInvoice($paymentMethod, DraftInvoice $invoi return; } - $draftInvoice = $invoice->save(); - if($draftInvoice === null) { + if ($draftInvoice === null) { return; } - if ($paymentMethod->get_option('economic_invoice_draft') === 'book') { - $bookedInvoice = BookedInvoice::createFromDraft($draftInvoice->draftInvoiceNumber); + $bookedInvoice = BookedInvoice::createFromDraft($draftInvoice->draftInvoiceNumber); self::sendInvoicePdf($bookedInvoice, $customer); @@ -227,18 +223,18 @@ public static function sendInvoicePdf(BookedInvoice $bookedInvoice, Customer $cu { $mailRecipient = $customer->email; - $fileName = $bookedInvoice->bookedInvoiceNumber .'-'.$customer->name . '-' . time() . '.pdf'; + $fileName = $bookedInvoice->bookedInvoiceNumber.'-'.$customer->name.'-'.time().'.pdf'; - $file = $bookedInvoice->pdf->getPdfContentResponse()->getBody(); //example.pdf + $file = $bookedInvoice->pdf->getPdfContentResponse()->getBody(); //example.pdf - $filepath = WP_CONTENT_DIR . '/private/'. $fileName; + $filepath = WP_CONTENT_DIR.'/private/'.$fileName; - $fp = fopen($filepath, "w"); + $fp = fopen($filepath, 'w'); fwrite($fp, $file); - if(fclose($fp)){ + if (fclose($fp)) { $mail = \wp_mail($mailRecipient, 'Faktura', 'Se vedhæftet faktura', '', $filepath); //TODO: Style mail - if($mail){ + if ($mail) { unlink($filepath); } } diff --git a/src/Woocommerce/ProductService.php b/src/Woocommerce/ProductService.php index 697abcb..9531bb3 100644 --- a/src/Woocommerce/ProductService.php +++ b/src/Woocommerce/ProductService.php @@ -22,7 +22,7 @@ public static function addEconomicProductFieldWithWrapper(): void public static function addEconomicProductField($loop, $variation_data, $variation): void { woocommerce_wp_text_input([ - 'id' => 'economic_product_id_' . "[{$loop}]", + 'id' => 'economic_product_id_'."[{$loop}]", 'label' => __('E-conomic produkt id', 'mt-wc-economic'), 'placeholder' => __('E-conomic produkt id', 'mt-wc-economic'), 'desc_tip' => 'true', diff --git a/src/Woocommerce/WC_Gateway_Economic_Invoice.php b/src/Woocommerce/WC_Gateway_Economic_Invoice.php index 19f4a0e..6eebba1 100644 --- a/src/Woocommerce/WC_Gateway_Economic_Invoice.php +++ b/src/Woocommerce/WC_Gateway_Economic_Invoice.php @@ -24,7 +24,7 @@ public function __construct() $this->description = $this->get_option('description'); // This action hook saves the settings - add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']); + add_action('woocommerce_update_options_payment_gateways_'.$this->id, [$this, 'process_admin_options']); add_action('woocommerce_new_order', [$this, 'onNewOrder'], 10, 1); add_action('woocommerce_order_status_completed', [$this, 'onOrderCompleted'], 10, 1); @@ -274,7 +274,7 @@ public function eanFieldUpdateOrderMeta($order_id): void public function addEanFieldFisplayAdminOrderMeta($order): void { - echo '

' . __('EAN nummer', 'woocommerce') . ': ' . get_post_meta($order->get_id(), 'economic_billing_ean', true) . '

'; + echo '

'.__('EAN nummer', 'woocommerce').': '.get_post_meta($order->get_id(), 'economic_billing_ean', true).'

'; } public function isSettingsPage() diff --git a/src/WoocommerceEconomic.php b/src/WoocommerceEconomic.php index caf875c..a132df0 100755 --- a/src/WoocommerceEconomic.php +++ b/src/WoocommerceEconomic.php @@ -33,6 +33,6 @@ public static function requireGateway(): void if (! class_exists('WC_Payment_Gateway')) { return; } - require_once __DIR__ . '/Woocommerce/WC_Gateway_Economic_Invoice.php'; + require_once __DIR__.'/Woocommerce/WC_Gateway_Economic_Invoice.php'; } } From 9e214052532ea49e0e083650d1b7e81718300e61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20B=C3=A6rentsen?= Date: Fri, 17 May 2024 10:29:07 +0200 Subject: [PATCH 35/41] =?UTF-8?q?=E2=9C=A8=20Add=20comments=20to=20order?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Services/ActionScheduleService.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Services/ActionScheduleService.php b/src/Services/ActionScheduleService.php index e82ad16..72273f7 100644 --- a/src/Services/ActionScheduleService.php +++ b/src/Services/ActionScheduleService.php @@ -22,11 +22,17 @@ public static function handleCreateInvoiceJob(int $orderId): void try { OrderService::createInvoice($order, $paymentMethod); + + $order->add_order_note(__('Ordren er oprettet i Economic.', 'mt-wc-economic')); } catch (\Exception $e) { EconomicLoggerService::critical('Could not create invoice', [ 'exception' => $e, ]); + $order->add_order_note(sprintf( + __('Kunne ikke oprette ordre i Economic.
%s', 'mt-wc-economic'), + method_exists($e, 'getDetailedMessage') ? $e->getDetailedMessage() : $e->getMessage(), + )); $order->update_status('failed'); } From 1a2ee4d18d975ecac5b816434fed112a0d3e2810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20B=C3=A6rentsen?= Date: Fri, 17 May 2024 10:29:43 +0200 Subject: [PATCH 36/41] =?UTF-8?q?=E2=9C=A8=20Add=20filter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Woocommerce/WC_Gateway_Economic_Invoice.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Woocommerce/WC_Gateway_Economic_Invoice.php b/src/Woocommerce/WC_Gateway_Economic_Invoice.php index 6eebba1..b57214e 100644 --- a/src/Woocommerce/WC_Gateway_Economic_Invoice.php +++ b/src/Woocommerce/WC_Gateway_Economic_Invoice.php @@ -37,7 +37,7 @@ public function __construct() public function init_form_fields(): void { - $this->form_fields = [ + $this->form_fields = \apply_filters('woocommerce-economic/wc-gateway/form-fields', [ 'enabled' => [ 'title' => __('Aktiver/Deaktiver', 'mt-wc-economic'), 'type' => 'checkbox', @@ -145,7 +145,7 @@ public function init_form_fields(): void 'default' => '', 'desc_tip' => true, ], - ]; + ]); } From 4527e8e4acfd9924cbdbceb3076f996b896a0810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20B=C3=A6rentsen?= Date: Fri, 17 May 2024 10:29:55 +0200 Subject: [PATCH 37/41] =?UTF-8?q?=E2=99=BB=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Woocommerce/OrderService.php | 65 ++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 25 deletions(-) diff --git a/src/Woocommerce/OrderService.php b/src/Woocommerce/OrderService.php index ec4202b..ab90b95 100644 --- a/src/Woocommerce/OrderService.php +++ b/src/Woocommerce/OrderService.php @@ -11,6 +11,7 @@ use Morningtrain\Economic\Resources\PaymentTerm; use Morningtrain\Economic\Resources\Product; use Morningtrain\Economic\Resources\VatZone; +use Morningtrain\Economic\Services\EconomicApiService; use Morningtrain\Economic\Services\EconomicLoggerService; class OrderService @@ -71,31 +72,34 @@ private static function getPaymentTerm($paymentMethod): PaymentTerm private static function getCustomer(\WC_Order $order, ?VatZone $vatZone, PaymentTerm $paymentTerm, \WC_Gateway_Economic_Invoice $paymentMethod): mixed { - $customer = apply_filters('woocommerce_economic_invoice_get_customer', null); - - if (! $customer) { - $customer = Customer::where('email', $order->get_billing_email())->first(); - - if (! $customer) { - $customerGroup = apply_filters('woocommerce_economic_invoice_customer_group', null); - - if (! $customerGroup) { - $customerGroup = $paymentMethod->get_option('economic_customer_group'); - } - - $customer = Customer::create( - name: $order->get_billing_email(), - customerGroup: $customerGroup, - currency: $order->get_currency(), - vatZone: $vatZone, - paymentTerms: $paymentTerm, - email: $order->get_billing_email(), - ean: get_metadata('post', $order->get_id(), 'economic_billing_ean', true) - ); - } + $customer = apply_filters('woocommerce-economic/order/customer', null, $order, $vatZone, $paymentTerm, $paymentMethod); + + if ($customer !== null) { + return $customer; + } + + $customer = Customer::where('email', $order->get_billing_email()) + ->first(); + + if (! empty($customer)) { + return $customer; + } + + $customerGroup = apply_filters('woocommerce-economic/order/customer-group', null, $order, $vatZone, $paymentTerm, $paymentMethod); + + if (! $customerGroup) { + $customerGroup = $paymentMethod->get_option('economic_customer_group'); } - return $customer; + return Customer::create( + name: $order->get_billing_email(), + customerGroup: $customerGroup, + currency: $order->get_currency(), + vatZone: $vatZone, + paymentTerms: $paymentTerm, + email: $order->get_billing_email(), + ean: get_metadata('post', $order->get_id(), 'economic_billing_ean', true) + ); } private static function getRecipient(\WC_Order $order, ?VatZone $vatZone): Recipient @@ -172,8 +176,19 @@ private static function addLineItems(\WC_Order $order, DraftInvoice $invoice, \W return $invoice; } + $shippingProductId = $paymentMethod->get_option('economic_shipping_product'); + $shippingProduct = Product::find($shippingProductId); + + if (! $shippingProduct) { + EconomicLoggerService::critical('Shipping product not found', [ + 'economic_product_id' => $shippingProductId, + ]); + + throw new \Exception('Shipping product not found'); + } + $invoice->addLine(ProductLine::new( - product: $paymentMethod->get_option('economic_shipping_product'), + product: $shippingProduct, quantity: 1, unitNetPrice: $order->get_shipping_total(), description: __('Fragt', 'mt-wc-economic'), @@ -225,7 +240,7 @@ public static function sendInvoicePdf(BookedInvoice $bookedInvoice, Customer $cu $fileName = $bookedInvoice->bookedInvoiceNumber.'-'.$customer->name.'-'.time().'.pdf'; - $file = $bookedInvoice->pdf->getPdfContentResponse()->getBody(); //example.pdf + $file = EconomicApiService::get($bookedInvoice->pdf->download)->getBody(); $filepath = WP_CONTENT_DIR.'/private/'.$fileName; From ce141497a1da7eaa679a98c63abeb50e292f925c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20B=C3=A6rentsen?= Date: Fri, 17 May 2024 10:40:34 +0200 Subject: [PATCH 38/41] =?UTF-8?q?=E2=9E=95=20Update=20morningtrain/wp-econ?= =?UTF-8?q?omic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index f7c2c57..bc93c54 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ ], "require": { "php": "^8.1", - "morningtrain/wp-economic": "^0.1" + "morningtrain/wp-economic": "v0.7" }, "require-dev": { "pestphp/pest": "^2.15", From 9236fe1113b2867a5fa8d111c61d281ce8ae984c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20B=C3=A6rentsen?= Date: Fri, 17 May 2024 13:58:12 +0200 Subject: [PATCH 39/41] =?UTF-8?q?=E2=9E=95=20Change=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index bc93c54..9af5727 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ ], "require": { "php": "^8.1", - "morningtrain/wp-economic": "v0.7" + "morningtrain/wp-economic": "^v0.7.1" }, "require-dev": { "pestphp/pest": "^2.15", From eda6da00d0e3ebd9fd5ed65ac50ebc8de0567728 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20B=C3=A6rentsen?= Date: Tue, 21 May 2024 09:56:23 +0200 Subject: [PATCH 40/41] =?UTF-8?q?=F0=9F=90=9B=20Fix=20missing=20private=20?= =?UTF-8?q?folder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Woocommerce/OrderService.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Woocommerce/OrderService.php b/src/Woocommerce/OrderService.php index ab90b95..170e054 100644 --- a/src/Woocommerce/OrderService.php +++ b/src/Woocommerce/OrderService.php @@ -237,12 +237,16 @@ private static function getEconomicProduct(\WC_Order_Item_Product $item, DraftIn public static function sendInvoicePdf(BookedInvoice $bookedInvoice, Customer $customer) { $mailRecipient = $customer->email; - - $fileName = $bookedInvoice->bookedInvoiceNumber.'-'.$customer->name.'-'.time().'.pdf'; - + $folder = WP_CONTENT_DIR . '/private/'; $file = EconomicApiService::get($bookedInvoice->pdf->download)->getBody(); - $filepath = WP_CONTENT_DIR.'/private/'.$fileName; + // We only want to create the folder if it doesn't exist + if (! file_exists($folder)) { + // Create the folder with the correct permissions + mkdir($folder, 0770, true); + } + + $filepath = $folder . $bookedInvoice->bookedInvoiceNumber . '-' . $customer->name . '-' . time() . '.pdf'; $fp = fopen($filepath, 'w'); fwrite($fp, $file); From a4d308562499f878e61f37ff3014795937b4c707 Mon Sep 17 00:00:00 2001 From: matbaek Date: Tue, 21 May 2024 07:57:43 +0000 Subject: [PATCH 41/41] Fix styling --- src/Woocommerce/OrderService.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Woocommerce/OrderService.php b/src/Woocommerce/OrderService.php index 170e054..5d0c2ea 100644 --- a/src/Woocommerce/OrderService.php +++ b/src/Woocommerce/OrderService.php @@ -237,7 +237,7 @@ private static function getEconomicProduct(\WC_Order_Item_Product $item, DraftIn public static function sendInvoicePdf(BookedInvoice $bookedInvoice, Customer $customer) { $mailRecipient = $customer->email; - $folder = WP_CONTENT_DIR . '/private/'; + $folder = WP_CONTENT_DIR.'/private/'; $file = EconomicApiService::get($bookedInvoice->pdf->download)->getBody(); // We only want to create the folder if it doesn't exist @@ -246,7 +246,7 @@ public static function sendInvoicePdf(BookedInvoice $bookedInvoice, Customer $cu mkdir($folder, 0770, true); } - $filepath = $folder . $bookedInvoice->bookedInvoiceNumber . '-' . $customer->name . '-' . time() . '.pdf'; + $filepath = $folder.$bookedInvoice->bookedInvoiceNumber.'-'.$customer->name.'-'.time().'.pdf'; $fp = fopen($filepath, 'w'); fwrite($fp, $file);